Skip to content

Commit ef440d9

Browse files
committed
ci
1 parent 014a2eb commit ef440d9

File tree

5 files changed

+219
-33
lines changed

5 files changed

+219
-33
lines changed

.github/workflows/deploy.yml

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,10 @@ jobs:
3232
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY}.tar.gz -O - |\
3333
tar xz && mv ${BINARY} /usr/local/bin/yq
3434
35-
- name: Deploy Production Version
35+
- name: Deploy
3636
env:
3737
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
3838
run: |
3939
# build application
40-
yq -i '.d1_databases[0].database_id = "${{ secrets.CLOUDFLARE_DATABASE_ID }}"' wrangler.toml
41-
yq -i '.vars.MODE = "mixed"' wrangler.toml
42-
yq -i '.vars.PROVIDER_CLOUDFLARE_BUILTIN = false' wrangler.toml
43-
pnpm build
44-
45-
# build website
46-
cd docs
47-
pnpm install
48-
pnpm build
49-
cp -r ./out/ ../dist/home
50-
cd ..
51-
52-
# deploy to cloudflare
53-
pnpm deploy
54-
55-
- name: Deploy Preview Version
56-
run: |
57-
yq -i '.name = "typix-preview"' wrangler.toml
58-
yq -i '.vars.MODE = "client"' wrangler.toml
59-
yq -i '.vars.PROVIDER_CLOUDFLARE_BUILTIN = true' wrangler.toml
60-
pnpm build
61-
pnpm deploy
40+
sed -i "s/DATABASE_ID/${{ secrets.CLOUDFLARE_DATABASE_ID }}/g" wrangler.toml
41+
node scripts/deploy.js --preview --production

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dev:worker": "wrangler dev",
99
"build": "tsc -b && cross-env RUNTIME=cloudflare vite build",
1010
"build:node": "tsc -b && vite build && tsup --minify",
11+
"build:docs": "cd ./docs && pnpm build && cp -r ./out/ ../dist/home && cd ..",
1112
"analyze": "vite build --mode analyze",
1213
"lint": "eslint .",
1314
"preview": "vite preview",
@@ -72,7 +73,6 @@
7273
"@biomejs/biome": "^1.9.4",
7374
"@cloudflare/workers-types": "^4.20250525.0",
7475
"@hono/vite-dev-server": "^0.19.1",
75-
"@iarna/toml": "^2.2.5",
7676
"@tanstack/router-plugin": "^1.120.10",
7777
"@types/node": "^22.15.21",
7878
"@types/react": "^19.1.2",
@@ -81,6 +81,7 @@
8181
"cross-env": "^7.0.3",
8282
"drizzle-kit": "^0.31.1",
8383
"globals": "^16.0.0",
84+
"smol-toml": "^1.4.1",
8485
"tsup": "^8.5.0",
8586
"tw-animate-css": "^1.3.0",
8687
"typescript": "~5.8.3",

