@o3r/schematics
Version:
Schematics module of the Otter framework
62 lines • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerCollectionSchematics = registerCollectionSchematics;
exports.getDefaultOptionsForSchematic = getDefaultOptionsForSchematic;
exports.getSchematicOptions = getSchematicOptions;
/**
* Register the collection schematic to the workspace
* @param workspace Workspace to add the collection to
* @param collection Collection to add to the workspace schematic collection
* @returns the updated workspace
*/
function registerCollectionSchematics(workspace, collection) {
workspace.cli ||= {};
workspace.cli.schematicCollections ||= ['@schematics/angular'];
if (!workspace.cli.schematicCollections.includes(collection)) {
workspace.cli.schematicCollections.unshift(collection);
}
return workspace;
}
/**
* Get default options for a schematic
* This will look inside angular.json file for schematics with keys containing wildcards like `*:ng-add` or `@o3r/core:*`
* @param workspace
* @param collection
* @param schematicName
* @param options
* @param options.projectName
*/
function getDefaultOptionsForSchematic(workspace, collection, schematicName, options) {
if (!workspace) {
return {};
}
const schematicsDefaultParams = [
workspace.schematics,
...options?.projectName ? [workspace.projects[options.projectName]?.schematics] : []
];
return schematicsDefaultParams.reduce((acc, schematics) => {
if (!schematics) {
return acc;
}
return Object.entries(schematics)
.filter(([key, _]) => new RegExp(key.replace(/\*/g, '.*')).test(`${collection}:${schematicName}`))
.sort(([a], [b]) => (a.match(/\*/g)?.length || 0) - (b.match(/\*/g)?.length || 0))
.map(([_, value]) => value)
.reduce((config, value) => ({ ...config, ...value }), acc);
}, {});
}
/**
* Retrieves the schematics options of a given schematics name
* If the schematics name is not found, then the generic options (*:*) will be returned. If the latter is not present the function returns undefined
* @param config
* @param context
*/
function getSchematicOptions(config, context) {
const schematicName = `${context.schematic.description.collection.name}:${context.schematic.description.name}`;
const options = config.schematics && Object.entries(config.schematics)
.filter(([name]) => new RegExp(name.replace(/\*/g, '.*')).test(schematicName))
.sort(([a], [b]) => ((a.match(/\*/g)?.length || 0) - (b.match(/\*/g)?.length || 0)))
.reduce((acc, [, opts]) => ({ ...acc, ...opts }), {});
return options && Object.keys(options).length > 0 ? options : config.schematics?.['*:*'];
}
//# sourceMappingURL=collection.js.map