@nxext/stencil
Version:
Nx plugin for stenciljs
38 lines • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.readTsSourceFile = readTsSourceFile;
exports.addImport = addImport;
exports.addAfterLastImport = addAfterLastImport;
const devkit_1 = require("@nx/devkit");
const tsquery_1 = require("@phenomnomnominal/tsquery");
function readTsSourceFile(host, path) {
if (!host.exists(path)) {
throw new Error(`Typescript file not readable (${path}).`);
}
else {
const contentBuffer = host.read(path);
return tsquery_1.tsquery.ast(contentBuffer.toString());
}
}
function addImport(source, statement) {
return [addAfterLastImport(source, statement)];
}
function addAfterLastImport(source, statement) {
const allImports = (0, tsquery_1.tsquery)(source, '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