-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.js
89 lines (76 loc) · 2.08 KB
/
compile.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
78
79
80
81
82
83
84
85
86
87
88
89
const { Compiler } = require('./compiler')
const assert = require('nanoassert')
const Batch = require('batch')
const path = require('path')
const glob = require('glob')
/**
* Compiles the glob result of `target`
* @param {String|Array} target
* @param {?(Object)} opts
* @param {?(String)} opts.cwd
* @param {?(String)} opts.output
* @param {?(Object)} opts.storage
* @param {?(Boolean)} opts.autoOpen
* @param {Function} callback
* @return {Compiler}
*/
function compile(target, opts, callback) {
if ('function' === typeof opts) {
callback = opts
}
if (!opts || 'object' !== typeof opts) {
opts = {}
}
opts = Object.assign({ cwd: process.cwd() }, opts) // copy
assert('function' === typeof callback, 'Callback must be a function.')
const compiler = new Compiler(opts)
compiler.ready(onready)
return compiler
function onready(err) {
// istanbul ignore next
if (err) { return callback(err) }
if (Array.isArray(target)) {
onfiles(null, target)
} else {
try {
glob(target, opts, onfiles)
} catch (err) {
return callback(err)
}
}
}
function onfiles(err, files) {
// istanbul ignore next
if (err) { return callback(err) }
if (!files || 0 === files.length) {
return callback(new Error('Target does not exist.'))
}
const batch = new Batch()
for (const file of files) {
try {
const pathspec = require.resolve(path.resolve(opts.cwd, file))
const copts = Object.assign({}, opts)
if (copts.output && files.length > 1) {
copts.output = path.join(copts.output, path.basename(pathspec))
} else if (opts.storage && !copts.output) {
copts.output = pathspec
}
batch.push((next) => compiler.target(pathspec, copts).open(next))
} catch (err) {
// istanbul ignore next
return callback(err)
}
}
batch.end((err) => {
// istanbul ignore next
if (err) { return callback(err) }
compiler.compile(opts, callback)
})
}
}
/**
* Module exports.
*/
module.exports = {
compile
}