UNPKG

@o3r/schematics

Version:

Schematics module of the Otter framework

208 lines • 9.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.findFilesInTree = findFilesInTree; exports.getWorkspaceConfig = getWorkspaceConfig; exports.writeAngularJson = writeAngularJson; exports.readPackageJson = readPackageJson; exports.getProjectNewDependenciesTypes = getProjectNewDependenciesTypes; exports.getTemplateFolder = getTemplateFolder; exports.getAllFilesInTree = getAllFilesInTree; exports.globInTree = globInTree; exports.getFilesInFolderFromWorkspaceProjectsInTree = getFilesInFolderFromWorkspaceProjectsInTree; exports.getComponentFilesInFolderFromWorkspaceProjectsInTree = getComponentFilesInFolderFromWorkspaceProjectsInTree; exports.getFilesWithExtensionFromTree = getFilesWithExtensionFromTree; exports.getFilesFromRootOfWorkspaceProjects = getFilesFromRootOfWorkspaceProjects; exports.getFilesFromWorkspaceProjects = getFilesFromWorkspaceProjects; exports.getSourceFilesFromWorkspaceProjects = getSourceFilesFromWorkspaceProjects; exports.getAllDependencies = getAllDependencies; const tslib_1 = require("tslib"); const path = tslib_1.__importStar(require("node:path")); const schematics_1 = require("@angular-devkit/schematics"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const dependencies_1 = require("@schematics/angular/utility/dependencies"); const minimatch_1 = require("minimatch"); const ts = tslib_1.__importStar(require("typescript")); function findFilesInTreeRec(memory, directory, fileMatchesCriteria, ignoreDirectories) { if (ignoreDirectories.some((i) => directory.path.split(path.posix.sep).includes(i))) { return memory; } directory.subfiles .filter((file) => fileMatchesCriteria(file)) .forEach((file) => memory.add(directory.file(file))); directory.subdirs .forEach((dir) => findFilesInTreeRec(memory, directory.dir(dir), fileMatchesCriteria, ignoreDirectories)); return memory; } /** * * Helper function that looks for files in the Tree * @param directory where to perform the search * @param fileMatchesCriteria a function defining the criteria to look for * @param ignoreDirectories optional parameter to ignore folders */ function findFilesInTree(directory, fileMatchesCriteria, ignoreDirectories = ['node_modules', '.git', '.yarn']) { const memory = new Set(); findFilesInTreeRec(memory, directory, fileMatchesCriteria, ignoreDirectories); return Array.from(memory); } /** * Load the Workspace configuration object * @param tree File tree * @param workspaceConfigFile Workspace config file path, /angular.json in an Angular project * @returns null if the given config file does not exist */ function getWorkspaceConfig(tree, workspaceConfigFile = '/angular.json') { if (!tree.exists(workspaceConfigFile)) { return null; } return tree.readJson(workspaceConfigFile); } /** * Update angular.json file * @param tree File tree * @param workspace Angular workspace * @param angularJsonFile Angular.json file path */ function writeAngularJson(tree, workspace, angularJsonFile = '/angular.json') { tree.overwrite(angularJsonFile, JSON.stringify(workspace, null, 2)); return tree; } /** * Load the target's package.json file * @param tree File tree * @param workspaceProject Angular workspace project * @throws {SchematicsException} JSON invalid or non exist */ function readPackageJson(tree, workspaceProject) { const packageJsonPath = `${workspaceProject.root}/package.json`; if (!tree.exists(packageJsonPath)) { throw new schematics_1.SchematicsException('Could not find NPM Package'); } const workspaceConfig = tree.readJson(packageJsonPath); return workspaceConfig; } /** * Return the types of install to run depending on the project type * @param project */ function getProjectNewDependenciesTypes(project) { return project?.projectType === 'library' ? [dependencies_1.NodeDependencyType.Peer, dependencies_1.NodeDependencyType.Dev] : [dependencies_1.NodeDependencyType.Default]; } /** * Get the folder of the templates for a specific sub-schematics * @param rootPath Root directory of the schematics ran * @param currentPath Directory of the current sub-schematics ran * @param templateFolder Folder containing the templates */ function getTemplateFolder(rootPath, currentPath, templateFolder = 'templates') { const templateFolderPath = path.resolve(currentPath, templateFolder).replace(/\\/g, '/'); return path.relative(rootPath, templateFolderPath); } /** * Get the path of all the files in the Tree * @param tree Schematics file tree * @param basePath Base path from which starting the list * @param excludes Array of globs to be ignored * @param recursive determine if the function will walk through the sub folders */ function getAllFilesInTree(tree, basePath = '/', excludes = [], recursive = true) { if (excludes.some((e) => (0, minimatch_1.minimatch)(basePath, e, { dot: true }))) { return []; } return [ ...tree.getDir(basePath).subfiles.map((file) => path.posix.join(basePath, file)), ...(recursive ? tree.getDir(basePath).subdirs.flatMap((dir) => getAllFilesInTree(tree, path.posix.join(basePath, dir), excludes, recursive)) : []) ]; } /** * Get the path of all the files in the Tree * @param tree Schematics file tree * @param patterns Array of globs to be searched in the tree * @param excludes Array of globs to be ignored */ function globInTree(tree, patterns, excludes = []) { const files = getAllFilesInTree(tree, '/', excludes); return files.filter((basePath) => patterns.some((p) => (0, minimatch_1.minimatch)(basePath, p, { dot: true }))); } /** * Get all files with specific extension from the specified folder for all the projects described in the workspace * @param tree * @param folderInProject * @param extension */ function getFilesInFolderFromWorkspaceProjectsInTree(tree, folderInProject, extension) { const workspace = getWorkspaceConfig(tree); const extensionMatcher = new RegExp(`\\.${extension.replace(/^\./, '')}$`); const excludes = ['**/node_modules/**', '**/.cache/**']; return Object.values(workspace?.projects || {}) .flatMap((project) => getAllFilesInTree(tree, path.posix.join(project.root, folderInProject), excludes)) .filter((filePath) => extensionMatcher.test(filePath)); } /** * Get all component files from the specified folder for all the projects described in the workspace * @param tree * @param folderInProject */ function getComponentFilesInFolderFromWorkspaceProjectsInTree(tree, folderInProject) { const workspace = getWorkspaceConfig(tree); const excludes = ['**/node_modules/**', '**/.cache/**']; return Object.values(workspace?.projects || {}) .flatMap((project) => getAllFilesInTree(tree, path.posix.join(project.root, folderInProject), excludes)) .filter((filePath) => /^(?!.*\.spec\.ts$).*\.ts$/.test(filePath)) .filter((filePath) => { const fileContent = tree.readText(filePath); const sourceFile = ts.createSourceFile(filePath, fileContent, ts.ScriptTarget.ES2015, true); return (0, ast_utils_1.isImported)(sourceFile, 'Component', '@angular/core') && fileContent.includes('@Component'); }); } /** * Get all files with specific extension from the tree * @param tree * @param extension */ function getFilesWithExtensionFromTree(tree, extension) { const excludes = ['**/node_modules/**', '**/.cache/**']; const extensionMatcher = new RegExp(`\\.${extension}$`); return getAllFilesInTree(tree, '/', excludes) .filter((filePath) => extensionMatcher.test(filePath)); } /** * Get all files with specific extension from the root of all the projects described in the workspace * @param tree * @param extension */ function getFilesFromRootOfWorkspaceProjects(tree, extension) { return getFilesInFolderFromWorkspaceProjectsInTree(tree, '', extension); } /** * Get all files with specific extension from the src folder for all the projects described in the workspace * @param tree * @param extension */ function getFilesFromWorkspaceProjects(tree, extension) { return getFilesInFolderFromWorkspaceProjectsInTree(tree, 'src', extension); } /** * Get all the typescript files from the src folder for all the projects described in the workspace * @param tree */ function getSourceFilesFromWorkspaceProjects(tree) { return getFilesFromWorkspaceProjects(tree, 'ts'); } /** * Get all dependencies from all the package.json files in the tree * @param tree * @param dependencyTypes */ function getAllDependencies(tree, dependencyTypes = ['dependencies', 'devDependencies']) { const packageJsonPaths = globInTree(tree, ['**/package.json'], ['**/node_modules']); const allDependencies = new Set(); for (const packageJsonPath of packageJsonPaths) { const packageJson = tree.readJson(packageJsonPath); for (const depType of dependencyTypes) { Object.keys(packageJson[depType] || {}).forEach((dep) => allDependencies.add(dep)); } } return allDependencies; } //# sourceMappingURL=loaders.js.map