This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathbuild.js
69 lines (55 loc) · 1.74 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
var browserify = require('browserify'),
uglify = require('uglify-js'),
fs = require('fs'),
path = require('path');
// yer a wizard Caprica
var wizard = browserify({
basedir: 'source',
paths: ['.', './source', './static']
});
wizard.add('util.js');
wizard.add('bot.js');
var plugins = getJSFiles('source/plugins'),
pluginLoaderPath = '_plugin-loader.js';
fs.writeFileSync('source/' + pluginLoaderPath, generatePluginLoader(plugins));
wizard.add(pluginLoaderPath);
console.log('Bundling...');
wizard.bundle((err, buff) => {
if (err) {
console.error('Oh noes!');
console.error(err);
return;
}
console.log('writing master.js...');
fs.writeFile('master.js', buff, function () {
console.log('wrote master.js');
});
console.log('minifying...');
var minified = uglify.minify(buff.toString('utf8'), { fromString: true });
console.log('Minified. Writing master.min.js...');
fs.writeFile('master.min.js', minified.code, function() {
console.log('wrote master.min.js');
});
fs.unlink('source/' + pluginLoaderPath, function(err) {
if( err ) {
console.error('Oh noes!')
console.error(err);
}
});
});
function getJSFiles(dirPath) {
var fullPath = path.resolve(dirPath);
var paths = fs.readdirSync(fullPath)
.filter(file => file.endsWith('.js') || file.endsWith('.json'))
.map(file => path.resolve(fullPath, file));
paths.forEach(p => console.log('\t', p));
return paths;
}
function generatePluginLoader(plugins) {
var loaders = plugins.map(
plugin => `require("plugins/${path.basename(plugin)}")(bot);`
).join('\n');
return `module.exports = function (bot) {
${loaders}
};`;
}