/
generate.js
executable file
·69 lines (62 loc) · 1.61 KB
/
generate.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
#!/usr/bin/env babel-node
import raml from 'raml-parser'
import fs from 'fs'
import path from 'path-browserify'
const generatedPath = __dirname
const samplePath = '../samples'
const dir = fs.readdirSync(samplePath)
const ramlFiles = dir.filter(d => {
const match = d.match(/[yr]a?ml$/)
return match
})
const items = []
for (const file of dir) {
const content = fs.readFileSync(path.join(samplePath, file)).toString()
items.push({
content: content,
file: {
name: file,
path: samplePath
}
})
}
const reader = (_items) => {
const _reader = {
readFileAsync: (filePath) => {
for (const item of _items) {
if (filePath.indexOf(
path.join(item.file.path, item.file.name)
) === 0) {
return new Promise((resolve) => {
resolve(item.content)
})
}
}
return new Promise((resolve) => {
resolve(
'::fileRef::' +
filePath
)
})
}
}
return _reader
}
const writeRAMLFile = file => {
const content = fs.readFileSync(path.join(samplePath, file)).toString()
raml.load(content, path.join(samplePath, file), {
reader: reader(items)
}).then(data => {
const _content = JSON.stringify(data, null, ' ')
/* eslint-disable no-console */
console.log('file', file.replace(/\.[yr]a?ml$/, '.json'))
/* eslint-enable no-console */
fs.writeFileSync(
path.join(generatedPath, file.replace(/\.[yr]a?ml$/, '.json')),
_content
)
})
}
for (const file of ramlFiles) {
writeRAMLFile(file)
}