mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-15 10:07:33 +02:00
refactor: use findAsyncSequential in a few places (#6377)
* refactor: use findAsyncSequential in a few places * fixes * fix
This commit is contained in:
parent
ad16f4fdd9
commit
3cb0972b79
5 changed files with 53 additions and 46 deletions
|
@ -10,6 +10,7 @@ import {
|
|||
posixPath,
|
||||
escapePath,
|
||||
getFileLoaderUtils,
|
||||
findAsyncSequential,
|
||||
} from '@docusaurus/utils';
|
||||
import visit from 'unist-util-visit';
|
||||
import path from 'path';
|
||||
|
@ -76,20 +77,20 @@ async function getImageAbsolutePath(
|
|||
} else if (path.isAbsolute(imagePath)) {
|
||||
// absolute paths are expected to exist in the static folder
|
||||
const possiblePaths = staticDirs.map((dir) => path.join(dir, imagePath));
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const possiblePath of possiblePaths) {
|
||||
const imageFilePath = possiblePath;
|
||||
if (await fs.pathExists(imageFilePath)) {
|
||||
return imageFilePath;
|
||||
}
|
||||
}
|
||||
throw new Error(
|
||||
`Image ${possiblePaths
|
||||
.map((p) => toMessageRelativeFilePath(p))
|
||||
.join(' or ')} used in ${toMessageRelativeFilePath(
|
||||
filePath,
|
||||
)} not found.`,
|
||||
const imageFilePath = await findAsyncSequential(
|
||||
possiblePaths,
|
||||
fs.pathExists,
|
||||
);
|
||||
if (!imageFilePath) {
|
||||
throw new Error(
|
||||
`Image ${possiblePaths
|
||||
.map((p) => toMessageRelativeFilePath(p))
|
||||
.join(' or ')} used in ${toMessageRelativeFilePath(
|
||||
filePath,
|
||||
)} not found.`,
|
||||
);
|
||||
}
|
||||
return imageFilePath;
|
||||
}
|
||||
// We try to convert image urls without protocol to images with require calls
|
||||
// going through webpack ensures that image assets exist at build time
|
||||
|
|
|
@ -10,6 +10,7 @@ import {
|
|||
posixPath,
|
||||
escapePath,
|
||||
getFileLoaderUtils,
|
||||
findAsyncSequential,
|
||||
} from '@docusaurus/utils';
|
||||
import visit from 'unist-util-visit';
|
||||
import path from 'path';
|
||||
|
@ -79,12 +80,12 @@ async function getAssetAbsolutePath(
|
|||
await ensureAssetFileExist(assetFilePath, filePath);
|
||||
return assetFilePath;
|
||||
} else if (path.isAbsolute(assetPath)) {
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const staticDir of staticDirs) {
|
||||
const assetFilePath = path.join(staticDir, assetPath);
|
||||
if (await fs.pathExists(assetFilePath)) {
|
||||
return assetFilePath;
|
||||
}
|
||||
const assetFilePath = await findAsyncSequential(
|
||||
staticDirs.map((dir) => path.join(dir, assetPath)),
|
||||
fs.pathExists,
|
||||
);
|
||||
if (assetFilePath) {
|
||||
return assetFilePath;
|
||||
}
|
||||
} else {
|
||||
const assetFilePath = path.join(path.dirname(filePath), assetPath);
|
||||
|
|
|
@ -15,7 +15,11 @@ import type {
|
|||
SidebarItemCategoryLinkConfig,
|
||||
} from './types';
|
||||
import {sortBy, last} from 'lodash';
|
||||
import {addTrailingSlash, posixPath} from '@docusaurus/utils';
|
||||
import {
|
||||
addTrailingSlash,
|
||||
posixPath,
|
||||
findAsyncSequential,
|
||||
} from '@docusaurus/utils';
|
||||
import logger from '@docusaurus/logger';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
|
@ -76,17 +80,15 @@ async function readCategoryMetadataFile(
|
|||
throw e;
|
||||
}
|
||||
}
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const ext of ['.json', '.yml', '.yaml']) {
|
||||
// Simpler to use only posix paths for mocking file metadata in tests
|
||||
const filePath = posixPath(
|
||||
path.join(categoryDirPath, `${CategoryMetadataFilenameBase}${ext}`),
|
||||
);
|
||||
if (await fs.pathExists(filePath)) {
|
||||
return tryReadFile(filePath);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
const filePath = await findAsyncSequential(
|
||||
['.json', '.yml', '.yaml'].map((ext) =>
|
||||
posixPath(
|
||||
path.join(categoryDirPath, `${CategoryMetadataFilenameBase}${ext}`),
|
||||
),
|
||||
),
|
||||
fs.pathExists,
|
||||
);
|
||||
return filePath ? tryReadFile(filePath) : null;
|
||||
}
|
||||
|
||||
// Comment for this feature: https://github.com/facebook/docusaurus/issues/3464#issuecomment-818670449
|
||||
|
@ -154,13 +156,12 @@ export const DefaultSidebarItemsGenerator: SidebarItemsGenerator = async ({
|
|||
docs.forEach((doc) => {
|
||||
const breadcrumb = getRelativeBreadcrumb(doc);
|
||||
let currentDir = treeRoot; // We walk down the file's path to generate the fs structure
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const dir of breadcrumb) {
|
||||
breadcrumb.forEach((dir) => {
|
||||
if (typeof currentDir[dir] === 'undefined') {
|
||||
currentDir[dir] = {}; // Create new folder.
|
||||
}
|
||||
currentDir = currentDir[dir]!; // Go into the subdirectory.
|
||||
}
|
||||
});
|
||||
currentDir[`${docIdPrefix}${doc.id}`] = null; // We've walked through the file path. Register the file in this directory.
|
||||
});
|
||||
return treeRoot;
|
||||
|
|
|
@ -321,11 +321,11 @@ describe('mapAsyncSequential', () => {
|
|||
const timeTotal = timeAfter - timeBefore;
|
||||
|
||||
const totalTimeouts = sum(Object.values(itemToTimeout));
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts);
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(totalTimeouts - 5);
|
||||
|
||||
expect(itemMapStartsAt['1']).toBeGreaterThanOrEqual(0);
|
||||
expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual(itemMapEndsAt['1']);
|
||||
expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(itemMapEndsAt['2']);
|
||||
expect(itemMapStartsAt['2']).toBeGreaterThanOrEqual(itemMapEndsAt['1'] - 5);
|
||||
expect(itemMapStartsAt['3']).toBeGreaterThanOrEqual(itemMapEndsAt['2'] - 5);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -353,8 +353,8 @@ describe('findAsyncSequential', () => {
|
|||
expect(findFn).toHaveBeenNthCalledWith(2, '2');
|
||||
|
||||
const timeTotal = timeAfter - timeBefore;
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(100);
|
||||
expect(timeTotal).toBeLessThan(150);
|
||||
expect(timeTotal).toBeGreaterThanOrEqual(95);
|
||||
expect(timeTotal).toBeLessThan(105);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -33,13 +33,17 @@ export function resolveModuleName(
|
|||
moduleType: 'preset' | 'theme' | 'plugin',
|
||||
): string {
|
||||
const modulePatterns = getNamePatterns(moduleName, moduleType);
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (const module of modulePatterns) {
|
||||
const module = modulePatterns.find((m) => {
|
||||
try {
|
||||
moduleRequire.resolve(module);
|
||||
return module;
|
||||
} catch (e) {}
|
||||
moduleRequire.resolve(m);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
if (!module) {
|
||||
throw new Error(`Docusaurus was unable to resolve the "${moduleName}" ${moduleType}. Make sure one of the following packages are installed:
|
||||
${modulePatterns.map((m) => `- ${m}`).join('\n')}`);
|
||||
}
|
||||
throw new Error(`Docusaurus was unable to resolve the "${moduleName}" ${moduleType}. Make sure one of the following packages are installed:
|
||||
${modulePatterns.map((module) => `- ${module}`).join('\n')}`);
|
||||
return module;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue