|
| 1 | +import fs from "graceful-fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import {promisify} from "node:util"; |
| 4 | +import logger from "@ui5/logger"; |
| 5 | + |
| 6 | +const readFile = promisify(fs.readFile); |
| 7 | +const log = logger.getLogger("graph:Workspace"); |
| 8 | + |
| 9 | +/** |
| 10 | + * Dependency graph node representing a module |
| 11 | + * |
| 12 | + * @public |
| 13 | + * @typedef {object} @ui5/project/graph/Workspace~WorkspaceConfiguration |
| 14 | + * @property {string} node.specVersion |
| 15 | + * @property {object} node.metadata Version of the project |
| 16 | + * @property {object} node.dependencyManagement |
| 17 | + */ |
| 18 | + |
| 19 | +/** |
| 20 | + * Workspace representation |
| 21 | + * |
| 22 | + * @public |
| 23 | + * @class |
| 24 | + * @alias @ui5/project/graph/Workspace |
| 25 | + */ |
| 26 | +class Workspace { |
| 27 | + /** |
| 28 | + * @param {object} options |
| 29 | + * @param {object} options.cwd |
| 30 | + * @param {@ui5/project/graph/Workspace~WorkspaceConfiguration} options.workspaceConfiguration |
| 31 | + * Workspace configuration |
| 32 | + */ |
| 33 | + constructor({cwd, workspaceConfiguration}) { |
| 34 | + if (!cwd || !workspaceConfiguration) { |
| 35 | + throw new Error("[Workspace] One or more mandatory parameters not provided"); |
| 36 | + } |
| 37 | + |
| 38 | + this._cwd = cwd; |
| 39 | + this._name = workspaceConfiguration.metadata.name; |
| 40 | + this._dependencyManagement = workspaceConfiguration.dependencyManagement; |
| 41 | + } |
| 42 | + |
| 43 | + getName() { |
| 44 | + return this._name; |
| 45 | + } |
| 46 | + |
| 47 | + async getNode(id) { |
| 48 | + const nodes = await this._getResolvedNodes(); |
| 49 | + return nodes.get(id); |
| 50 | + } |
| 51 | + |
| 52 | + getNodes() { |
| 53 | + return this._getResolvedNodes(); |
| 54 | + } |
| 55 | + |
| 56 | + _getResolvedNodes() { |
| 57 | + if (this._pResolvedNodes) { |
| 58 | + return this._pResolvedNodes; |
| 59 | + } |
| 60 | + |
| 61 | + return this._pResolvedNodes = this._resolveNodes(); |
| 62 | + } |
| 63 | + |
| 64 | + async _resolveNodes() { |
| 65 | + if (!this._dependencyManagement?.resolutions?.length) { |
| 66 | + return new Map(); |
| 67 | + } |
| 68 | + |
| 69 | + let resolvedNodes = await Promise.all(this._dependencyManagement.resolutions.map(async (resolutionConfig) => { |
| 70 | + if (!resolutionConfig.path) { |
| 71 | + throw new Error( |
| 72 | + `Missing property 'path' in dependency resolution configuration of workspace ${this._name}`); |
| 73 | + } |
| 74 | + const nodes = await this._getNodesFromPath(this._cwd, resolutionConfig.path); |
| 75 | + |
| 76 | + if (!Array.isArray(nodes) && resolutionConfig.configuration) { |
| 77 | + nodes.configuration = resolutionConfig.configuration; |
| 78 | + } |
| 79 | + return nodes; |
| 80 | + })); |
| 81 | + |
| 82 | + // Flatten array since workspaces might have lead to nested arrays |
| 83 | + resolvedNodes = Array.prototype.concat.apply([], resolvedNodes); |
| 84 | + return new Map(resolvedNodes.map((node) => { |
| 85 | + return [node.id, node]; |
| 86 | + })); |
| 87 | + } |
| 88 | + |
| 89 | + async _getNodesFromPath(cwd, relPath, resolveWorkspace = true) { |
| 90 | + const nodePath = path.join(this._cwd, relPath); |
| 91 | + const pkg = await this._readPackageJson(nodePath); |
| 92 | + if (pkg.workspaces?.length) { |
| 93 | + if (!resolveWorkspace) { |
| 94 | + log.info(`Ignoring nested package workspace of module ${pkg.name} at ${nodePath}`); |
| 95 | + return []; |
| 96 | + } |
| 97 | + return Promise.all(pkg.workspaces.map(async (workspacePath) => { |
| 98 | + const nodes = await this._getNodesFromPath(nodePath, workspacePath, false); |
| 99 | + if (nodes.lengh > 1) { |
| 100 | + throw new Error( |
| 101 | + `Package workspace of module ${pkg.name} at ${nodePath} ` + |
| 102 | + `unexpectedly resolved to multiple modules`); |
| 103 | + } |
| 104 | + return nodes[0]; |
| 105 | + })); |
| 106 | + } else { |
| 107 | + return this._getNodeFromPackage(pkg, nodePath); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + _getNodeFromPackage(pkg, path) { |
| 112 | + return { |
| 113 | + id: pkg.name, |
| 114 | + version: pkg.version, |
| 115 | + path: path |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Reads the package.json file and returns its content |
| 121 | + * |
| 122 | + * @private |
| 123 | + * @param {string} modulePath Path to the module containing the package.json |
| 124 | + * @returns {object} Package json content |
| 125 | + */ |
| 126 | + async _readPackageJson(modulePath) { |
| 127 | + const content = await readFile(path.join(modulePath, "package.json"), "utf8"); |
| 128 | + return JSON.parse(content); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +export default Workspace; |
0 commit comments