forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packages.ts
262 lines (221 loc) · 7.61 KB
/
packages.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* @license
* Copyright Google Inc. 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.io/license
*/
// tslint:disable-next-line: no-global-tslint-disable
// tslint:disable: no-implicit-dependencies
import { JsonObject } from '@angular-devkit/core';
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';
const distRoot = path.join(__dirname, '../dist');
const { packages: monorepoPackages } = require('../.monorepo.json');
export interface PackageInfo {
name: string;
root: string;
bin: { [name: string]: string};
relative: string;
main: string;
dist: string;
build: string;
tar: string;
private: boolean;
experimental: boolean;
packageJson: JsonObject;
dependencies: string[];
reverseDependencies: string[];
snapshot: boolean;
snapshotRepo: string;
snapshotHash: string;
version: string;
}
export type PackageMap = { [name: string]: PackageInfo };
function loadPackageJson(p: string) {
const root = require('../package.json');
const pkg = require(p);
for (const key of Object.keys(root)) {
switch (key) {
// Keep the following keys from the package.json of the package itself.
case 'bin':
case 'description':
case 'dependencies':
case 'name':
case 'main':
case 'peerDependencies':
case 'optionalDependencies':
case 'typings':
case 'version':
case 'private':
case 'workspaces':
case 'resolutions':
case 'scripts':
continue;
// Remove the following keys from the package.json.
case 'devDependencies':
delete pkg[key];
continue;
// Merge the following keys with the root package.json.
case 'keywords':
const a = pkg[key] || [];
const b = Object.keys(
root[key].concat(a).reduce((acc: {[k: string]: boolean}, curr: string) => {
acc[curr] = true;
return acc;
}, {}));
pkg[key] = b;
break;
// Overwrite engines to a common default.
case 'engines':
pkg['engines'] = {
'node': '>= 10.13.0',
'npm': '>= 6.11.0',
'yarn': '>= 1.13.0',
};
break;
// Overwrite the package's key with to root one.
default:
pkg[key] = root[key];
}
}
return pkg;
}
function _findAllPackageJson(dir: string, exclude: RegExp): string[] {
const result: string[] = [];
fs.readdirSync(dir)
.forEach(fileName => {
const p = path.join(dir, fileName);
if (exclude.test(p)) {
return;
} else if (/[\/\\]node_modules[\/\\]/.test(p)) {
return;
} else if (fileName == 'package.json') {
result.push(p);
} else if (fs.statSync(p).isDirectory() && fileName != 'node_modules') {
result.push(..._findAllPackageJson(p, exclude));
}
});
return result;
}
const tsConfigPath = path.join(__dirname, '../tsconfig.json');
const tsConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
const pattern = '^('
+ (tsConfig.config.exclude as string[])
.map(ex => path.join(path.dirname(tsConfigPath), ex))
.map(ex => '('
+ ex
.replace(/[\-\[\]{}()+?./\\^$|]/g, '\\$&')
.replace(/(\\\\|\\\/)\*\*/g, '((\/|\\\\).+?)?')
.replace(/\*/g, '[^/\\\\]*')
+ ')')
.join('|')
+ ')($|/|\\\\)';
const excludeRe = new RegExp(pattern);
// Find all the package.json that aren't excluded from tsconfig.
const packageJsonPaths = _findAllPackageJson(path.join(__dirname, '..'), excludeRe)
// Remove the root package.json.
.filter(p => p != path.join(__dirname, '../package.json'));
function _exec(cmd: string) {
return execSync(cmd).toString().trim();
}
let gitShaCache: string;
function _getSnapshotHash(_pkg: PackageInfo): string {
if (!gitShaCache) {
gitShaCache = _exec('git log --format=%h -n1');
}
return gitShaCache;
}
let stableVersion = '';
let experimentalVersion = '';
function _getVersionFromGit(experimental: boolean): string {
if (stableVersion && experimentalVersion) {
return experimental ? experimentalVersion : stableVersion;
}
const hasLocalChanges = _exec(`git status --porcelain`) != '';
const scmVersionTagRaw = _exec(`git describe --match v[0-9]*.[0-9]*.[0-9]* --abbrev=7 --tags`)
.slice(1);
stableVersion = scmVersionTagRaw.replace(/-([0-9]+)-g/, '+$1.');
if (hasLocalChanges) {
stableVersion += stableVersion.includes('+') ? '.with-local-changes' : '+with-local-changes';
}
experimentalVersion = stableToExperimentalVersion(stableVersion);
return experimental ? experimentalVersion : stableVersion;
}
/**
* Convert a stable version to its experimental equivalent. For example,
* stable = 10.2.3, experimental = 0.1002.3
* @param stable Must begin with d+.d+ where d is a 0-9 digit.
*/
export function stableToExperimentalVersion(stable: string): string {
return `0.${stable.replace(/^(\d+)\.(\d+)/, (_, major, minor) => {
return '' + (parseInt(major, 10) * 100 + parseInt(minor, 10));
})}`;
}
// All the supported packages. Go through the packages directory and create a map of
// name => PackageInfo. This map is partial as it lacks some information that requires the
// map itself to finish building.
export const packages: PackageMap =
packageJsonPaths
.map(pkgPath => ({ root: path.dirname(pkgPath) }))
.reduce((packages: PackageMap, pkg) => {
const pkgRoot = pkg.root;
const packageJson = loadPackageJson(path.join(pkgRoot, 'package.json'));
const name = packageJson['name'];
if (!name) {
// Only build the entry if there's a package name.
return packages;
}
if (!(name in monorepoPackages)) {
throw new Error(
`Package ${name} found in ${JSON.stringify(pkg.root)}, not found in .monorepo.json.`,
);
}
const bin: {[name: string]: string} = {};
Object.keys(packageJson['bin'] || {}).forEach(binName => {
let p = path.resolve(pkg.root, packageJson['bin'][binName]);
if (!fs.existsSync(p)) {
p = p.replace(/\.js$/, '.ts');
}
bin[binName] = p;
});
const experimental = !!packageJson.private || !!packageJson.experimental;
packages[name] = {
build: path.join(distRoot, pkgRoot.substr(path.dirname(__dirname).length)),
dist: path.join(distRoot, name),
root: pkgRoot,
relative: path.relative(path.dirname(__dirname), pkgRoot),
main: path.resolve(pkgRoot, 'src/index.ts'),
private: !!packageJson.private,
experimental,
// yarn doesn't take kindly to @ in tgz filenames
// https://github.com/yarnpkg/yarn/issues/6339
tar: path.join(distRoot, name.replace(/\/|@/g, '_') + '.tgz'),
bin,
name,
packageJson,
snapshot: !!monorepoPackages[name].snapshotRepo,
snapshotRepo: monorepoPackages[name].snapshotRepo,
get snapshotHash() {
return _getSnapshotHash(this);
},
dependencies: [],
reverseDependencies: [],
get version() {
return _getVersionFromGit(experimental);
},
};
return packages;
}, {});
// Update with dependencies.
for (const pkgName of Object.keys(packages)) {
const pkg = packages[pkgName];
const pkgJson = require(path.join(pkg.root, 'package.json'));
pkg.dependencies = Object.keys(packages).filter(name => {
return name in (pkgJson.dependencies || {})
|| name in (pkgJson.devDependencies || {});
});
pkg.dependencies.forEach(depName => packages[depName].reverseDependencies.push(pkgName));
}