This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathbuild.js
More file actions
200 lines (156 loc) · 5.46 KB
/
Copy pathbuild.js
File metadata and controls
200 lines (156 loc) · 5.46 KB
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
195
196
197
198
199
200
'use strict';
// Imports
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const getCdnVersion = require('./get-cdn-version');
const utils = require('./utils');
// Constants
const ROOT_DIR = '.';
const DST_DIR = 'build';
const SRC_DIR = 'src';
const CDN_VERSIONS = ['1.2', '1.8'];
const CDN_REPLACE_FILES = ['index.html', 'js/download-data.js'];
const GIT_BRANCH_DIST = 'dist';
const PTOR_CONF = process.env.TRAVIS ? 'protractorConfTravis.js' : 'protractorConfLocal.js';
const PTOR_PORT = '8100';
const PTOR_ENV = {
ANGULAR_HOME_HOST: `http://localhost:${PTOR_PORT}`,
ANGULAR_DOWNLOAD_VERSIONS: '-',
ANGULAR_VERSION: '-',
CHECK_SCRIPT_TAG: 'true'
};
// Variables - Private
const args = process.argv.slice(2);
const actions = parseArgs(args);
// Run
_main(actions);
// Functions - Definitions
function _main(actions) {
const callbacks = [];
if (actions.copy) {
callbacks.push(copySource, getCdnVersions, updateProtractorEnv, replaceCdnVersionsInFiles);
}
if (actions.test) {
callbacks.push(testBuild);
}
if (actions.dist) {
callbacks.push(updateDist);
}
return callbacks.
reduce((promise, cb) => promise.then(cb), Promise.resolve()).
catch(onError);
}
function announce(message) {
const ruler = new Array(81).join('-');
console.log(`${ruler}\n${message}\n`);
}
function copySource() {
announce(`Copying source files from '${SRC_DIR}' to '${DST_DIR}'...`);
return Promise.resolve().
then(() => utils.removeDir(DST_DIR)).
then(() => utils.createDir(DST_DIR)).
then(() => utils.copyContent(SRC_DIR, DST_DIR));
}
function getCdnVersions() {
announce(`Getting the latest versions available on CDN...`);
return Promise.all(CDN_VERSIONS.map(getCdnVersion));
}
function mapVersionsToPlaceholders(cdnVersions) {
return cdnVersions.reduce((map, v) => {
const tokens = v.split('.');
map[v] = new RegExp(`\\\${CDN_VERSION_${tokens[0]}_${tokens[1]}}`, 'g');
return map;
}, {});
}
function onError(err) {
const cmd = `build${args.length ? ' ' + args.join(' ') : ''}`;
if (isFinite(err)) err = `Exit code: ${err}`;
console.error(`ERROR (running '${cmd}'): ${err}\n ${err.stack || ''}`);
process.exit(1);
}
function parseArgs(args) {
var actions = {
copy: false,
test: false,
dist: false
};
if (!args.length) {
actions.copy = true;
actions.test = true;
} else {
args.forEach(arg => {
switch (arg) {
case 'copy':
case 'test':
case 'dist':
actions[arg] = true;
break;
default:
onError(`unrecognized option ${arg}`);
break;
}
});
}
return actions;
}
function replaceCdnVersionsInFiles(cdnVersions) {
announce(`Replacing CDN versions in files (${CDN_REPLACE_FILES.join(', ')})...`);
const versionMap = mapVersionsToPlaceholders(cdnVersions);
const replaceVersionsInFile = file => {
const filePath = path.join(DST_DIR, file);
return utils.replaceInFile(versionMap, filePath);
};
return Promise.all(CDN_REPLACE_FILES.map(replaceVersionsInFile));
}
function testBuild() {
announce(`Testing the current build (ENV: ${JSON.stringify(PTOR_ENV, null, 2)})...`);
const protractorOptions = {
env: Object.assign(process.env, PTOR_ENV),
stdio: 'inherit'
};
const installCmd = `${utils.getExecutable('yarn', true)} install`;
const httpServerCmd = `${utils.getExecutable('http-server')} -p ${PTOR_PORT} ${DST_DIR}`;
const wdrManagerCmd = `${utils.getExecutable('webdriver-manager')} update`;
const protractorCmd = `${utils.getExecutable('protractor')} ${PTOR_CONF}`;
let setupPromise = Promise.resolve();
if (!process.env.TRAVIS) {
setupPromise = chain(setupPromise, installCmd);
setupPromise = chain(setupPromise, wdrManagerCmd);
}
const httpServerPromise = chain(setupPromise, httpServerCmd);
const protractorPromise = chain(setupPromise, protractorCmd, protractorOptions);
const killHttpServer = () => httpServerPromise.$$killProcess();
return utils.finallyAsPromised(protractorPromise, killHttpServer);
// Helpers
function chain(promise, cmd, options) {
let innerPromise;
promise = promise.then(() => innerPromise = utils.spawnAsPromised(cmd, options));
promise.$$killProcess = () => innerPromise && utils.killProcess(innerPromise.$$process);
return promise;
}
}
function updateDist() {
announce(`Updating '${GIT_BRANCH_DIST}' branch with the current build...`);
return utils.
execAsPromised('git rev-parse --abbrev-ref HEAD').
then(originalBranch => {
const restoreBranch = () => utils.spawnAsPromised(`git checkout ${originalBranch.trim()}`);
const promise = Promise.resolve().
then(() => utils.spawnAsPromised(`git checkout ${GIT_BRANCH_DIST}`)).
then(() => utils.keepOnly(DST_DIR)).
then(() => utils.copyContent(DST_DIR, ROOT_DIR)).
then(() => utils.removeDir(DST_DIR)).
then(() => utils.spawnAsPromised(`git add --all ${ROOT_DIR}`)).
then(() => utils.spawnAsPromised('git commit -m "update site from src"').catch(() => {}));
return utils.finallyAsPromised(promise, restoreBranch);
});
}
function updateProtractorEnv(cdnVersions) {
announce(`Updating version-related environmental variables (for Protractor)...`);
PTOR_ENV.ANGULAR_VERSION = cdnVersions[cdnVersions.length - 1];
PTOR_ENV.ANGULAR_DOWNLOAD_VERSIONS = cdnVersions.
map((cdnVersion, idx) => `${cdnVersion}:${CDN_VERSIONS[idx]}.x`).
join(' ');
return cdnVersions;
}