-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.js
More file actions
114 lines (100 loc) · 3.04 KB
/
Copy pathbuild.js
File metadata and controls
114 lines (100 loc) · 3.04 KB
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
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, '*'));
}
function run(command, args, cwd) {
return new Promise((resolve, reject) => {
let executable = command;
let finalArgs = args;
const isWin = /^win/.test(process.platform);
if (isWin) {
// Sometimes cmd.exe is not available in the path
// See: http://goo.gl/ADmzoD
finalArgs = ['/c', executable].concat(args);
executable = process.env.comspec || 'cmd.exe';
}
var child = spawn(executable, finalArgs, { cwd, stdio: 'inherit' });
child.on('error', reject);
child.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`${command} ${args.join(' ')} exited with code ${code}`));
}
});
});
}
function tarDirectory(srcDir, bundlePath) {
return tar.c({
file: bundlePath,
cwd: srcDir,
portable: true,
gzip: { level: 9 }
}, ['.']);
}
function buildWithTar(appPath, tmpPath, api, bundlePath) {
const ig = ignore();
const ignorePath = api.resolvePath(appPath, '.mupignore');
if (fs.existsSync(ignorePath)) {
ig.add(fs.readFileSync(ignorePath).toString())
}
return new Promise((resolve, reject) => {
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();
}
})
});
}
async function buildWithPnpmDeploy(appPath, tmpPath, api, bundlePath) {
const appDir = path.resolve(api.getBasePath(), appPath);
const srcDir = path.join(tmpPath, 'bundle-src');
await run('pnpm', ['deploy', '--filter', './', srcDir], appDir);
await tarDirectory(srcDir, bundlePath);
}
module.exports = function (appPath, tmpPath, api, bundleMethod) {
cleanTmp(tmpPath);
var bundlePath = api.resolvePath(tmpPath, 'bundle.tar.gz');
if (bundleMethod === 'pnpm-deploy') {
return buildWithPnpmDeploy(appPath, tmpPath, api, bundlePath);
}
return buildWithTar(appPath, tmpPath, api, bundlePath);
}