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 json_file_1 = require("../utility/json-file");
|
---|
15 | const latest_versions_1 = require("../utility/latest-versions");
|
---|
16 | const lint_fix_1 = require("../utility/lint-fix");
|
---|
17 | const paths_1 = require("../utility/paths");
|
---|
18 | const validation_1 = require("../utility/validation");
|
---|
19 | const workspace_1 = require("../utility/workspace");
|
---|
20 | const workspace_models_1 = require("../utility/workspace-models");
|
---|
21 | function updateTsConfig(packageName, ...paths) {
|
---|
22 | return (host) => {
|
---|
23 | if (!host.exists('tsconfig.json')) {
|
---|
24 | return host;
|
---|
25 | }
|
---|
26 | const file = new json_file_1.JSONFile(host, 'tsconfig.json');
|
---|
27 | const jsonPath = ['compilerOptions', 'paths', packageName];
|
---|
28 | const value = file.get(jsonPath);
|
---|
29 | file.modify(jsonPath, Array.isArray(value) ? [...value, ...paths] : paths);
|
---|
30 | };
|
---|
31 | }
|
---|
32 | function addDependenciesToPackageJson() {
|
---|
33 | return (host) => {
|
---|
34 | [
|
---|
35 | {
|
---|
36 | type: dependencies_1.NodeDependencyType.Dev,
|
---|
37 | name: '@angular/compiler-cli',
|
---|
38 | version: latest_versions_1.latestVersions.Angular,
|
---|
39 | },
|
---|
40 | {
|
---|
41 | type: dependencies_1.NodeDependencyType.Dev,
|
---|
42 | name: '@angular-devkit/build-angular',
|
---|
43 | version: latest_versions_1.latestVersions.DevkitBuildAngular,
|
---|
44 | },
|
---|
45 | {
|
---|
46 | type: dependencies_1.NodeDependencyType.Dev,
|
---|
47 | name: 'ng-packagr',
|
---|
48 | version: latest_versions_1.latestVersions['ng-packagr'],
|
---|
49 | },
|
---|
50 | {
|
---|
51 | type: dependencies_1.NodeDependencyType.Default,
|
---|
52 | name: 'tslib',
|
---|
53 | version: latest_versions_1.latestVersions['tslib'],
|
---|
54 | },
|
---|
55 | {
|
---|
56 | type: dependencies_1.NodeDependencyType.Dev,
|
---|
57 | name: 'typescript',
|
---|
58 | version: latest_versions_1.latestVersions['typescript'],
|
---|
59 | },
|
---|
60 | ].forEach((dependency) => dependencies_1.addPackageJsonDependency(host, dependency));
|
---|
61 | return host;
|
---|
62 | };
|
---|
63 | }
|
---|
64 | function addLibToWorkspaceFile(options, projectRoot, projectName) {
|
---|
65 | return workspace_1.updateWorkspace((workspace) => {
|
---|
66 | if (workspace.projects.size === 0) {
|
---|
67 | workspace.extensions.defaultProject = projectName;
|
---|
68 | }
|
---|
69 | workspace.projects.add({
|
---|
70 | name: projectName,
|
---|
71 | root: projectRoot,
|
---|
72 | sourceRoot: `${projectRoot}/src`,
|
---|
73 | projectType: workspace_models_1.ProjectType.Library,
|
---|
74 | prefix: options.prefix,
|
---|
75 | targets: {
|
---|
76 | build: {
|
---|
77 | builder: workspace_models_1.Builders.NgPackagr,
|
---|
78 | defaultConfiguration: 'production',
|
---|
79 | options: {
|
---|
80 | project: `${projectRoot}/ng-package.json`,
|
---|
81 | },
|
---|
82 | configurations: {
|
---|
83 | production: {
|
---|
84 | tsConfig: `${projectRoot}/tsconfig.lib.prod.json`,
|
---|
85 | },
|
---|
86 | development: {
|
---|
87 | tsConfig: `${projectRoot}/tsconfig.lib.json`,
|
---|
88 | },
|
---|
89 | },
|
---|
90 | },
|
---|
91 | test: {
|
---|
92 | builder: workspace_models_1.Builders.Karma,
|
---|
93 | options: {
|
---|
94 | main: `${projectRoot}/src/test.ts`,
|
---|
95 | tsConfig: `${projectRoot}/tsconfig.spec.json`,
|
---|
96 | karmaConfig: `${projectRoot}/karma.conf.js`,
|
---|
97 | },
|
---|
98 | },
|
---|
99 | },
|
---|
100 | });
|
---|
101 | });
|
---|
102 | }
|
---|
103 | function default_1(options) {
|
---|
104 | return async (host) => {
|
---|
105 | if (!options.name) {
|
---|
106 | throw new schematics_1.SchematicsException(`Invalid options, "name" is required.`);
|
---|
107 | }
|
---|
108 | const prefix = options.prefix;
|
---|
109 | validation_1.validateProjectName(options.name);
|
---|
110 | // If scoped project (i.e. "@foo/bar"), convert projectDir to "foo/bar".
|
---|
111 | const projectName = options.name;
|
---|
112 | const packageName = core_1.strings.dasherize(projectName);
|
---|
113 | let scopeName = null;
|
---|
114 | if (/^@.*\/.*/.test(options.name)) {
|
---|
115 | const [scope, name] = options.name.split('/');
|
---|
116 | scopeName = scope.replace(/^@/, '');
|
---|
117 | options.name = name;
|
---|
118 | }
|
---|
119 | const workspace = await workspace_1.getWorkspace(host);
|
---|
120 | const newProjectRoot = workspace.extensions.newProjectRoot || '';
|
---|
121 | const scopeFolder = scopeName ? core_1.strings.dasherize(scopeName) + '/' : '';
|
---|
122 | const folderName = `${scopeFolder}${core_1.strings.dasherize(options.name)}`;
|
---|
123 | const projectRoot = core_1.join(core_1.normalize(newProjectRoot), folderName);
|
---|
124 | const distRoot = `dist/${folderName}`;
|
---|
125 | const pathImportLib = `${distRoot}/${folderName.replace('/', '-')}`;
|
---|
126 | const sourceDir = `${projectRoot}/src/lib`;
|
---|
127 | const templateSource = schematics_1.apply(schematics_1.url('./files'), [
|
---|
128 | schematics_1.applyTemplates({
|
---|
129 | ...core_1.strings,
|
---|
130 | ...options,
|
---|
131 | packageName,
|
---|
132 | projectRoot,
|
---|
133 | distRoot,
|
---|
134 | relativePathToWorkspaceRoot: paths_1.relativePathToWorkspaceRoot(projectRoot),
|
---|
135 | prefix,
|
---|
136 | angularLatestVersion: latest_versions_1.latestVersions.Angular.replace(/\~|\^/, ''),
|
---|
137 | tsLibLatestVersion: latest_versions_1.latestVersions['tslib'].replace(/\~|\^/, ''),
|
---|
138 | folderName,
|
---|
139 | }),
|
---|
140 | schematics_1.move(projectRoot),
|
---|
141 | ]);
|
---|
142 | return schematics_1.chain([
|
---|
143 | schematics_1.mergeWith(templateSource),
|
---|
144 | addLibToWorkspaceFile(options, projectRoot, projectName),
|
---|
145 | options.skipPackageJson ? schematics_1.noop() : addDependenciesToPackageJson(),
|
---|
146 | options.skipTsConfig ? schematics_1.noop() : updateTsConfig(packageName, pathImportLib, distRoot),
|
---|
147 | schematics_1.schematic('module', {
|
---|
148 | name: options.name,
|
---|
149 | commonModule: false,
|
---|
150 | flat: true,
|
---|
151 | path: sourceDir,
|
---|
152 | project: projectName,
|
---|
153 | }),
|
---|
154 | schematics_1.schematic('component', {
|
---|
155 | name: options.name,
|
---|
156 | selector: `${prefix}-${options.name}`,
|
---|
157 | inlineStyle: true,
|
---|
158 | inlineTemplate: true,
|
---|
159 | flat: true,
|
---|
160 | path: sourceDir,
|
---|
161 | export: true,
|
---|
162 | project: projectName,
|
---|
163 | }),
|
---|
164 | schematics_1.schematic('service', {
|
---|
165 | name: options.name,
|
---|
166 | flat: true,
|
---|
167 | path: sourceDir,
|
---|
168 | project: projectName,
|
---|
169 | }),
|
---|
170 | options.lintFix ? lint_fix_1.applyLintFix(sourceDir) : schematics_1.noop(),
|
---|
171 | (_tree, context) => {
|
---|
172 | if (!options.skipPackageJson && !options.skipInstall) {
|
---|
173 | context.addTask(new tasks_1.NodePackageInstallTask());
|
---|
174 | }
|
---|
175 | },
|
---|
176 | ]);
|
---|
177 | };
|
---|
178 | }
|
---|
179 | exports.default = default_1;
|
---|