@o3r/schematics
Version:
Schematics module of the Otter framework
30 lines • 1.22 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addImportsRule = void 0;
const tslib_1 = require("tslib");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const change_1 = require("@schematics/angular/utility/change");
const ts = tslib_1.__importStar(require("typescript"));
/**
* Rule to add imports to a file
* @param filePath
* @param imports
*/
const addImportsRule = (filePath, imports) => (tree, context) => {
if (!tree.exists(filePath)) {
context.logger.warn(`No update applied on file because ${filePath} does not exist.`);
return;
}
imports.forEach(({ importNames, from }) => {
importNames.forEach((importName) => {
const sourceFile = ts.createSourceFile(filePath, tree.readText(filePath), ts.ScriptTarget.ES2020, true);
const recorder = tree.beginUpdate(filePath);
const change = (0, ast_utils_1.insertImport)(sourceFile, filePath, importName, from);
(0, change_1.applyToUpdateRecorder)(recorder, [change]);
tree.commitUpdate(recorder);
});
});
return tree;
};
exports.addImportsRule = addImportsRule;
//# sourceMappingURL=index.js.map