@o3r/core
Version:
Core of the Otter Framework
41 lines • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertToO3rComponent = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@o3r/schematics");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const change_1 = require("@schematics/angular/utility/change");
const ts = require("typescript");
/**
* Convert an Angular component into an Otter component
* @param options
*/
function convertToO3rComponentFn(options) {
const updateComponentFile = (tree) => {
const sourceFile = ts.createSourceFile(options.path, tree.readText(options.path), ts.ScriptTarget.ES2020, true);
const recorder = tree.beginUpdate(options.path);
const changes = [];
changes.push((0, ast_utils_1.insertImport)(sourceFile, options.path, 'O3rComponent', '@o3r/core'));
const ngComponentDeclaration = sourceFile.statements.find((s) => ts.isClassDeclaration(s) && (0, schematics_2.isNgClassComponent)(s));
if (!ngComponentDeclaration) {
throw new schematics_2.O3rCliError(`No Angular component found in ${options.path}.`);
}
if ((0, schematics_2.isO3rClassComponent)(ngComponentDeclaration)) {
throw new schematics_2.O3rCliError(`${ngComponentDeclaration.name?.escapedText.toString() || 'It'} is already an Otter component.`);
}
changes.push(new change_1.InsertChange(options.path, ngComponentDeclaration.getStart(), `@O3rComponent({ componentType: '${options.componentType}' })\n`));
(0, change_1.applyToUpdateRecorder)(recorder, changes);
tree.commitUpdate(recorder);
return tree;
};
return (0, schematics_1.chain)([
updateComponentFile,
options.skipLinter ? (0, schematics_1.noop)() : (0, schematics_2.applyEsLintFix)()
]);
}
/**
* Convert an Angular component into an Otter component
* @param options
*/
exports.convertToO3rComponent = (0, schematics_2.createOtterSchematic)(convertToO3rComponentFn);
//# sourceMappingURL=index.js.map