UNPKG

ng-devui-admin

Version:

Schematics for ng-devui-admin

145 lines 6.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const schematics_1 = require("@angular-devkit/schematics"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const utils_1 = require("../utils"); const fs = require("fs"); const path = require("path"); let projectWorkspace; function getWorkSpace(tree) { var _a; let angularJSON; if (tree.read(utils_1.ANGULAR_JSON_PATH)) { angularJSON = JSON.parse((_a = tree.read(utils_1.ANGULAR_JSON_PATH)) === null || _a === void 0 ? void 0 : _a.toString()); } else { throw new schematics_1.SchematicsException('Please make sure the project is an Angular project.'); } let defaultProject = Object.keys(angularJSON.projects)[0]; projectWorkspace = { root: '/', sourceRoot: angularJSON.projects[defaultProject].sourceRoot, defaultProject, }; return projectWorkspace; } function removeOriginalFiles() { return (tree) => { [ `${projectWorkspace.root}/README.md`, `${projectWorkspace.root}/tslint.json`, `${projectWorkspace.sourceRoot}/main.ts`, `${projectWorkspace.sourceRoot}/index.html`, `${projectWorkspace.sourceRoot}/styles.scss`, `${projectWorkspace.sourceRoot}/styles.css`, `${projectWorkspace.sourceRoot}/favicon.ico`, `${projectWorkspace.sourceRoot}/app/app.module.ts`, `${projectWorkspace.sourceRoot}/app/app.component.spec.ts`, `${projectWorkspace.sourceRoot}/app/app.component.ts`, `${projectWorkspace.sourceRoot}/app/app.component.html`, `${projectWorkspace.sourceRoot}/app/app.component.scss`, `${projectWorkspace.sourceRoot}/app/app.component.css`, `${projectWorkspace.sourceRoot}/app/app-routing.module.ts`, ] .filter((f) => tree.exists(f)) .forEach((f) => tree.delete(f)); }; } function modifyTsConfig() { return (tree) => { const configPath = path.join(__dirname, '..', '..', '..', '..', 'tsconfig.json'); let content = fs.readFileSync(configPath, 'utf-8'); let strContent; if (content) { strContent = JSON.parse(content.replace(/\/\*(.*?)\*\//, '')); strContent.compilerOptions['strict'] = false; strContent.compilerOptions['noImplicitReturns'] = false; strContent.angularCompilerOptions['strictPropertyInitialization'] = false; } else { throw new schematics_1.SchematicsException('Please make sure there is a tsconfig.json.'); } tree.overwrite('tsconfig.json', JSON.stringify(strContent, null, 2)); return tree; }; } function addDependencies(options) { return (tree) => { const packages = options.i18n ? utils_1.PACKAGES_I18N : utils_1.PACKAGES; (0, utils_1.addPackage)(tree, packages, 'dependencies'); }; } function addLintFiles(options, tree) { if (options.eslintFile) { addLintScripts(tree); return (0, schematics_1.chain)([(0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/lint-files'), [(0, schematics_1.move)(projectWorkspace.root)]))]); } else { return (0, schematics_1.chain)([]); } } function addLintScripts(tree) { var _a; let packageJson; if (tree.read(utils_1.PACKAGE_JSON_PATH)) { packageJson = JSON.parse((_a = tree.read(utils_1.PACKAGE_JSON_PATH)) === null || _a === void 0 ? void 0 : _a.toString()); } else { throw new schematics_1.SchematicsException('Please make sure there is a package.json file in your project.'); } packageJson.scripts['prettier'] = 'prettier --config ./.prettierrc --write "src/app/pages/**/**/*.html"'; packageJson.scripts['stylelint'] = 'stylelint "src/app/pages/**/**/*.{scss,css}" --config ./.stylelintrc.json --fix'; packageJson.scripts['tslint'] = 'tslint --fix -c ./tslint.json "src/**/*{.ts}"'; tree.overwrite(utils_1.PACKAGE_JSON_PATH, JSON.stringify(packageJson, null, 2)); } function addSourceFiles(options) { return (0, schematics_1.chain)([ (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/src'), [ (0, schematics_1.applyTemplates)({ defaultLanguage: options.defaultLanguage, i18n: options.i18n, mock: options.mock, }), (0, schematics_1.move)(projectWorkspace.sourceRoot), ])), ]); } function addI18nFiles(options) { if (options.i18n) { return (0, schematics_1.chain)([(0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/i18n'), [(0, schematics_1.move)(`${projectWorkspace.sourceRoot}/assets/i18n`)]))]); } else { return (0, schematics_1.chain)([]); } } function addMockFiles(options) { if (options.mock) { return (0, schematics_1.chain)([(0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/mock-files'), [(0, schematics_1.move)(`${projectWorkspace.sourceRoot}/app/@core`)]))]); } else { return (0, schematics_1.chain)([]); } } function installPackages() { return (_tree, context) => { context.addTask(new tasks_1.NodePackageInstallTask()); }; } function default_1(options) { return (tree) => { getWorkSpace(tree); return (0, schematics_1.chain)([ removeOriginalFiles(), (0, utils_1.modifyAngularJson)(utils_1.STYLES, projectWorkspace.defaultProject), modifyTsConfig(), addDependencies(options), addLintFiles(options, tree), addSourceFiles(options), addI18nFiles(options), addMockFiles(options), installPackages(), ]); }; } exports.default = default_1; //# sourceMappingURL=index.js.map