forked from webcat12345/ngx-ui-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
152 lines (137 loc) · 4.09 KB
/
build.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
/* tslint:disable:import-blacklist */
// from https://github.com/angular/angularfire2/blob/master/tools/build.js
import { rollup } from 'rollup';
import { spawn } from 'child_process';
import { Observable } from 'rxjs';
import { copy } from 'fs-extra';
import * as copyfiles from 'copy';
import * as filesize from 'rollup-plugin-filesize';
import * as sourcemaps from 'rollup-plugin-sourcemaps';
const pkg = require(`${process.cwd()}/package.json`);
// Rollup globals
const GLOBALS = {
'@angular/core': 'ng.core',
'@angular/common': 'ng.common',
'@angular/forms': 'ng.forms',
'@angular/animations': 'ng.animations',
'@angular/platform-browser': 'ng.platformBrowser',
'rxjs': 'Rx',
'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx',
'rxjs/Observer': 'Rx',
'rxjs/Subscription': 'Rx',
};
// Constants for running typescript commands
const NGC = './node_modules/.bin/ngc';
const TSC_ARGS = (config = 'build') => [`-p`, `${process.cwd()}/src/lib/tsconfig-${config}.json`];
/**
* Create an Observable of a spawned child process.
* @param {string} command
* @param {string[]} args
*/
function spawnObservable(command, args) {
return Observable.create(observer => {
const cmd = spawn(command, args);
observer.next(''); // hack to kick things off, not every command will have a stdout
cmd.stdout.on('data', (data) => { observer.next(data.toString()); });
cmd.stderr.on('data', (data) => { observer.error(data.toString()); });
cmd.on('close', (data) => { observer.complete(); });
});
}
function generateBundle(input, file, globals, name, format) {
const plugins = [
sourcemaps(),
filesize(),
];
return rollup({
input,
external: Object.keys(globals),
file,
plugins,
}).then(bundle => {
console.log(file);
return bundle.write({
file,
name,
globals,
format,
sourcemap: true,
});
});
}
function createUmd(globals) {
const name = 'ngx-ui-switch';
const entry = `${process.cwd()}/dist/es5/index.js`;
return generateBundle(
entry,
`${process.cwd()}/dist/packages-dist/ui-switch.umd.js`,
globals,
name,
'umd',
);
}
function createEs(globals, target) {
const name = 'ngx-ui-switch';
const entry = `${process.cwd()}/dist/${target}/index.js`;
return generateBundle(
entry,
`${process.cwd()}/dist/packages-dist/ui-switch.${target}.js`,
globals,
name,
'es',
);
}
function getVersions() {
const paths = [
`${process.cwd()}/dist/packages-dist/package.json`,
];
return paths
.map(path => require(path))
.map(pkgs => pkgs.version);
}
function verifyVersions() {
const versions = getVersions();
console.log(versions);
versions.map(version => {
if (version !== pkg.version) {
throw new Error('Versions mismatch');
}
});
}
function buildModule(globals) {
const es2015$ = spawnObservable(NGC, TSC_ARGS());
const esm$ = spawnObservable(NGC, TSC_ARGS('esm'));
return Observable
.forkJoin(es2015$, esm$);
}
function createBundles(globals) {
return Observable
.forkJoin(
Observable.from(createUmd(globals)),
Observable.from(createEs(globals, 'es2015')),
Observable.from(createEs(globals, 'es5')),
);
}
function copyFiles() {
const copyAll: ((s: string, s1: string) => any) = Observable.bindCallback(copyfiles);
return Observable
.forkJoin(
copyAll(`${process.cwd()}/dist/es5/**/*.d.ts`, `${process.cwd()}/dist/packages-dist`),
copyAll(`${process.cwd()}/dist/es5/**/*.metadata.json`, `${process.cwd()}/dist/packages-dist`),
Observable.from(copy(`${process.cwd()}/README.md`, `${process.cwd()}/dist/packages-dist/README.md`)),
Observable.from(copy(`${process.cwd()}/src/lib/package.json`, `${process.cwd()}/dist/packages-dist/package.json`)),
);
}
function buildLibrary(globals) {
const modules$ = buildModule(globals);
return Observable
.forkJoin(modules$)
.switchMap(() => createBundles(globals))
.switchMap(() => copyFiles())
.do(() => verifyVersions());
}
buildLibrary(GLOBALS).subscribe(
data => console.log('success'),
err => console.log('err', err),
() => console.log('complete'),
);