forked from open-indy/Koa11y
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-script.js
335 lines (285 loc) · 10.5 KB
/
build-script.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* eslint-disable no-console */
var start = Date.now();
// ///////////////////////// //
// IMPORTS //
// ///////////////////////// //
var fs = require('fs-extra');
var path = require('path');
var exec = require('child_process').execSync;
var NwBuilder = require('nw-builder');
var rcedit = require('rcedit');
var zip = require('7zip-bin');
// ///////////////////////// //
// SETTINGS //
// ///////////////////////// //
var year = '2017';
var company = 'Open Indy Brigade';
var description = 'Find accessibility issues in webpages.';
var platform = process.platform;
if (process.platform === 'linux' && process.arch === 'ia32') {
platform = 'linux32';
} else if (process.platform === 'linux' && process.arch === 'x64') {
platform = 'linux64';
} else if (process.platform === 'darwin') {
platform = 'osx64';
}
var nwBuildSettings = {
version: '0.14.7',
flavor: 'normal',
appName: 'Koa11y',
appVersion: '4.0.0',
files: [
'./fonts/**/*',
'./img/**/*',
'./markup/**/*',
'./scripts/**/*',
'./style/style.css',
'./style/ven.bootstrap.css',
'./style/ven.bootstrap/bootstrap.css',
'./style/ven.bootswatch/flatly.min.css',
'./index.htm',
'./package.json',
'./LICENSE'
],
winIco: './img/fav.ico',
platforms: [ platform ],
zip: false
};
var junk = [
'd3dcompiler_47.dll',
'dbghelp.dll',
'libEGL.dll',
'libexif.dll',
'libGLESv2.dll',
'nw_200_percent.pak',
'snapshot_blob.bin',
'package.nw/node_modules/vue/src/',
'package.nw/node_modules/vue/types/',
'package.nw/node_modules/vue/dist/vue.common.js',
'package.nw/node_modules/vue/dist/vue.esm.js',
'package.nw/node_modules/vue/dist/vue.js',
'package.nw/node_modules/vue/dist/vue.runtime.common.js',
'package.nw/node_modules/vue/dist/vue.runtime.esm.js',
'package.nw/node_modules/vue/dist/vue.runtime.js',
'package.nw/node_modules/vue/dist/vue.runtime.min.js',
'package.nw/node_modules/performance-now/src/',
'package.nw/node_modules/performance-now/test/',
'package.nw/node_modules/ajv/dist/ajv.min.js.map',
'package.nw/node_modules/ajv/scripts/',
'package.nw/node_modules/ajv/lib/ajv.d.ts',
'package.nw/node_modules/ajv/dist/',
'package.nw/node_modules/truffler/example/',
'package.nw/node_modules/truffler/test/',
'package.nw/node_modules/hawk/example/',
'package.nw/node_modules/hawk/test/',
'package.nw/node_modules/hawk/images/',
'package.nw/node_modules/pa11y/example/',
'package.nw/node_modules/pa11y/test/',
'package.nw/node_modules/es6-promise/es6-promise.d.ts'
];
// ///////////////////////// //
// FUNCTIONS //
// ///////////////////////// //
function updateExe (done) {
var extension = '';
if (platform === 'win32') {
extension = '.exe';
}
var executable = './build/' + nwBuildSettings.appName + '/' + platform + '/' + nwBuildSettings.appName + extension;
var options = {
'version-string': {
'CompanyName': company,
'FileDescription': description,
'LegalCopyright': 'Copyright ' + year + '. ' + company,
'ProductName': nwBuildSettings.appName
},
'file-version': nwBuildSettings.appVersion,
'product-version': nwBuildSettings.appVersion
};
if (platform === 'win32') {
rcedit(executable, options, function (a, b, c) {
if (a) {
console.log(a);
}
if (b) {
console.log(b);
}
if (c) {
console.log(c);
}
if (typeof(done) === 'function') {
done();
}
});
} else if (platform === 'linux64' || platform === 'linux32') {
exec('chmod +x ' + executable);
var fileName = 'Koa11y.desktop';
var fileData =
'[Desktop Entry]\n' +
'Type=Application\n' +
'Terminal=true\n' +
'Name=Run ' + nwBuildSettings.appName + '\n' +
'Icon=utilities-terminal\n' +
'Exec=gnome-terminal -e "bash -c \'./' + nwBuildSettings.appName + ';$SHELL\'"\n' +
'Categories=Application;';
var filePath = './build/' + nwBuildSettings.appName + '/' + platform + '/' + fileName;
fs.writeFileSync(filePath, fileData);
// TODO: On Linux auto-make the exe runnable, or a runnable .desktop file
exec('chmod +x ' + filePath);
done();
} else if (process.platform === 'darwin') {
fs.copySync('./img/app.icns', './build/' + nwBuildSettings.appName + '/osx64/' + nwBuildSettings.appName + '.app/Contents/Resources/app.icns');
fs.copySync('./img/document.icns', './build/' + nwBuildSettings.appName + '/osx64/' + nwBuildSettings.appName + '.app/Contents/Resources/document.icns');
done();
} else {
done();
}
}
function copyManifest () {
var manifest = fs.readJsonSync('./package.json');
manifest.devDependencies = {};
manifest.name = nwBuildSettings.appName;
var output = JSON.stringify(manifest, null, 2);
var buildPackagePath = './build/' + nwBuildSettings.appName + '/' + platform + '/package.json';
if (process.platform === 'darwin') {
buildPackagePath = path.join('.', 'build', nwBuildSettings.appName, platform, nwBuildSettings.appName + '.app', 'Contents', 'Resources', 'app.nw', 'package.json');
}
fs.writeFileSync(buildPackagePath, output);
}
function changeDirectoryToBuildFolder () {
var buildFolder = path.join(process.cwd(), 'build', nwBuildSettings.appName, platform);
if (process.platform === 'darwin') {
buildFolder = path.join(process.cwd(), 'build', nwBuildSettings.appName, platform, nwBuildSettings.appName + '.app', 'Contents', 'Resources', 'app.nw');
}
process.chdir(buildFolder);
}
function npmInstallBuildFolder () {
exec('npm install');
}
function moveFilesIntoPackageNW () {
if (process.platform !== 'darwin') {
console.log(' ∙ Started moving files into package.nw');
nwBuildSettings.files.forEach(function (file) {
var src = file.replace('/**/*', '');
var dest = path.join('package.nw', src);
fs.moveSync(src, dest);
console.log(' ∙ moved ' + src.replace('./', ''));
});
fs.moveSync('./node_modules', path.join('.', 'package.nw', 'node_modules'));
console.log(' ∙ moved node_modules');
}
}
function removeJunk () {
console.log(' ∙ Started removing junk files');
if (process.platform !== 'darwin') {
junk.push('style');
}
junk.forEach(function (item) {
var file = path.join(process.cwd(), item);
fs.removeSync(file);
console.log(' ∙ deleted ' + item);
});
}
function runApp () {
if (platform === 'win32') {
exec(nwBuildSettings.appName + '.exe');
} else if (process.platform === 'darwin') {
exec('open ' + nwBuildSettings.appName + '.app');
} else {
console.log('WARNING: WE STILL DON\'T KNOW HOW TO AUTO RUN EXE ON LINUX');
}
}
function goUpOneDirectory () {
process.chdir('..');
}
function renameBuiltFolder () {
// win32 => Koa11y
// linux64 => Koa11y
fs.renameSync('./' + platform, './' + nwBuildSettings.appName);
}
function zipApp () {
var zipExe = zip.path7za;
var buildInput = '';
var outputZip = '';
var filename = nwBuildSettings.appName + '_' + nwBuildSettings.appVersion + '.zip';
if (process.platform === 'win32') {
buildInput = nwBuildSettings.appName;
outputZip = path.join('WIN_' + filename);
} else if (process.platform === 'darwin') {
buildInput = path.join(nwBuildSettings.appName + '.app');
outputZip = path.join('OSX_' + filename);
} else if (process.platform === 'linux') {
buildInput = path.join(nwBuildSettings.appName);
if (process.arch === 'x64') {
outputZip = path.join('LIN64_' + filename);
} else if (process.arch === 'ia32' || process.arch === 'x86') {
outputZip = path.join('LIN32_' + filename);
}
}
fs.removeSync(outputZip);
// a = create archive
// -bd = do not display a progress bar in the CLI
// -tzip = create a zip formatted file
// -mx=9 = use maximum compression
// -y = auto answer yes to all prompts
exec(zipExe + ' a -tzip -mx=9 -y "' + outputZip + '" "' + buildInput + '"');
}
function totalBuildTime () {
var done = Date.now() - start;
var time = Math.round(done / 1000);
var minutes = Math.floor(time / 60);
var seconds = time - (minutes * 60);
seconds = (new Array(3).join('0') + seconds).slice(-2);
var finalTime = minutes + ':' + seconds;
var timeMinutes = '\nBuild took ' + finalTime;
return timeMinutes;
}
// ///////////////////////// //
// CLEAN //
// ///////////////////////// //
fs.removeSync('./build');
// ///////////////////////// //
// EXECUTION //
// ///////////////////////// //
var nw = new NwBuilder(nwBuildSettings);
nw.on('log', function (msg) {
console.log(msg);
});
nw.build().then(function () {
console.log(' ∙ NW-Builder Complete');
updateExe(function () {
if (process.platform === 'win32') {
console.log(' ∙ Updated ' + nwBuildSettings.appName + '.exe');
}
copyManifest();
console.log(' ∙ Copied package.json');
changeDirectoryToBuildFolder();
console.log(' ∙ cd to build folder');
npmInstallBuildFolder();
console.log(' ∙ npm install build folder');
moveFilesIntoPackageNW();
console.log(' ∙ Finished moving files into package.nw');
removeJunk();
console.log(' ∙ Finished removing junk files');
if (process.platform === 'darwin') {
// go up 4 folders to get to the folder that holds YourAppName.app
for (var i = 0; i < 4; i++) {
goUpOneDirectory();
}
console.log(' ∙ Went up four directories');
}
runApp();
console.log(' ∙ Ran app');
if (process.platform !== 'darwin') {
goUpOneDirectory();
console.log(' ∙ Went up one directory');
renameBuiltFolder();
console.log(' ∙ Renamed built folder');
}
zipApp();
console.log(' ∙ Zipped app');
console.log(totalBuildTime());
});
}).catch(function (err) {
console.log('nw-builder err', err);
});