-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.js
80 lines (67 loc) · 2.12 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
/**
* Build the browser version of nedb
*/
var fs = require('fs')
, path = require('path')
, child_process = require('child_process')
, toCopy = ['lib', 'node_modules']
, async, browserify, uglify
;
// Ensuring both node_modules (the source one and build one), src and out directories exist
function ensureDirExists (name) {
try {
fs.mkdirSync(path.join(__dirname, name));
} catch (e) {
if (e.code !== 'EEXIST') {
console.log("Error ensuring that node_modules exists");
process.exit(1);
}
}
}
ensureDirExists('../node_modules');
ensureDirExists('node_modules');
ensureDirExists('out');
ensureDirExists('src');
// Installing build dependencies and require them
console.log("Installing build dependencies");
child_process.exec('npm install', { cwd: __dirname }, function (err, stdout, stderr) {
if (err) { console.log("Error reinstalling dependencies"); process.exit(1); }
fs = require('fs-extra');
async = require('async');
browserify = require('browserify');
uglify = require('uglify-js');
async.waterfall([
function (cb) {
console.log("Installing source dependencies if needed");
child_process.exec('npm install', { cwd: path.join(__dirname) }, function (err) { return cb(err); });
}
, function (cb) {
console.log("Browserifying the code");
var b = browserify()
, srcPath = path.join(__dirname, 'src/datastore.js');
b.add(srcPath);
b.bundle({ standalone: 'Nedb' }, function (err, out) {
if (err) { return cb(err); }
fs.writeFile(path.join(__dirname, 'out/nedb.js'), out, 'utf8', function (err) {
if (err) {
return cb(err);
} else {
return cb(null, out);
}
});
});
}
// , function (out, cb) {
// console.log("Creating the minified version");
// var compressedCode = uglify.minify(out, { fromString: true });
// fs.writeFile(path.join(__dirname, 'out/nedb.min.js'), compressedCode.code, 'utf8', cb);
// }
], function (err) {
if (err) {
console.log("Error during build");
console.log(err);
} else {
console.log("Build finished with success");
}
});
});