-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
165 lines (165 loc) · 6.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict";
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const schematics_1 = require("@angular-devkit/schematics");
const tasks_1 = require("@angular-devkit/schematics/tasks");
const posix_1 = require("node:path/posix");
const dependencies_1 = require("../utility/dependencies");
const json_file_1 = require("../utility/json-file");
const latest_versions_1 = require("../utility/latest-versions");
const paths_1 = require("../utility/paths");
const workspace_1 = require("../utility/workspace");
const workspace_models_1 = require("../utility/workspace-models");
function updateTsConfig(packageName, ...paths) {
return (host) => {
if (!host.exists('tsconfig.json')) {
return host;
}
const file = new json_file_1.JSONFile(host, 'tsconfig.json');
const jsonPath = ['compilerOptions', 'paths', packageName];
const value = file.get(jsonPath);
file.modify(jsonPath, Array.isArray(value) ? [...value, ...paths] : paths);
};
}
function addDependenciesToPackageJson() {
return (host) => {
[
{
type: dependencies_1.NodeDependencyType.Dev,
name: '@angular/compiler-cli',
version: latest_versions_1.latestVersions.Angular,
},
{
type: dependencies_1.NodeDependencyType.Dev,
name: '@angular/build',
version: latest_versions_1.latestVersions.AngularBuild,
},
{
type: dependencies_1.NodeDependencyType.Dev,
name: 'ng-packagr',
version: latest_versions_1.latestVersions.NgPackagr,
},
{
type: dependencies_1.NodeDependencyType.Default,
name: 'tslib',
version: latest_versions_1.latestVersions['tslib'],
},
{
type: dependencies_1.NodeDependencyType.Dev,
name: 'typescript',
version: latest_versions_1.latestVersions['typescript'],
},
].forEach((dependency) => (0, dependencies_1.addPackageJsonDependency)(host, dependency));
return host;
};
}
function addLibToWorkspaceFile(options, projectRoot, projectName) {
return (0, workspace_1.updateWorkspace)((workspace) => {
workspace.projects.add({
name: projectName,
root: projectRoot,
sourceRoot: `${projectRoot}/src`,
projectType: workspace_models_1.ProjectType.Library,
prefix: options.prefix,
targets: {
build: {
builder: workspace_models_1.Builders.BuildNgPackagr,
defaultConfiguration: 'production',
options: {
project: `${projectRoot}/ng-package.json`,
},
configurations: {
production: {
tsConfig: `${projectRoot}/tsconfig.lib.prod.json`,
},
development: {
tsConfig: `${projectRoot}/tsconfig.lib.json`,
},
},
},
test: {
builder: workspace_models_1.Builders.BuildKarma,
options: {
tsConfig: `${projectRoot}/tsconfig.spec.json`,
polyfills: ['zone.js', 'zone.js/testing'],
},
},
},
});
});
}
function default_1(options) {
return async (host) => {
const prefix = options.prefix;
// If scoped project (i.e. "@foo/bar"), convert projectDir to "foo/bar".
const packageName = options.name;
if (/^@.*\/.*/.test(options.name)) {
const [, name] = options.name.split('/');
options.name = name;
}
const workspace = await (0, workspace_1.getWorkspace)(host);
const newProjectRoot = workspace.extensions.newProjectRoot || '';
let folderName = packageName.startsWith('@') ? packageName.slice(1) : packageName;
if (/[A-Z]/.test(folderName)) {
folderName = schematics_1.strings.dasherize(folderName);
}
const libDir = options.projectRoot !== undefined
? (0, posix_1.join)(options.projectRoot)
: (0, posix_1.join)(newProjectRoot, folderName);
const distRoot = `dist/${folderName}`;
const sourceDir = `${libDir}/src/lib`;
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [
(0, schematics_1.applyTemplates)({
...schematics_1.strings,
...options,
packageName,
libDir,
distRoot,
relativePathToWorkspaceRoot: (0, paths_1.relativePathToWorkspaceRoot)(libDir),
prefix,
angularLatestVersion: latest_versions_1.latestVersions.Angular.replace(/~|\^/, ''),
tsLibLatestVersion: latest_versions_1.latestVersions['tslib'].replace(/~|\^/, ''),
folderName,
}),
(0, schematics_1.move)(libDir),
]);
return (0, schematics_1.chain)([
(0, schematics_1.mergeWith)(templateSource),
addLibToWorkspaceFile(options, libDir, packageName),
options.skipPackageJson ? (0, schematics_1.noop)() : addDependenciesToPackageJson(),
options.skipTsConfig ? (0, schematics_1.noop)() : updateTsConfig(packageName, './' + distRoot),
options.standalone
? (0, schematics_1.noop)()
: (0, schematics_1.schematic)('module', {
name: options.name,
commonModule: false,
flat: true,
path: sourceDir,
project: packageName,
}),
(0, schematics_1.schematic)('component', {
name: options.name,
selector: `${prefix}-${options.name}`,
inlineStyle: true,
inlineTemplate: true,
flat: true,
path: sourceDir,
export: true,
standalone: options.standalone,
project: packageName,
}),
(_tree, context) => {
if (!options.skipPackageJson && !options.skipInstall) {
context.addTask(new tasks_1.NodePackageInstallTask());
}
},
]);
};
}