-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (44 loc) · 1.19 KB
/
index.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
var ModuleMap = require('module-map')(__dirname)
var path = require('path')
var fs = require('fs')
var sh = require('shelljs')
var TmpDir = require('quick-tmp')('compile-module-map')
var spawn = require('child_process').spawn
module.exports = function(dir) {
// copy dir
// kill
// rerun with doreplace env var set
// if doreplace replace original file with compiled
var env = process.env
var BUILD_DIR = dir + '/build'
var TMP_BUILD_DIR = TmpDir()
var DIR = dir
var MAP_REPLACE = env.MAP_REPLACE
MAP_REPLACE ? replace() : prepare()
function prepare() {
sh.rm('-rf', BUILD_DIR)
sh.rm('-rf', TMP_BUILD_DIR)
sh.cp('-R', DIR + '/*', TMP_BUILD_DIR)
sh.mv(TMP_BUILD_DIR, BUILD_DIR)
spawn(process.execPath, ['./compile.js'], {
cwd: BUILD_DIR,
stdio: 'inherit',
env: {
MAP_REPLACE: 1
}
}).on('close', function(errCode) {
process.exit(errCode)
})
}
function replace() {
ModuleMap(function(src) {
return src.replace('REPLACEME', 'REPLACED')
})
ModuleMap.after(function(src, filename) {
console.log(arguments)
fs.writeFileSync(filename, src)
return src
})
require('./index.js')
}
}