refactor: mark all functions that import external modules as async (#6521)

This commit is contained in:
Joshua Chen 2022-01-31 13:04:45 +08:00 committed by GitHub
parent aa446b7a9c
commit c56e6194b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 125 additions and 112 deletions

View file

@ -14,7 +14,7 @@ export default async function externalCommand(
siteDir: string,
): Promise<void> {
const context = await loadContext(siteDir);
const pluginConfigs = loadPluginConfigs(context);
const pluginConfigs = await loadPluginConfigs(context);
const plugins = await initPlugins({pluginConfigs, context});
// Plugin Lifecycle - extendCli.

View file

@ -147,7 +147,7 @@ export default async function swizzle(
danger?: boolean,
): Promise<void> {
const context = await loadContext(siteDir);
const pluginConfigs = loadPluginConfigs(context);
const pluginConfigs = await loadPluginConfigs(context);
const pluginNames = getPluginNames(pluginConfigs);
const plugins = await initPlugins({
pluginConfigs,

View file

@ -124,7 +124,7 @@ async function transformMarkdownFile(
*/
async function getPathsToWatch(siteDir: string): Promise<string[]> {
const context = await loadContext(siteDir);
const pluginConfigs = loadPluginConfigs(context);
const pluginConfigs = await loadPluginConfigs(context);
const plugins = await initPlugins({
pluginConfigs,
context,

View file

@ -80,7 +80,7 @@ export default async function writeTranslations(
customConfigFilePath: options.config,
locale: options.locale,
});
const pluginConfigs = loadPluginConfigs(context);
const pluginConfigs = await loadPluginConfigs(context);
const plugins = await initPlugins({
pluginConfigs,
context,

View file

@ -129,8 +129,12 @@ export async function loadContext(
};
}
export function loadPluginConfigs(context: LoadContext): PluginConfig[] {
let {plugins: presetPlugins, themes: presetThemes} = loadPresets(context);
export async function loadPluginConfigs(
context: LoadContext,
): Promise<PluginConfig[]> {
let {plugins: presetPlugins, themes: presetThemes} = await loadPresets(
context,
);
const {siteConfig, siteConfigPath} = context;
const require = createRequire(siteConfigPath);
function normalizeShorthand(
@ -298,7 +302,7 @@ export async function load(
codeTranslations,
} = context;
// Plugins.
const pluginConfigs: PluginConfig[] = loadPluginConfigs(context);
const pluginConfigs: PluginConfig[] = await loadPluginConfigs(context);
const {plugins, pluginsRouteConfigs, globalData, themeConfigTranslated} =
await loadPlugins({pluginConfigs, context});

View file

@ -20,7 +20,7 @@ describe('initPlugins', () => {
async function loadSite(options: LoadContextOptions = {}) {
const siteDir = path.join(__dirname, '__fixtures__', 'site-with-plugin');
const context = await loadContext(siteDir, options);
const pluginConfigs = loadPluginConfigs(context);
const pluginConfigs = await loadPluginConfigs(context);
const plugins = await initPlugins({
pluginConfigs,
context,

View file

@ -34,10 +34,10 @@ type NormalizedPluginConfig = {
};
};
function normalizePluginConfig(
async function normalizePluginConfig(
pluginConfig: PluginConfig,
pluginRequire: NodeRequire,
): NormalizedPluginConfig {
): Promise<NormalizedPluginConfig> {
// plugins: ['./plugin']
if (typeof pluginConfig === 'string') {
const pluginModuleImport = pluginConfig;
@ -182,7 +182,7 @@ export default async function initPlugins({
async function initializePlugin(
pluginConfig: PluginConfig,
): Promise<InitializedPlugin> {
const normalizedPluginConfig = normalizePluginConfig(
const normalizedPluginConfig = await normalizePluginConfig(
pluginConfig,
pluginRequire,
);

View file

@ -11,12 +11,12 @@ import loadPresets from '../index';
import type {LoadContext} from '@docusaurus/types';
describe('loadPresets', () => {
test('no presets', () => {
test('no presets', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {},
} as LoadContext;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [],
@ -25,14 +25,14 @@ describe('loadPresets', () => {
`);
});
test('string form', () => {
test('string form', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {
presets: [path.join(__dirname, '__fixtures__/preset-bar.js')],
},
} as LoadContext;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [
@ -50,7 +50,7 @@ describe('loadPresets', () => {
`);
});
test('string form composite', () => {
test('string form composite', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {
@ -60,7 +60,7 @@ describe('loadPresets', () => {
],
},
} as LoadContext;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [
@ -86,14 +86,14 @@ describe('loadPresets', () => {
`);
});
test('array form', () => {
test('array form', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {
presets: [[path.join(__dirname, '__fixtures__/preset-bar.js')]],
},
} as Partial<LoadContext>;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [
@ -111,7 +111,7 @@ describe('loadPresets', () => {
`);
});
test('array form with options', () => {
test('array form with options', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {
@ -123,7 +123,7 @@ describe('loadPresets', () => {
],
},
} as Partial<LoadContext>;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [
@ -143,7 +143,7 @@ describe('loadPresets', () => {
`);
});
test('array form composite', () => {
test('array form composite', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {
@ -159,7 +159,7 @@ describe('loadPresets', () => {
],
},
} as Partial<LoadContext>;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [
@ -189,7 +189,7 @@ describe('loadPresets', () => {
`);
});
test('mixed form', () => {
test('mixed form', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {
@ -202,7 +202,7 @@ describe('loadPresets', () => {
],
},
} as LoadContext;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [
@ -230,7 +230,7 @@ describe('loadPresets', () => {
`);
});
test('mixed form with themes', () => {
test('mixed form with themes', async () => {
const context = {
siteConfigPath: __dirname,
siteConfig: {
@ -244,7 +244,7 @@ describe('loadPresets', () => {
],
},
} as LoadContext;
const presets = loadPresets(context);
const presets = await loadPresets(context);
expect(presets).toMatchInlineSnapshot(`
Object {
"plugins": Array [

View file

@ -15,10 +15,10 @@ import type {
} from '@docusaurus/types';
import {resolveModuleName} from '../moduleShorthand';
export default function loadPresets(context: LoadContext): {
export default async function loadPresets(context: LoadContext): Promise<{
plugins: PluginConfig[];
themes: PluginConfig[];
} {
}> {
// We need to resolve presets from the perspective of the siteDir, since the
// siteDir's package.json declares the dependency on these presets.
const presetRequire = createRequire(context.siteConfigPath);