Skip to content

Commit be120ac

Browse files
committed
Refactor graph module structure
1 parent f460786 commit be120ac

File tree

11 files changed

+117
-123
lines changed

11 files changed

+117
-123
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ module.exports = {
1111
* @type {import('./lib/projectPreprocessor')}
1212
*/
1313
projectPreprocessor: "./lib/projectPreprocessor",
14+
/**
15+
* @type {import('./lib/generateProjectGraph')}
16+
*/
17+
generateProjectGraph: "./lib/generateProjectGraph",
1418
/**
1519
* @public
1620
* @alias module:@ui5/project.ui5Framework
@@ -66,10 +70,6 @@ module.exports = {
6670
* @type {typeof import('./lib/graph/ProjectGraph')}
6771
*/
6872
ProjectGraph: "./lib/graph/ProjectGraph",
69-
/**
70-
* @type {typeof import('./lib/graph/projectGraphFromTree')}
71-
*/
72-
projectGraphFromTree: "./lib/graph/projectGraphFromTree"
7373
},
7474
};
7575

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
const projectGraphBuilder = require("./projectGraphBuilder");
2-
const ui5Framework = require("./providers/ui5Framework");
3-
const log = require("@ui5/logger").getLogger("graph:projectGraphFromDirectory");
1+
const path = require("path");
2+
const projectGraphBuilder = require("./graph/providers/projectGraphBuilder");
3+
const ui5Framework = require("./graph/helpers/ui5Framework");
4+
const log = require("@ui5/logger").getLogger("generateProjectGraph");
45

