1 | "use strict";
|
---|
2 | /**
|
---|
3 | * @license
|
---|
4 | * Copyright Google LLC All Rights Reserved.
|
---|
5 | *
|
---|
6 | * Use of this source code is governed by an MIT-style license that can be
|
---|
7 | * found in the LICENSE file at https://angular.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | const core_1 = require("@angular-devkit/core");
|
---|
11 | const schematics_1 = require("@angular-devkit/schematics");
|
---|
12 | const tasks_1 = require("@angular-devkit/schematics/tasks");
|
---|
13 | const dependencies_1 = require("../utility/dependencies");
|
---|
14 | const latest_versions_1 = require("../utility/latest-versions");
|
---|
15 | const lint_fix_1 = require("../utility/lint-fix");
|
---|
16 | const paths_1 = require("../utility/paths");
|
---|
17 | const validation_1 = require("../utility/validation");
|
---|
18 | const workspace_1 = require("../utility/workspace");
|
---|
19 | const workspace_models_1 = require("../utility/workspace-models");
|
---|
20 | const schema_1 = require("./schema");
|
---|
21 | function addDependenciesToPackageJson(options) {
|
---|
22 | return (host, context) => {
|
---|
23 | [
|
---|
24 | {
|
---|
25 | type: dependencies_1.NodeDependencyType.Dev,
|
---|
26 | name: '@angular/compiler-cli',
|
---|
27 | version: latest_versions_1.latestVersions.Angular,
|
---|
28 | },
|
---|
29 | {
|
---|
30 | type: dependencies_1.NodeDependencyType.Dev,
|
---|
31 | name: '@angular-devkit/build-angular',
|
---|
32 | version: latest_versions_1.latestVersions.DevkitBuildAngular,
|
---|
33 | },
|
---|
34 | {
|
---|
35 | type: dependencies_1.NodeDependencyType.Dev,
|
---|
36 | name: 'typescript',
|
---|
37 | version: latest_versions_1.latestVersions['typescript'],
|
---|
38 | },
|
---|
39 | ].forEach((dependency) => dependencies_1.addPackageJsonDependency(host, dependency));
|
---|
40 | if (!options.skipInstall) {
|
---|
41 | context.addTask(new tasks_1.NodePackageInstallTask());
|
---|
42 | }
|
---|
43 | return host;
|
---|
44 | };
|
---|
45 | }
|
---|
46 | function addAppToWorkspaceFile(options, appDir) {
|
---|
47 | var _a, _b;
|
---|
48 | let projectRoot = appDir;
|
---|
49 | if (projectRoot) {
|
---|
50 | projectRoot += '/';
|
---|
51 | }
|
---|
52 | const schematics = {};
|
---|
53 | if (options.inlineTemplate ||
|
---|
54 | options.inlineStyle ||
|
---|
55 | options.minimal ||
|
---|
56 | options.style !== schema_1.Style.Css) {
|
---|
57 | const componentSchematicsOptions = {};
|
---|
58 | if ((_a = options.inlineTemplate) !== null && _a !== void 0 ? _a : options.minimal) {
|
---|
59 | componentSchematicsOptions.inlineTemplate = true;
|
---|
60 | }
|
---|
61 | if ((_b = options.inlineStyle) !== null && _b !== void 0 ? _b : options.minimal) {
|
---|
62 | componentSchematicsOptions.inlineStyle = true;
|
---|
63 | }
|
---|
64 | if (options.style && options.style !== schema_1.Style.Css) {
|
---|
65 | componentSchematicsOptions.style = options.style;
|
---|
66 | }
|
---|
67 | schematics['@schematics/angular:component'] = componentSchematicsOptions;
|
---|
68 | }
|
---|
69 | if (options.skipTests || options.minimal) {
|
---|
70 | ['class', 'component', 'directive', 'guard', 'interceptor', 'pipe', 'service'].forEach((type) => {
|
---|
71 | if (!(`@schematics/angular:${type}` in schematics)) {
|
---|
72 | schematics[`@schematics/angular:${type}`] = {};
|
---|
73 | }
|
---|
74 | schematics[`@schematics/angular:${type}`].skipTests = true;
|
---|
75 | });
|
---|
76 | }
|
---|
77 | if (options.strict) {
|
---|
78 | if (!('@schematics/angular:application' in schematics)) {
|
---|
79 | schematics['@schematics/angular:application'] = {};
|
---|
80 | }
|
---|
81 | schematics['@schematics/angular:application'].strict = true;
|
---|
82 | }
|
---|
83 | const sourceRoot = core_1.join(core_1.normalize(projectRoot), 'src');
|
---|
84 | let budgets = [];
|
---|
85 | if (options.strict) {
|
---|
86 | budgets = [
|
---|
87 | {
|
---|
88 | type: 'initial',
|
---|
89 | maximumWarning: '500kb',
|
---|
90 | maximumError: '1mb',
|
---|
91 | },
|
---|
92 | {
|
---|
93 | type: 'anyComponentStyle',
|
---|
94 | maximumWarning: '2kb',
|
---|
95 | maximumError: '4kb',
|
---|
96 | },
|
---|
97 | ];
|
---|
98 | }
|
---|
99 | else {
|
---|
100 | budgets = [
|
---|
101 | {
|
---|
102 | type: 'initial',
|
---|
103 | maximumWarning: '2mb',
|
---|
104 | maximumError: '5mb',
|
---|
105 | },
|
---|
106 | {
|
---|
107 | type: 'anyComponentStyle',
|
---|
108 | maximumWarning: '6kb',
|
---|
109 | maximumError: '10kb',
|
---|
110 | },
|
---|
111 | ];
|
---|
112 | }
|
---|
113 | const inlineStyleLanguage = (options === null || options === void 0 ? void 0 : options.style) !== schema_1.Style.Css ? options.style : undefined;
|
---|
114 | const project = {
|
---|
115 | root: core_1.normalize(projectRoot),
|
---|
116 | sourceRoot,
|
---|
117 | projectType: workspace_models_1.ProjectType.Application,
|
---|
118 | prefix: options.prefix || 'app',
|
---|
119 | schematics,
|
---|
120 | targets: {
|
---|
121 | build: {
|
---|
122 | builder: workspace_models_1.Builders.Browser,
|
---|
123 | defaultConfiguration: 'production',
|
---|
124 | options: {
|
---|
125 | outputPath: `dist/${options.name}`,
|
---|
126 | index: `${sourceRoot}/index.html`,
|
---|
127 | main: `${sourceRoot}/main.ts`,
|
---|
128 | polyfills: `${sourceRoot}/polyfills.ts`,
|
---|
129 | tsConfig: `${projectRoot}tsconfig.app.json`,
|
---|
130 | inlineStyleLanguage,
|
---|
131 | assets: [`${sourceRoot}/favicon.ico`, `${sourceRoot}/assets`],
|
---|
132 | styles: [`${sourceRoot}/styles.${options.style}`],
|
---|
133 | scripts: [],
|
---|
134 | },
|
---|
135 | configurations: {
|
---|
136 | production: {
|
---|
137 | budgets,
|
---|
138 | fileReplacements: [
|
---|
139 | {
|
---|
140 | replace: `${sourceRoot}/environments/environment.ts`,
|
---|
141 | with: `${sourceRoot}/environments/environment.prod.ts`,
|
---|
142 | },
|
---|
143 | ],
|
---|
144 | outputHashing: 'all',
|
---|
145 | },
|
---|
146 | development: {
|
---|
147 | buildOptimizer: false,
|
---|
148 | optimization: false,
|
---|
149 | vendorChunk: true,
|
---|
150 | extractLicenses: false,
|
---|
151 | sourceMap: true,
|
---|
152 | namedChunks: true,
|
---|
153 | },
|
---|
154 | },
|
---|
155 | },
|
---|
156 | serve: {
|
---|
157 | builder: workspace_models_1.Builders.DevServer,
|
---|
158 | defaultConfiguration: 'development',
|
---|
159 | options: {},
|
---|
160 | configurations: {
|
---|
161 | production: {
|
---|
162 | browserTarget: `${options.name}:build:production`,
|
---|
163 | },
|
---|
164 | development: {
|
---|
165 | browserTarget: `${options.name}:build:development`,
|
---|
166 | },
|
---|
167 | },
|
---|
168 | },
|
---|
169 | 'extract-i18n': {
|
---|
170 | builder: workspace_models_1.Builders.ExtractI18n,
|
---|
171 | options: {
|
---|
172 | browserTarget: `${options.name}:build`,
|
---|
173 | },
|
---|
174 | },
|
---|
175 | test: options.minimal
|
---|
176 | ? undefined
|
---|
177 | : {
|
---|
178 | builder: workspace_models_1.Builders.Karma,
|
---|
179 | options: {
|
---|
180 | main: `${sourceRoot}/test.ts`,
|
---|
181 | polyfills: `${sourceRoot}/polyfills.ts`,
|
---|
182 | tsConfig: `${projectRoot}tsconfig.spec.json`,
|
---|
183 | karmaConfig: `${projectRoot}karma.conf.js`,
|
---|
184 | inlineStyleLanguage,
|
---|
185 | assets: [`${sourceRoot}/favicon.ico`, `${sourceRoot}/assets`],
|
---|
186 | styles: [`${sourceRoot}/styles.${options.style}`],
|
---|
187 | scripts: [],
|
---|
188 | },
|
---|
189 | },
|
---|
190 | },
|
---|
191 | };
|
---|
192 | return workspace_1.updateWorkspace((workspace) => {
|
---|
193 | if (workspace.projects.size === 0) {
|
---|
194 | workspace.extensions.defaultProject = options.name;
|
---|
195 | }
|
---|
196 | workspace.projects.add({
|
---|
197 | name: options.name,
|
---|
198 | ...project,
|
---|
199 | });
|
---|
200 | });
|
---|
201 | }
|
---|
202 | function minimalPathFilter(path) {
|
---|
203 | const toRemoveList = /(test.ts|tsconfig.spec.json|karma.conf.js).template$/;
|
---|
204 | return !toRemoveList.test(path);
|
---|
205 | }
|
---|
206 | function default_1(options) {
|
---|
207 | return async (host) => {
|
---|
208 | var _a, _b;
|
---|
209 | if (!options.name) {
|
---|
210 | throw new schematics_1.SchematicsException(`Invalid options, "name" is required.`);
|
---|
211 | }
|
---|
212 | validation_1.validateProjectName(options.name);
|
---|
213 | const appRootSelector = `${options.prefix}-root`;
|
---|
214 | const componentOptions = !options.minimal
|
---|
215 | ? {
|
---|
216 | inlineStyle: options.inlineStyle,
|
---|
217 | inlineTemplate: options.inlineTemplate,
|
---|
218 | skipTests: options.skipTests,
|
---|
219 | style: options.style,
|
---|
220 | viewEncapsulation: options.viewEncapsulation,
|
---|
221 | }
|
---|
222 | : {
|
---|
223 | inlineStyle: (_a = options.inlineStyle) !== null && _a !== void 0 ? _a : true,
|
---|
224 | inlineTemplate: (_b = options.inlineTemplate) !== null && _b !== void 0 ? _b : true,
|
---|
225 | skipTests: true,
|
---|
226 | style: options.style,
|
---|
227 | viewEncapsulation: options.viewEncapsulation,
|
---|
228 | };
|
---|
229 | const workspace = await workspace_1.getWorkspace(host);
|
---|
230 | const newProjectRoot = workspace.extensions.newProjectRoot || '';
|
---|
231 | const isRootApp = options.projectRoot !== undefined;
|
---|
232 | const appDir = isRootApp
|
---|
233 | ? core_1.normalize(options.projectRoot || '')
|
---|
234 | : core_1.join(core_1.normalize(newProjectRoot), core_1.strings.dasherize(options.name));
|
---|
235 | const sourceDir = `${appDir}/src/app`;
|
---|
236 | return schematics_1.chain([
|
---|
237 | addAppToWorkspaceFile(options, appDir),
|
---|
238 | schematics_1.mergeWith(schematics_1.apply(schematics_1.url('./files'), [
|
---|
239 | options.minimal ? schematics_1.filter(minimalPathFilter) : schematics_1.noop(),
|
---|
240 | schematics_1.applyTemplates({
|
---|
241 | utils: core_1.strings,
|
---|
242 | ...options,
|
---|
243 | relativePathToWorkspaceRoot: paths_1.relativePathToWorkspaceRoot(appDir),
|
---|
244 | appName: options.name,
|
---|
245 | isRootApp,
|
---|
246 | }),
|
---|
247 | schematics_1.move(appDir),
|
---|
248 | ]), schematics_1.MergeStrategy.Overwrite),
|
---|
249 | schematics_1.schematic('module', {
|
---|
250 | name: 'app',
|
---|
251 | commonModule: false,
|
---|
252 | flat: true,
|
---|
253 | routing: options.routing,
|
---|
254 | routingScope: 'Root',
|
---|
255 | path: sourceDir,
|
---|
256 | project: options.name,
|
---|
257 | }),
|
---|
258 | schematics_1.schematic('component', {
|
---|
259 | name: 'app',
|
---|
260 | selector: appRootSelector,
|
---|
261 | flat: true,
|
---|
262 | path: sourceDir,
|
---|
263 | skipImport: true,
|
---|
264 | project: options.name,
|
---|
265 | ...componentOptions,
|
---|
266 | }),
|
---|
267 | schematics_1.mergeWith(schematics_1.apply(schematics_1.url('./other-files'), [
|
---|
268 | options.strict ? schematics_1.noop() : schematics_1.filter((path) => path !== '/package.json.template'),
|
---|
269 | componentOptions.inlineTemplate
|
---|
270 | ? schematics_1.filter((path) => !path.endsWith('.html.template'))
|
---|
271 | : schematics_1.noop(),
|
---|
272 | componentOptions.skipTests
|
---|
273 | ? schematics_1.filter((path) => !path.endsWith('.spec.ts.template'))
|
---|
274 | : schematics_1.noop(),
|
---|
275 | schematics_1.applyTemplates({
|
---|
276 | utils: core_1.strings,
|
---|
277 | ...options,
|
---|
278 | selector: appRootSelector,
|
---|
279 | ...componentOptions,
|
---|
280 | }),
|
---|
281 | schematics_1.move(sourceDir),
|
---|
282 | ]), schematics_1.MergeStrategy.Overwrite),
|
---|
283 | options.skipPackageJson ? schematics_1.noop() : addDependenciesToPackageJson(options),
|
---|
284 | options.lintFix ? lint_fix_1.applyLintFix(appDir) : schematics_1.noop(),
|
---|
285 | ]);
|
---|
286 | };
|
---|
287 | }
|
---|
288 | exports.default = default_1;
|
---|