-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
128 lines (116 loc) · 3.42 KB
/
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
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
/*
* This script builds the Scout homepage. It copies static files to the /dist folder
* and transpiles ES6 JavaScript code in ES5. The output of this build may be deployed
* to the web server of the Scout homepage.
*
* IMPORTANT: you must start the express server before running the build!
* npm run start
*
* Copyright (c) BSI Business Systems Integration AG. All rights reserved.
* http://www.bsiag.com/
*/
const wget = require('node-wget');
const fs = require('fs');
const fse = require('fs-extra');
const babel = require('@babel/core');
const SERVER_ROOT = 'http://localhost:8095/scout';
const DIST_DIR = './dist/';
const JS_SRC_DIR = './js/';
const PAGES = [
{url: '/', filename: 'index.html'},
{url: '/versions.html', filename: 'versions.html'},
{url: '/features.html', filename: 'features.html'},
{url: '/css/styles.css', filename: 'css/styles.css'}
];
/**
* Export Pages built with Handlebars and LESS as plain static HTML and CSS files.
*/
function exportFromExpressServer() {
ensureFolderExists(DIST_DIR);
ensureFolderExists(DIST_DIR + '/css');
return new Promise((resolve, reject) => {
let numExportedPages = 0;
PAGES.forEach(page => {
let url = SERVER_ROOT + page.url;
let dest = DIST_DIR + page.filename;
let pageInfo = 'url=' + page.url + ' dest=' + DIST_DIR + page.filename;
console.log('Exporting page ' + pageInfo + '...');
wget({
url: url,
dest: dest
}, (error, response, body) => {
if (error) {
console.error('Failed to export ' + pageInfo, error);
throw error;
} else {
console.log('Exported ' + pageInfo + ' (' + body.length + ' bytes)');
numExportedPages++;
if (numExportedPages === PAGES.length) {
resolve(true);
}
}
});
});
});
}
function copyStaticFiles() {
console.log('Copy static files to ' + DIST_DIR + '...');
// 1. Copy everything
return Promise.all([
copy('browserconfig.xml'),
copy('favicon.ico'),
copy('LICENSE'),
copy('manifest.json'),
copy('robots.txt'),
copy('css'),
copy('img'),
copy('js')
])
// 2. Clean-up unwanted files
.then(() => {
return unlink('css/styles.less');
});
// Shortcut for an 1:1 copy to the DIST dir
function copy(filename) {
return fse.copy(filename, DIST_DIR + filename);
}
function unlink(filename) {
return new Promise((resolve, reject) => {
fs.unlink(DIST_DIR + filename, error => {
if (error) {
if (error.code === 'ENOENT') {
console.warn('File ' + DIST_DIR + filename + ' does not exist');
} else {
throw error;
}
}
resolve(true);
});
});
}
}
function transpileES6Files() {
console.log('Transpiling ES6 files to ES5 to ' + DIST_DIR + '...');
let srcFile = JS_SRC_DIR + 'main.js';
let destFile = DIST_DIR + 'js/main.js'; // Overrides the existing un-transpiled file
return babel.transformFileAsync(srcFile)
.then(result => {
return new Promise((resolve, reject) => {
fs.writeFile(destFile, result.code, (error, data) => {
resolve(true);
});
});
});
}
function ensureFolderExists(folder) {
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder)
}
}
// ---- MAIN
exportFromExpressServer()
.then(copyStaticFiles)
.then(transpileES6Files)
.then(() => {
console.log('DONE build successful!');
});