This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·219 lines (214 loc) · 6.3 KB
/
index.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
#!/usr/bin/env node
const assert = require('assert')
const fs = require('fs')
const path = require('path')
const {inspect, promisify} = require('util')
const Unified = require('unified')
const RemarkParse = require('remark-parse')
const RemarkStringify = require('remark-stringify')
const Minimist = require('minimist')
const parser = require('./parser')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
function parseOptions ({_: options, report}) {
const requires = {}
let documentFilename
options.forEach((option) => {
if (option.includes('=')) {
const [name, requireFilename] = option.split('=')
requires[name] = requireFilename
} else {
documentFilename = option
}
})
if (!documentFilename) {
throw new Error('missing document filename')
}
return {
report,
requires,
documentFilename
}
}
function flatten (arrays) {
return [].concat(...arrays)
}
async function linearize (closure, array) {
const results = []
await array.reduce(async (previous, child) => {
await previous
const result = closure(child)
results.push(result)
return result
}, Promise.resolve())
return Promise.all(results)
}
function scopedEval (code) {
return eval(code) // eslint-disable-line no-eval
}
function SpecCheck ({requires, errors}) {
Object.keys(requires).forEach((name) => {
eval(`${name} = require('${path.join(process.cwd(), requires[name])}')`) // eslint-disable-line no-eval
})
return async (root) => {
root.children = flatten(await linearize(async (node) => {
if (!(node.type === 'code' && ['js', 'javascript'].includes(node.lang))) {
return [node]
}
const rawLines = node.value.split('\n')
let input
let result
let inputFailed
let expected
try {
const lines = parser(rawLines)
for (let i = 0; i < lines.length; i++) {
if (lines[i].type === 'input') {
if (inputFailed) {
continue
}
input = lines[i].code
try {
result = scopedEval(input)
} catch (error) {
result = error
inputFailed = true
}
} else {
if (i === 0) {
throw new Error('at least one input required')
}
const output = lines[i]
try {
if (output.type === 'resolve') {
try {
result = {
Resolve: await result
}
} catch (error) {
result = {
Reject: error
}
}
expected = {
Resolve: scopedEval(output.code)
}
} else if (output.type === 'reject') {
try {
result = {
Resolve: await result
}
} catch (error) {
result = {
Reject: error
}
}
expected = {
Reject: scopedEval(output.code)
}
} else if (output.type === 'reject error') {
try {
result = {
Resolve: await result
}
} catch (error) {
result = `Reject error: ${error.message}`
}
expected = `Reject error: ${output.message}`
} else if (output.type === 'reject error code') {
try {
result = {
Resolve: await result
}
} catch (error) {
result = `Reject error code: ${error.errorCode}`
}
expected = `Reject error code: ${output.code}`
} else if (output.type === 'error') {
result = `Error: ${result.message}`
expected = `Error: ${output.message}`
} else if (output.type === 'error code') {
result = `Error code: ${result.code}`
expected = `Error code: ${output.errorCode}`
} else {
expected = scopedEval(output.code)
}
} catch (error) {
throw new Error(`failed to eval output \`${lines[i].code}\`: ${error.message}`)
}
try {
assert.deepEqual(result, expected)
} catch (error) {
throw new Error(`${input} => expected ${inspect(expected, false, null)}, but got ${inspect(result, false, null)}`)
}
}
}
return [node, {
type: 'paragraph',
children: [
{
type: 'text',
value: '✅ '
},
{
type: 'link',
url: 'https://github.com/concept-not-found/spec-check',
children: [
{
type: 'inlineCode',
value: 'spec-check'
}
]
},
{
type: 'text',
value: 'ed'
}
]
}]
} catch (error) {
errors.push({node, error})
return [node, {
type: 'paragraph',
children: [
{
type: 'text',
value: '❌ '
},
{
type: 'inlineCode',
value: `Error: ${error.message}`
}
]
}]
}
}, root.children))
return root
}
}
async function main () {
const [, , ...options] = process.argv
const {report, requires, documentFilename} = parseOptions(Minimist(options))
const document = await readFile(path.join(process.cwd(), documentFilename), 'utf8')
const errors = []
const processed = await Unified()
.use(RemarkParse)
.use(SpecCheck, {
requires,
errors
})
.use(RemarkStringify)
.process(document)
if (report) {
await writeFile(report, processed)
} else if (errors.length !== 0) {
errors.forEach(({node, error}) => console.error(`Line ${node.position.start.line}: ${error.message}`))
throw new Error('Errors present in specification')
}
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})