1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createGlobPatternsForDependencies = void 0;
|
4 | const devkit_1 = require("@nrwl/devkit");
|
5 | const workspace_root_1 = require("nx/src/utils/workspace-root");
|
6 | const path_1 = require("path");
|
7 | const project_graph_1 = require("nx/src/project-graph/project-graph");
|
8 | const project_graph_utils_1 = require("nx/src/utils/project-graph-utils");
|
9 | const fs_1 = require("fs");
|
10 | const ignore_1 = require("ignore");
|
11 | const find_project_for_path_1 = require("nx/src/project-graph/utils/find-project-for-path");
|
12 | function 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 |
|
23 |
|
24 |
|
25 |
|
26 | function 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 |
|
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 |
|
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
|
71 | due 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 | }
|
80 | exports.createGlobPatternsForDependencies = createGlobPatternsForDependencies;
|
81 |
|
\ | No newline at end of file |