forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·194 lines (173 loc) · 6.1 KB
/
build.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
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
#!/usr/bin/env node
'use strict';
/* eslint-disable no-console */
const chalk = require('chalk');
const denodeify = require('denodeify');
const fs = require('fs');
const glob = denodeify(require('glob'));
const path = require('path');
const npmRun = require('npm-run');
const rimraf = denodeify(require('rimraf'));
const root = path.join(__dirname, '../..');
const dist = path.join(root, 'dist');
const packagesRoot = path.join(root, 'packages');
function copy(from, to) {
from = path.relative(process.cwd(), from);
to = path.relative(process.cwd(), to);
return new Promise((resolve, reject) => {
const rs = fs.createReadStream(from);
const ws = fs.createWriteStream(to);
rs.on('error', reject);
ws.on('error', reject);
ws.on('close', resolve);
rs.pipe(ws);
});
}
function rm(p) {
path.relative(process.cwd(), p);
return new Promise((resolve, reject) => {
fs.unlink(p, err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
function getDeps(pkg) {
const packageJson = require(pkg.packageJson);
return Object.assign({}, packageJson['dependencies'], packageJson['devDependencies']);
}
// First delete the dist folder.
Promise.resolve()
.then(() => console.log('Deleting dist folder...'))
.then(() => rimraf(dist))
.then(() => console.log('Creating schema.d.ts...'))
.then(() => {
const script = path.join(root, 'scripts/build-schema-dts.js');
const input = path.join(root, 'packages/@angular/cli/lib/config/schema.json');
const output = path.join(root, 'packages/@angular/cli/lib/config/schema.d.ts');
return npmRun.execSync(`node "${script}" "${input}" "${output}"`);
})
.then(() => console.log('Compiling packages...'))
.then(() => {
const packages = require('../../lib/packages');
// Order packages in order of dependency.
// We use bubble sort because we need a full topological sort but adding another dependency
// or implementing a full topo sort would be too much work and I'm lazy. We don't anticipate
// any large number of
const sortedPackages = Object.keys(packages);
let swapped = false;
do {
swapped = false;
for (let i = 0; i < sortedPackages.length - 1; i++) {
for (let j = i + 1; j < sortedPackages.length; j++) {
const a = sortedPackages[i];
const b = sortedPackages[j];
if (Object.keys(getDeps(packages[a])).indexOf(b) != -1) {
// Swap them.
[sortedPackages[i], sortedPackages[i + 1]] = [sortedPackages[i + 1], sortedPackages[i]];
swapped = true;
}
}
}
} while(swapped);
return sortedPackages
.reduce((promise, packageName) => {
const pkg = packages[packageName];
const name = path.relative(packagesRoot, pkg.root);
return promise.then(() => {
console.log(` ${name}`);
try {
return npmRun.execSync(`tsc -p "${path.relative(process.cwd(), pkg.root)}"`);
} catch (err) {
throw new Error(`Compilation error.\n${err.stdout}`);
}
});
}, Promise.resolve());
})
.then(() => console.log('Copying uncompiled resources...'))
.then(() => glob(path.join(packagesRoot, '**/*'), { dot: true }))
.then(files => {
console.log(`Found ${files.length} files...`);
return files
.map((fileName) => path.relative(packagesRoot, fileName))
.filter((fileName) => {
if (/^@angular[\\\/]cli[\\\/]blueprints/.test(fileName)) {
return true;
}
if (/\.d\.ts$/.test(fileName)) {
// The last thing we want is d.ts files...
return false;
}
if (/\.spec\.ts$/.test(fileName)) {
// Also spec.ts files...
return false;
}
if (/spec-utils.ts/.test(fileName)) {
// TODO: get rid of this by splitting spec utils in its own package.
return false;
}
if (/\.ts$/.test(fileName)) {
// Verify that it was actually built.
if (!fs.existsSync(path.join(dist, fileName).replace(/ts$/, 'js'))) {
throw new Error(`Source found but compiled file not found: "${fileName}".`);
}
// Skip all source files, since we have their JS files now.
return false;
}
// The only remaining file we want to ignore is tsconfig and spec files.
return !(/tsconfig\.json$/.test(fileName))
&& !(/\.spec\./.test(fileName))
&& !(/[\/\\]tests[\/\\]/.test(fileName));
})
.map((fileName) => {
const source = path.join(packagesRoot, fileName);
const dest = path.join(dist, fileName);
if (fs.statSync(source).isDirectory()) {
try {
fs.mkdirSync(dest);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
} else {
return copy(source, dest);
}
})
.reduce((promise, current) => {
return promise.then(() => current);
}, Promise.resolve());
})
.then(() => glob(path.join(dist, '**/*.spec.*')))
.then(specFiles => specFiles.filter(fileName => {
return !/[\\\/]@angular[\\\/]cli[\\\/]blueprints/.test(fileName);
}))
.then(specFiles => {
console.log(`Found ${specFiles.length} spec files...`);
return Promise.all(specFiles.map(rm));
})
.then(() => {
// Copy all resources that might have been missed.
const extraFiles = ['CHANGELOG.md', 'CONTRIBUTING.md', 'README.md'];
return Promise.all(extraFiles.map(fileName => {
console.log(`Copying ${fileName}...`);
return copy(fileName, path.join('dist/@angular/cli', fileName));
}));
})
.then(() => {
// Copy LICENSE into all the packages
console.log('Copying LICENSE...');
const packages = require('../../lib/packages');
return Promise.all(Object.keys(packages).map(pkgName => {
const pkg = packages[pkgName];
console.log(` ${pkgName}`);
return copy('LICENSE', path.join(pkg.dist, 'LICENSE'));
}));
})
.then(() => process.exit(0), (err) => {
console.error(chalk.red(err.message));
process.exit(1);
});