A design system in a box.
hip-ui.tngl.io/docs/introduction
1import { execFileSync } from "node:child_process";
2import { cp, mkdtemp, readdir, rm } from "node:fs/promises";
3import os from "node:os";
4import path from "node:path";
5import process from "node:process";
6
7const branch = process.env.DOCS_BRANCH ?? "docs";
8const remote = process.env.DOCS_REMOTE ?? "origin";
9const sourceDir = path.resolve(
10 process.cwd(),
11 process.env.DOCS_DIST_DIR ?? "apps/docs/dist/client",
12);
13
14function getDeployRemoteUrl(remoteUrl: string) {
15 if (process.env.DOCS_PUSH_REMOTE_URL) {
16 return process.env.DOCS_PUSH_REMOTE_URL;
17 }
18
19 if (!URL.canParse(remoteUrl)) {
20 return remoteUrl;
21 }
22
23 const url = new URL(remoteUrl);
24
25 if (url.protocol !== "http:" && url.protocol !== "https:") {
26 return remoteUrl;
27 }
28
29 const pathname = url.pathname.replace(/\/+$/, "");
30 return `ssh://git@${url.host}${pathname}`;
31}
32
33function run(command: string, args: Array<string>, cwd: string) {
34 execFileSync(command, args, {
35 cwd,
36 stdio: "inherit",
37 });
38}
39
40async function main() {
41 const tempDir = await mkdtemp(path.join(os.tmpdir(), "hip-ui-docs-"));
42 const publishDir = path.join(tempDir, "site");
43 const distEntries = await readdir(sourceDir).catch(() => {});
44
45 try {
46 if (!distEntries || distEntries.length === 0) {
47 throw new Error(
48 `Docs output not found at "${sourceDir}". Run the docs build before publishing.`,
49 );
50 }
51
52 const remoteUrl = execFileSync("git", ["remote", "get-url", remote], {
53 cwd: process.cwd(),
54 encoding: "utf8",
55 }).trim();
56 const deployRemoteUrl = getDeployRemoteUrl(remoteUrl);
57 const commitSha = execFileSync("git", ["rev-parse", "--short", "HEAD"], {
58 cwd: process.cwd(),
59 encoding: "utf8",
60 }).trim();
61
62 await cp(sourceDir, publishDir, { recursive: true });
63
64 run("git", ["init"], publishDir);
65 run("git", ["checkout", "--orphan", branch], publishDir);
66 run("git", ["add", "--all"], publishDir);
67 run(
68 "git",
69 [
70 "-c",
71 "user.name=Cursor",
72 "-c",
73 "user.email=cursor@example.com",
74 "commit",
75 "-m",
76 `Publish docs from ${commitSha}`,
77 ],
78 publishDir,
79 );
80 run("git", ["remote", "add", "deploy", deployRemoteUrl], publishDir);
81 run("git", ["push", "--force", "deploy", `HEAD:${branch}`], publishDir);
82 } finally {
83 await rm(tempDir, { recursive: true, force: true });
84 }
85}
86
87// oxlint-disable-next-line unicorn/prefer-top-level-await
88main().catch((error: unknown) => {
89 console.error(error);
90 process.exitCode = 1;
91});