@nxext/stencil
Version:
Nx plugin for stenciljs
39 lines • 1.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addImport = exports.readTsSourceFile = void 0;
const ts = require("typescript");
const devkit_1 = require("@nrwl/devkit");
const ast_utils_1 = require("@nrwl/workspace/src/utils/ast-utils");
function readTsSourceFile(host, path) {
if (!host.exists(path)) {
throw new Error(`Typescript file not readable (${path}).`);
}
else {
const contentBuffer = host.read(path);
return ts.createSourceFile(path, contentBuffer.toString(), ts.ScriptTarget.Latest, true);
}
}
exports.readTsSourceFile = readTsSourceFile;
function addImport(source, statement) {
return [addAfterLastImport(source, statement)];
}
exports.addImport = addImport;
function addAfterLastImport(source, statement) {
const allImports = (0, ast_utils_1.findNodes)(source, ts.SyntaxKind.ImportDeclaration);
if (allImports.length > 0) {
const lastImport = allImports[allImports.length - 1];
return ({
type: devkit_1.ChangeType.Insert,
index: lastImport.end + 1,
text: `\n${statement}\n`
});
}
else {
return ({
type: devkit_1.ChangeType.Insert,
index: 0,
text: `\n${statement}\n`
});
}
}
//# sourceMappingURL=ast-utils.js.map