-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
60 lines (51 loc) · 1.12 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
var fs = require('fs-extra')
var path = require('path')
var async = require('async')
module.exports = function(dest, data, cb){
var src = f('./template')
try {
fs.mkdirpSync(dest)
fs.copySync(src, dest)
} catch(err) {
if (err) return cb(err)
}
async.waterfall([
renameFiles,
fillFiles
], cb)
function fillFiles(cb){
async.each([
'package.json',
'README.md'
], function(file, cb){
file = path.resolve(dest, file)
replace(file, data, cb)
}, cb)
}
function renameFiles(cb){
async.each([
'0.gitignore',
'0.npmignore'
], function(file, cb){
file = path.resolve(dest, file)
file_ = path.join(
path.dirname(file),
path.basename(file).slice(1)
)
fs.rename(file, file_, cb)
}, cb)
}
}
function replace(file, data, cb){
fs.readFile(file, function(err, buf){
if (err) return cb(err)
var txt = buf.toString()
txt = txt.replace(/\{\{([^\}]+)\}\}/g, function($0, $1){
return data[$1]
})
fs.writeFile(file, txt, cb)
})
}
function f(file){
return path.resolve(__dirname, file)
}