-
Notifications
You must be signed in to change notification settings - Fork 118
/
ngc-build.js
92 lines (70 loc) · 2.26 KB
/
ngc-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
const sass = require('node-sass');
const fs = require('fs-extra');
const path = require('path');
const { exec } = require('child_process');
const tempFolder = '.tmp';
const componentPath = `./${tempFolder}/src/image-viewer.component.ts`;
const scssPath = `./${tempFolder}/src/image-viewer.scss`;
process();
async function process() {
console.log('Running build...');
prepareTempFolder();
const component = await getFileContent(componentPath);
const css = getCss(await getFileContent(scssPath));
console.log('Inlining styles...');
const newComponent = component.replace('styles: []', `styles: ['${css}']`);
const write = await writeFile(componentPath, newComponent);
console.log('About to run ngc async...');
const es2015 = runNgc(`${tempFolder}/tsconfig.json`);
const umd = runNgc(`${tempFolder}/tsconfig.umd.json`);
const build = await Promise.all([es2015, umd]);
console.log('Moving /dist to root');
moveDist();
}
function moveDist() {
return fs.copy(`${tempFolder}/dist`, 'dist', { overwrite: true });
}
function runNgc(tsConfigPath) {
console.log('Started for', tsConfigPath);
const ngc = path.resolve('node_modules', '.bin', 'ngc');
return new Promise((resolve, reject) => {
exec(`${ngc} -p ${tsConfigPath}`, (err, stdout, stdeer) => {
if (err) {
console.log('Error !', err);
reject(err);
}
console.log('Done for', tsConfigPath);
resolve(tsConfigPath);
});
});
}
function getCss(scss_content) {
const style = sass.renderSync({
data: scss_content
});
return style.css
.toString()
.replace(/([\n\r]\s*)+/gm, ' ')
.replace(/"/g, '\\"');
}
async function getFileContent(path) {
return fs.readFile(path, 'utf8');
}
function writeFile(path, data) {
return new Promise((resolve, reject) => {
fs.writeFile(path, data, err => {
if (err) {
console.log('Error while writing file !', err);
reject(err);
}
resolve(path);
});
});
}
function prepareTempFolder() {
fs.removeSync(tempFolder);
fs.copySync('src', `${tempFolder}/src`);
fs.copySync('ionic-img-viewer.ts', `${tempFolder}/ionic-img-viewer.ts`);
fs.copySync('tsconfig.json', `${tempFolder}/tsconfig.json`);
fs.copySync('tsconfig.umd.json', `${tempFolder}/tsconfig.umd.json`);
}