pnpm-lock.yaml

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/deploy.js

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
// @ts-nocheck
2+
import { execSync } from "node:child_process";
3+
import fs from "node:fs";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
import { parseArgs } from "node:util";
7+
import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
8+
9+
const __filename = fileURLToPath(import.meta.url);
10+
const __dirname = path.dirname(__filename);
11+
12+
const config = {
13+
preview: {
14+
docs: false,
15+
override: {
16+
name: "typix-preview",
17+
vars: {
18+
MODE: "client",
19+
PROVIDER_CLOUDFLARE_BUILTIN: true,
20+
},
21+
},
22+
},
23+
production: {
24+
docs: true,
25+
override: {
26+
vars: {
27+
MODE: "mixed",
28+
PROVIDER_CLOUDFLARE_BUILTIN: false,
29+
},
30+
},
31+
},
32+
};
33+
34+
/**
35+
* Read wrangler.toml file
36+
* @returns {any} Parsed TOML configuration object
37+
*/
38+
function readWranglerConfig() {
39+
const wranglerPath = path.join(__dirname, "..", "wrangler.toml");
40+
const content = fs.readFileSync(wranglerPath, "utf8");
41+
return parseToml(content);
42+
}
43+
44+
/**
45+
* Write wrangler.toml file
46+
* @param {any} config - Configuration object
47+
*/
48+
function writeWranglerConfig(config) {
49+
const wranglerPath = path.join(__dirname, "..", "wrangler.toml");
50+
const content = stringifyToml(config);
51+
fs.writeFileSync(wranglerPath, content, "utf8");
52+
}
53+
54+
/**
55+
* Deep merge objects
56+
* @param {any} target - Target object
57+
* @param {any} source - Source object
58+
* @returns {any} Merged object
59+
*/
60+
function deepMerge(target, source) {
61+
const result = { ...target };
62+
63+
for (const key in source) {
64+
if (
65+
source[key] &&
66+
typeof source[key] === "object" &&
67+
!Array.isArray(source[key])
68+
) {
69+
result[key] = deepMerge(result[key] || {}, source[key]);
70+
} else {
71+
result[key] = source[key];
72+
}
73+
}
74+
75+
return result;
76+
}
77+
78+
/**
79+
* Update wrangler configuration
80+
* @param {any} override - Configuration override for the environment
81+
* @param {any} baseConfig - Base configuration copy
82+
*/
83+
function updateWranglerConfig(override, baseConfig) {
84+
const updatedConfig = deepMerge(baseConfig, override);
85+
writeWranglerConfig(updatedConfig);
86+
}
87+
88+
/**
89+
* Build and deploy
90+
*/
91+
function buildAndDeploy(docs) {
92+
console.log("📦 Building application...");
93+
execSync("pnpm build", { stdio: "inherit" });
94+
console.log("✅ Application built successfully");
95+
96+
if (docs) {
97+
console.log("📚 Building documentation...");
98+
execSync("pnpm build:docs", { stdio: "inherit" });
99+
console.log("✅ Documentation built successfully");
100+
101+
console.log("📦 Documentation copied to dist/home successfully");
102+
}
103+
104+
console.log("🚀 Deploying to Cloudflare...");
105+
execSync("pnpm deploy", { stdio: "inherit" });
106+
console.log("✅ Deployed successfully");
107+
}
108+
109+
/**
110+
* Deploy multiple environments
111+
* @param {string[]} environments - Environment list
112+
*/
113+
function deployEnvironments(environments) {
114+
console.log(
115+
`🔄 Starting deployment for environments: ${environments.join(", ")}`
116+
);
117+
118+
// Read original configuration as a copy
119+
const baseConfig = readWranglerConfig();
120+
console.log("📋 Base configuration loaded");
121+
122+
for (const environment of environments) {
123+
console.log(`\n--- Deploying ${environment} environment ---`);
124+
const envConfig = config[environment];
125+
try {
126+
updateWranglerConfig(envConfig.override, baseConfig);
127+
console.log(
128+
`✅ ${environment} environment configuration updated successfully`
129+
);
130+
131+
// Build and deploy
132+
buildAndDeploy(envConfig.docs);
133+
} catch (error) {
134+
console.error(
135+
`❌ Failed to deploy ${environment} environment:`,
136+
error instanceof Error ? error.message : String(error)
137+
);
138+
process.exit(1);
139+
}
140+
}
141+
142+
console.log(
143+
`\n🎉 All environments (${environments.join(", ")}) deployed successfully!`
144+
);
145+
}
146+
147+
// Command line interface
148+
function main() {
149+
try {
150+
const { values } = parseArgs({
151+
options: {
152+
preview: {
153+
type: "boolean",
154+
short: "p",
155+
default: false,
156+
},
157+
production: {
158+
type: "boolean",
159+
short: "P",
160+
default: false,
161+
},
162+
help: {
163+
type: "boolean",
164+
short: "h",
165+
default: false,
166+
},
167+
},
168+
allowPositionals: false,
169+
});
170+
171+
const environments = [];
172+
173+
if (values.preview) {
174+
environments.push("preview");
175+
}
176+
177+
if (values.production) {
178+
environments.push("production");
179+
}
180+
181+
if (environments.length === 0) {
182+
environments.push("production");
183+
}
184+
185+
for (const env of environments) {
186+
if (!(env in config)) {
187+
console.error(`❌ Unknown environment: ${env}`);
188+
console.log("Available environments: production, preview");
189+
process.exit(1);
190+
}
191+
}
192+
193+
deployEnvironments(environments);
194+
} catch (error) {
195+
console.error(
196+
"❌ Error parsing arguments:",
197+
error instanceof Error ? error.message : String(error)
198+
);
199+
console.log("Use --help for usage information");
200+
process.exit(1);
201+
}
202+
}
203+
204+
main();

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import devServer from "@hono/vite-dev-server";
4-
import * as TOML from "@iarna/toml";
54
import tailwindcss from "@tailwindcss/vite";
65
import { TanStackRouterVite } from "@tanstack/router-plugin/vite";
76
import react from "@vitejs/plugin-react-swc";
7+
import TOML from "smol-toml";
88
import { defineConfig } from "vite";
99
import { analyzer } from "vite-bundle-analyzer";
1010

0 commit comments

Comments
 (0)