-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
config.ts
115 lines (104 loc) · 3.6 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import path from 'path';
import fs from 'fs-extra';
import logger from '@docusaurus/logger';
import {
DEFAULT_CONFIG_FILE_NAME,
DEFAULT_THEME_FILE_NAME,
findAsyncSequential,
loadFreshModule,
} from '@docusaurus/utils';
import {validateConfig} from './configValidation';
import type {DocusaurusConfig, LoadContext} from '@docusaurus/types';
async function getConventionalSiteConfigPath(siteDir: string) {
// We could support .mjs, .ts, etc. in the future
const candidates = ['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs'].map(
(ext) => DEFAULT_CONFIG_FILE_NAME + ext,
);
const configPath = await findAsyncSequential(
candidates.map((file) => path.join(siteDir, file)),
fs.pathExists,
);
if (!configPath) {
const errorMessage = logger.interpolate`No config file found.
Expected one of:${candidates}
You can provide a custom config path with the code=${'--config'} option.`;
throw new Error(errorMessage);
}
return configPath;
}
export async function loadSiteConfig({
siteDir,
customConfigFilePath,
}: {
siteDir: string;
customConfigFilePath?: string;
}): Promise<Pick<LoadContext, 'siteConfig' | 'siteConfigPath'>> {
const siteConfigPath = customConfigFilePath
? path.resolve(siteDir, customConfigFilePath)
: await getConventionalSiteConfigPath(siteDir);
if (!(await fs.pathExists(siteConfigPath))) {
throw new Error(`Config file at "${siteConfigPath}" not found.`);
}
const importedConfig = await loadFreshModule(siteConfigPath);
const loadedConfig: unknown =
typeof importedConfig === 'function'
? await importedConfig()
: await importedConfig;
const siteConfig = validateConfig(
loadedConfig,
path.relative(siteDir, siteConfigPath),
);
return {siteConfig, siteConfigPath};
}
async function findConventionalThemeConfigPath(
siteDir: string,
): Promise<string | undefined> {
// We could support .mjs, .ts, etc. in the future
const candidates = ['.tsx', '.ts', '.jsx', '.js'].map(
(ext) => DEFAULT_THEME_FILE_NAME + ext,
);
const themeConfigPath = await findAsyncSequential(
candidates.map((file) => path.join(siteDir, file)),
fs.pathExists,
);
return themeConfigPath;
}
// TODO add tests for this
export async function findThemeConfigPath(
siteDir: string,
siteConfig: DocusaurusConfig,
): Promise<string | undefined> {
// TODO add support for custom themeConfig file path
// EX: siteConfig.themeConfig: './theme.tsx'
// Note: maybe it would be simpler to provide this path through the CLI?
if (typeof siteConfig.themeConfig === 'string') {
const customThemeConfigPath = siteConfig.themeConfig;
if (!(await fs.pathExists(customThemeConfigPath))) {
throw new Error(
`Theme config file at "${customThemeConfigPath}" not found.`,
);
}
return customThemeConfigPath;
}
const conventionalThemeConfigPath = await findConventionalThemeConfigPath(
siteDir,
);
// In Docusaurus v3 we require users to provide either the theme config
// through the conventional path, or through the legacy siteConfig attribute
// To avoid issues we prevent users to not provide any theme config at all
if (
!conventionalThemeConfigPath &&
Object.keys(siteConfig.themeConfig ?? {}).length
) {
throw new Error(
`Theme config file couldn't be found at ${DEFAULT_THEME_FILE_NAME}.js or ${DEFAULT_THEME_FILE_NAME}.tsx`,
);
}
return conventionalThemeConfigPath;
}