-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.ts
113 lines (88 loc) · 3.77 KB
/
util.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
import { BuilderContext } from '@angular-devkit/architect/src/index2';
import { buildOptimizer } from '@angular-devkit/build-optimizer';
import { ClosureBuilderSchema } from './closure/schema.interface';
import { RollupBuilderSchema } from './rollup/schema.interface';
import * as ts from 'typescript';
import { Observable, of } from 'rxjs';
import { exec } from 'child_process';
import { normalize, join } from 'path';
import { glob } from 'glob';
import { readFileSync, writeFile, readFile } from 'fs';
import { AbstractBuilderSchema } from './abstract.interface';
export function handleEnvironment(
options: AbstractBuilderSchema | RollupBuilderSchema | ClosureBuilderSchema,
context: BuilderContext
): Observable<{}> {
// TODO: handle different environments
return of(exec('cp '+
normalize('out-tsc/src/environments/environment.prod.js') + ' ' +
normalize('out-tsc/src/environments/environment.js')
));
}
export function optimizeBuild(
options: AbstractBuilderSchema | RollupBuilderSchema | ClosureBuilderSchema,
context: BuilderContext
): Observable<{}> {
// TODO: convert to Observable pattern
const files = glob.sync(normalize('out-tsc/**/*.component.js'));
return of(Promise.all(files.map((file) => {
return new Promise((res, rej) => {
readFile(file, 'utf-8', (err, data) => {
if (err) rej(err);
writeFile(file, buildOptimizer({ content: data }).content, (err) => {
if (err) rej(err);
res(file);
});
});
})
})));
}
export function compileMain(
options: AbstractBuilderSchema | RollupBuilderSchema | ClosureBuilderSchema,
context: BuilderContext
): Observable<{}> {
context.reportStatus('aot');
return new Observable((observer) => {
const inFile = normalize(context.workspaceRoot+'/src/main.ts');
const outFile = normalize('out-tsc/src/main.js');
const tsConfig = JSON.parse(readFileSync(join(context.workspaceRoot, options.tsConfig), 'utf8'));
readFile(inFile, 'utf8', (err, contents) => {
if (err) observer.error(err);
contents = contents.replace(/platformBrowserDynamic/g, 'platformBrowser');
contents = contents.replace(/platform-browser-dynamic/g, 'platform-browser');
contents = contents.replace(/bootstrapModule/g, 'bootstrapModuleFactory');
contents = contents.replace(/AppModule/g, 'AppModuleNgFactory');
contents = contents.replace(/.module/g, '.module.ngfactory');
const outputContent = ts.transpileModule(contents, {
compilerOptions: tsConfig.compilerOptions,
moduleName: 'app'
})
writeFile(outFile, outputContent.outputText, (err) => {
context.reportProgress(options.step++, options.tally, 'aot');
if (err) observer.error(err);
observer.next(outputContent.outputText);
});
});
});
}
export function ngc(
options: AbstractBuilderSchema | RollupBuilderSchema | ClosureBuilderSchema,
context: BuilderContext
): Observable<{}> {
context.reportProgress(options.step++, options.tally, 'ngc');
return new Observable((observer) => {
exec(normalize(context.workspaceRoot +'/node_modules/.bin/ngc') +
' -p ' + options.tsConfig,
{},
(error, stdout, stderr) => {
if (stderr) {
context.reportStatus(stderr);
observer.error(stderr);
} else {
context.reportStatus('Compilation complete.');
context.reportProgress(options.step++, options.tally, 'ngc');
observer.next(stdout);
}
});
});
}