UNPKG

ng-matero

Version:
187 lines 8.57 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const schematics_2 = require("@angular/cdk/schematics"); const workspace_1 = require("@schematics/angular/utility/workspace"); const global_loader_1 = require("./global-loader"); const material_fonts_1 = require("./material-fonts"); const package_config_1 = require("./package-config"); const packages_1 = require("./packages"); const theming_1 = require("./theming"); /** * Scaffolds the basics of a Angular Material application, this includes: * - Add Starter files to root * - Add Scripts to `package.json` * - Add proxy to `angular.json` * - Add style to `angular.json` * - Add fileReplacements to `angular.json` * - Add Fonts & Icons to `index.html` * - Add Preloader to `index.html` * - Add Packages to `package.json` */ function default_1(options) { return (0, schematics_1.chain)([ deleteExsitingFiles(options), addStarterFiles(options), addScriptsToPackageJson(), addESLintToAngularJson(options), addProxyToAngularJson(options), addStyleToAngularJson(options), addFileReplacementsToAngularJson(options), (0, material_fonts_1.addFontsToIndex)(options), (0, global_loader_1.addLoaderToIndex)(options), installPackages(), ]); } /** delete exsiting files to be overwrite */ function deleteExsitingFiles(options) { return (host) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(host); const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project); [ `${project.root}/.vscode/extensions.json`, `${project.root}/.vscode/settings.json`, `${project.root}/tsconfig.json`, `${project.sourceRoot}/app/app-routing.module.ts`, `${project.sourceRoot}/app/app.module.ts`, `${project.sourceRoot}/app/app.config.ts`, `${project.sourceRoot}/app/app.routes.ts`, `${project.sourceRoot}/app/app.component.spec.ts`, `${project.sourceRoot}/app/app.component.ts`, `${project.sourceRoot}/app/app.component.html`, `${project.sourceRoot}/app/app.component.scss`, `${project.sourceRoot}/environments/environment.prod.ts`, `${project.sourceRoot}/environments/environment.ts`, `${project.sourceRoot}/styles.scss`, ] .filter(p => host.exists(p)) .forEach(p => host.delete(p)); }); } /** Add scripts to `package.json` */ function addScriptsToPackageJson() { return (host) => { (0, package_config_1.addScriptToPackageJson)(host, 'build:prod', 'ng build --prod'); (0, package_config_1.addScriptToPackageJson)(host, 'lint', `ng lint --fix && npm run lint:scss`); (0, package_config_1.addScriptToPackageJson)(host, 'lint:ts', `eslint "src/**/*.ts" --fix`); (0, package_config_1.addScriptToPackageJson)(host, 'lint:scss', `stylelint "src/**/*.scss" --fix`); }; } /** Add ESLint to `angular.json` */ function addESLintToAngularJson(options) { return (0, workspace_1.updateWorkspace)(workspace => { const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project); let lintFilePatternsRoot = ''; // Default Angular CLI project at the root of the workspace if (project.root === '') { lintFilePatternsRoot = 'src'; } else { lintFilePatternsRoot = project.root; } const eslintTargetConfig = { builder: '@angular-eslint/builder:lint', options: { lintFilePatterns: [`${lintFilePatternsRoot}/**/*.ts`, `${lintFilePatternsRoot}/**/*.html`], }, }; project.targets.set('lint', eslintTargetConfig); }); } /** Add proxy to `angular.json` */ function addProxyToAngularJson(options) { return (0, workspace_1.updateWorkspace)(workspace => { const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project); const targetServeConfig = project.targets.get('serve'); if (targetServeConfig.options) { targetServeConfig.options.buildTarget = `${options.project}:build`; targetServeConfig.options.proxyConfig = 'proxy.config.js'; } else { targetServeConfig.options = { buildTarget: `${options.project}:build`, proxyConfig: 'proxy.config.js', }; } }); } /** Add style to `angular.json` */ function addStyleToAngularJson(options) { return (_host, context) => { // Path needs to be always relative to the `package.json` or workspace root. const themePath = `src/styles.scss`; return (0, schematics_1.chain)([ (0, theming_1.addThemeStyleToTarget)(options.project, 'build', themePath, context.logger), (0, theming_1.addThemeStyleToTarget)(options.project, 'test', themePath, context.logger), ]); }; } /** Add fileReplacements to 'angular.json' */ function addFileReplacementsToAngularJson(options) { return (0, workspace_1.updateWorkspace)(workspace => { const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project); const targetBuildConfig = project.targets.get('build'); // The previous environment file has been deleted const replace = { replace: 'src/environments/environment.ts', with: 'src/environments/environment.prod.ts', }; const production = targetBuildConfig.configurations.production; // Add file replacement option entry for the configuration environment file const replacements = production.fileReplacements; if (replacements === undefined) { production.fileReplacements = [replace]; } else { const existing = replacements.find(value => value.replace === replace.replace); if (existing) { if (existing.with !== replace.with) { existing.with = replace.with; } } else { replacements.push(replace); } } }); } /** Add starter files to root */ function addStarterFiles(options) { return (host) => __awaiter(this, void 0, void 0, function* () { const workspace = yield (0, workspace_1.getWorkspace)(host); const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project); const mainFilePath = (0, schematics_2.getProjectMainFile)(project); return (0, schematics_1.chain)([ (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/common-files'), [ (0, schematics_1.template)(Object.assign(Object.assign({}, core_1.strings), options)), ])), (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)((0, schematics_2.isStandaloneApp)(host, mainFilePath) ? './files/standalone-files' : './files/module-files'), [ (0, schematics_1.template)(Object.assign(Object.assign({}, core_1.strings), options)), (0, schematics_1.move)('./src'), ]), schematics_1.MergeStrategy.Overwrite), ]); }); } /** Install packages */ function installPackages() { return (host, context) => { // Add 3rd packages (0, packages_1.add3rdPkgsToPackageJson)(host); context.addTask(new tasks_1.NodePackageInstallTask()); }; } //# sourceMappingURL=setup-project.js.map