comparison build-prod.js @ 138:efa231b10580

Script for running ng build --prod, but with some beautifully brittle bodges for workarounds I've been doing manually. No automation for git commits and pushing to gh-pages yet.
author Lucas Thompson <dev@lucas.im>
date Tue, 21 Mar 2017 01:06:13 +0000
parents
children 7005b0d11509
comparison
equal deleted inserted replaced
137:ec3978a3ed30 138:efa231b10580
1 /**
2 * Created by lucas on 20/03/2017.
3 */
4 'use strict';
5 const webpack = require('webpack');
6 const cli = require('@angular/cli');
7 const fs = require('fs');
8 webpack.optimize = Object.assign({}, webpack.optimize, {
9 UglifyJsPlugin: function () { this.apply = () => {}; }
10 });
11
12 const bundlePrefixes = [
13 'main',
14 'polyfills',
15 'scripts',
16 'styles',
17 'vendor',
18 'inline'
19 ];
20
21 const distDir = './dist';
22 const tmpDir = `${distDir}/gh-pages`;
23
24 const getGeneratedBundleNames = () => {
25 return new Promise((res, rej) => {
26 fs.readdir(distDir, (err, files) => {
27 if (err) {
28 rej(1);
29 } else {
30 res(files.filter(file => bundlePrefixes.find(
31 prefix => file.startsWith(prefix)
32 )));
33 }
34 })
35 });
36 };
37
38 const throwIfBuildReportedError = (result) => {
39 return new Promise((res, rej) => {
40 if (typeof result === 'object' && result.exitCode != null) {
41 if (result.exitCode === 0) {
42 res();
43 } else {
44 rej(result.exitCode);
45 }
46 } else if(typeof result === 'number') {
47 if (result === 0) {
48 res();
49 } else {
50 rej(result);
51 }
52 } else {
53 res();
54 }
55 });
56 };
57
58 const copyToTmpDir = (bundles) => {
59 // TODO should really handle errors for all of this
60 // and it could be async but i'm too lazy & it doesn't really matter for this
61 if (!fs.existsSync(tmpDir)){
62 fs.mkdirSync(tmpDir);
63 }
64 for (let bundle of bundles) {
65 const parts = bundle.split('.');
66 const bundlePrefix = parts[0];
67 const bundleExtension = parts[parts.length - 1];
68 const contents = fs.readFileSync(`${distDir}/${bundle}`);
69 fs.writeFileSync(
70 `${tmpDir}/${bundlePrefix}.bundle.${bundleExtension}`,
71 contents
72 );
73 }
74 };
75
76 cli({
77 cliArgs: ['build', '--prod', '--aot', 'false', '-bh', '/ugly-duckling/'],
78 inputStream: process.stdin,
79 outputStream: process.stdout
80 })
81 .then(throwIfBuildReportedError)
82 .then(getGeneratedBundleNames)
83 .then(copyToTmpDir)
84 .then(process.exit)
85 .catch(process.exit);