mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 23:57:22 +02:00
refactor: convert all fs methods to async (#6725)
* refactor: convert all fs methods to async * fix snap
This commit is contained in:
parent
c0b3c9af65
commit
c6d0d812eb
46 changed files with 518 additions and 429 deletions
|
@ -185,9 +185,9 @@ describe('docsVersion', () => {
|
|||
});
|
||||
|
||||
test('first time versioning', async () => {
|
||||
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDirSync').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFileSync');
|
||||
const copyMock = jest.spyOn(fs, 'copy').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDir').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFile');
|
||||
let versionedSidebar;
|
||||
let versionedSidebarPath;
|
||||
writeMock.mockImplementationOnce((filepath, content) => {
|
||||
|
@ -242,9 +242,9 @@ describe('docsVersion', () => {
|
|||
});
|
||||
|
||||
test('not the first time versioning', async () => {
|
||||
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDirSync').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFileSync');
|
||||
const copyMock = jest.spyOn(fs, 'copy').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDir').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFile');
|
||||
let versionedSidebar;
|
||||
let versionedSidebarPath;
|
||||
writeMock.mockImplementationOnce((filepath, content) => {
|
||||
|
@ -301,9 +301,9 @@ describe('docsVersion', () => {
|
|||
test('second docs instance versioning', async () => {
|
||||
const pluginId = 'community';
|
||||
|
||||
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDirSync').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFileSync');
|
||||
const copyMock = jest.spyOn(fs, 'copy').mockImplementation();
|
||||
const ensureMock = jest.spyOn(fs, 'ensureDir').mockImplementation();
|
||||
const writeMock = jest.spyOn(fs, 'writeFile');
|
||||
let versionedSidebar;
|
||||
let versionedSidebarPath;
|
||||
writeMock.mockImplementationOnce((filepath, content) => {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import shell from 'shelljs';
|
||||
|
||||
|
@ -64,9 +64,9 @@ describe('lastUpdate', () => {
|
|||
|
||||
test('temporary created file that has no git timestamp', async () => {
|
||||
const tempFilePath = path.join(__dirname, '__fixtures__', '.temp');
|
||||
fs.writeFileSync(tempFilePath, 'Lorem ipsum :)');
|
||||
await fs.writeFile(tempFilePath, 'Lorem ipsum :)');
|
||||
await expect(getFileLastUpdate(tempFilePath)).resolves.toBeNull();
|
||||
fs.unlinkSync(tempFilePath);
|
||||
await fs.unlink(tempFilePath);
|
||||
});
|
||||
|
||||
test('Git does not exist', async () => {
|
||||
|
|
|
@ -47,8 +47,8 @@ async function createVersionedSidebarFile({
|
|||
versionedSidebarsDir,
|
||||
`version-${version}-sidebars.json`,
|
||||
);
|
||||
fs.ensureDirSync(path.dirname(newSidebarFile));
|
||||
fs.writeFileSync(
|
||||
await fs.ensureDir(path.dirname(newSidebarFile));
|
||||
await fs.writeFile(
|
||||
newSidebarFile,
|
||||
`${JSON.stringify(sidebars, null, 2)}\n`,
|
||||
'utf8',
|
||||
|
@ -104,8 +104,8 @@ export async function cliDocsVersionCommand(
|
|||
// Load existing versions.
|
||||
let versions = [];
|
||||
const versionsJSONFile = getVersionsFilePath(siteDir, pluginId);
|
||||
if (fs.existsSync(versionsJSONFile)) {
|
||||
versions = JSON.parse(fs.readFileSync(versionsJSONFile, 'utf8'));
|
||||
if (await fs.pathExists(versionsJSONFile)) {
|
||||
versions = JSON.parse(await fs.readFile(versionsJSONFile, 'utf8'));
|
||||
}
|
||||
|
||||
// Check if version already exists.
|
||||
|
@ -120,10 +120,13 @@ export async function cliDocsVersionCommand(
|
|||
// Copy docs files.
|
||||
const docsDir = path.join(siteDir, docsPath);
|
||||
|
||||
if (fs.existsSync(docsDir) && fs.readdirSync(docsDir).length > 0) {
|
||||
if (
|
||||
(await fs.pathExists(docsDir)) &&
|
||||
(await fs.readdir(docsDir)).length > 0
|
||||
) {
|
||||
const versionedDir = getVersionedDocsDirPath(siteDir, pluginId);
|
||||
const newVersionDir = path.join(versionedDir, `version-${version}`);
|
||||
fs.copySync(docsDir, newVersionDir);
|
||||
await fs.copy(docsDir, newVersionDir);
|
||||
} else {
|
||||
throw new Error(`${pluginIdLogPrefix}: there is no docs to version!`);
|
||||
}
|
||||
|
@ -137,8 +140,11 @@ export async function cliDocsVersionCommand(
|
|||
|
||||
// Update versions.json file.
|
||||
versions.unshift(version);
|
||||
fs.ensureDirSync(path.dirname(versionsJSONFile));
|
||||
fs.writeFileSync(versionsJSONFile, `${JSON.stringify(versions, null, 2)}\n`);
|
||||
await fs.ensureDir(path.dirname(versionsJSONFile));
|
||||
await fs.writeFile(
|
||||
versionsJSONFile,
|
||||
`${JSON.stringify(versions, null, 2)}\n`,
|
||||
);
|
||||
|
||||
logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`;
|
||||
}
|
||||
|
|
|
@ -89,23 +89,26 @@ function createMarkdownOptions(
|
|||
};
|
||||
}
|
||||
|
||||
const transform = (filepath: string, options?: Partial<DocsMarkdownOption>) => {
|
||||
const transform = async (
|
||||
filepath: string,
|
||||
options?: Partial<DocsMarkdownOption>,
|
||||
) => {
|
||||
const markdownOptions = createMarkdownOptions(options);
|
||||
const content = fs.readFileSync(filepath, 'utf-8');
|
||||
const content = await fs.readFile(filepath, 'utf-8');
|
||||
const transformedContent = linkify(content, filepath, markdownOptions);
|
||||
return [content, transformedContent];
|
||||
};
|
||||
|
||||
test('transform nothing', () => {
|
||||
test('transform nothing', async () => {
|
||||
const doc1 = path.join(versionCurrent.contentPath, 'doc1.md');
|
||||
const [content, transformedContent] = transform(doc1);
|
||||
const [content, transformedContent] = await transform(doc1);
|
||||
expect(transformedContent).toMatchSnapshot();
|
||||
expect(content).toEqual(transformedContent);
|
||||
});
|
||||
|
||||
test('transform to correct links', () => {
|
||||
test('transform to correct links', async () => {
|
||||
const doc2 = path.join(versionCurrent.contentPath, 'doc2.md');
|
||||
const [content, transformedContent] = transform(doc2);
|
||||
const [content, transformedContent] = await transform(doc2);
|
||||
expect(transformedContent).toMatchSnapshot();
|
||||
expect(transformedContent).toContain('](/docs/doc1');
|
||||
expect(transformedContent).toContain('](/docs/doc2');
|
||||
|
@ -118,19 +121,19 @@ test('transform to correct links', () => {
|
|||
expect(content).not.toEqual(transformedContent);
|
||||
});
|
||||
|
||||
test('transform relative links', () => {
|
||||
test('transform relative links', async () => {
|
||||
const doc3 = path.join(versionCurrent.contentPath, 'subdir', 'doc3.md');
|
||||
|
||||
const [content, transformedContent] = transform(doc3);
|
||||
const [content, transformedContent] = await transform(doc3);
|
||||
expect(transformedContent).toMatchSnapshot();
|
||||
expect(transformedContent).toContain('](/docs/doc2');
|
||||
expect(transformedContent).not.toContain('](../doc2.md)');
|
||||
expect(content).not.toEqual(transformedContent);
|
||||
});
|
||||
|
||||
test('transforms reference links', () => {
|
||||
test('transforms reference links', async () => {
|
||||
const doc4 = path.join(versionCurrent.contentPath, 'doc4.md');
|
||||
const [content, transformedContent] = transform(doc4);
|
||||
const [content, transformedContent] = await transform(doc4);
|
||||
expect(transformedContent).toMatchSnapshot();
|
||||
expect(transformedContent).toContain('[doc1]: /docs/doc1');
|
||||
expect(transformedContent).toContain('[doc2]: /docs/doc2');
|
||||
|
@ -139,10 +142,10 @@ test('transforms reference links', () => {
|
|||
expect(content).not.toEqual(transformedContent);
|
||||
});
|
||||
|
||||
test('report broken markdown links', () => {
|
||||
test('report broken markdown links', async () => {
|
||||
const doc5 = path.join(versionCurrent.contentPath, 'doc5.md');
|
||||
const onBrokenMarkdownLink = jest.fn();
|
||||
const [content, transformedContent] = transform(doc5, {
|
||||
const [content, transformedContent] = await transform(doc5, {
|
||||
onBrokenMarkdownLink,
|
||||
});
|
||||
expect(transformedContent).toEqual(content);
|
||||
|
@ -169,9 +172,9 @@ test('report broken markdown links', () => {
|
|||
} as BrokenMarkdownLink);
|
||||
});
|
||||
|
||||
test('transforms absolute links in versioned docs', () => {
|
||||
test('transforms absolute links in versioned docs', async () => {
|
||||
const doc2 = path.join(version100.contentPath, 'doc2.md');
|
||||
const [content, transformedContent] = transform(doc2);
|
||||
const [content, transformedContent] = await transform(doc2);
|
||||
expect(transformedContent).toMatchSnapshot();
|
||||
expect(transformedContent).toContain('](/docs/1.0.0/subdir/doc1');
|
||||
expect(transformedContent).toContain('](/docs/1.0.0/doc2#existing-docs');
|
||||
|
@ -180,9 +183,9 @@ test('transforms absolute links in versioned docs', () => {
|
|||
expect(content).not.toEqual(transformedContent);
|
||||
});
|
||||
|
||||
test('transforms relative links in versioned docs', () => {
|
||||
test('transforms relative links in versioned docs', async () => {
|
||||
const doc1 = path.join(version100.contentPath, 'subdir', 'doc1.md');
|
||||
const [content, transformedContent] = transform(doc1);
|
||||
const [content, transformedContent] = await transform(doc1);
|
||||
expect(transformedContent).toMatchSnapshot();
|
||||
expect(transformedContent).toContain('](/docs/1.0.0/doc2');
|
||||
expect(transformedContent).not.toContain('](../doc2.md)');
|
||||
|
|
|
@ -85,7 +85,7 @@ export async function loadSidebarsFileUnsafe(
|
|||
// Non-existent sidebars file: no sidebars
|
||||
// Note: this edge case can happen on versioned docs, not current version
|
||||
// We avoid creating empty versioned sidebars file with the CLI
|
||||
if (!fs.existsSync(sidebarFilePath)) {
|
||||
if (!(await fs.pathExists(sidebarFilePath))) {
|
||||
return DisabledSidebars;
|
||||
}
|
||||
|
||||
|
|
|
@ -418,7 +418,7 @@ function createVersionMetadata({
|
|||
};
|
||||
}
|
||||
|
||||
function checkVersionMetadataPaths({
|
||||
async function checkVersionMetadataPaths({
|
||||
versionMetadata,
|
||||
context,
|
||||
}: {
|
||||
|
@ -429,7 +429,7 @@ function checkVersionMetadataPaths({
|
|||
const {siteDir} = context;
|
||||
const isCurrentVersion = versionName === CURRENT_VERSION_NAME;
|
||||
|
||||
if (!fs.existsSync(contentPath)) {
|
||||
if (!(await fs.pathExists(contentPath))) {
|
||||
throw new Error(
|
||||
`The docs folder does not exist for version "${versionName}". A docs folder is expected to be found at ${path.relative(
|
||||
siteDir,
|
||||
|
@ -446,7 +446,7 @@ function checkVersionMetadataPaths({
|
|||
if (
|
||||
isCurrentVersion &&
|
||||
typeof sidebarFilePath === 'string' &&
|
||||
!fs.existsSync(sidebarFilePath)
|
||||
!(await fs.pathExists(sidebarFilePath))
|
||||
) {
|
||||
throw new Error(`The path to the sidebar file does not exist at "${path.relative(
|
||||
siteDir,
|
||||
|
@ -585,8 +585,10 @@ export async function readVersionsMetadata({
|
|||
options,
|
||||
}),
|
||||
);
|
||||
versionsMetadata.forEach((versionMetadata) =>
|
||||
checkVersionMetadataPaths({versionMetadata, context}),
|
||||
await Promise.all(
|
||||
versionsMetadata.map((versionMetadata) =>
|
||||
checkVersionMetadataPaths({versionMetadata, context}),
|
||||
),
|
||||
);
|
||||
return versionsMetadata;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue