-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacker
executable file
·89 lines (77 loc) · 1.84 KB
/
packer
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
#!/usr/bin/env node
const fs = require('fs')
const { Transform } = require('stream')
const readdirp = require('readdirp')
//#region get files, dirs
let files, dirs, fileNames, dirNames
const getDirs = async () => {
try {
dirs = await readdirp.promise('./test', {
root: './',
type: 'directories',
directoryFilter: [ '!.git', '!*modules' ],
alwaysStat: true
})
dirNames = dirs.map(dir => dir.path)
} catch (err) {
throw err
}
}
const getFiles = async () => {
try {
files = await readdirp.promise('./test', {
root: './',
type: 'files',
directoryFilter: [ '!.git', '!*modules' ],
fileFilter: ['!*.png', '!*.ico', '!.DS_Store', '!*.lock'],
alwaysStat: true
})
fileNames = files.map(file => [file.path, file.fullPath])
} catch (err) {
throw err
}
}
// #endregion
//#region process files
const writeMe = fs.createWriteStream('output', {flags: 'a'})
const TransformStream = new Transform({
transform(chunk, encoding, callback) {
// console.log('before: ', chunk.toString())
const reNewLine = /\n/g
const reTab = /\t/g
const reQuote = /'/g
const reDQuote = /"/g
const newChunk = chunk.toString()
.replace(reNewLine, '\\n')
.replace(reTab, '\\t')
.replace(reQuote, "\\'")
.replace(reDQuote, '\\"')
writeMe.write(` content: "${newChunk}"},\n`)
// console.log('after: ', newChunk)
callback()
}
})
const processFiles = async () => {
await getFiles()
for (const file of fileNames) {
await fs.createReadStream(file[1])
.on('data', (chunk) => {
writeMe.write(`{ file: '${file[0]}',\n`)
})
.pipe(TransformStream)
.end()
}
}
processFiles()
const processDirs = async () => {
await getDirs()
writeMe.write(`let dirs = [\n`)
for (const dir of dirNames) {
console.log(dir)
writeMe.write(`\t'${dir}',\n`)
}
writeMe.write(`]\n\n`)
writeMe.end()
}
// processDirs()
//#endregion