UNPKG

ng-zorro-antd-mobile

Version:

An enterprise-class mobile UI components based on Ant Design and Angular

98 lines 4.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildComponent = void 0; const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const ts = require("typescript"); const ast_utils_1 = require("./ast-utils"); const change_1 = require("./change"); const find_module_1 = require("./find-module"); const config_1 = require("./config"); const parse_name_1 = require("./parse-name"); const validation_1 = require("./validation"); function addDeclarationToNgModule(options) { return (host) => { if (options.skipImport || !options.module) { return host; } const modulePath = options.module; const text = host.read(modulePath); if (text === null) { throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`); } const sourceText = text.toString('utf-8'); const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); const componentPath = `/${options.path}/` + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') + core_1.strings.dasherize(options.name) + '.component'; const relativePath = (0, find_module_1.buildRelativePath)(modulePath, componentPath); const classifiedName = core_1.strings.classify(`${options.name}Component`); const declarationChanges = (0, ast_utils_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. const text = host.read(modulePath); if (text === null) { throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`); } const sourceText = text.toString('utf-8'); const source = ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); const exportRecorder = host.beginUpdate(modulePath); const exportChanges = (0, ast_utils_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); } return host; }; } function buildSelector(options) { let selector = core_1.strings.dasherize(options.name); if (options.prefix) { selector = `${options.prefix}-${selector}`; } return selector; } function buildComponent(options) { return (host, context) => { const workspace = (0, config_1.getWorkspace)(host); if (!options.project) { options.project = Object.keys(workspace.projects)[0]; } const project = workspace.projects[options.project]; if (options.path === undefined) { options.path = `/${project.root}/src/app`; } options.selector = options.selector || buildSelector(options); options.module = (0, find_module_1.findModuleFromOptions)(host, options); const parsedPath = (0, parse_name_1.parseName)(options.path, options.name); options.name = parsedPath.name; options.path = parsedPath.path; (0, validation_1.validateName)(options.name); const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [ options.spec ? (0, schematics_1.noop)() : (0, schematics_1.filter)(path => !path.endsWith('.spec.ts')), options.inlineStyle ? (0, schematics_1.filter)(path => !path.endsWith('.__styleext__')) : (0, schematics_1.noop)(), options.inlineTemplate ? (0, schematics_1.filter)(path => !path.endsWith('.html')) : (0, schematics_1.noop)(), (0, schematics_1.template)(Object.assign(Object.assign(Object.assign({}, core_1.strings), { 'if-flat': (s) => options.flat ? '' : s }), options)), (0, schematics_1.move)(null, parsedPath.path), ]); return (0, schematics_1.chain)([ (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([ addDeclarationToNgModule(options), (0, schematics_1.mergeWith)(templateSource), ])), ])(host, context); }; } exports.buildComponent = buildComponent; //# sourceMappingURL=component.js.map