/
cli.ts
142 lines (127 loc) · 4.3 KB
/
cli.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
/**
* 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 fs from 'fs-extra';
import path from 'path';
import logger from '@docusaurus/logger';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import {
getVersionsFilePath,
getVersionDocsDirPath,
getVersionSidebarsPath,
getDocsDirPathLocalized,
readVersionsFile,
} from './versions/files';
import {validateVersionName} from './versions/validation';
import {loadSidebarsFileUnsafe} from './sidebars';
import {CURRENT_VERSION_NAME} from './constants';
import type {PluginOptions} from '@docusaurus/plugin-content-docs';
import type {LoadContext} from '@docusaurus/types';
async function createVersionedSidebarFile({
siteDir,
pluginId,
sidebarPath,
version,
}: {
siteDir: string;
pluginId: string;
sidebarPath: string | false | undefined;
version: string;
}) {
// Load current sidebar and create a new versioned sidebars file (if needed).
// Note: we don't need the sidebars file to be normalized: it's ok to let
// plugin option changes to impact older, versioned sidebars
// We don't validate here, assuming the user has already built the version
const sidebars = await loadSidebarsFileUnsafe(sidebarPath);
// Do not create a useless versioned sidebars file if sidebars file is empty
// or sidebars are disabled/false)
const shouldCreateVersionedSidebarFile = Object.keys(sidebars).length > 0;
if (shouldCreateVersionedSidebarFile) {
await fs.outputFile(
getVersionSidebarsPath(siteDir, pluginId, version),
`${JSON.stringify(sidebars, null, 2)}\n`,
'utf8',
);
}
}
// Tests depend on non-default export for mocking.
export async function cliDocsVersionCommand(
version: unknown,
{id: pluginId, path: docsPath, sidebarPath}: PluginOptions,
{siteDir, i18n}: LoadContext,
): Promise<void> {
// It wouldn't be very user-friendly to show a [default] log prefix,
// so we use [docs] instead of [default]
const pluginIdLogPrefix =
pluginId === DEFAULT_PLUGIN_ID ? '[docs]' : `[${pluginId}]`;
try {
validateVersionName(version);
} catch (err) {
logger.info`${pluginIdLogPrefix}: Invalid version name provided. Try something like: 1.0.0`;
throw err;
}
const versions = (await readVersionsFile(siteDir, pluginId)) ?? [];
// Check if version already exists.
if (versions.includes(version)) {
throw new Error(
`${pluginIdLogPrefix}: this version already exists! Use a version tag that does not already exist.`,
);
}
if (i18n.locales.length > 1) {
logger.info`Versioned docs will be created for the following locales: name=${i18n.locales}`;
}
await Promise.all(
i18n.locales.map(async (locale) => {
// TODO duplicated logic from core; we need to support customization per-
// locale in the future
const i18nDir = path.resolve(siteDir, i18n.path, locale);
// Copy docs files.
const docsDir =
locale === i18n.defaultLocale
? path.resolve(siteDir, docsPath)
: getDocsDirPathLocalized({
i18nDir,
pluginId,
versionName: CURRENT_VERSION_NAME,
});
if (
!(await fs.pathExists(docsDir)) ||
(await fs.readdir(docsDir)).length === 0
) {
if (locale === i18n.defaultLocale) {
throw new Error(
logger.interpolate`${pluginIdLogPrefix}: no docs found in path=${docsDir}.`,
);
} else {
logger.warn`${pluginIdLogPrefix}: no docs found in path=${docsDir}. Skipping.`;
return;
}
}
const newVersionDir =
locale === i18n.defaultLocale
? getVersionDocsDirPath(siteDir, pluginId, version)
: getDocsDirPathLocalized({
i18nDir,
pluginId,
versionName: version,
});
await fs.copy(docsDir, newVersionDir);
}),
);
await createVersionedSidebarFile({
siteDir,
pluginId,
version,
sidebarPath,
});
// Update versions.json file.
versions.unshift(version);
await fs.outputFile(
getVersionsFilePath(siteDir, pluginId),
`${JSON.stringify(versions, null, 2)}\n`,
);
logger.success`name=${pluginIdLogPrefix}: version name=${version} created!`;
}