mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 00:27:21 +02:00
refactor: prefer fs.outputFile to ensureDir + writeFile (#6880)
* refactor: prefer fs.outputFile to ensureDir + writeFile * fix test * fix
This commit is contained in:
parent
73df485aec
commit
23a34c1a07
15 changed files with 66 additions and 108 deletions
|
@ -120,7 +120,7 @@ describe('readOutputHTMLFile', () => {
|
|||
});
|
||||
|
||||
test('generate', async () => {
|
||||
const writeMock = jest.spyOn(fs, 'writeFile').mockImplementation(() => {});
|
||||
const writeMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
|
||||
const existsMock = jest.spyOn(fs, 'pathExists');
|
||||
const readMock = jest.spyOn(fs, 'readFile');
|
||||
|
||||
|
|
|
@ -88,9 +88,9 @@ describe('mapAsyncSequential', () => {
|
|||
|
||||
test('map sequentially', async () => {
|
||||
const itemToTimeout: Record<string, number> = {
|
||||
'1': 50,
|
||||
'2': 150,
|
||||
'3': 100,
|
||||
'1': 200,
|
||||
'2': 600,
|
||||
'3': 400,
|
||||
};
|
||||
const items = Object.keys(itemToTimeout);
|
||||
|
||||
|
@ -112,14 +112,14 @@ describe('mapAsyncSequential', () => {
|
|||
const timeTotal = timeAfter - timeBefore;
|
||||
|
||||
const totalTimeouts = _.sum(Object.values(itemToTimeout));
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 20);
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 100);
|
||||
|
||||
expect(itemMapStartsAt['1']).toBeGreaterThanOrEqual(0);
|
||||
expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual(
|
||||
itemMapEndsAt['1'] - 20,
|
||||
itemMapEndsAt['1'] - 100,
|
||||
);
|
||||
expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(
|
||||
itemMapEndsAt['2'] - 20,
|
||||
itemMapEndsAt['2'] - 100,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
@ -135,7 +135,7 @@ describe('findAsyncSequential', () => {
|
|||
const items = ['1', '2', '3'];
|
||||
|
||||
const findFn = jest.fn(async (item: string) => {
|
||||
await sleep(50);
|
||||
await sleep(400);
|
||||
return item === '2';
|
||||
});
|
||||
|
||||
|
@ -148,8 +148,8 @@ describe('findAsyncSequential', () => {
|
|||
expect(findFn).toHaveBeenNthCalledWith(2, '2');
|
||||
|
||||
const timeTotal = timeAfter - timeBefore;
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(80);
|
||||
expect(timeTotal).toBeLessThan(120);
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(600);
|
||||
expect(timeTotal).toBeLessThan(1000);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -22,8 +22,7 @@ export async function generate(
|
|||
const filepath = path.join(generatedFilesDir, file);
|
||||
|
||||
if (skipCache) {
|
||||
await fs.ensureDir(path.dirname(filepath));
|
||||
await fs.writeFile(filepath, content);
|
||||
await fs.outputFile(filepath, content);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -41,8 +40,7 @@ export async function generate(
|
|||
const currentHash = createHash('md5').update(content).digest('hex');
|
||||
|
||||
if (lastHash !== currentHash) {
|
||||
await fs.ensureDir(path.dirname(filepath));
|
||||
await fs.writeFile(filepath, content);
|
||||
await fs.outputFile(filepath, content);
|
||||
fileHash.set(filepath, currentHash);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue