UNPKG

1.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.insertStatement = void 0;
4const devkit_1 = require("@nrwl/devkit");
5const typescript_1 = require("typescript");
6/**
7 * Insert a statement after the last import statement in a file
8 */
9function insertStatement(tree, path, statement) {
10 const contents = tree.read(path, 'utf-8');
11 const sourceFile = (0, typescript_1.createSourceFile)(path, contents, typescript_1.ScriptTarget.ESNext);
12 const importStatements = sourceFile.statements.filter(typescript_1.isImportDeclaration);
13 const index = importStatements.length > 0
14 ? importStatements[importStatements.length - 1].getEnd()
15 : 0;
16 if (importStatements.length > 0) {
17 statement = `\n${statement}`;
18 }
19 const newContents = (0, devkit_1.applyChangesToString)(contents, [
20 {
21 type: devkit_1.ChangeType.Insert,
22 index,
23 text: statement,
24 },
25 ]);
26 tree.write(path, newContents);
27}
28exports.insertStatement = insertStatement;
29//# sourceMappingURL=insert-statement.js.map
\No newline at end of file