@o3r/schematics
Version:
Schematics module of the Otter framework
169 lines • 7.17 kB
JavaScript
;
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.getFilesWithExtensionFromTree = getFilesWithExtensionFromTree;
exports.getFilesFromRootOfWorkspaceProjects = getFilesFromRootOfWorkspaceProjects;
exports.getFilesFromWorkspaceProjects = getFilesFromWorkspaceProjects;
exports.getSourceFilesFromWorkspaceProjects = getSourceFilesFromWorkspaceProjects;
const path = require("node:path");
const schematics_1 = require("@angular-devkit/schematics");
const dependencies_1 = require("@schematics/angular/utility/dependencies");
const minimatch_1 = require("minimatch");
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 Package 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 search in the tree
*/
function globInTree(tree, patterns) {
const files = getAllFilesInTree(tree);
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 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');
}
//# sourceMappingURL=loaders.js.map