Skip to content

Commit 319979c

Browse files
committed
chore(scripts/website): refactor acquit file loading
to make use of parallel promises also improve use-ability
1 parent b2b0acc commit 319979c

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

scripts/website.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,50 @@ markdown.setOptions({
5555

5656
const testPath = path.resolve(cwd, 'test')
5757

58-
const tests = [
59-
...acquit.parse(fs.readFileSync(path.join(testPath, 'geojson.test.js')).toString()),
60-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/transactions.test.js')).toString()),
61-
...acquit.parse(fs.readFileSync(path.join(testPath, 'schema.alias.test.js')).toString()),
62-
...acquit.parse(fs.readFileSync(path.join(testPath, 'model.middleware.test.js')).toString()),
63-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/date.test.js')).toString()),
64-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/lean.test.js')).toString()),
65-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/cast.test.js')).toString()),
66-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/findoneandupdate.test.js')).toString()),
67-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/custom-casting.test.js')).toString()),
68-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/getters-setters.test.js')).toString()),
69-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/virtuals.test.js')).toString()),
70-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/defaults.test.js')).toString()),
71-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/discriminators.test.js')).toString()),
72-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/promises.test.js')).toString()),
73-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schematypes.test.js')).toString()),
74-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/validation.test.js')).toString()),
75-
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schemas.test.js')).toString())
58+
/** additional test files to scan, relative to `test/` */
59+
const additionalTestFiles = [
60+
'geojson.test.js',
61+
'schema.alias.test.js'
7662
];
63+
/** ignored files from `test/docs/` */
64+
const ignoredTestFiles = [
65+
// ignored because acquit does not like "for await"
66+
'asyncIterator.test.js'
67+
];
68+
69+
/**
70+
* Load all test file contents with acquit
71+
* @returns {Object[]} acquit ast array
72+
*/
73+
async function getTests() {
74+
const promiseArray = [];
75+
76+
for (const file of additionalTestFiles) {
77+
const filePath = path.join(testPath, file);
78+
promiseArray.push(fs.promises.readFile(filePath).then(v => ({value: v.toString(), path: filePath})));
79+
}
80+
81+
const testDocs = path.resolve(testPath, 'docs');
82+
83+
for (const file of await fs.promises.readdir(testDocs)) {
84+
if (ignoredTestFiles.includes(file)) {
85+
continue;
86+
}
87+
88+
const filePath = path.join(testDocs, file);
89+
promiseArray.push(fs.promises.readFile(filePath).then(v => ({value: v.toString(), path: filePath})));
90+
}
91+
92+
return (await Promise.all(promiseArray)).flatMap(v => {
93+
try {
94+
return acquit.parse(v.value);
95+
} catch (err) {
96+
// add a file path to a acquit error, for better debugging
97+
err.filePath = v.path;
98+
throw err;
99+
}
100+
})
101+
}
77102

78103
/**
79104
* Array of array of semver numbers, sorted with highest number first
@@ -351,7 +376,7 @@ async function pugify(filename, options, isReload = false) {
351376
let contents = fs.readFileSync(path.resolve(cwd, inputFile)).toString();
352377

353378
if (options.acquit) {
354-
contents = transform(contents, tests);
379+
contents = transform(contents, await getTests());
355380

356381
contents = contents.replaceAll(/^```acquit$/gmi, "```javascript");
357382
}

0 commit comments

Comments
 (0)