mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-23 19:48:54 +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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue