-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
executable file
·167 lines (142 loc) · 3.95 KB
/
build.js
File metadata and controls
executable file
·167 lines (142 loc) · 3.95 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env node
const ID = 0,TYPE = 1,VALUE = 2,EXTRA = 3
const args = require('pico-args')
const defaults = {
bundle:['', 'bundle name, comma separated if more than one, optional colon sperates input and output filename'],
wd:['.','working directory'],
out: ['', 'output directory'],
main:['main','path to main directory'],
cfg:['cfg','path to configuration directory'],
env:['dev','environment file to be bundled'],
bin:['bin','path to binary/output directory'],
lean:[true,'embed lean library'],
pico:[true,'embed pico library'],
global:['', 'global variables, common separated if more than one'],
help:[false,'show this help']
}
const opt = args.parse(defaults)
if (!opt || opt.help || !opt.bundle) return args.usage(defaults)
args.print('build options', opt)
const path = require('path')
const fs = require('fs')
const cwd = path.resolve(process.cwd(), opt.wd)
function setAdd(set, ele){
if (!ele) return
if (Array.isArray(ele)) ele.forEach(set.add)
else set.add(ele)
}
/**
* add file path to be included in bundle
*
* @param {array} spec - module spec
* @param {array} include - file paths to be included
* @returns {boolean} - true to drill down
*/
function getPath(spec, include){
switch(spec[TYPE]){
case 'file':
case 'type':
include.add(spec[VALUE])
return false
case 'view':
setAdd(include, spec[EXTRA] || spec[ID])
return true
case 'list':
case 'map':
case 'model':
return true
default:
setAdd(include, spec[EXTRA])
return false
}
}
function scanObj(obj, include, cb){
if (!obj) return cb(null, include)
const keys = Object.keys(obj)
if (!keys.length) return cb(null, include)
scan(keys.map(k => obj[k] ), include, cb)
}
function scan(arr, include, cb){
if (!Array.isArray(arr) || !arr.length) return cb(null, include)
const spec = arr.shift()
if (!Array.isArray(spec) || !spec.length) return scan(arr, include, cb)
if (getPath(spec, include)){
if ('map' === spec[TYPE]){
return scanObj(spec[VALUE], include, (err, include) => {
if (err) return cb(err)
scan(arr, include, cb)
})
}
return scan(spec[VALUE], include, (err, include) => {
if (err) return cb(err)
scan(arr, include, cb)
})
}
scan(arr, include, cb)
}
function mkdirPSync(arr, access) {
arr.reduce((parentDir, childDir) => {
const curDir = path.resolve(parentDir, childDir)
try {
fs.mkdirSync(curDir, access)
} catch (exp) {
if ('EEXIST' !== exp.code) throw exp
}
return curDir
}, arr.shift())
}
function deps(){
const deps = []
opt.lean && deps.push(path.join('lib','lean','lean.min.js'))
opt.pico && deps.push(path.join('lib','common','pico.min.js'))
return deps
}
function addBundle(output, entry, deps, exclude){
const json = fs.readFileSync(path.resolve(cwd, opt.cfg, entry[0] + '.env.json'))
const spec = JSON.parse(json)
scan(spec, new Set, (err, include) => {
const json = fs.readFileSync(path.resolve(cwd, opt.cfg, entry[0] + '.json'))
const spec = JSON.parse(json)
scan(spec, include, (err, include) => {
output.push({
entry: entry[1],
deps,
include: [...include],
exclude
})
})
})
}
const bundleNames = opt.bundle.split(',')
const [mainCfg, mainEntry = mainCfg] = bundleNames.shift().split(':')
let out = [mainCfg, opt.main]
if (opt.out){
out = opt.out.split(path.sep)
}
mkdirPSync([cwd, opt.bin, ...out], 0o755)
const output = [
cwd,
opt.main,
path.join(opt.bin, ...out),
]
addBundle(output, [mainCfg, mainEntry], deps())
bundleNames.forEach(bundleName => {
const [cfgName, entryName = cfgName] = bundleName.split(':')
addBundle(output, [cfgName, entryName], void 0, [entryName])
})
const handler = {get(target,name){
switch(name){
case 'load':
case 'ajax': return null
case 'getElementById': return () => ({dataset:{ build: opt.env}})
default: return () => {}
}
}}
// global
pico = require('pico-common/bin/pico-cli')
__ = window = document = new Proxy({}, handler)
opt.global.split(',').forEach(g => {
global[g] = __
})
const pBuild = pico.export('pico/build')
pBuild(output)