/
replication-process.js
157 lines (128 loc) · 4.36 KB
/
replication-process.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
const { homedir } = require('os')
const path = require('path')
const mustache = require('mustache')
const yaml = require('js-yaml')
const Replicator = require('./Replicator')
const ReplicationRecipe = require('./ReplicationRecipe')
const resolveReplicantWorkDir = () => {
return path.join(homedir(), '.replicante').replace(/\\/g, '/')
}
const buildRecipe = (replicationInstructions, toolbox) => {
const { replicationRecipeFile } = replicationInstructions
const { readFile } = toolbox
let rawData = readFile(replicationRecipeFile)
let data = JSON.parse(rawData)
if (data.customDelimiters && data.customDelimiters.length !== 2)
throw new Error(
"Custom delimiters should have length of 2. Example: ['{{', '}}']"
)
const recipe = ReplicationRecipe.fromRecipeJson(
data,
resolveReplicantWorkDir()
)
return recipe
}
const generateFileFromTemplate = (options, toolbox) => {
const { readFile, writeFile } = toolbox
const { template, target, view, directory } = options
const templateContent = readFile(path.join(directory, template))
var output = mustache.render(templateContent, view)
writeFile(target, output)
}
const decomposeTemplateFile = (filePath, toolbox) => {
const { readFile } = toolbox
const content = readFile(filePath)
let parts = content.split('---\n')
return {
header: yaml.load(parts[1]),
content: parts[2].trimEnd()
}
}
const generateReplicantFromTemplate = (replicator, recipe, toolbox) => {
const {
writeFile,
listFiles,
stringCases: { lowerCase, kebabCase },
prints: { info },
isBinaryFile
} = toolbox
const {
templateName,
replicantName,
templateDir,
delimiters
} = replicator.replicationRecipe
info(`Replicating sample from ${templateName}. Generating ${replicantName}.`)
const realTemplateDir = path.join(templateDir, 'new')
const fileList = listFiles(realTemplateDir)
const templateFiles = fileList.map(child => child.name)
const view = Object.create({})
for (let i = 0; i < recipe.customVariables.length; i++) {
const { name, value } = recipe.customVariables[i]
view[name] = value
view[`${name}UpperCase`] = value.toUpperCase()
view[`${name}LowerCase`] = value.toLowerCase()
view[`${name}LowerDasherized`] = kebabCase(lowerCase(replicantName))
view[`${name}UpperDasherized`] = kebabCase(
lowerCase(replicantName)
).toUpperCase()
}
mustache.tags = delimiters
const tempDir = path.join(templateDir, '_temp')
for (let i = 0; i < templateFiles.length; i++) {
const fileName = templateFiles[i]
const partialFilePath = path.join(tempDir, fileName)
if (isBinaryFile(path.join(realTemplateDir, fileName))) {
continue
}
// renders new template with header patched
generateFileFromTemplate(
{
template: fileName,
target: partialFilePath,
view: view,
directory: realTemplateDir
},
toolbox
)
const { header, content } = decomposeTemplateFile(partialFilePath, toolbox)
const newTarget = path.join(
resolveReplicantWorkDir(),
header.to.replace(/"/g, '')
)
writeFile(newTarget, content)
}
const binaryFiles = replicator.pipelineData.loadFromDisk()
prepareAndCopyBinaryFiles(binaryFiles, view, toolbox)
}
const prepareAndCopyBinaryFiles = (binaryFiles, view, toolbox) => {
const { copyFile } = toolbox
for (let i = 0; i < binaryFiles.length; i++) {
const { from, to } = binaryFiles[i]
var targetPath = path.join(
resolveReplicantWorkDir(),
mustache.render(to, view)
)
copyFile(from, targetPath)
}
}
const generateReplicant = async (replicationInstructions, toolbox) => {
const { resetDirectory, makeDirectory } = toolbox
const recipe = buildRecipe(replicationInstructions, toolbox)
const replicator = new Replicator(recipe, toolbox)
resetDirectory(replicator.replicationDirectory)
resetDirectory(recipe.templateDir)
makeDirectory(path.join(recipe.templateDir, 'new'))
makeDirectory(path.join(recipe.templateDir, '_temp'))
const { sampleDirectory } = replicationInstructions
await replicator.processRecipeFiles(sampleDirectory)
generateReplicantFromTemplate(replicator, recipe, toolbox)
return {
recipeUsed: recipe,
replicantDirectory: path.join(
resolveReplicantWorkDir(),
recipe.replicantName
)
}
}
module.exports = { generateReplicant, resolveReplicantWorkDir }