-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateBlueprint.js
138 lines (114 loc) · 3.52 KB
/
createBlueprint.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
const path = require('path')
const fs = require('fs')
const replace = require('stream-replace')
const junk = require('junk')
const escapeRegExp = require('./utils').escapeRegExp
const newFilePath = require('./utils').newFilePath
const capitalizeFirstLetter = require('./utils').capitalizeFirstLetter
const createDirectory = require('./utils').createDirectory
function readdir(dir) {
return new Promise((resolve) => {
fs.readdir(dir, (err, files) => {
if (err) {
throw err
}
resolve(files)
})
})
}
function injectCopy(from, to, type, name) {
return new Promise((resolve, reject) => {
const read = fs.createReadStream(from)
const write = fs.createWriteStream(to)
const capitalizedType = capitalizeFirstLetter(type)
const capitalizedName = capitalizeFirstLetter(name)
read.on('error', reject)
write.on('error', reject)
write.on('finish', resolve)
read
.pipe(replace(new RegExp(escapeRegExp(type), 'g'), name))
.pipe(
replace(new RegExp(escapeRegExp(capitalizedType), 'g'), capitalizedName)
)
.pipe(write)
})
}
function replaceBasePath(filePath, replaced, replacer) {
return filePath.replace(new RegExp(escapeRegExp(replaced), 'g'), replacer)
}
async function walk(fileOrDirectoryList, opts) {
const fileOperations = fileOrDirectoryList.map(
(fileOrDirectoryPath) =>
new Promise((resolve, reject) => {
fs.stat(fileOrDirectoryPath, async (err, stats) => {
if (err) {
throw err
}
const { blueprintSourceDir, destinationDir, type, name } = opts
const destinationFileOrDirectory = newFilePath(
replaceBasePath(
fileOrDirectoryPath,
blueprintSourceDir,
destinationDir
),
type,
name
)
if (stats.isDirectory()) {
try {
const files = await readdir(fileOrDirectoryPath)
await createDirectory(destinationFileOrDirectory)
const relativePaths = files
.filter(junk.not)
.map((file) => path.join(fileOrDirectoryPath, file))
const results = await walk(relativePaths, opts)
resolve(results)
} catch (e) {
throw e
}
} else {
const err = await injectCopy(
fileOrDirectoryPath,
destinationFileOrDirectory,
type,
name
)
if (err) {
throw err
}
resolve(destinationFileOrDirectory)
}
})
})
)
return Promise.all(fileOperations)
}
async function createBluePrint(type, name, destination, configs) {
if (!configs) {
throw new Error('No configs found')
}
const blueprintSource = configs[type]
if (!blueprintSource) {
throw new Error(`No configs found for type ${type}`)
}
const blueprintSourceDir = path.resolve(process.cwd(), blueprintSource)
const destinationDir = path.resolve(process.cwd(), destination)
try {
const files = await readdir(blueprintSourceDir)
if (!files || files.length === 0) {
throw new Error("The blueprint is empty or doesn't exist")
}
const relativeFiles = files
.filter(junk.not)
.map((file) => path.join(blueprintSourceDir, file))
return await walk(relativeFiles, {
blueprintSourceDir,
destinationDir,
type,
name,
})
} catch (e) {
throw e
}
}
module.exports = createBluePrint