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