Skip to content

Commit

Permalink
fix: work around tschaub/mock-fs#377
Browse files Browse the repository at this point in the history
Also removes `fs-extra`, which seems less necessary now than it did back when I added it, and updates a bunch of stuff to use async I/O.

If we update all of the output-generating tests to put the output in a temp directory, then I think we could remove `mock-fs` entirely.
  • Loading branch information
hegemonic committed Dec 19, 2023
1 parent 52d6508 commit b09bafe
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 302 deletions.
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
limitations under the License.
*/

import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import glob from 'fast-glob';
import fs from 'fs-extra';
import _ from 'lodash';
import stripJsonComments from 'strip-json-comments';

Expand Down
8 changes: 5 additions & 3 deletions lib/tasks/copy-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
See the License for the specific language governing permissions and
limitations under the License.
*/

import { copyFile } from 'node:fs/promises';
import path from 'node:path';

import { Task } from '@jsdoc/task-runner';
import fs from 'fs-extra';
import ensureDir from 'make-dir';

export default class CopyFiles extends Task {
constructor(opts) {
Expand All @@ -36,11 +38,11 @@ export default class CopyFiles extends Task {

promise = promise
.then(
() => fs.ensureDir(outputDir),
() => ensureDir(outputDir),
(e) => Promise.reject(e)
)
.then(
() => fs.copyFile(ticket.source, outputFile),
() => copyFile(ticket.source, outputFile),
(e) => Promise.reject(e)
);
promises.push(promise);
Expand Down
11 changes: 5 additions & 6 deletions lib/tasks/generate-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
limitations under the License.
*/

import { writeFile } from 'node:fs/promises';
import path from 'node:path';

import { Task } from '@jsdoc/task-runner';
import fs from 'fs-extra';
import ensureDir from 'make-dir';

import { KIND_TO_CATEGORY } from '../enums.js';

Expand Down Expand Up @@ -54,15 +55,13 @@ export default class GenerateFiles extends Task {
options.beautify = false;
}

// TODO: Replace with async method. (For some reason, calling `fs.ensureDir` in
// this class's tests used the real `fs` instead of the mock. Need to figure out
// why.)
fs.ensureDirSync(outputDir);
// TODO: Replace with async method.
ensureDir.sync(outputDir);
promise = promise.then(
async () => {
output = await template.render(ticket.viewName, ticket.data, options);

return fs.writeFile(outputFile, output);
return writeFile(outputFile, output);
},
(e) => Promise.reject(e)
);
Expand Down
7 changes: 3 additions & 4 deletions lib/tasks/generate-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
limitations under the License.
*/

import fs from 'fs-extra';
import { readFile } from 'node:fs/promises';

import Ticket from '../ticket.js';
import GenerateFiles from './generate-files.js';
Expand All @@ -26,7 +26,7 @@ export default class GenerateIndex extends GenerateFiles {
this.url = opts.url;
}

run(ctx) {
async run(ctx) {
try {
const data = {
allLongnamesTree: ctx.allLongnamesTree,
Expand All @@ -35,8 +35,7 @@ export default class GenerateIndex extends GenerateFiles {
prefix: ctx.pageTitlePrefix,
title: ctx.template.translate('brandDefault'),
}),
// TODO: Figure out why `fs.readFile` doesn't get mocked correctly.
readme: ctx.readme ? fs.readFileSync(ctx.readme, ctx.template.encoding) : null,
readme: ctx.readme ? await readFile(ctx.readme, ctx.template.encoding) : null,
};

this.url ??= ctx.linkManager.getUri('index');
Expand Down
27 changes: 20 additions & 7 deletions lib/tasks/generate-source-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import path from 'node:path';

import fs from 'fs-extra';
import { readFile } from 'node:fs/promises';
import path from 'node:path';

import { CATEGORIES } from '../enums.js';
import Ticket from '../ticket.js';
import GenerateFiles from './generate-files.js';

export default class GenerateSourceFiles extends GenerateFiles {
run(ctx) {
try {
const sourceFiles = ctx.sourceFiles;
async run(ctx) {
const pendingReads = [];
const sourceFiles = ctx.sourceFiles;

this.tickets = [];
this.tickets = [];

try {
for (const file of Object.keys(sourceFiles)) {
const sourceFile = sourceFiles[file];
const data = {
Expand All @@ -38,11 +39,21 @@ export default class GenerateSourceFiles extends GenerateFiles {
pageTitle: sourceFile,
pageTitlePrefix: ctx.pageTitlePrefix,
};
let promise;
let url;

// Links are keyed to the shortened path.
url = ctx.linkManager.getUri(sourceFile);
data.docs = fs.readFileSync(file, ctx.template.encoding);
// Don't wait for the promise to settle before continuing the loop.
promise = readFile(file, ctx.template.encoding).then(
(value) => {
data.docs = value;
},
(e) => {
throw e;
}
);
pendingReads.push(promise);

this.tickets.push(
new Ticket({
Expand All @@ -56,6 +67,8 @@ export default class GenerateSourceFiles extends GenerateFiles {
return Promise.reject(e);
}

await Promise.all(pendingReads);

return super.run(ctx);
}
}
4 changes: 3 additions & 1 deletion lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ export default class Template {

async #loadYaml(filepath) {
let parsedObject;
let yamlData;

try {
if (filepath) {
parsedObject = yaml.load(await readFile(filepath, 'utf8'));
yamlData = await readFile(filepath, 'utf8');
parsedObject = yaml.load(yamlData);
}
} catch (e) {
this.log.fatal(`Unable to load the file ${filepath}: ${e}`);
Expand Down
76 changes: 7 additions & 69 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
"deep-extend": "^0.6.0",
"escape-string-regexp": "^5.0.0",
"fast-glob": "^3.3.2",
"fs-extra": "^11.2.0",
"highlight.js": "^11.9.0",
"html-escaper": "^3.0.3",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"make-dir": "^4.0.0",
"markdown-it": "^14.0.0",
"markdown-it-anchor": "^8.6.7",
"nunjucks": "^3.2.4",
Expand Down
Loading

0 comments on commit b09bafe

Please sign in to comment.