forked from rstackjs/rstack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-release.js
More file actions
80 lines (64 loc) · 2.57 KB
/
Copy pathprepare-release.js
File metadata and controls
80 lines (64 loc) · 2.57 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
#!/usr/bin/env node
import { spawn } from 'node:child_process';
import { copyFile, mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises';
import path from 'node:path';
const rootDir = path.resolve(import.meta.dirname, '..');
const websiteDir = path.join(rootDir, 'website');
const websiteDistDir = path.join(websiteDir, 'doc_build');
const packageDocsDir = path.join(rootDir, 'packages/rstack/docs');
const run = (command, args) =>
new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd: rootDir,
env: {
...process.env,
RSPRESS_INJECT_LLMS_HINT: 'false',
},
stdio: 'inherit',
});
child.on('error', reject);
child.on('exit', (code, signal) => {
if (code === 0) {
resolve();
return;
}
const reason = signal ? `signal ${signal}` : `exit code ${code}`;
reject(new Error(`${command} ${args.join(' ')} failed with ${reason}.`));
});
});
const collectMarkdownFiles = async (directory, relativeDir = '') => {
const entries = await readdir(directory, { withFileTypes: true });
const files = [];
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
if (entry.isDirectory() && entry.name === 'zh') {
continue;
}
const relativePath = path.join(relativeDir, entry.name);
const absolutePath = path.join(directory, entry.name);
if (entry.isDirectory()) {
files.push(...(await collectMarkdownFiles(absolutePath, relativePath)));
} else if (entry.isFile() && path.extname(entry.name) === '.md') {
files.push(relativePath);
}
}
return files;
};
const pnpmCommand = process.platform === 'win32' ? 'pnpm.cmd' : 'pnpm';
console.log('Building the website...');
await run(pnpmCommand, ['--dir', websiteDir, 'build']);
const markdownFiles = await collectMarkdownFiles(websiteDistDir);
if (markdownFiles.length === 0) {
throw new Error(`No English Markdown files found in ${websiteDistDir}.`);
}
await rm(packageDocsDir, { recursive: true, force: true });
for (const relativePath of markdownFiles) {
const destination = path.join(packageDocsDir, relativePath);
await mkdir(path.dirname(destination), { recursive: true });
await copyFile(path.join(websiteDistDir, relativePath), destination);
}
const llmsTxt = await readFile(path.join(websiteDistDir, 'llms.txt'), 'utf8');
const packageLlmsTxt = llmsTxt.replace(/\]\(\/(?!\/)/g, '](./');
await writeFile(path.join(packageDocsDir, 'llms.txt'), packageLlmsTxt);
console.log(
`Copied ${markdownFiles.length} English Markdown files and llms.txt to ${packageDocsDir}.`,
);