useasdemo
Version:
344 lines • 11.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const schematics_1 = require("@angular-devkit/schematics");
const project_1 = require("../utils/project");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const json_1 = require("../utils/json");
const path = require("path");
const process = require("child_process");
const lib_version_1 = require("../utils/lib.version");
const content_1 = require("../utils/content");
let project;
const overwriteDataFileRoot = path.join(__dirname, 'overwrites');
/** 执行ng add ng-alain 暂时不用**/
function executeAddNgAlain() {
return (host, context) => {
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) {
return (host, context) => {
// ecip/*
json_1.addPackageToPackageJson(host, [
'service',
'shared',
].map(pkg => `@ecip/${pkg}@${lib_version_1.ECIPRBAC_VERSION}`));
json_1.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, context) => {
context.addTask(new tasks_1.NodePackageInstallTask());
};
}
/**
* todo: 添加文件中的mergeWith方法和方法中的apply方法需要研究
* @param options
*/
function addFilesToRoot(options) {
return schematics_1.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, context) => {
const json = json_1.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;
json_1.overwritePackage(host, json);
return host;
};
}
/**
* 方法保留,ecip-rbac中tsconfig.json、 tsconfig.app.json、 tsconfig.spec.json
* 目前保持与ng-alain一致,不需要更改tsconfig.json中的paths
**/
function alterPathsInTsJson() {
return (host, context) => {
[
{
path: 'tsconfig.json',
baseUrl: `${project.sourceRoot}/`,
},
{
path: `${project.sourceRoot}/tsconfig.app.json`,
baseUrl: './',
},
{
path: `${project.sourceRoot}/tsconfig.spec.json`,
baseUrl: './',
},
].forEach(item => {
const json = json_1.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/*'];
json_1.overwriteJSON(host, item.path, json);
});
return host;
};
}
/**
* todo: fixMain()作用有待校验
* ecip-rbac与ng-alain main.ts一致,故此方法暂不用理会
**/
function fixMain() {
return (host) => {
// 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) => {
const pkg = json_1.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';
json_1.overwritePackage(host, pkg);
};
}
/** 新建route-routing.module.ts 暂时不用**/
function addRoutingModuleFile(options) {
const moduleContents = [];
if (options.log) {
console.log(options.log);
moduleContents.push(content_1.MODULE_ROUTE_CONTENTS.LOG);
}
if (options.user) {
moduleContents.push(content_1.MODULE_ROUTE_CONTENTS.USER);
}
if (options.permission) {
moduleContents.push(content_1.MODULE_ROUTE_CONTENTS.PERMISSION);
}
if (options.system) {
moduleContents.push(content_1.MODULE_ROUTE_CONTENTS.SYSTEM);
}
if (options.app) {
moduleContents.push(content_1.MODULE_ROUTE_CONTENTS.APP);
}
if (options.auth) {
moduleContents.push(content_1.MODULE_ROUTE_CONTENTS.AUTH);
}
const routingContent = content_1.routingModuleContent(moduleContents);
return (tree, context) => {
tree.create(`${project.sourceRoot}/app/routes/routes-routing.module.ts`, routingContent);
};
}
/**
* 修改、补充angular.json中schematics的schematic
**/
function alterSchematics() {
return (host, context) => {
const angularJsonFile = 'angular.json';
const json = json_1.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,
};
json_1.overwriteJSON(host, angularJsonFile, json);
};
}
function removeSrcFiles(srcPath) {
return tree => {
// fromPath is a directory
tree.getDir(srcPath).visit(path => {
tree.delete(path);
});
return tree;
};
}
function addFilesToSrcAndRoot(options) {
return schematics_1.chain([
schematics_1.mergeWith(schematics_1.apply(schematics_1.url('./files/src'), [
schematics_1.template({
app: options.app,
auth: options.auth,
system: options.system,
permission: options.permission,
user: options.user,
log: options.log,
}),
schematics_1.move(`${project.sourceRoot}/`),
])),
schematics_1.mergeWith(schematics_1.apply(schematics_1.url('./files/root'), [
schematics_1.move(`${project.root}/`),
])),
]);
}
/**
* 默认方法,即schematic: application的入口方法
* @param options
*/
function default_1(options) {
return (host, context) => {
// 获取项目
project = project_1.getProject(host, options.project);
return schematics_1.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(),
]);
};
}
exports.default = default_1;
//# sourceMappingURL=index.js.map