-
Notifications
You must be signed in to change notification settings - Fork 66
/
cli.js
265 lines (243 loc) · 9.17 KB
/
cli.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
'use strict'
// NPM imports
const path = require('path')
const util = require('util')
const fs = require('fs')
const prettier = require("prettier");
const readdir = util.promisify(fs.readdir)
const mkdir = util.promisify(fs.mkdir)
const readFile = util.promisify(fs.readFile)
const fsWriteFile = util.promisify(fs.writeFile)
// Elm imports
const worker = require('./Morphir.Elm.CLI').Elm.Morphir.Elm.CLI.init()
async function make(projectDir, options) {
const morphirJsonPath = path.join(projectDir, 'morphir.json')
const morphirJsonContent = await readFile(morphirJsonPath)
const morphirJson = JSON.parse(morphirJsonContent.toString())
const sourceFiles = await readElmSources(path.join(projectDir, morphirJson.sourceDirectory))
return packageDefinitionFromSource(morphirJson, sourceFiles, options)
}
async function packageDefinitionFromSource(morphirJson, sourceFiles, options) {
return new Promise((resolve, reject) => {
worker.ports.jsonDecodeError.subscribe(err => {
reject(err)
})
worker.ports.packageDefinitionFromSourceResult.subscribe(([err, ok]) => {
if (err) {
reject(err)
} else {
resolve(ok)
}
})
const opts = {
typesOnly: options.typesOnly
}
worker.ports.packageDefinitionFromSource.send([opts, morphirJson, sourceFiles])
})
}
async function readElmSources(dir) {
const readElmSource = async function (filePath) {
const content = await readFile(filePath)
return {
path: filePath,
content: content.toString()
}
}
const readDir = async function (currentDir) {
const entries = await readdir(currentDir, {
withFileTypes: true
})
const elmSources =
entries
.filter(entry => entry.isFile() && entry.name.endsWith('.elm'))
.map(entry => readElmSource(path.join(currentDir, entry.name)))
const subDirSources =
entries
.filter(entry => entry.isDirectory())
.map(entry => readDir(path.join(currentDir, entry.name)))
.reduce(async (soFarPromise, nextPromise) => {
const soFar = await soFarPromise
const next = await nextPromise
return soFar.concat(next)
}, Promise.resolve([]))
return elmSources.concat(await subDirSources)
}
return Promise.all(await readDir(dir))
}
async function gen(input, outputPath, options) {
await mkdir(outputPath, {
recursive: true
})
const morphirIrJson = await readFile(path.resolve(input))
const opts = options
opts.limitToModules = options.modulesToInclude ? options.modulesToInclude.split(",") : null
opts.includeCodecs = options.includeCodecs ? true : false
opts.filename = options.filename == '' ? '' : options.filename
if (options.decorations) {
if (await fileExist(path.resolve(options.decorations))) {
options.decorationsObj = JSON.parse(await readFile(path.resolve(options.decorations)))
}
}
const fileMap = await generate(opts, JSON.parse(morphirIrJson.toString()))
const writePromises =
fileMap.map(async ([
[dirPath, fileName], content
]) => {
const fileDir = dirPath.reduce((accum, next) => path.join(accum, next), outputPath)
const filePath = path.join(fileDir, fileName)
if (await fileExist(filePath)) {
console.log(`UPDATE - ${filePath}`)
} else {
await mkdir(fileDir, {
recursive: true
})
console.log(`INSERT - ${filePath}`)
}
if (options.target == 'TypeScript') {
return fsWriteFile(filePath, prettier.format(content, { parser: "typescript" }))
} else {
return fsWriteFile(filePath, content)
}
})
const filesToDelete = await findFilesToDelete(outputPath, fileMap)
const deletePromises =
filesToDelete.map(async (fileToDelete) => {
console.log(`DELETE - ${fileToDelete}`)
return fs.unlinkSync(fileToDelete)
})
copyRedistributables(options, outputPath)
return Promise.all(writePromises.concat(deletePromises))
}
function copyRedistributables(options, outputPath) {
const copyFiles = (src, dest) => {
const sourceDirectory = path.join(path.dirname(__dirname), 'redistributable', src)
copyRecursiveSync(sourceDirectory, outputPath)
}
if (options.target == 'SpringBoot') {
copyFiles('SpringBoot', outputPath)
} else if (options.target == 'Scala' && options.copyDeps) {
const copyScalaFeature = (feature) => {
copyFiles(`Scala/sdk/${feature}/src`, outputPath)
copyFiles(`Scala/sdk/${feature}/src-${options.targetVersion}`, outputPath)
}
if (options.includeCodecs) {
copyScalaFeature('json')
}
copyScalaFeature('core')
} else if (options.target == 'TypeScript') {
copyFiles('TypeScript/', outputPath)
} else if (options.target == 'Snowpark') {
copyFiles('Snowpark/', outputPath)
}
}
function copyRecursiveSync(src, dest) {
const exists = fs.existsSync(src);
if (exists) {
const stats = exists && fs.statSync(src);
const isDirectory = exists && stats.isDirectory();
if (isDirectory) {
if (!fs.existsSync(dest))
fs.mkdirSync(dest);
fs.readdirSync(src).forEach(function (childItemName) {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
console.log(`COPY - ${dest}`)
}
}
}
async function generate(options, ir) {
return new Promise((resolve, reject) => {
worker.ports.jsonDecodeError.subscribe(err => {
reject(err)
})
worker.ports.generateResult.subscribe(([err, ok]) => {
if (err) {
reject(err)
} else {
resolve(ok)
}
})
worker.ports.generate.send([options, ir])
})
}
async function fileExist(filePath) {
return new Promise((resolve, reject) => {
fs.access(filePath, fs.F_OK, (err) => {
if (err) {
resolve(false)
} else {
resolve(true)
}
})
});
}
async function findFilesToDelete(outputPath, fileMap) {
const readDir = async function (currentDir, generatedFiles) {
const entries = await readdir(currentDir, {
withFileTypes: true
})
const filesToDelete =
entries
.map(entry => [entry, path.resolve(path.join(currentDir, entry.name))])
.filter(([entry, absolutePath]) => {
return entry.isFile() && !generatedFiles.includes(absolutePath)
})
.map(([entry, absolutePath]) => {
return absolutePath
})
const subDirFilesToDelete =
entries
.filter(entry => entry.isDirectory())
.map(entry => readDir(path.join(currentDir, entry.name), generatedFiles))
.reduce(async (soFarPromise, nextPromise) => {
const soFar = await soFarPromise
const next = await nextPromise
return soFar.concat(next)
}, Promise.resolve([]))
return filesToDelete.concat(await subDirFilesToDelete)
}
const files =
fileMap.map(([
[dirPath, fileName], content
]) => {
const fileDir = dirPath.reduce((accum, next) => path.join(accum, next), outputPath)
return path.resolve(fileDir, fileName)
})
return Promise.all(await readDir(outputPath, files))
}
async function writeFile(filePath, content) {
await mkdir(path.dirname(filePath), {
recursive: true
})
return await fsWriteFile(filePath, content)
}
async function test(projectDir) {
const morphirIRJsonPath = path.join(projectDir, 'morphir-ir.json')
const morphirIRJsonContent = await readFile(morphirIRJsonPath)
const morphirIRJson = JSON.parse(morphirIRJsonContent.toString())
const morphirTestsJsonPath = path.join(projectDir, 'morphir-tests.json')
const morphirTestsJsonContent = await readFile(morphirTestsJsonPath)
const morphirTestsJson = JSON.parse(morphirTestsJsonContent.toString())
return testResult(morphirIRJson, morphirTestsJson)
}
async function testResult(morphirIRJson, morphirTestsJson) {
return new Promise((resolve, reject) => {
worker.ports.jsonDecodeError.subscribe(err => {
reject(err)
})
worker.ports.runTestCasesResultError.subscribe(err => {
reject(err)
})
worker.ports.runTestCasesResult.subscribe(ok => {
resolve(ok)
})
worker.ports.runTestCases.send([morphirIRJson, morphirTestsJson])
});
}
exports.make = make;
exports.gen = gen;
exports.test = test;
exports.writeFile = writeFile;