-
Notifications
You must be signed in to change notification settings - Fork 12
/
write.js
104 lines (85 loc) · 3 KB
/
write.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import path from 'path'
import Promise from 'bluebird'
import fs from 'fs'
import { log } from 'sigh-core'
import fse from 'fs-extra'
const glob = Promise.promisify(require('glob'))
const writeFile = Promise.promisify(fs.writeFile)
const unlink = Promise.promisify(fs.unlink)
const rm = Promise.promisify(fse.remove) // TODO: not used yet, see later comment
const ensureDir = Promise.promisify(fse.ensureDir)
import { mapEvents } from 'sigh-core/lib/stream'
export function writeEvent(basePath, event) {
const { fileType } = event
const projectFile = path.basename(event.path)
const { projectPath } = event
// the projectPath remains the same but the basePath is changed to point to
// the output directory
event.basePath = basePath
const outputPath = event.path
if (event.type === 'remove') {
return unlink(outputPath).then(() => {
return event.supportsSourceMap ? unlink(outputPath + '.map').then(() => event) : event
})
}
let { data } = event
const outputDir = path.dirname(outputPath)
let promise = ensureDir(path.dirname(outputPath)).then(() => {
return writeFile(outputPath, data, {encoding: event.encoding})
})
if (event.supportsSourceMap) {
let sourceMap
try {
sourceMap = event.sourceMap
}
catch (e) {
log.warn('\x07could not construct identity source map for %s', projectPath)
if (e.message)
log.warn(e.message)
}
if (sourceMap) {
const mapPath = projectFile + '.map'
let suffix
if (fileType === 'js')
suffix = '//# sourceMappingURL=' + mapPath
else if (fileType === 'css')
suffix = '/*# sourceMappingURL=' + mapPath + ' */'
if (suffix)
data += '\n' + suffix
promise = promise.then(() => {
sourceMap.sources = sourceMap.sources.map(source => path.relative(outputDir, source))
return writeFile(path.join(outputDir, mapPath), JSON.stringify(sourceMap))
})
}
}
return promise.then(() => event)
}
// basePath = base directory in which to write output files
export default function(op, options, basePath) {
if (! basePath) {
basePath = options
options = {}
}
let clobberPromise
let { clobber } = options
if (clobber) {
// sanitize a path we are about to recursively remove... it must be below
// the current working directory (which contains sigh.js)
if (! basePath || basePath[0] === '/' || basePath.substr(0, 3) === '../')
throw Error(`refusing to clobber '${basePath}' outside of project`)
if (clobber === true) {
clobberPromise = rm(basePath)
}
else {
if (! (clobber instanceof Array))
clobber = [ clobber ]
clobberPromise = Promise.map(clobber, pattern => {
return glob(pattern, { cwd: basePath }).then(
matches => Promise.map(matches, match => rm(path.join(basePath, match)))
)
})
}
}
const streamPromise = mapEvents(op.stream, writeEvent.bind(this, basePath))
return clobberPromise ? clobberPromise.thenReturn(streamPromise) : streamPromise
}