56
/**
67
* Helper module to create a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph}
78
* from a directory
89
*
910
* @public
10-
* @alias module:@ui5/project.graph.projectGraphFromTree
11+
* @alias module:@ui5/project.generateProjectGraph
1112
* @param {TreeNode} tree Dependency tree as returned by a translator
1213
* @returns {module:@ui5/project.graph.ProjectGraph} A new project graph instance
1314
*/
14-
const projectGraphFromDirectory = {
15+
const generateProjectGraph = {
1516
/**
1617
* Generates a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph} by resolving
1718
* dependencies from package.json files and configuring projects from ui5.yaml files
1819
*
1920
* @public
2021
* @param {object} [options]
21-
* @param {string} [options.cwd=.] Directory to start searching for the root module
22+
* @param {string} [options.cwd=process.cwd()] Directory to start searching for the root module
2223
* @param {object} [options.rootConfiguration]
2324
* Configuration object to use for the root module instead of reading from a configuration file
2425
* @param {string} [options.rootConfigPath]
2526
* Configuration file to use for the root module instead the default ui5.yaml
2627
* @param {string} [options.versionOverride] Framework version to use instead of the one defined in the root project
2728
* @returns {Promise<module:@ui5/project.graph.ProjectGraph>} Promise resolving to a Project Graph instance
2829
*/
29-
usingNpm: async function({cwd = ".", rootConfiguration, rootConfigPath, versionOverride}) {
30+
usingNodePackageDependencies: async function({cwd, rootConfiguration, rootConfigPath, versionOverride}) {
3031
log.verbose(`Creating project graph using npm provider...`);
31-
const NpmProvider = require("./providers/npm");
32+
const NpmProvider = require("./graph/providers/NodePackageDependencies");
3233

3334
const provider = new NpmProvider({
34-
cwd: cwd,
35+
cwd: cwd ? path.resolve(cwd) : process.cwd(),
3536
rootConfiguration,
3637
rootConfigPath
3738
});
@@ -44,36 +45,62 @@ const projectGraphFromDirectory = {
4445
},
4546

4647
/**
47-
* Generates a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph} from on a
48+
* Generates a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph} from a
4849
* YAML file following the structure of the
4950
* [@ui5/project.graph.projectGraphFromTree]{@link module:@ui5/project.graph.projectGraphFromTree} API
5051
*
5152
* @public
5253
* @param {object} options
5354
* @param {object} options.filePath Path to the file dependency configuration file
54-
* @param {string} [options.cwd=.] Directory to start searching for the root module
55+
* @param {string} [options.cwd=process.cwd()] Directory to start searching for the root module
5556
* @param {string} [options.versionOverride] Framework version to use instead of the one defined in the root project
5657
* @returns {Promise<module:@ui5/project.graph.ProjectGraph>} Promise resolving to a Project Graph instance
5758
*/
58-
usingStaticFile: async function({cwd = ".", filePath, versionOverride}) {
59+
usingStaticFile: async function({cwd, filePath, versionOverride}) {
5960
log.verbose(`Creating project graph using static file...`);
60-
const staticTranslator = require("../translators/static");
61-
const DependencyTreeProvider = require("./providers/DependencyTree");
61+
const staticTranslator = require("./translators/static");
6262

63-
const tree = await staticTranslator.generateDependencyTree(null, {
63+
const dependencyTree = await staticTranslator.generateDependencyTree(cwd ? path.resolve(cwd) : process.cwd(), {
6464
parameters: [filePath] // *sigh*
6565
});
6666

67+
const DependencyTreeProvider = require("./graph/providers/DependencyTree");
6768
const provider = new DependencyTreeProvider({
68-
tree
69+
dependencyTree
6970
});
7071

7172
const projectGraph = await projectGraphBuilder(provider);
7273

7374
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride});
7475

76+
return projectGraph;
77+
},
78+
79+
/**
80+
* Generates a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph} from a
81+
* YAML file following the structure of the
82+
* [@ui5/project.graph.projectGraphFromTree]{@link module:@ui5/project.graph.projectGraphFromTree} API
83+
*
84+
* @public
85+
* @param {object} options
86+
* @param {module:@ui5/project.graph.providers.DependencyTree.TreeNode} options.dependencyTree
87+
* @param {string} [options.versionOverride] Framework version to use instead of the one defined in the root project
88+
* @returns {Promise<module:@ui5/project.graph.ProjectGraph>} Promise resolving to a Project Graph instance
89+
*/
90+
usingObject: async function({dependencyTree, versionOverride}) {
91+
log.verbose(`Creating project graph using object...`);
92+
93+
const DependencyTreeProvider = require("./graph/providers/DependencyTree");
94+
const dependencyTreeProvider = new DependencyTreeProvider({
95+
dependencyTree
96+
});
97+
98+
const projectGraph = await projectGraphBuilder(dependencyTreeProvider);
99+
100+
await ui5Framework.enrichProjectGraph(projectGraph, {versionOverride});
101+
75102
return projectGraph;
76103
}
77104
};
78105

79-
module.exports = projectGraphFromDirectory;
106+
module.exports = generateProjectGraph;

lib/graph/providers/ui5Framework.js renamed to lib/graph/helpers/ui5Framework.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const Module = require("../Module");
22
const ProjectGraph = require("../ProjectGraph");
3-
const log = require("@ui5/logger").getLogger("graph:providers:ui5Framework");
3+
const log = require("@ui5/logger").getLogger("graph:helpers:ui5Framework");
44

55
class ProjectProcessor {
66
constructor({libraryMetadata}) {

lib/graph/projectGraphFromTree.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/graph/providers/DependencyTree.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ class DependencyTree {
1717
* from a dependency tree as returned by translators.
1818
*
1919
* @public
20-
* @alias module:@ui5/project.graph.projectGraphFromTree
21-
* @param {TreeNode} tree Dependency tree as returned by a translator
20+
* @alias module:@ui5/project.graph.providers.DependencyTree
21+
* @param {object} parameters
22+
* @param {TreeNode} parameters.dependencyTree Dependency tree as returned by a translator
2223
*/
23-
constructor(tree) {
24-
if (!tree) {
25-
throw new Error(`Failed to instantiate DependencyTree provider: Missing parameter 'tree'`);
24+
constructor({dependencyTree}) {
25+
if (!dependencyTree) {
26+
throw new Error(`Failed to instantiate DependencyTree provider: Missing parameter 'dependencyTree'`);
2627
}
27-
this._tree= tree;
28+
this._tree = dependencyTree;
2829
}
2930

3031
async getRootNode() {

lib/graph/providers/npm.js renamed to lib/graph/providers/NodePackageDependencies.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const {promisify} = require("util");
55
const fs = require("graceful-fs");
66
const realpath = promisify(fs.realpath);
77
const resolveModulePath = promisify(require("resolve"));
8-
const log = require("@ui5/logger").getLogger("graph:providers:npm");
8+
const log = require("@ui5/logger").getLogger("graph:providers:NodePackageDependencies");
99

1010
// Packages to consider:
1111
// * https://github.com/npm/read-package-json-fast
1212
// * https://github.com/npm/name-from-folder ?
1313

1414

15-
class Npm {
15+
class NodePackageDependencies {
1616
/**
1717
* Generates a project graph from npm modules
1818
*
@@ -145,4 +145,4 @@ class Npm {
145145
}
146146
}
147147

148-
module.exports = Npm;
148+
module.exports = NodePackageDependencies;

lib/graph/projectGraphBuilder.js renamed to lib/graph/providers/projectGraphBuilder.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const path = require("path");
2-
const Module = require("./Module");
3-
const ProjectGraph = require("./ProjectGraph");
4-
const ShimCollection = require("./ShimCollection");
5-
const log = require("@ui5/logger").getLogger("graph:projectGraphBuilder");
2+
const Module = require("../Module");
3+
const ProjectGraph = require("../ProjectGraph");
4+
const ShimCollection = require("../ShimCollection");
5+
const log = require("@ui5/logger").getLogger("graph:providers:projectGraphBuilder");
66

77
function _handleExtensions(graph, shimCollection, extensions) {
88
extensions.forEach((extension) => {
@@ -64,8 +64,8 @@ function _handleExtensions(graph, shimCollection, extensions) {
6464
*/
6565

6666
/**
67-
* Helper module to create a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph}
68-
* from a dependency tree as returned by translators.
67+
* Generic helper module to create a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph}.
68+
* For example from a dependency tree as returned by the legacy "translators".
6969
*
7070
* @public
7171
* @alias module:@ui5/project.graph.projectGraphBuilder

lib/normalizer.js

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,6 @@ const Normalizer = {
4545
return tree;
4646
},
4747

48-
/**
49-
* Generates a [@ui5/project.graph.ProjectGraph]{@link module:@ui5/project.graph.ProjectGraph}
50-
*
51-
* @public
52-
* @param {object} [options]
53-
* @param {string} [options.cwd] Current working directory
54-
* @param {string} [options.configPath] Path to configuration file
55-
* @param {string} [options.translatorName] Translator to use
56-
* @param {object} [options.translatorOptions] Options to pass to translator
57-
* @param {object} [options.frameworkOptions] Options to pass to the framework installer
58-
* @param {string} [options.frameworkOptions.versionOverride]
59-
* Framework version to use instead of the one defined in the root project
60-
* framework
61-
* @returns {Promise<module:@ui5/project.graph.ProjectGraph>} Promise resolving to a Project Graph instance
62-
*/
63-
generateProjectGraph: async function(options = {}) {
64-
// const projectGraphFromTree = require("./graph/projectGraphFromTree");
65-
// const tree = await Normalizer.generateDependencyTree(options);
66-
const projectGraphFromDirectory = require("./graph/projectGraphFromDirectory");
67-
68-
const projectGraph = await projectGraphFromDirectory.usingNpm({
69-
rootConfigPath: options.configPath,
70-
versionOverride: options.versionOverride
71-
});
72-
73-
return projectGraph;
74-
},
75-
7648
/**
7749
* Generates a project and dependency tree via translators
7850
*

0 commit comments

Comments
 (0)