UNPKG

@nrwl/workspace

Version:

The Workspace plugin contains executors and generators that are useful for any Nx workspace. It should be present in every Nx workspace and other plugins build on it.

75 lines 3.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGlobPatternsForDependencies = void 0; const devkit_1 = require("@nrwl/devkit"); const workspace_root_1 = require("nx/src/utils/workspace-root"); const path_1 = require("path"); const project_graph_1 = require("nx/src/project-graph/project-graph"); const project_graph_utils_1 = require("nx/src/utils/project-graph-utils"); const fs_1 = require("fs"); const ignore_1 = require("ignore"); function configureIgnore() { let ig; const pathToGitIgnore = (0, path_1.join)(workspace_root_1.workspaceRoot, '.gitignore'); if ((0, fs_1.existsSync)(pathToGitIgnore)) { ig = (0, ignore_1.default)(); ig.add((0, fs_1.readFileSync)(pathToGitIgnore, { encoding: 'utf-8' })); } return ig; } /** * Generates a set of glob patterns based off the source root of the app and its dependencies * @param dirPath workspace relative directory path that will be used to infer the parent project and dependencies * @param fileGlobPattern pass a custom glob pattern to be used */ function createGlobPatternsForDependencies(dirPath, fileGlobPattern) { let ig = configureIgnore(); const filenameRelativeToWorkspaceRoot = (0, path_1.relative)(workspace_root_1.workspaceRoot, dirPath); const projectGraph = (0, project_graph_1.readCachedProjectGraph)(); // find the project let projectName; try { projectName = (0, project_graph_utils_1.getProjectNameFromDirPath)(filenameRelativeToWorkspaceRoot, projectGraph); } catch (e) { throw new Error(`createGlobPatternsForDependencies: Error when trying to determine main project.\n${e === null || e === void 0 ? void 0 : e.message}`); } // generate the glob try { const [projectDirs, warnings] = (0, project_graph_utils_1.getSourceDirOfDependentProjects)(projectName, projectGraph); const dirsToUse = []; const recursiveScanDirs = (dirPath) => { const children = (0, fs_1.readdirSync)(dirPath); for (const child of children) { const childPath = (0, path_1.join)(dirPath, child); if ((ig === null || ig === void 0 ? void 0 : ig.ignores(childPath)) || !(0, fs_1.lstatSync)(childPath).isDirectory()) { continue; } if ((0, fs_1.existsSync)((0, path_1.join)(childPath, 'ng-package.json'))) { dirsToUse.push(childPath); } else { recursiveScanDirs(childPath); } } }; for (const srcDir of projectDirs) { dirsToUse.push(srcDir); const root = (0, path_1.dirname)(srcDir); recursiveScanDirs(root); } if (warnings.length > 0) { devkit_1.logger.warn(` [createGlobPatternsForDependencies] Failed to generate glob pattern for the following: ${warnings.join('\n- ')}\n due to missing "sourceRoot" in the dependencies' project configuration `); } return dirsToUse.map((sourceDir) => (0, path_1.resolve)(workspace_root_1.workspaceRoot, (0, devkit_1.joinPathFragments)(sourceDir, fileGlobPattern))); } catch (e) { throw new Error(`createGlobPatternsForDependencies: Error when generating globs.\n${e === null || e === void 0 ? void 0 : e.message}`); } } exports.createGlobPatternsForDependencies = createGlobPatternsForDependencies; //# sourceMappingURL=generate-globs.js.map