-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.js
236 lines (204 loc) · 7.78 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
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
/*
* MolPaintJS
* Copyright 2021 Leibniz-Institut f. Pflanzenbiochemie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* This build script requires the following non-standard packages:
* - yargs
* - pegjs
* - terser
*/
"use strict";
const yargs = require('yargs');
var fs = require('fs');
var pathInfo = require('path');
var tar = require('tar');
var util = require('util');
var peg = require("pegjs");
var { minify } = require("terser");
var pkgcfg = require("./package.json");
// console.log(util.inspect(pkgcfg, {showHidden: false, depth: null}));
const GITHUB_RELEASE_URL = 'https://github.com/ipb-halle/MolPaintJS/releases/latest/download';
const BUILD_DIR = 'molpaintjs';
const DIST_FILE = 'molpaintjs.tar.gz';
const LIBRARIES = [ {'src': 'node_modules/meta-png/dist/meta-png.umd.js', 'dest': 'js/meta-png.umd.js'}, ];
var entryPoint = "MolPaintJS.js";
function readResource(path, dirent, header, encoding) {
var code = '"' + dirent.name + '":"' + header;
if (encoding == 'base64') {
code += fs.readFileSync(pathInfo.join(path, dirent.name), {'encoding': encoding});
} else {
code += encodeURI(fs.readFileSync(pathInfo.join(path, dirent.name), {'encoding': encoding}));
}
code += '",\n';
return code;
}
function readPEG(path, dirent) {
return '/* MolPaintJS generated PEGJS parser module */\n'
+ 'var molPaintJS = (function (molpaintjs) {\n'
+ ' "use strict";\n'
+ ' molpaintjs.MDLParser = '
+ peg.generate(
fs.readFileSync(pathInfo.join(path, dirent.name), {'encoding':'UTF-8'}),
{'output':'source'})
+ ';\n'
+ ' return molpaintjs;\n'
+ '}(molPaintJS || {}));\n';
}
/*
* recursively read code files, compile them (PEGJS rules only),
* and concatenate them. Returns a single code string.
*/
function readCode(path) {
var code = '';
var entryCode = '';
var resources = '';
fs.readdirSync(path, {'withFileTypes': true})
.forEach(dirent => {
if (dirent.isDirectory()) {
var result = readCode(pathInfo.join(path, dirent.name));
resources += result[0];
code += result[1];
entryCode += result[2];
} else {
if (dirent.name == entryPoint) {
entryCode = fs.readFileSync(pathInfo.join(path, dirent.name), {'encoding':'UTF-8'});
} else {
if (dirent.name.match(/\.pegjs$/)) {
code += readPEG(path, dirent);
}
if (dirent.name.match(/\.js$/)) {
code += fs.readFileSync(pathInfo.join(path, dirent.name), {'encoding':'UTF-8'});
}
if (dirent.name.match(/\.png$/)) {
resources += readResource(path, dirent, 'data:;base64,', 'base64');
}
if (dirent.name.match(/\.css$/)) {
resources += readResource(path, dirent, 'data:text/css;base64,', 'base64');
}
if (dirent.name.match(/\.mol$/)) {
resources += readResource(path, dirent, '', 'base64');
}
if (dirent.name.match(/\.html$/)) {
resources += readResource(path, dirent, 'data:text/html;charset=utf-8,', 'UTF-8');
}
}
}
});
return [resources, code, entryCode];
}
async function minifyCode(code) {
var code = await minify(code, {compress:true, mangle:true} );
return code;
}
async function compile(src, dest, compress) {
var result = readCode(src);
var code = '/* MolPaintJS generated resources module */\n'
+ 'var molPaintJS = (function (molpaintjs) {\n'
+ ' "use strict";\n'
+ ' molpaintjs.Resources = {\n'
+ result[0]
+ '"version":"' + pkgcfg.version + '"\n'
+ ' };\n'
+ ' return molpaintjs;\n'
+ '}(molPaintJS || {}));\n'
+ result[1]
+ result[2];
if (compress == true) {
code = (await minifyCode(code)).code;
}
// console.log(util.inspect(code, {showHidden: false, depth: null}));
fs.writeFile(dest, code, err => {
if (err) throw err;
});
}
function copyLibraries(target) {
for (let asset of LIBRARIES) {
fs.copyFileSync(pathInfo.join(__dirname, asset.src),
pathInfo.join(__dirname, target, asset.dest));
}
}
function copyTemplate(src, dest, replacements) {
fs.mkdirSync(dest, {'recursive':true, });
fs.readdirSync(src, {'withFileTypes': true})
.forEach(dirent => {
if (dirent.isDirectory()) {
var newDest = pathInfo.join(dest, dirent.name);
fs.mkdirSync(newDest, {'recursive':true, });
copyTemplate(pathInfo.join(src, dirent.name), newDest, replacements);
} else {
if (replacements[dirent.name] != null) {
var content = fs.readFileSync(pathInfo.join(src, dirent.name), {'encoding':'UTF-8'});
replacements[dirent.name].forEach(entry => {
content = content.replace(entry.key, entry.replacement);
});
fs.writeFileSync(pathInfo.join(dest, dirent.name), content, err => {
if (err) throw err;
});
} else {
fs.copyFileSync(pathInfo.join(src, dirent.name),
pathInfo.join(dest, dirent.name));
}
}
});
}
function copyToplevelFiles(dest) {
for(var name of ['NOTICE', 'LICENSE', 'README.md']) {
fs.copyFileSync(
pathInfo.join(__dirname, name),
pathInfo.join(dest, name));
}
}
function build(release, compress) {
var replacements = {
'index.html': [ { 'key':'%MOLPAINTJS%', 'replacement':'js' }, ],
'large.html': [ { 'key':'%MOLPAINTJS%', 'replacement':'js' }, ],
};
fs.mkdirSync(pathInfo.join(__dirname, BUILD_DIR, 'js'), {'recursive':true, });
copyTemplate(pathInfo.join(__dirname , 'template'),
pathInfo.join(__dirname, BUILD_DIR),
replacements);
copyToplevelFiles(pathInfo.join(__dirname , BUILD_DIR));
copyLibraries(BUILD_DIR);
if (release) {
replacements['index.html'] = [ {'key':'%MOLPAINTJS%', 'replacement':GITHUB_RELEASE_URL }, ];
replacements['large.html'] = [ {'key':'%MOLPAINTJS%', 'replacement':GITHUB_RELEASE_URL }, ];
copyTemplate(pathInfo.join(__dirname , 'template'),
pathInfo.join(__dirname, 'docs'),
replacements);
copyToplevelFiles(pathInfo.join(__dirname , 'docs'));
copyLibraries('docs');
}
// always compress on release
compile(pathInfo.join(__dirname , 'src'),
pathInfo.join(__dirname, BUILD_DIR, 'js', 'molpaint.js'),
release | compress);
tar.c({
file: pathInfo.join(__dirname, DIST_FILE),
gzip:true, },
[ BUILD_DIR, ]);
}
const argv = yargs
.option('nocompress', {
alias: 'n',
description: 'Do not minify / compress the output',
type: 'boolean', })
.option('release', {
alias: 'r',
description: 'Build for release on GitHub / GitHub Pages',
type: 'boolean', })
.help().alias('help', 'h')
.argv;
build(argv.release, ! argv.nocompress);