-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
346 lines (320 loc) · 12.8 KB
/
Copy pathcli.js
File metadata and controls
346 lines (320 loc) · 12.8 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const args = process.argv;
const path = require("path");
const config = require("./src/config");
const fse = require("fs-extra");
const fs = require("fs");
const {
jaSiderbarPath,
zhSiderbarPath,
enSiderbarPath,
locales,
commonSiderBars,
} = config;
const versions = [
{ branch: "main" },
...require("./versions.json").map((v) => ({ branch: v })),
];
const exec = require("child_process").exec;
const execSync = require("child_process").execSync;
const tempDir = path.join(__dirname, "temp");
const docsDir = path.join(__dirname, config.docDir);
const deleteDirIfExist = (path) => {
if (fs.existsSync(path)) {
fs.rmSync(path, { recursive: true });
}
};
const cleanup = () => {
const versionedBranches = versions
.filter(v => v.branch !== 'main')
.map(v => v.branch);
const latestVersion = versionedBranches[0];
// Recursively delete all files with a given name under a directory
const removeFilesNamed = (dir, filename) => {
if (!fs.existsSync(dir)) return;
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) removeFilesNamed(full, filename);
else if (entry.name === filename) fs.rmSync(full);
}
};
// Delete a path (file or directory) if it exists
const removeSilent = (p) => {
if (fs.existsSync(p)) fs.rmSync(p, { recursive: true, force: true });
};
// Absolute path to the versioned content dir for a given locale and version branch
const localeDirFor = (locale, ver) => {
if (locale === 'zh') return path.join(__dirname, 'i18n', 'zh', 'docusaurus-plugin-content-docs', `version-${ver}`);
if (locale === 'ja') return path.join(__dirname, 'i18n', 'ja', 'docusaurus-plugin-content-docs', `version-${ver}`);
return path.join(__dirname, 'versioned_docs', `version-${ver}`); // en
};
const allLocaleDirsFor = (ver) => ['en', 'zh', 'ja'].map(l => localeDirFor(l, ver));
// Copy files whose names match a shell-style glob pattern from srcDir to each destination dir
const matchGlob = (name, pattern) => {
const re = new RegExp(
'^' + pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*') + '$'
);
return re.test(name);
};
const copyGlob = (srcDir, pattern, destDirs) => {
if (!fs.existsSync(srcDir)) return;
const files = fs.readdirSync(srcDir).filter(f => matchGlob(f, pattern));
for (const file of files) {
for (const dest of destDirs) {
fse.copySync(path.join(srcDir, file), path.join(dest, file), { overwrite: true });
}
}
};
// Move files matching a shell-style glob from srcDir into destDir
const moveGlob = (srcDir, pattern, destDir) => {
if (!fs.existsSync(srcDir)) return;
fse.ensureDirSync(destDir);
const files = fs.readdirSync(srcDir).filter(f => matchGlob(f, pattern));
for (const file of files) {
fse.moveSync(path.join(srcDir, file), path.join(destDir, file), { overwrite: true });
}
};
// ── Pre-copy cleanup ──────────────────────────────────────────────────────
console.log('rm TOCs');
removeFilesNamed(path.join(__dirname, 'i18n'), 'TOC.md');
removeFilesNamed(path.join(__dirname, 'versioned_docs'), 'TOC.md');
console.log('rm READMEs');
for (const ver of versionedBranches) {
for (const dir of allLocaleDirsFor(ver)) {
removeSilent(path.join(dir, 'README.md'));
}
}
console.log('rm ecosystem_release');
for (const ver of versionedBranches) {
for (const dir of allLocaleDirsFor(ver)) {
removeSilent(path.join(dir, 'ecosystem_release'));
}
}
// ── Stage release notes and developer docs ────────────────────────────────
// Clear existing release_notes and developers dirs before repopulating
for (const ver of versionedBranches) {
for (const dir of allLocaleDirsFor(ver)) {
removeSilent(path.join(dir, 'release_notes'));
removeSilent(path.join(dir, 'developers'));
}
}
fse.ensureDirSync(path.join(__dirname, 'releasenotes'));
fse.ensureDirSync(path.join(__dirname, 'common'));
for (const ver of versionedBranches) {
for (const dir of allLocaleDirsFor(ver)) {
fse.ensureDirSync(path.join(dir, '_assets'));
fse.ensureDirSync(path.join(dir, 'developers'));
}
}
// Copy shared images from latest version's _assets to all other versions.
// Source of truth is always the English versioned_docs for the latest version.
const latestEnAssets = path.join(__dirname, 'versioned_docs', `version-${latestVersion}`, '_assets');
const otherVersions = versionedBranches.slice(1);
// Heap profile, debug info, and trace images go to:
// en: all versions except latest (which is the source)
// zh + ja: all versions
for (const pattern of ['visualized_heap_profile.png', 'debug_info.png', '*trace*.png']) {
copyGlob(latestEnAssets, pattern, [
...otherVersions.map(v => path.join(__dirname, 'versioned_docs', `version-${v}`, '_assets')),
...versionedBranches.map(v => path.join(localeDirFor('zh', v), '_assets')),
...versionedBranches.map(v => path.join(localeDirFor('ja', v), '_assets')),
]);
}
// IDE images go to:
// zh: version-3.1 only (other zh versions have them from their git branches)
// ja: all versions (ja branches do not include IDE images)
// en: not needed (present in each version's git branch)
for (const pattern of ['IDEA*.png', 'ide*.png']) {
copyGlob(latestEnAssets, pattern, [
path.join(localeDirFor('zh', '3.1'), '_assets'),
...versionedBranches.map(v => path.join(localeDirFor('ja', v), '_assets')),
]);
}
// Move release note markdown files from common/ staging area to Docusaurus content dirs
const releaseNotesLocales = [
{
src: path.join(__dirname, 'common', 'releasenotes', 'en-us'),
dest: path.join(__dirname, 'releasenotes'),
},
{
src: path.join(__dirname, 'common', 'releasenotes', 'zh-cn'),
dest: path.join(__dirname, 'i18n', 'zh', 'docusaurus-plugin-content-docs-releasenotes', 'current'),
},
{
src: path.join(__dirname, 'common', 'releasenotes', 'ja'),
dest: path.join(__dirname, 'i18n', 'ja', 'docusaurus-plugin-content-docs-releasenotes', 'current'),
},
];
for (const { src, dest } of releaseNotesLocales) {
moveGlob(src, 'release-4*.md', dest);
moveGlob(src, 'release-3*.md', dest);
moveGlob(src, 'release-2.5.md', dest);
moveGlob(src, '*or.md', dest);
}
// Copy developer docs from the common/ staging area into the latest version,
// then propagate to all other versions.
const developerFiles = ['debuginfo.md', 'How_to_Contribute.md', 'jemalloc_heap_profile.md', 'versions.md'];
const developerSubdirs = ['build-starrocks', 'code-style-guides', 'development-environment', 'trace-tools'];
const devSourceByLocale = [
{ src: path.join(__dirname, 'common', 'releasenotes', 'en-us'), locale: 'en' },
{ src: path.join(__dirname, 'common', 'releasenotes', 'zh-cn'), locale: 'zh' },
{ src: path.join(__dirname, 'common', 'releasenotes', 'ja'), locale: 'ja' },
];
for (const { src, locale } of devSourceByLocale) {
const latestDevDest = path.join(localeDirFor(locale, latestVersion), 'developers');
for (const file of developerFiles) {
fse.copySync(path.join(src, file), path.join(latestDevDest, file));
}
for (const subdir of developerSubdirs) {
fse.copySync(path.join(src, subdir), path.join(latestDevDest, subdir));
}
for (const ver of otherVersions) {
fse.copySync(latestDevDest, path.join(localeDirFor(locale, ver), 'developers'), { overwrite: true });
}
}
// versions.md is specific to the latest release; remove it from older versions
for (const ver of ['3.2', '3.1']) {
for (const dir of allLocaleDirsFor(ver)) {
removeSilent(path.join(dir, 'developers', 'versions.md'));
}
}
// ── Post-copy cleanup ─────────────────────────────────────────────────────
console.log('verifying Markdown');
execSync('npx docusaurus-mdx-checker -c versioned_docs', { stdio: 'inherit' });
execSync('npx docusaurus-mdx-checker -c i18n', { stdio: 'inherit' });
// The English git checkout includes a zh/ subdir inside versioned_docs; remove it
// because Chinese content is served from i18n/zh/ instead
for (const ver of versionedBranches) {
removeSilent(path.join(__dirname, 'versioned_docs', `version-${ver}`, 'zh'));
}
console.log('removing temp files');
deleteDirIfExist(tempDir);
};
const copyDocs = () => {
const deleteExistDir = () => {
const enDir = path.join(__dirname, "versioned_docs");
const zhDir = path.join(
__dirname,
"i18n/zh/docusaurus-plugin-content-docs/"
);
const jaDir = path.join(
__dirname,
"i18n/ja/docusaurus-plugin-content-docs/"
);
if (fs.existsSync(enDir)) {
fs.rmSync(enDir, { recursive: true });
}
if (fs.existsSync(jaDir)) {
const jaFolders = fs.readdirSync(jaDir);
jaFolders.forEach((folder) => {
if (folder.startsWith("version-")) {
fs.rmSync(jaDir + "/" + folder, { recursive: true });
}
});
}
const zhFolders = fs.readdirSync(zhDir);
zhFolders.forEach((folder) => {
if (folder.startsWith("version-")) {
fs.rmSync(zhDir + "/" + folder, { recursive: true });
}
});
};
deleteExistDir();
console.log("begin iterating over locales");
locales.forEach((l) => {
const tempLocaleDir = `${tempDir}/${l.sourceDir}`;
const normalFrom = path.join(
tempLocaleDir,
("/" + l.docsPath).slice(0, -1)
);
let from = normalFrom;
console.log("begin iterating over each version");
versions.forEach((v) => {
console.log("working on locale " + l.id + " and version " + v.branch);
const targetBranch =
v.branch === "main" ? "main" : l.branchPrefix + v.branch;
let to = path.join(__dirname, `versioned_docs/version-${v.branch}`);
if (l.id === "zh-cn") {
to = path.join(
__dirname,
`i18n/zh/docusaurus-plugin-content-docs/version-${v.branch}`
);
} else if (l.id === "ja") {
to = path.join(
__dirname,
`i18n/ja/docusaurus-plugin-content-docs/version-${v.branch}`
);
}
// added `mkdir -p` because older versions do not have `docs/ja` dir, so `cd` fails
execSync(
`mkdir -p ${from} && cd ${from} && git checkout ${targetBranch}`
);
if (targetBranch === "main") {
const floderPath =
{
"en-us": enSiderbarPath,
"zh-cn": zhSiderbarPath,
ja: jaSiderbarPath,
}[l.id] || "/";
to = path.join(__dirname, floderPath);
deleteDirIfExist(to);
commonSiderBars.forEach((floder) => {
from = path.join(
tempLocaleDir,
("/" + l.docsPath).slice(0, -1) + "/" + floder
);
fse.copySync(from, to);
from = normalFrom;
});
} else {
// check for locale so we only copy the sidebars file
// once (it is same for both locales)
if (l.id === "zh-cn") {
let fromSidebarPath = path.join(
tempDir,
"docs/docusaurus/sidebars.json"
);
let toSidebarPath = path.join(
__dirname,
"versioned_sidebars/version-" + v.branch + "-sidebars.json"
);
let fromTranslationFilePath = path.join(
tempDir,
"docs/docusaurus/i18n/zh/docusaurus-plugin-content-docs/current.json"
);
let toTranslationFilePath = path.join(
__dirname,
`i18n/zh/docusaurus-plugin-content-docs/version-${v.branch}.json`
);
console.log(
"\n fromSidebarPath: ",
fromSidebarPath,
"\n toSidebarPath: ",
toSidebarPath,
"\n toTranslationFilePath: ",
toTranslationFilePath
);
fse.copySync(fromSidebarPath, toSidebarPath);
fse.copySync(fromTranslationFilePath, toTranslationFilePath);
} else if (l.id === "ja") {
let fromTranslationFilePath = path.join(
tempDir,
"docs/docusaurus/i18n/ja/docusaurus-plugin-content-docs/current.json"
);
let toTranslationFilePath = path.join(
__dirname,
`i18n/ja/docusaurus-plugin-content-docs/version-${v.branch}.json`
);
console.log("\n toTranslationFilePath: ", toTranslationFilePath);
fse.copySync(fromTranslationFilePath, toTranslationFilePath);
}
fse.copySync(from, to);
}
});
});
console.log("Starting cleanup");
cleanup();
console.log("done");
};
if (args[2] === "copy") {
copyDocs();
}