-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
279 lines (229 loc) · 6.25 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import { join, basename } from 'path'
import chokidar from 'chokidar'
import minimatch from 'minimatch'
import { readFile, outputFile, copy, remove, existsSync } from 'fs-extra'
import { mark, stop } from 'marky'
import pretty from 'pretty-ms'
import chalk from 'chalk'
import { version } from './package.json'
const { log } = console
/**
* Transform file contents, when returning `false` transform is
* skipped and file is copied
*
* @param {string} dest
* @param {(function|object|false)} transform
* @returns {(base: string, item: string) => Promise<string>}
*/
const transformer = (dest, transform) => async (base, file) => {
if (!transform || !file) return false
if (![ 'function', 'object' ].includes(typeof transform)) {
throw new Error(`${file} transform must be of type object or function`)
}
try {
existsSync(file)
} catch (error) {
throw new Error(error)
}
const content = await readFile(file)
if (typeof transform === 'function') {
log(chalk`{dim - Transformed:) {cyan ${file}}`)
return transform({ name: file, content, dest })
}
if (typeof transform === 'object') {
if (typeof transform[base] === 'string') {
if (transform[base] !== base) return transform[base]
}
const item = Object.keys(transform).find((glob) => minimatch(file, glob))
// execute copy
if (!item) return false
if (typeof transform[item] === 'string') return transform[item]
if (typeof transform[item] === 'function') {
log(chalk`{dim - Transformed:) {cyan ${file}}`)
return transform[item]({ content, dest, name: file })
}
log(
chalk.dim(`The transform used by ${item} must of type function or string`)
)
}
}
/**
* Rename file transform. Good for multiple return approaches.
*
* @param {string} base
* @param {object} file
* @param {Map} files
* @param {string} item
*/
const rename = (base, file, item, files) => {
if (file === false) return base
const ext = item.substring(item.lastIndexOf('.'), item.length)
const filename =
typeof file === 'object' && file.name
? file.name
: typeof file === 'string'
? basename(file)
: base
const name = filename
.replace(/\[\bname\b\]/, base)
.replace(/\[\bext\b\]/, ext || null)
if (!files.has(item)) files.set(item, name)
if (base !== name) {
log(chalk`{dim renamed ${base} to ${name}}`)
}
return files.get(item)
}
/**
* Repath file destination
*
* @param {string} base
* @param {object} file
* @param {string} dest
*/
const repath = (base, file, dest) => {
if (typeof file === 'object') {
if (file.dest) {
return file.dest.replace(/\[\bname\b\]/, base)
}
}
return dest
}
/**
* Changed Event
*
* @param {Map} files
* @param {string} dest
* @param {function|object|false} transform
* @returns {(item: string) => Promise<void>}
*/
const changes = (files, dest, transform) => async (item) => {
mark(item)
const base = basename(item)
const file = await transform(base, item)
if (typeof file === 'boolean' || typeof file === 'string') {
await copy(
item,
join(repath(base, file, dest), rename(base, file, item, files))
)
log(
chalk`{bold copied} {cyan ${item}} in {dim ${pretty(
stop(item).duration
)}}`
)
} else if (typeof file === 'object') {
if (file.content && typeof file.content !== 'string') {
throw new Error(`The ${item} content property did not return a string!`)
}
file.dest = repath(base, file, dest)
file.name = rename(base, file, item, files)
if (file.content) {
await outputFile(join(file.dest, file.name), file.content, 'utf8')
log(
chalk`{bold modified} {cyan ${item}} in {dim ${pretty(
stop(item).duration
)}}`
)
} else {
await copy(item, join(file.dest, file.name))
log(
chalk`{bold copied} {cyan ${item}} in {dim ${pretty(
stop(item).duration
)}}`
)
}
}
}
/**
* Remove Event
*
* @param {Map} files
* @param {string} dest
* @returns {(item: string) => Promise<void>}
*/
const removal = (files, dest) => async (item) => {
mark(item)
const path = join(dest, basename(item))
await remove(path)
files.delete(item)
log(
chalk`{bold.red deleted} {red ${path}} in {dim ${pretty(
stop(item).duration
)}}`
)
}
/**
* Chokidor is ready
*
* @param {object} watch
* @returns {(resolve: function) => function}
*/
const ready = (watch) => (resolve) =>
watch.on('ready', () => {
const watched = watch.getWatched()
log(`Watching ${Object.keys(watched).length} paths`)
return resolve()
})
/**
* Globs pattern builder
*
* @param {array} globs
* @param {string} dest
*/
const patterns = (globs, dest) => [
...globs.filter(Boolean),
'!**/node_modules/**',
`!${dest}/**`
]
/**
* Rollup Plugin Globs
*
* This code is essentially a hard fork of `rollup-plugin-globsync`
* by tivac. I've merely modified the code to fit my coding style,
* eliminated the manifests feature, provided type definitions and
* brought support for file transformations that do what the fuck they
* are intended to do, transform files.
*
*/
export default (options = {}) => {
const {
globs = false,
clean = true,
dest = './package',
cwd = process.cwd(),
transform = false
} = options
if (!globs) throw new Error('Missing { globs: [] } in rollup-plugin-globlin')
let runs = 0
let build
let watch
let initialize
return {
name: 'rollup-plugin-globlin',
options (config) {
build = typeof config.watch === 'undefined'
},
async buildStart () {
if (runs++) return
if (clean) await remove(dest)
const files = new Map()
const unlink = removal(files, dest)
const change = changes(files, dest, transformer(dest, transform))
watch = chokidar.watch(patterns(globs, dest), { cwd })
watch.on('add', change)
watch.on('change', change)
watch.on('unlink', unlink)
watch.on('unlinkDir', unlink)
watch.on('error', (error) => {
throw error
})
initialize = new Promise(ready(watch))
log(chalk`{underline rollup-plugin-globlin v${version}}`)
},
async generateBundle () {
await initialize
},
buildEnd () {
if (build) watch.close()
}
}
}