-
Notifications
You must be signed in to change notification settings - Fork 241
/
publish-docs.mts
59 lines (48 loc) · 1.82 KB
/
publish-docs.mts
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
// publishDocs.ts
import { $ } from 'zx';
import { cp, unlink } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import chalk from 'chalk';
const argv = yargs(hideBin(process.argv)).parseSync();
let currentBranch: string;
try {
// Check if the git working tree is clean
const gitStatus = await $`git status --porcelain`;
if (gitStatus.stdout.trim() !== '') {
console.error(chalk.red('Git working tree is not clean. Aborting.'));
process.exit(1);
}
// Get the current branch
currentBranch = (await $`git symbolic-ref --short HEAD`).stdout.trim();
// Run yarn build in the docs directory
console.log(chalk.blue('Building docs...'));
await $`cd packages/docs && yarn build`;
// Copy the build folder to a temp directory
const tempDir = join(tmpdir(), 'docs-build');
await cp('packages/docs/build', tempDir, { recursive: true });
// Check out the docs branch
await $`git checkout docs`;
// Delete files in the docs branch (excluding .git, CNAME, and README)
const filesToDelete = (await $`git ls-files`).stdout.trim().split('\n');
const ignoreDelete = ['.git', 'CNAME', 'README.md', '.gitignore'];
for (const file of filesToDelete.filter((file) => !ignoreDelete.includes(file))) {
await unlink(file);
}
// Copy the files from the temp directory
await cp(tempDir, '.', { recursive: true });
// Commit the changes
await $`git add .`;
await $`git commit -m "Docs publish"`;
} catch (error) {
console.error(chalk.red('An error occurred:', error.message));
} finally {
// Check out the previous branch using -f
if (currentBranch) {
await $`git checkout -f ${currentBranch}`;
await $`git reset --hard HEAD`;
}
console.log(chalk.green('Docs published successfully.'));
}