133 lines
6.3 KiB
JavaScript
Executable file
133 lines
6.3 KiB
JavaScript
Executable file
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
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 parse_name_1 = require("@schematics/angular/utility/parse-name");
|
|
const validation_1 = require("@schematics/angular/utility/validation");
|
|
const workspace_1 = require("@schematics/angular/utility/workspace");
|
|
const ts = require("typescript");
|
|
const util_1 = require("../util");
|
|
const ast_util_1 = require("../util/ast-util");
|
|
const change_1 = require("../util/change");
|
|
function readIntoSourceFile(host, modulePath) {
|
|
const text = host.read(modulePath);
|
|
if (text === null) {
|
|
throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`);
|
|
}
|
|
const sourceText = text.toString('utf-8');
|
|
return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true);
|
|
}
|
|
function addImportToNgModule(options) {
|
|
return (host) => {
|
|
if (!options.module) {
|
|
return host;
|
|
}
|
|
if (!options.createModule && options.module) {
|
|
addImportToDeclarations(host, options);
|
|
}
|
|
if (options.createModule && options.module) {
|
|
addImportToImports(host, options);
|
|
}
|
|
return host;
|
|
};
|
|
}
|
|
function addImportToDeclarations(host, options) {
|
|
if (options.module) {
|
|
const modulePath = options.module;
|
|
let source = readIntoSourceFile(host, modulePath);
|
|
const componentPath = `/${options.path}/` +
|
|
(options.flat ? '' : core_1.strings.dasherize(options.name) + '/') +
|
|
core_1.strings.dasherize(options.name) +
|
|
'.component';
|
|
const relativePath = find_module_1.buildRelativePath(modulePath, componentPath);
|
|
const classifiedName = core_1.strings.classify(`${options.name}Component`);
|
|
const declarationChanges = ast_util_1.addDeclarationToModule(source, modulePath, classifiedName, relativePath);
|
|
const declarationRecorder = host.beginUpdate(modulePath);
|
|
for (const change of declarationChanges) {
|
|
if (change instanceof change_1.InsertChange) {
|
|
declarationRecorder.insertLeft(change.pos, change.toAdd);
|
|
}
|
|
}
|
|
host.commitUpdate(declarationRecorder);
|
|
if (options.export) {
|
|
// Need to refresh the AST because we overwrote the file in the host.
|
|
source = readIntoSourceFile(host, modulePath);
|
|
const exportRecorder = host.beginUpdate(modulePath);
|
|
const exportChanges = ast_util_1.addExportToModule(source, modulePath, core_1.strings.classify(`${options.name}Component`), relativePath);
|
|
for (const change of exportChanges) {
|
|
if (change instanceof change_1.InsertChange) {
|
|
exportRecorder.insertLeft(change.pos, change.toAdd);
|
|
}
|
|
}
|
|
host.commitUpdate(exportRecorder);
|
|
}
|
|
if (options.entryComponent) {
|
|
// Need to refresh the AST because we overwrote the file in the host.
|
|
source = readIntoSourceFile(host, modulePath);
|
|
const entryComponentRecorder = host.beginUpdate(modulePath);
|
|
const entryComponentChanges = ast_util_1.addEntryComponentToModule(source, modulePath, core_1.strings.classify(`${options.name}Component`), relativePath);
|
|
for (const change of entryComponentChanges) {
|
|
if (change instanceof change_1.InsertChange) {
|
|
entryComponentRecorder.insertLeft(change.pos, change.toAdd);
|
|
}
|
|
}
|
|
host.commitUpdate(entryComponentRecorder);
|
|
}
|
|
}
|
|
}
|
|
function addImportToImports(host, options) {
|
|
if (options.module) {
|
|
const modulePath = options.module;
|
|
const moduleSource = readIntoSourceFile(host, modulePath);
|
|
const componentModulePath = `/${options.path}/` +
|
|
(options.flat ? '' : core_1.strings.dasherize(options.name) + '/') +
|
|
core_1.strings.dasherize(options.name) +
|
|
'.module';
|
|
const relativePath = find_module_1.buildRelativePath(modulePath, componentModulePath);
|
|
const classifiedName = core_1.strings.classify(`${options.name}ComponentModule`);
|
|
const importChanges = ast_util_1.addSymbolToNgModuleMetadata(moduleSource, modulePath, 'imports', classifiedName, relativePath);
|
|
const importRecorder = host.beginUpdate(modulePath);
|
|
for (const change of importChanges) {
|
|
if (change instanceof change_1.InsertChange) {
|
|
importRecorder.insertLeft(change.pos, change.toAdd);
|
|
}
|
|
}
|
|
host.commitUpdate(importRecorder);
|
|
}
|
|
}
|
|
function default_1(options) {
|
|
return async (host) => {
|
|
var _a;
|
|
if (!options.project) {
|
|
throw new schematics_1.SchematicsException('Option (project) is required.');
|
|
}
|
|
const workspace = await workspace_1.getWorkspace(host);
|
|
const project = workspace.projects.get(options.project);
|
|
if (project && options.path === undefined) {
|
|
options.path = workspace_1.buildDefaultPath(project);
|
|
}
|
|
const parsedPath = parse_name_1.parseName(options.path, options.name);
|
|
options.name = parsedPath.name;
|
|
options.path = parsedPath.path;
|
|
options.selector = options.selector
|
|
? options.selector
|
|
: util_1.buildSelector(options, (_a = project === null || project === void 0 ? void 0 : project.prefix) !== null && _a !== void 0 ? _a : 'app');
|
|
validation_1.validateName(options.name);
|
|
validation_1.validateHtmlSelector(options.selector);
|
|
const templateSource = schematics_1.apply(schematics_1.url('./files'), [
|
|
options.spec ? schematics_1.noop() : schematics_1.filter(p => !p.endsWith('.spec.ts')),
|
|
options.createModule ? schematics_1.noop() : schematics_1.filter(p => !p.endsWith('.module.ts')),
|
|
schematics_1.template({
|
|
...core_1.strings,
|
|
'if-flat': (s) => (options.flat ? '' : s),
|
|
...options,
|
|
}),
|
|
schematics_1.move(parsedPath.path),
|
|
]);
|
|
return schematics_1.chain([
|
|
schematics_1.branchAndMerge(schematics_1.chain([addImportToNgModule(options), schematics_1.mergeWith(templateSource)])),
|
|
]);
|
|
};
|
|
}
|
|
exports.default = default_1;
|