-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
162 lines (131 loc) · 3.39 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
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
const assert = require('assert')
const fs = require('fs-extra')
const path = require('path')
const {
glob
} = require('glob-gitignore')
const { EventEmitter } = require('events')
const REGEX_IS_GLOB_FILE = /[^\/]$/
module.exports = class Scaffold extends EventEmitter {
constructor ({
render,
override = true,
backup = true,
data,
ignore
} = {}) {
super()
assert(render && typeof render === 'function',
'options.render must be a function.')
assert(Object(data) === data, 'options.data must be an object.')
this._render = render
this._override = override
this._backup = backup
this._data = data
this._ignore = ignore
}
copy (from, to) {
if (Object(from) === from) {
return this._copyFiles(from)
}
return this._copy(from, to)
}
async _copy (from, to) {
const stat = await fs.stat(from)
if (stat.isDirectory()) {
return this._copyDir(from, to)
}
return fs.stat(to)
.then(
stat => {
if (stat.isDirectory()) {
// Only substitute path when `to` is not explicitly specified.
const name = this._to(path.basename(from))
to = path.join(to, name)
}
return this._copyFile(from, to)
},
() => this._copyFile(from, to)
)
}
async _copyDir (from, to) {
const files = await this._globDir(from)
const map = {}
files.forEach(file => {
const file_from = path.join(from, file)
const file_to = path.join(to, file)
// Only substitute path when `to` is not explicitly specified.
map[file_from] = this._to(file_to)
})
return this._copyFiles(map)
}
async _globDir (root) {
const options = {
cwd: root,
dot: true,
// Then, the dirs in `files` will end with a slash `/`
mark: true
}
if (this._ignore) {
options.ignore = this._ignore
}
const files = await glob('**/*', options)
return files.filter(REGEX_IS_GLOB_FILE.test, REGEX_IS_GLOB_FILE)
}
_copyFiles (map) {
const tasks = Object.keys(map).map(from => {
const to = map[from]
return this._copyFile(from, to)
})
return Promise.all(tasks)
}
// Substitute filename
_to (to) {
return this._render(to, this._data)
}
async _readAndTemplate (path) {
const content = await fs.readFile(path)
return this._render(content.toString(), this._data)
}
async write (to, template) {
const override = await this._shouldOverride(to)
if (!override) {
return
}
const content = this._render(template, this._data)
return fs.outputFile(to, content)
}
async _copyFile (from, to) {
const override = await this._shouldOverride(to)
if (!override) {
return
}
const content = await this._readAndTemplate(from)
const stat = await fs.stat(from)
return fs.outputFile(to, content, {
mode: stat.mode
})
}
_shouldOverride (file) {
const override = this._override
const backup = this._backup
return fs.exists(file)
.then(exists => {
// File not exists
if (!exists) {
return true
}
// Exists, and not override
if (!override) {
return false
}
// Exists, override, and need not to create backup
if (!backup) {
return true
}
const backFile = file + '.bak'
return fs.copy(file, backFile)
.then(() => true)
})
}
}