UNPKG

4.08 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createGlobPatternsForDependencies = void 0;
4const devkit_1 = require("@nrwl/devkit");
5const workspace_root_1 = require("nx/src/utils/workspace-root");
6const path_1 = require("path");
7const project_graph_1 = require("nx/src/project-graph/project-graph");
8const project_graph_utils_1 = require("nx/src/utils/project-graph-utils");
9const fs_1 = require("fs");
10const ignore_1 = require("ignore");
11const find_project_for_path_1 = require("nx/src/project-graph/utils/find-project-for-path");
12function configureIgnore() {
13 let ig;
14 const pathToGitIgnore = (0, path_1.join)(workspace_root_1.workspaceRoot, '.gitignore');
15 if ((0, fs_1.existsSync)(pathToGitIgnore)) {
16 ig = (0, ignore_1.default)();
17 ig.add((0, fs_1.readFileSync)(pathToGitIgnore, { encoding: 'utf-8' }));
18 }
19 return ig;
20}
21/**
22 * Generates a set of glob patterns based off the source root of the app and its dependencies
23 * @param dirPath workspace relative directory path that will be used to infer the parent project and dependencies
24 * @param fileGlobPattern pass a custom glob pattern to be used
25 */
26function createGlobPatternsForDependencies(dirPath, fileGlobPattern) {
27 let ig = configureIgnore();
28 const filenameRelativeToWorkspaceRoot = (0, devkit_1.normalizePath)((0, path_1.relative)(workspace_root_1.workspaceRoot, dirPath));
29 const projectGraph = (0, project_graph_1.readCachedProjectGraph)();
30 const projectRootMappings = (0, find_project_for_path_1.createProjectRootMappings)(projectGraph.nodes);
31 // find the project
32 let projectName;
33 try {
34 projectName = (0, find_project_for_path_1.findProjectForPath)(filenameRelativeToWorkspaceRoot, projectRootMappings);
35 if (!projectName) {
36 throw new Error(`Could not find any project containing the file "${filenameRelativeToWorkspaceRoot}" among it's project files`);
37 }
38 }
39 catch (e) {
40 throw new Error(`createGlobPatternsForDependencies: Error when trying to determine main project.\n${e === null || e === void 0 ? void 0 : e.message}`);
41 }
42 // generate the glob
43 try {
44 const [projectDirs, warnings] = (0, project_graph_utils_1.getSourceDirOfDependentProjects)(projectName, projectGraph);
45 const dirsToUse = [];
46 const recursiveScanDirs = (dirPath) => {
47 const children = (0, fs_1.readdirSync)((0, path_1.resolve)(workspace_root_1.workspaceRoot, dirPath));
48 for (const child of children) {
49 const childPath = (0, path_1.join)(dirPath, child);
50 if ((ig === null || ig === void 0 ? void 0 : ig.ignores(childPath)) ||
51 !(0, fs_1.lstatSync)((0, path_1.resolve)(workspace_root_1.workspaceRoot, childPath)).isDirectory()) {
52 continue;
53 }
54 if ((0, fs_1.existsSync)((0, path_1.join)(workspace_root_1.workspaceRoot, childPath, 'ng-package.json'))) {
55 dirsToUse.push(childPath);
56 }
57 else {
58 recursiveScanDirs(childPath);
59 }
60 }
61 };
62 for (const srcDir of projectDirs) {
63 dirsToUse.push(srcDir);
64 const root = (0, path_1.dirname)(srcDir);
65 recursiveScanDirs(root);
66 }
67 if (warnings.length > 0) {
68 devkit_1.logger.warn(`
69[createGlobPatternsForDependencies] Failed to generate glob pattern for the following:
70${warnings.join('\n- ')}\n
71due to missing "sourceRoot" in the dependencies' project configuration
72 `);
73 }
74 return dirsToUse.map((sourceDir) => (0, path_1.resolve)(workspace_root_1.workspaceRoot, (0, devkit_1.joinPathFragments)(sourceDir, fileGlobPattern)));
75 }
76 catch (e) {
77 throw new Error(`createGlobPatternsForDependencies: Error when generating globs.\n${e === null || e === void 0 ? void 0 : e.message}`);
78 }
79}
80exports.createGlobPatternsForDependencies = createGlobPatternsForDependencies;
81//# sourceMappingURL=generate-globs.js.map
\No newline at end of file