UNPKG

@o3r/schematics

Version:

Schematics module of the Otter framework

29 lines 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addImportsRule = void 0; const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const change_1 = require("@schematics/angular/utility/change"); const ts = 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