/
files.ts
216 lines (199 loc) · 6.48 KB
/
files.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* 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 {getPluginI18nPath, DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import {
VERSIONS_JSON_FILE,
VERSIONED_DOCS_DIR,
VERSIONED_SIDEBARS_DIR,
CURRENT_VERSION_NAME,
} from '../constants';
import {validateVersionNames} from './validation';
import type {
PluginOptions,
VersionMetadata,
} from '@docusaurus/plugin-content-docs';
import type {VersionContext} from './index';
/** Add a prefix like `community_version-1.0.0`. No-op for default instance. */
function addPluginIdPrefix(fileOrDir: string, pluginId: string): string {
return pluginId === DEFAULT_PLUGIN_ID
? fileOrDir
: `${pluginId}_${fileOrDir}`;
}
/** `[siteDir]/community_versioned_docs/version-1.0.0` */
export function getVersionDocsDirPath(
siteDir: string,
pluginId: string,
versionName: string,
): string {
return path.join(
siteDir,
addPluginIdPrefix(VERSIONED_DOCS_DIR, pluginId),
`version-${versionName}`,
);
}
/** `[siteDir]/community_versioned_sidebars/version-1.0.0-sidebars.json` */
export function getVersionSidebarsPath(
siteDir: string,
pluginId: string,
versionName: string,
): string {
return path.join(
siteDir,
addPluginIdPrefix(VERSIONED_SIDEBARS_DIR, pluginId),
`version-${versionName}-sidebars.json`,
);
}
export function getDocsDirPathLocalized({
i18nDir,
pluginId,
versionName,
}: {
i18nDir: string;
pluginId: string;
versionName: string;
}): string {
return getPluginI18nPath({
i18nDir,
pluginName: 'docusaurus-plugin-content-docs',
pluginId,
subPaths: [
versionName === CURRENT_VERSION_NAME
? CURRENT_VERSION_NAME
: `version-${versionName}`,
],
});
}
/** `community` => `[siteDir]/community_versions.json` */
export function getVersionsFilePath(siteDir: string, pluginId: string): string {
return path.join(siteDir, addPluginIdPrefix(VERSIONS_JSON_FILE, pluginId));
}
/**
* Reads the plugin's respective `versions.json` file, and returns its content.
*
* @throws Throws if validation fails, i.e. `versions.json` doesn't contain an
* array of valid version names.
*/
export async function readVersionsFile(
siteDir: string,
pluginId: string,
): Promise<string[] | null> {
const versionsFilePath = getVersionsFilePath(siteDir, pluginId);
if (await fs.pathExists(versionsFilePath)) {
const content: unknown = await fs.readJSON(versionsFilePath);
validateVersionNames(content);
return content;
}
return null;
}
/**
* Reads the `versions.json` file, and returns an ordered list of version names.
*
* - If `disableVersioning` is turned on, it will return `["current"]` (requires
* `includeCurrentVersion` to be true);
* - If `includeCurrentVersion` is turned on, "current" will be inserted at the
* beginning, if not already there.
*
* You need to use {@link filterVersions} after this.
*
* @throws Throws an error if `disableVersioning: true` but `versions.json`
* doesn't exist (i.e. site is not versioned)
* @throws Throws an error if versions list is empty (empty `versions.json` or
* `disableVersioning` is true, and not including current version)
*/
export async function readVersionNames(
siteDir: string,
options: PluginOptions,
): Promise<string[]> {
const versionFileContent = await readVersionsFile(siteDir, options.id);
if (!versionFileContent && options.disableVersioning) {
throw new Error(
`Docs: using "disableVersioning: true" option on a non-versioned site does not make sense.`,
);
}
const versions = options.disableVersioning ? [] : versionFileContent ?? [];
// We add the current version at the beginning, unless:
// - user don't want to; or
// - it's already been explicitly added to versions.json
if (
options.includeCurrentVersion &&
!versions.includes(CURRENT_VERSION_NAME)
) {
versions.unshift(CURRENT_VERSION_NAME);
}
if (versions.length === 0) {
throw new Error(
`It is not possible to use docs without any version. No version is included because you have requested to not include ${path.resolve(
options.path,
)} through "includeCurrentVersion: false", while ${
options.disableVersioning
? 'versioning is disabled with "disableVersioning: true"'
: `the versions file is empty/non-existent`
}.`,
);
}
return versions;
}
/**
* Gets the path-related version metadata.
*
* @throws Throws if the resolved docs folder or sidebars file doesn't exist.
* Does not throw if a versioned sidebar is missing (since we don't create empty
* files).
*/
export async function getVersionMetadataPaths({
versionName,
context,
options,
}: VersionContext): Promise<
Pick<
VersionMetadata,
'contentPath' | 'contentPathLocalized' | 'sidebarFilePath'
>
> {
const isCurrent = versionName === CURRENT_VERSION_NAME;
const contentPathLocalized = getDocsDirPathLocalized({
i18nDir: context.i18nDir,
pluginId: options.id,
versionName,
});
const contentPath = isCurrent
? path.resolve(context.siteDir, options.path)
: getVersionDocsDirPath(context.siteDir, options.id, versionName);
const sidebarFilePath = isCurrent
? options.sidebarPath
: getVersionSidebarsPath(context.siteDir, options.id, versionName);
if (!(await fs.pathExists(contentPath))) {
throw new Error(
`The docs folder does not exist for version "${versionName}". A docs folder is expected to be found at ${path.relative(
context.siteDir,
contentPath,
)}.`,
);
}
// If the current version defines a path to a sidebar file that does not
// exist, we throw! Note: for versioned sidebars, the file may not exist (as
// we prefer to not create it rather than to create an empty file)
// See https://github.com/facebook/docusaurus/issues/3366
// See https://github.com/facebook/docusaurus/pull/4775
if (
versionName === CURRENT_VERSION_NAME &&
typeof sidebarFilePath === 'string' &&
!(await fs.pathExists(sidebarFilePath))
) {
throw new Error(`The path to the sidebar file does not exist at "${path.relative(
context.siteDir,
sidebarFilePath,
)}".
Please set the docs "sidebarPath" field in your config file to:
- a sidebars path that exists
- false: to disable the sidebar
- undefined: for Docusaurus to generate it automatically`);
}
return {contentPath, contentPathLocalized, sidebarFilePath};
}