forked from zodern/mup-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
66 lines (58 loc) · 1.69 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
var spawn = require('child_process').spawn;
var path = require('path');
var tar = require('tar');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var containsPath = require('contains-path');
var ignore = require('ignore');
var fs = require('fs');
function cleanTmp(tmpPath) {
try {
mkdirp.sync(tmpPath);
} catch (e) {
console.log(e);
}
rimraf.sync(path.join(tmpPath, '*'));
}
module.exports = function (appPath, tmpPath, api) {
cleanTmp(tmpPath);
const ig = ignore();
const ignorePath = api.resolvePath(appPath, '.mupignore');
if (fs.existsSync(ignorePath)) {
ig.add(fs.readFileSync(ignorePath).toString())
}
return new Promise((resolve, reject) => {
var bundlePath = api.resolvePath(tmpPath, 'bundle.tar.gz');
tar.c({
file: bundlePath,
onwarn(message, data) { console.log(message, data)},
cwd: path.resolve(api.getBasePath(), appPath),
portable: true,
gzip: {
level: 9
},
filter(itemPath, stat) {
if (containsPath(itemPath, './.git')) {
return false;
} else if (containsPath(itemPath, './node_modules')) {
return false;
}
// Since itemPath usually starts with "./", we resolve it first so it isn't relative to the cwd
const relativePath = path.relative(appPath, path.resolve(appPath, itemPath));
if (relativePath.length === 0) {
return true;
}
const result = !ig.ignores(relativePath);
return result;
}
}, ['.'], (err) => {
if (err) {
console.log('err bundling');
console.log(err);
reject(err);
} else {
resolve();
}
})
});
}