Skip to content

Commit

Permalink
Merge pull request #14116 from hasezoey/slightWebsite
Browse files Browse the repository at this point in the history
Update `scripts/website.js` to fetch all `test/docs/` files automatically
  • Loading branch information
vkarpov15 authored Nov 28, 2023
2 parents 79aab0c + c0c408c commit d31310a
Showing 1 changed file with 43 additions and 27 deletions.
70 changes: 43 additions & 27 deletions scripts/website.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require('acquit-ignore')();
const { marked: markdown } = require('marked');
const highlight = require('highlight.js');
const { promisify } = require("util");
const renderer = {
markdown.use({
heading: function(text, level, raw, slugger) {
const slug = slugger.slug(raw);
return `<h${level} id="${slug}">
Expand All @@ -41,7 +41,7 @@ const renderer = {
</a>
</h${level}>\n`;
}
};
});
markdown.setOptions({
highlight: function(code, language) {
if (!language) {
Expand All @@ -53,29 +53,45 @@ markdown.setOptions({
return highlight.highlight(code, { language }).value;
}
});
markdown.use({ renderer });

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

const tests = [
...acquit.parse(fs.readFileSync(path.join(testPath, 'geojson.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/transactions.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'schema.alias.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'model.middleware.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/date.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/lean.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/cast.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/findoneandupdate.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/custom-casting.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/getters-setters.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/virtuals.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/defaults.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/discriminators.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/promises.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schematypes.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/validation.test.js')).toString()),
...acquit.parse(fs.readFileSync(path.join(testPath, 'docs/schemas.test.js')).toString())

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

/** additional test files to scan, relative to `test/` */
const additionalTestFiles = [
'geojson.test.js',
'schema.alias.test.js'
];
/** ignored files from `test/docs/` */
const ignoredTestFiles = [
// ignored because acquit does not like "for await"
'asyncIterator.test.js'
];

/**
* Load all test file contents with acquit
* @returns {Object[]} acquit ast array
*/
function getTests() {
const testDocs = path.resolve(testPath, 'docs');
const filesToScan = [
...additionalTestFiles.map(v => path.join(testPath, v)),
...fs.readdirSync(testDocs).filter(v => !ignoredTestFiles.includes(v)).map(v => path.join(testDocs, v))
];

const retArray = [];

for (const file of filesToScan) {
try {
retArray.push(acquit.parse(fs.readFileSync(file).toString()));
} catch (err) {
// add a file path to a acquit error, for better debugging
err.filePath = file;
throw err;
}
}

return retArray.flat();
}

function deleteAllHtmlFiles() {
try {
Expand Down Expand Up @@ -407,7 +423,7 @@ async function pugify(filename, options, isReload = false) {
let contents = fs.readFileSync(path.resolve(cwd, inputFile)).toString();

if (options.acquit) {
contents = transform(contents, tests);
contents = transform(contents, getTests());

contents = contents.replaceAll(/^```acquit$/gmi, "```javascript");
}
Expand Down Expand Up @@ -466,7 +482,7 @@ async function pugify(filename, options, isReload = false) {
});
}

// extra function to start watching for file-changes, without having to call this file directly with "watch"
/** extra function to start watching for file-changes, without having to call this file directly with "watch" */
function startWatch() {
Object.entries(docsFilemap.fileMap).forEach(([file, fileValue]) => {
let watchPath = path.resolve(cwd, file);
Expand Down Expand Up @@ -534,7 +550,7 @@ const pathsToCopy = [
'docs/js',
'docs/css',
'docs/images'
]
];

/** Copy all static files when versionedDeploy is used */
async function copyAllRequiredFiles() {
Expand Down

0 comments on commit d31310a

Please sign in to comment.