UNPKG

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