@webhikers/cli 1.0.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/commands/create.js +121 -6
package/package.json
CHANGED
package/src/commands/create.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { execSync } from "child_process";
|
|
2
|
-
import { existsSync, writeFileSync } from "fs";
|
|
2
|
+
import { existsSync, writeFileSync, readFileSync } from "fs";
|
|
3
3
|
import { resolve } from "path";
|
|
4
4
|
import { loadConfig, getConfigPath } from "../utils/config-store.js";
|
|
5
5
|
|
|
6
6
|
const TEMPLATE_REPO = "Webhikers/nextjs-vibe-starter";
|
|
7
7
|
const GITHUB_ORG = "Webhikers";
|
|
8
|
+
const DOMAIN_SUFFIX = "preview.webhikers.dev";
|
|
8
9
|
|
|
9
10
|
function run(cmd, options = {}) {
|
|
10
11
|
console.log(` → ${cmd}`);
|
|
@@ -15,6 +16,28 @@ function runCapture(cmd) {
|
|
|
15
16
|
return execSync(cmd, { encoding: "utf-8" }).trim();
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
async function coolifyApi(config, method, path, body) {
|
|
20
|
+
const url = `${config.coolifyUrl}/api/v1${path}`;
|
|
21
|
+
const options = {
|
|
22
|
+
method,
|
|
23
|
+
headers: {
|
|
24
|
+
Authorization: `Bearer ${config.coolifyToken}`,
|
|
25
|
+
"Content-Type": "application/json",
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
if (body) options.body = JSON.stringify(body);
|
|
29
|
+
|
|
30
|
+
const res = await fetch(url, options);
|
|
31
|
+
const data = await res.json();
|
|
32
|
+
|
|
33
|
+
if (!res.ok) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Coolify API error (${res.status}): ${JSON.stringify(data)}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return data;
|
|
39
|
+
}
|
|
40
|
+
|
|
18
41
|
export async function createCommand(name) {
|
|
19
42
|
console.log(`\nCreating project: ${name}\n`);
|
|
20
43
|
|
|
@@ -44,10 +67,33 @@ export async function createCommand(name) {
|
|
|
44
67
|
|
|
45
68
|
// --- 2. Create GitHub repo from template ---
|
|
46
69
|
console.log("Creating GitHub repo from template...");
|
|
70
|
+
|
|
71
|
+
// Create repo first (without --clone, to avoid race condition)
|
|
47
72
|
run(
|
|
48
|
-
`gh repo create ${GITHUB_ORG}/${name} --template ${TEMPLATE_REPO} --private
|
|
73
|
+
`gh repo create ${GITHUB_ORG}/${name} --template ${TEMPLATE_REPO} --private`
|
|
49
74
|
);
|
|
50
75
|
|
|
76
|
+
// Wait for GitHub to finish creating the repo from template
|
|
77
|
+
console.log(" Waiting for GitHub to prepare the repository...");
|
|
78
|
+
const maxRetries = 12;
|
|
79
|
+
let cloned = false;
|
|
80
|
+
for (let i = 0; i < maxRetries; i++) {
|
|
81
|
+
await new Promise((r) => setTimeout(r, 5000));
|
|
82
|
+
try {
|
|
83
|
+
runCapture(`gh repo clone ${GITHUB_ORG}/${name} ${name}`);
|
|
84
|
+
cloned = true;
|
|
85
|
+
break;
|
|
86
|
+
} catch {
|
|
87
|
+
console.log(` Retrying clone... (${i + 1}/${maxRetries})`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!cloned) {
|
|
92
|
+
console.error("Error: Could not clone repo after 60 seconds.");
|
|
93
|
+
console.error(`Try manually: gh repo clone ${GITHUB_ORG}/${name}`);
|
|
94
|
+
process.exit(1);
|
|
95
|
+
}
|
|
96
|
+
|
|
51
97
|
const projectDir = resolve(process.cwd(), name);
|
|
52
98
|
if (!existsSync(projectDir)) {
|
|
53
99
|
console.error(`Error: Expected directory ${projectDir} not found.`);
|
|
@@ -61,6 +107,8 @@ export async function createCommand(name) {
|
|
|
61
107
|
// --- 4. Generate .env ---
|
|
62
108
|
console.log("\nGenerating .env...");
|
|
63
109
|
const payloadSecret = runCapture("openssl rand -hex 32");
|
|
110
|
+
const domain = `${name}.${DOMAIN_SUFFIX}`;
|
|
111
|
+
const siteUrl = `https://${domain}`;
|
|
64
112
|
const envContent = [
|
|
65
113
|
`PAYLOAD_SECRET=${payloadSecret}`,
|
|
66
114
|
`SITE_URL=http://localhost:3000`,
|
|
@@ -70,18 +118,85 @@ export async function createCommand(name) {
|
|
|
70
118
|
writeFileSync(resolve(projectDir, ".env"), envContent, "utf-8");
|
|
71
119
|
console.log(" .env written (PAYLOAD_SECRET + SITE_URL)");
|
|
72
120
|
|
|
73
|
-
// --- 5.
|
|
121
|
+
// --- 5. Get git remote URL ---
|
|
122
|
+
let gitRepo;
|
|
123
|
+
try {
|
|
124
|
+
gitRepo = runCapture("git remote get-url origin", { cwd: projectDir });
|
|
125
|
+
} catch {
|
|
126
|
+
console.error("Error: No git remote 'origin' found.");
|
|
127
|
+
process.exit(1);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// --- 6. Setup Coolify deployment ---
|
|
74
131
|
console.log("\nSetting up Coolify deployment...");
|
|
75
|
-
|
|
132
|
+
console.log(` Domain: ${domain}`);
|
|
133
|
+
console.log(` Git repo: ${gitRepo}`);
|
|
134
|
+
|
|
135
|
+
// Create project
|
|
136
|
+
console.log(" Creating Coolify project...");
|
|
137
|
+
const project = await coolifyApi(config, "POST", "/projects", {
|
|
138
|
+
name,
|
|
139
|
+
description: `${name} — deployed via webhikers CLI`,
|
|
140
|
+
});
|
|
141
|
+
const projectUuid = project.uuid;
|
|
142
|
+
console.log(` Project created: ${projectUuid}`);
|
|
143
|
+
|
|
144
|
+
// Create application
|
|
145
|
+
console.log(" Creating application...");
|
|
146
|
+
const app = await coolifyApi(config, "POST", "/applications/public", {
|
|
147
|
+
project_uuid: projectUuid,
|
|
148
|
+
server_uuid: config.serverUuid,
|
|
149
|
+
environment_name: "production",
|
|
150
|
+
git_repository: gitRepo,
|
|
151
|
+
git_branch: "master",
|
|
152
|
+
build_pack: "dockerfile",
|
|
153
|
+
ports_exposes: "3000",
|
|
154
|
+
instant_deploy: false,
|
|
155
|
+
});
|
|
156
|
+
const appUuid = app.uuid;
|
|
157
|
+
console.log(` Application created: ${appUuid}`);
|
|
158
|
+
|
|
159
|
+
// Set domain
|
|
160
|
+
console.log(` Setting domain to ${domain}...`);
|
|
161
|
+
await coolifyApi(config, "PATCH", `/applications/${appUuid}`, {
|
|
162
|
+
domains: `https://${domain}`,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Set environment variables
|
|
166
|
+
console.log(" Setting environment variables...");
|
|
167
|
+
await coolifyApi(config, "POST", `/applications/${appUuid}/envs`, {
|
|
168
|
+
key: "PAYLOAD_SECRET",
|
|
169
|
+
value: payloadSecret,
|
|
170
|
+
is_build_time: true,
|
|
171
|
+
});
|
|
172
|
+
await coolifyApi(config, "POST", `/applications/${appUuid}/envs`, {
|
|
173
|
+
key: "SITE_URL",
|
|
174
|
+
value: siteUrl,
|
|
175
|
+
is_build_time: true,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
// --- 7. Write .deploy.json ---
|
|
179
|
+
const deployConfig = {
|
|
180
|
+
serverIp: config.serverIp,
|
|
181
|
+
domain,
|
|
182
|
+
coolifyProjectUuid: projectUuid,
|
|
183
|
+
coolifyAppUuid: appUuid,
|
|
184
|
+
};
|
|
185
|
+
writeFileSync(
|
|
186
|
+
resolve(projectDir, ".deploy.json"),
|
|
187
|
+
JSON.stringify(deployConfig, null, 2) + "\n",
|
|
188
|
+
"utf-8"
|
|
189
|
+
);
|
|
190
|
+
console.log(" .deploy.json written");
|
|
76
191
|
|
|
77
|
-
// ---
|
|
192
|
+
// --- 8. Summary ---
|
|
78
193
|
console.log("");
|
|
79
194
|
console.log("=========================================");
|
|
80
195
|
console.log(" Project created successfully!");
|
|
81
196
|
console.log("=========================================");
|
|
82
197
|
console.log("");
|
|
83
198
|
console.log(` Repo: https://github.com/${GITHUB_ORG}/${name}`);
|
|
84
|
-
console.log(` Domain: https://${
|
|
199
|
+
console.log(` Domain: https://${domain}`);
|
|
85
200
|
console.log(` Dir: ${projectDir}`);
|
|
86
201
|
console.log("");
|
|
87
202
|
console.log(" MANUAL STEP:");
|