forked from yoshuawuyts/copy-template-dir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (69 loc) · 2.22 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
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
const maxstacheStream = require('maxstache-stream')
const maxstache = require('maxstache')
const parallel = require('run-parallel')
const eos = require('end-of-stream')
const readdirp = require('readdirp')
const assert = require('assert')
const mkdirp = require('mkdirp')
const noop = require('noop2')
const path = require('path')
const pump = require('pump')
const fs = require('graceful-fs')
module.exports = copyTemplateDir
// High throughput template dir writes
// (str, str, obj, fn) -> null
function copyTemplateDir (srcDir, outDir, vars, cb) {
if (!cb) {
if (!vars) vars = noop
cb = vars
vars = {}
}
assert.strictEqual(typeof srcDir, 'string')
assert.strictEqual(typeof outDir, 'string')
assert.strictEqual(typeof vars, 'object')
assert.strictEqual(typeof cb, 'function')
// create directory
mkdirp(outDir, function (err) {
if (err) return cb(err)
const rs = readdirp(srcDir, {})
const streams = []
const createdFiles = []
// create a new stream for every file emitted
rs.on('data', function (file) {
createdFiles.push(path.join(outDir, maxstache(removeUnderscore(file.path), vars)))
streams.push(writeFile(outDir, vars, file))
})
// delegate errors & close streams
eos(rs, function (err) {
if (err) return cb(err)
parallel(streams, function (err) {
if (err) return cb(err)
cb(null, createdFiles)
})
})
})
}
// write a file to a directory
// str -> stream
function writeFile (outDir, vars, file) {
return function (done) {
const fileName = file.path
const inFile = file.fullPath
const parentDir = path.basename(file.dirent.path)
const outFile = path.join(outDir, maxstache(removeUnderscore(fileName), vars))
mkdirp(path.join(outDir, maxstache(parentDir, vars)), function (err) {
if (err) return done(err)
const rs = fs.createReadStream(inFile)
const ts = maxstacheStream(vars)
const ws = fs.createWriteStream(outFile)
pump(rs, ts, ws, done)
})
}
}
// remove a leading underscore
// str -> str
function removeUnderscore (filepath) {
const parts = filepath.split(path.sep)
const filename = parts.pop().replace(/^_/, '')
return parts.concat([filename]).join(path.sep)
}