-
Notifications
You must be signed in to change notification settings - Fork 36
/
build.js
373 lines (339 loc) · 10.9 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
var srcDir = './src';
var outFile = 'dist/ZenzaWatch.user.js';
var watchDirs = [
srcDir,
'./packages/components/src',
'./packages/navi/src',
'./packages/lib/src',
'./packages/zenza/src',
];
var templates = [
{ src: '_template.js', dist: 'dist/ZenzaWatch.user.js', dev: true },
{ src: '_uquery.js', dist: 'dist/uQuery.user.js', dev: false },
{ src: '_pocket.js', dist: 'dist/MylistPocket.user.js', dev: false },
{ src: '_shape.js', dist: 'dist/MaskedWatch.user.js', dev: false },
{ src: '_setting.js', dist: 'dist/ZenzaAdvancedSettings.user.js', dev: false },
{ src: '_hls.js', dist: 'dist/ZenzaHLS.user.js', dev: false },
{ src: '_gamepad.js', dist: 'dist/ZenzaGamePad.user.js', dev: false },
{ src: '_blog.js', dist: 'dist/ZenzaBlogPartsButton.user.js', dev: false },
{ src: '_captube.js', dist: 'dist/CapTube.user.js', dev: false },
{ src: '_my4.js', dist: 'dist/MylistFilter.user.js', dev: false },
{ src: '_heatsync.js', dist: 'dist/HeatSync.user.js', dev: false },
// { src: '_navi.js', dist: 'dist/Navi.user.js', dev: false },
// { src: '_yomi.js', dist: 'dist/Yomi.user.js', dev: false },
// { src: '_vc.js', dist: 'dist/VoiceControl.user.js', dev: false }
];
const DEV_HEADER = {
'_template.js': {
name: '// @name ZenzaWatch DEV版',
description: '// @description ZenzaWatchの開発 先行バージョン'
},
'_uquery.js': {},
'_pocket.js': {},
'_yomi.js': {},
'_vc.js': {},
'_shape.js': {},
'_setting.js': {},
'_hls.js': {},
'_gamepad.js': {},
'_blog.js': {},
'_captube.js': {},
'_my4.js': {},
'_heatsync': {},
};
let REQMAP = {};
const throttle = (func, interval) => {
let lastTime = 0;
let lastArgs = null;
let timer;
const result = (...args) => {
const now = Date.now();
const timeDiff = now - lastTime;
if (timeDiff < interval) {
lastArgs = args;
if (!timer) {
timer = setTimeout(() => {
lastTime = Date.now();
timer = null;
func.apply(null, lastArgs);
lastArgs = null;
}, Math.max(interval - timeDiff, 0));
}
return;
}
if (timer) {
timer = clearTimeout(timer);
}
lastTime = now;
lastArgs = null;
func(...args);
};
result.cancel = () => {
if (timer) {
timer = clearTimeout(timer);
}
};
return result;
};
const debounce = (func, interval) => {
let timer;
const result = (...args) => {
if (timer) {
timer = clearTimeout(timer);
}
timer = setTimeout(() => func(...args), interval);
};
result.cancel = () => {
if (timer) { timer = clearTimeout(timer); }
};
return result;
};
async function writeIfModified(file, newData, callback) {
var fs = require('fs');
return new Promise(res => setTimeout(res, Math.random() * 1000)).then(() => {
var oldData = fs.readFileSync(file, 'utf-8');
if (oldData === newData) {
// callback('Not Modified', null);
return;
}
fs.writeFileSync(file, newData);
callback('OK', newData);
}).catch(e => {
console.log('Exception: ', e);
fs.writeFileSync(file, newData);
callback('OK', newData);
});
}
async function notify(title, message, options = {timeout: 3, subtitle: undefined}) {
const notifier = require('node-notifier');
let {timeout, subtitle} = options;
notifier.notify({title, message, timeout, subtitle});
}
function requireFile(srcDir, file, params, parent = '') {
var fs = require('fs');
var path = require('path');
var lines = [];
var begin = false;
var isComment = false;
var trim = false; //!params.dev && !/NicoTextParser\.js/.test(file);
var srcFile = path.join(srcDir, file);
var ignore = false;
const imports = {};
const fullpath = path.resolve(srcFile);
if (REQMAP[fullpath]) {
REQMAP[fullpath].push(parent);
console.warn('***WARN***\n"%s" has already required\n in\n %s', fullpath, REQMAP[fullpath].join('\n '));
// notify('already required', srcFile, parent);
return `// already required`;
}
REQMAP[fullpath] = REQMAP[fullpath] || [];
REQMAP[fullpath].push(parent || 1);
try {
fs.statSync(srcFile);
} catch (e) {
console.error('*** Error: %s\n\t "%s"', e.message || e, srcFile);
console.log(` required by "${parent}"`);
notify('build error', `${e.message}\n${file}`);
return `fild not exist "${srcFile}"\nfrom "${parent}"`;
}
fs.readFileSync(srcFile, 'utf-8').split('\n').some(function(line) {
let lt = line.trim();
if (!begin && line.trim().match(/^import\s+\{?(.+)\}?\s+from\s+['"](.+)['"]/)) {
const [$1, $2] = [RegExp.$1, RegExp.$2];
const modules = $1.split(/[\s,]+/).map(m => m.replace(/[{}*]/g, '').trim()).filter(m => m);
const modulePath = $2.replace(/\.js$/, '') + '.js';
modules.forEach(m => imports[m] = modulePath);
}
if (lt.startsWith('//@ignore-disable')) {
ignore = false;
return;
} else if (ignore) {
return;
} else if (lt.startsWith('//@ignore-enable')) {
ignore = true;
return;
}
if (lt.match(/\/\/=+BEGIN=+/)) {
begin = true;
return;
}
lt = lt.replace(/\/\*\*(.*)\*\//g, '');
if (!isComment && lt.match(/\/\*\*/)) {
isComment = true;
// console.log(file, '>', lt);
lt = lt.replace(/\/\*\*.*/, '');
} else if (isComment && lt.match(/\*\//)) {
isComment = false;
// console.log(file, '>', lt);
lt = lt.replace(/^.*\*\//, '');
} else if (isComment) {
// console.log(file, '>', lt);
return;
}
if (lt.startsWith('//') && !lt.match(/\/\/\s*[@=]/)) {
return;
}
if (begin) {
if (lt.match(/\/\/=+END=+/)) {
return true;
}
if ((params.dev && line.match(/^\s*\/\/@dev-require (.+)$/)) ||
line.match(/^\s*\/\/@require (.+)$/)) {
const m = (RegExp.$1 || '').trim()
var f = path.join(path.dirname(srcFile), imports[m] || m);
// imports[m] ? console.log('import ' + f) : console.log('require ' + f);
lines.push(requireFile(path.dirname(f), path.basename(f), params, path.resolve(srcFile)));
return;
}
// if (!trim) {
// lines.push(line);
// return;
// }
if (lt.length === 0) { return; }
if (trim) {
lines.push(line.replace(/^[\s]+/g, '').replace(/([ ]+)/g, ' ')
);
} else {
lines.push(line.replace(/^([\s+]+)/, g => '\t'.repeat(g.length / 2)));
}
}
});
return lines.join('\n');
}
function deploy(srcFile) {
var path = require('path');
var fs = require('fs');
if (!fs.existsSync('setting.json')) {
console.log('setting.json not exist');
return;
}
var setting = JSON.parse(fs.readFileSync('setting.json', 'utf-8'));
setting.deployTo.some(function(dir) {
// console.log('deploy To: ', dir);
if (!fs.existsSync(dir)) {
// console.log('path not exist: ', dir);
return;
}
var destFile = dir.replace(/\/+$/, '') + '/' + path.basename(srcFile);
// console.log('deploy file: ', destFile);
// fs.createReadStream(srcFile).pipe(fs.createWriteStream(destFile));
writeIfModified(destFile, fs.readFileSync(srcFile, 'utf-8'), function(err, newData) {
// console.log(err);
})
});
}
function loadTemplateFile(srcDir, indexFile, outFile, params) {
var fs = require('fs');
var path = require('path');
var lines = [];
var ver = null;
const imports = {};
const srcFile = path.join(srcDir, indexFile);
fs.readFileSync(srcFile, 'utf-8').split('\n').some(function(line) {
if (line.trim().match(/^import\s+\{?(.+)\}?\s+from\s+['"](.+)['"]/)) {
const [$1, $2] = [RegExp.$1, RegExp.$2];
const modules = $1.split(/[\s,]+/).map(m => m.replace(/[{}*]/g, '').trim()).filter(m => m);
const modulePath = $2.replace(/\.js$/, '') + '.js';
modules.forEach(m => imports[m] = modulePath);
return;
}
if (params.dev) {
if (line.match(/^\/\/\s*@([a-z0-9+]+)(.*)$/)) {
let name = RegExp.$1;
// console.log('header:', indexFile, name, RegExp.$2);
if (DEV_HEADER[indexFile][name]) {
line = DEV_HEADER[indexFile][name].trim();
}
}
}
if (line.match(/\/\/ *==\/UserScript==/)) {
line +='\n/* eslint-disable */';
}
if (line.match(/^(\s*)\/\/\s*@environment$/)) {
lines.push(`${RegExp.$1}const ENV = '${params.dev ? 'DEV' : 'STABLE'}';\n`);
return;
}
if (line.match(/^(\s*)\/\/\s*@version(.*)$/)) {
if (!ver) {
ver = RegExp.$2.trim();
console.log('ver: ' + ver);
lines.push(line);
} else {
lines.push(RegExp.$1 + 'var VER = \'' + ver + '\';');
}
return;
} else
if ((params.dev && line.match(/^\s*\/\/@dev-require (.+)$/)) ||
line.match(/^\s*\/\/@require (.+)$/)) {
const m = (RegExp.$1 || '').trim()
var f = path.join(path.dirname(srcFile), imports[m] || m);
// imports[m] ? console.log('import ' + f) : console.log('require ' + f);
lines.push(requireFile(path.dirname(f), path.basename(f), params, path.resolve(srcFile)));
return;
}
lines.push(line);
});
writeIfModified(outFile, lines.join('\n'), function(err, newData) {
err && console.log(err);
if (newData) {
console.log(`\n>>>>>>update "${outFile}" (${lines.join('\n').split('\n').length} lines)`);
deploy(outFile);
}
});
}
function build(params) {
var path = require('path');
templates.forEach(template => {
var _params = {...params};
var templateFile = template.src;
var outFile = template.dist;
_params.dev = template.dev && params.dev;
if (_params.dev && !/-dev\.user\.js$/.test(outFile)) {
outFile = outFile.replace(/\.user\.js$/, '-dev.user.js');
}
console.log('\n>>>>>>build: %s', path.basename(outFile));
REQMAP = {};
loadTemplateFile(srcDir, templateFile, outFile, _params);
});
}
const _build = debounce(build, 1000);
function watch(srcDir, params) {
var fs = require('fs');
const onChange = async function(event, filename) {
if (event === 'rename') { // Dropbox経由の更新など
_build.cancel();
await new Promise(res => setTimeout(res, Math.random() * 3000));
}
console.log('event: "%s", file: "%s"', event, filename);
try {
_build(params);
} catch(e) {
console.error('error: ', e);
notify('build error', `${e.message}`);
}
};
fs.watch(srcDir, {recursive: true}, onChange);
}
function run() {
let params = {};
process.argv.concat().splice(-2).forEach(v => {
if (/^--(.+)$/.test(v)) {
params[RegExp.$1] = 1;
} else if (/^([a-z0-9]+)=(.*)$/.test(v)) {
params[RegExp.$1] = RegExp.$2;
}
});
console.log(params);
build(params);
if (params.watch) {
notify('watch start', new Date().toLocaleString());
watchDirs.forEach(dir => { watch(dir, params); });
}
}
try {
run();
} catch(e) {
console.error('error', e);
console.trace();
notify('error', `${e.message || e}`);
}