ng-afelio
Version:
Extended Angular CLI
72 lines (71 loc) • 3.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.relativeCwdFromRelativeProjectPath = exports.addIntoIndex = void 0;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const find_module_1 = require("@schematics/angular/utility/find-module");
const ts = require("typescript");
const ast_util_1 = require("./ast-util");
const change_1 = require("./change");
/**
* @param type something like 'component'
*/
function getListNode(source, type) {
const keywords = (0, ast_util_1.findNodes)(source, ts.SyntaxKind.VariableStatement);
for (const keyword of keywords) {
if (ts.isVariableStatement(keyword)) {
const [declaration] = keyword.declarationList.declarations;
if (ts.isVariableDeclaration(declaration) &&
declaration.initializer &&
declaration.name.getText() === type) {
return declaration.initializer.getChildAt(1);
}
}
}
}
/**
* @param type something like 'component'
*/
function addIntoIndex(path, options, type, barrelType) {
return host => {
if (options.barrel) {
const changes = [];
const indexPath = (0, core_1.join)(path, 'index.ts');
const text = host.read(indexPath);
if (!text) {
throw new schematics_1.SchematicsException(`Can not add to barrel, ${indexPath} does not exist.`);
}
const sourceText = text.toString('utf8');
const source = ts.createSourceFile(indexPath, sourceText, ts.ScriptTarget.Latest, true);
// Add Store to ts import
const filePath = (0, core_1.join)(path, options.name, `${core_1.strings.dasherize(options.name)}.${type}`);
const relativeFilePath = (0, find_module_1.buildRelativePath)(indexPath, `${filePath}.ts`).slice(0, -3);
const className = `${core_1.strings.classify(options.name)}${core_1.strings.capitalize(type)}`;
changes.push((0, ast_util_1.insertImport)(source, indexPath, className, relativeFilePath));
changes.push((0, ast_util_1.insertExport)(source, indexPath, className, relativeFilePath));
// Add Store to Barrel array
const node = getListNode(source, barrelType || `${type}s`);
if (node) {
const commat = node.getChildCount() > 0 ? ',' : '';
changes.push(new change_1.InsertChange(indexPath, node.getEnd(), `${commat}\n ${className}`));
}
// Save changes
(0, change_1.applyChangesToHost)(host, indexPath, changes);
}
return host;
};
}
exports.addIntoIndex = addIntoIndex;
function relativeCwdFromRelativeProjectPath(relativePath) {
let currentCwd = process.cwd();
if (process.platform.startsWith('win')) {
currentCwd = currentCwd.replace(/\\/g, '/');
}
if (currentCwd.includes(relativePath)) {
return '/' + currentCwd.substring(currentCwd.indexOf(relativePath) + 1);
}
else {
return relativePath;
}
}
exports.relativeCwdFromRelativeProjectPath = relativeCwdFromRelativeProjectPath;