UNPKG

useasdemo

Version:

Schematics specific to ecip

383 lines (355 loc) 10.7 kB
import {Schema as ApplicationOptions} from "./schema"; import { apply, chain, filter, mergeWith, move, noop, template, url, MergeStrategy, Rule, SchematicContext, Tree, } from '@angular-devkit/schematics'; import {getProject, Project} from "../utils/project"; import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; import {addPackageToPackageJson, getJSON, getPackage, overwriteJSON, overwritePackage} from "../utils/json"; import * as path from "path"; import * as process from "child_process"; import {ECIPRBAC_VERSION} from "../utils/lib.version"; import {MODULE_ROUTE_CONTENTS, routingModuleContent} from "../utils/content"; let project: Project; const overwriteDataFileRoot = path.join(__dirname, 'overwrites'); /** 执行ng add ng-alain 暂时不用**/ function executeAddNgAlain(): Rule { return (host: Tree, context: SchematicContext) => { let cmd =`cd ${__dirname} && cd ../../../ && ng add ng-alain@2.0.1`; process.exec(cmd, function(error, stdout, stderr){ if (error) { console.log(error); }else { console.log("success"); } }); }; } /** 添加基础依赖 **/ function addDependenciesToPackageJson(options: ApplicationOptions) { return (host: Tree, context: SchematicContext) => { // ecip/* addPackageToPackageJson( host, [ 'service', 'shared', ].map(pkg => `@ecip/${pkg}@${ECIPRBAC_VERSION}`), ); addPackageToPackageJson( host, [ '@ngx-translate/core@10.0.2', '@ngx-translate/http-loader@3.0.1', 'rxjs-compat@6.2.2' ]); return host; }; } function installPackages() { return (host: Tree, context: SchematicContext) => { context.addTask(new NodePackageInstallTask()); }; } /** * todo: 添加文件中的mergeWith方法和方法中的apply方法需要研究 * @param options */ function addFilesToRoot(options: ApplicationOptions) { return chain([ // mergeWith( // apply(url('./files/src'), [ // options.i18n ? noop() : filter(p => p.indexOf('i18n') === -1), // options.form ? noop() : filter(p => p.indexOf('json-schema') === -1), // template({ // utils: strings, // ...options, // dot: '.', // VERSION, // ZORROVERSION, // }), // move(project.sourceRoot), // ]), // ), // mergeWith( // apply(url('./files/root'), [ // options.i18n ? noop() : filter(p => p.indexOf('i18n') === -1), // options.form ? noop() : filter(p => p.indexOf('json-schema') === -1), // template({ // utils: strings, // ...options, // dot: '.', // VERSION, // ZORROVERSION, // }), // // move('/') // ]), // MergeStrategy.Overwrite, // ), ]); } /** * 部分scripts 命令简化, * 移除color-less命令 **/ function alterRunScriptsInPackageJson() { return (host: Tree, context: SchematicContext) => { const json = getPackage(host, 'scripts'); if (json == null) return host; json.scripts['start'] = `ng serve -o`; json.scripts['build'] = `ng build --prod --build-optimizer`; json.scripts['analyze'] = `ng build --prod --build-optimizer --stats-json`; json.scripts['test-coverage'] = `ng test --code-coverage --watch=false`; json.scripts['color-less'] = undefined; overwritePackage(host, json); return host; }; } /** * 方法保留,ecip-rbac中tsconfig.json、 tsconfig.app.json、 tsconfig.spec.json * 目前保持与ng-alain一致,不需要更改tsconfig.json中的paths **/ function alterPathsInTsJson() { return (host: Tree, context: SchematicContext) => { [ { path: 'tsconfig.json', baseUrl: `${project.sourceRoot}/`, }, { path: `${project.sourceRoot}/tsconfig.app.json`, baseUrl: './', }, { path: `${project.sourceRoot}/tsconfig.spec.json`, baseUrl: './', }, ].forEach(item => { const json = getJSON(host, item.path, 'compilerOptions'); if (json == null) return host; if (!json.compilerOptions) json.compilerOptions = {}; if (!json.compilerOptions.paths) json.compilerOptions.paths = {}; json.compilerOptions.baseUrl = item.baseUrl; const paths = json.compilerOptions.paths; // ng-alain // paths['@shared'] = ['app/shared']; // paths['@shared/*'] = ['app/shared/*']; // paths['@core'] = ['app/core']; // paths['@core/*'] = ['app/core/*']; // paths['@testing'] = ['testing']; // paths['@testing/*'] = ['testing/*']; // paths['@env'] = ['environments']; // paths['@env/*'] = ['environments/*']; overwriteJSON(host, item.path, json); }); return host; }; } /** * todo: fixMain()作用有待校验 * ecip-rbac与ng-alain main.ts一致,故此方法暂不用理会 **/ function fixMain() { return (host: Tree) => { // fix: main.ts using no hmr file // tryAddFile( // host, // `${project.sourceRoot}/main.ts`, // HMR_CONTENT.NO_HMR_MAIN_DOT_TS, // ); }; } /** * angular相关package版本调整为6.1.0 * 此操作ng add ng-alain@2.0.1时进行 * 此方法保留,以作后期其他版本变更时需要用到的模版 **/ function fixedNg6() { return (host: Tree) => { const pkg = getPackage(host); // all @angular/* ['dependencies', 'devDependencies'].forEach(type => { Object.keys(pkg[type]) .filter(key => key.startsWith('@angular/')) .forEach(key => { pkg[type][key] = '^6.1.10'; }); }); pkg.devDependencies['@angular-devkit/build-angular'] = '~0.10.2'; pkg.devDependencies['typescript'] = '~2.9.2'; overwritePackage(host, pkg); }; } /** 新建route-routing.module.ts 暂时不用**/ function addRoutingModuleFile(options: ApplicationOptions) { const moduleContents: string[] = []; if (options.log) { console.log(options.log); moduleContents.push(MODULE_ROUTE_CONTENTS.LOG); } if (options.user) { moduleContents.push(MODULE_ROUTE_CONTENTS.USER); } if (options.permission) { moduleContents.push(MODULE_ROUTE_CONTENTS.PERMISSION); } if (options.system) { moduleContents.push(MODULE_ROUTE_CONTENTS.SYSTEM); } if (options.app) { moduleContents.push(MODULE_ROUTE_CONTENTS.APP); } if (options.auth) { moduleContents.push(MODULE_ROUTE_CONTENTS.AUTH); } const routingContent = routingModuleContent(moduleContents); return (tree: Tree, context: SchematicContext) => { tree.create(`${project.sourceRoot}/app/routes/routes-routing.module.ts`, routingContent); }; } /** * 修改、补充angular.json中schematics的schematic **/ function alterSchematics() { return (host: Tree, context: SchematicContext) => { const angularJsonFile = 'angular.json'; const json = getJSON(host, angularJsonFile, 'schematics'); if (json == null) return host; json.projects[project.name].architect.build.options.assets.push({ "glob": "**/*", "input": "./node_modules/@ant-design/icons-angular/src/inline-svg/", "output": "/assets/" }); // ecip json.schematics['ecip:app'] = { routing: true, spec: false, }; json.schematics['ecip:system'] = { routing: true, spec: false, }; json.schematics['ecip:auth'] = { routing: true, spec: false, }; json.schematics['ecip:permission'] = { routing: true, spec: false, }; json.schematics['ecip:user'] = { routing: true, spec: false, }; json.schematics['ecip:log'] = { routing: true, spec: false, }; // ng-alain json.schematics['ng-alain:module'] = { routing: true, spec: false, }; json.schematics['ng-alain:list'] = { spec: false, }; json.schematics['ng-alain:edit'] = { spec: false, modal: true, }; json.schematics['ng-alain:view'] = { spec: false, modal: true, }; json.schematics['ng-alain:curd'] = { spec: false, }; // angular json.schematics['@schematics/angular:module'] = { routing: true, spec: false, }; json.schematics['@schematics/angular:component'] = { spec: false, flat: false, inlineStyle: true, inlineTemplate: false, }; json.schematics['@schematics/angular:directive'] = { spec: false, }; json.schematics['@schematics/angular:service'] = { spec: false, }; overwriteJSON(host, angularJsonFile, json); }; } function removeSrcFiles(srcPath: string) { return tree => { // fromPath is a directory tree.getDir(srcPath).visit(path => { tree.delete(path); }); return tree; }; } function addFilesToSrcAndRoot(options: ApplicationOptions) { return chain([ mergeWith(apply(url('./files/src'), [ template({ app: options.app, auth: options.auth, system: options.system, permission: options.permission, user: options.user, log: options.log, }), move(`${project.sourceRoot}/`), ]), ), mergeWith(apply(url('./files/root'), [ move(`${project.root}/`), ]), ), ]); } /** * 默认方法,即schematic: application的入口方法 * @param options */ export default function (options: ApplicationOptions): Rule { return (host: Tree, context: SchematicContext) => { // 获取项目 project = getProject(host, options.project); return chain([ // executeAddNgAlain(), // // ecip/* dependencies 添加包到package.json addDependenciesToPackageJson(options), // // // ci 向 package.json scripts 添加 ci 命令 alterRunScriptsInPackageJson(), // 向最外层tsconfig.json 添加 paths // alterPathsInTsJson(), // 修改package.json 中的 codeStyles部分 // addCodeStylesToPackageJson(), // 在angular.json 文件中添加 schematics 命令 alterSchematics(), // // 删除src下文件 removeSrcFiles(project.sourceRoot), // // 添加文件到src下 addFilesToSrcAndRoot(options), // 安装依赖 installPackages(), ]); }; }