@o3r/schematics
Version:
Schematics module of the Otter framework
82 lines • 4.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePipes = void 0;
const node_path_1 = require("node:path");
const ts = require("typescript");
const ast_1 = require("./ast");
const component_1 = require("./component");
const loaders_1 = require("./loaders");
const applyChanges = (pipeReplacementInfo, ngDecorator, matchers, fileWithImports, templateFile, tree, context) => {
if (ts.isObjectLiteralExpression(ngDecorator.expression.arguments[0])) {
const importsProp = ngDecorator.expression.arguments[0].properties.find((prop) => ts.isPropertyAssignment(prop)
&& ts.isIdentifier(prop.name)
&& prop.name.text === 'imports'
&& ts.isArrayLiteralExpression(prop.initializer));
if (importsProp) {
matchers.forEach((matcher) => {
const pipeName = matcher[1];
const importModuleRegexp = new RegExp(`\\b${pipeReplacementInfo[pipeName].import}\\b`, 'g');
if (importsProp.initializer.elements.some((element) => importModuleRegexp.test(element.getText()))) {
if (pipeReplacementInfo[pipeName].new.import) {
tree.overwrite(fileWithImports, tree.readText(fileWithImports).replaceAll(importModuleRegexp, pipeReplacementInfo[pipeName].new.import));
}
tree.overwrite(templateFile, tree.readText(templateFile).replaceAll(new RegExp(`\\|\\s*${pipeName}`, 'g'), `| ${pipeReplacementInfo[pipeName].new.name}`));
}
else {
context.logger.warn(`Deprecated pipe ${pipeName} was found in ${templateFile} `
+ `but no associated import in ${fileWithImports}`
+ `to be able to migrate to the new pipe ${pipeReplacementInfo[pipeName].new.name}.`);
}
});
}
}
};
const isNgModuleDecorator = (decorator) => ts.isCallExpression(decorator.expression)
&& decorator.expression.expression
&& ts.isIdentifier(decorator.expression.expression)
&& decorator.expression.expression.text === 'NgModule';
/**
* Returns a rule tu update pipes
* @param pipeReplacementInfo
*/
const updatePipes = (pipeReplacementInfo) => (tree, context) => {
const pipeRegex = new RegExp(`\\|\\s*(${Object.keys(pipeReplacementInfo).join('|')})`, 'g');
const files = (0, loaders_1.findFilesInTree)(tree.root, (file) => file.endsWith('.html'));
files.forEach((file) => {
const matchers = Array.from(file.content.toString().matchAll(pipeRegex)).filter((matcher) => !!pipeReplacementInfo[matcher[1]]);
if (matchers.length > 0) {
const directory = (0, node_path_1.dirname)(file.path);
const baseFileName = (0, node_path_1.basename)((0, node_path_1.basename)(file.path, '.html'), '.template');
const componentFile = (0, node_path_1.join)(directory, `${baseFileName}.component.ts`);
const moduleFile = (0, node_path_1.join)(directory, `${baseFileName}.module.ts`);
let standalone = true;
if (tree.exists(componentFile)) {
try {
const info = (0, component_1.getO3rComponentInfoOrThrowIfNotFound)(tree, componentFile);
standalone = info.standalone;
}
catch { }
}
if (standalone) {
const componentSourceFile = ts.createSourceFile(componentFile, tree.readText(componentFile), ts.ScriptTarget.ES2020, true);
const ngClass = componentSourceFile.statements.find((statement) => ts.isClassDeclaration(statement)
&& (0, component_1.isNgClassComponent)(statement));
const ngDecorator = ((ngClass && ts.getDecorators(ngClass)) || []).find((decorator) => (0, component_1.isNgClassDecorator)(decorator));
if (ngDecorator) {
applyChanges(pipeReplacementInfo, ngDecorator, matchers, componentFile, file.path, tree, context);
}
}
else if (tree.exists(moduleFile)) {
const moduleSourceFile = ts.createSourceFile(moduleFile, tree.readText(moduleFile), ts.ScriptTarget.ES2020, true);
const ngModuleClass = moduleSourceFile.statements.find((statement) => ts.isClassDeclaration(statement)
&& (ts.getDecorators(statement) || []).some((decorator) => isNgModuleDecorator(decorator)));
const ngDecorator = ((ngModuleClass && ts.getDecorators(ngModuleClass)) || []).find((decorator) => isNgModuleDecorator(decorator));
if (ngDecorator && (0, ast_1.isDecoratorWithArg)(ngDecorator)) {
applyChanges(pipeReplacementInfo, ngDecorator, matchers, moduleFile, file.path, tree, context);
}
}
}
});
};
exports.updatePipes = updatePipes;
//# sourceMappingURL=update-pipes.js.map