openadminjs 0.1.10 → 0.1.11
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/README.md +2 -2
- package/dist/create-project.d.ts +1 -0
- package/dist/create-project.js +174 -13
- package/dist/create-project.test.js +3 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -102,9 +102,9 @@ Generate a starter plugin:
|
|
|
102
102
|
pnpm exec openadminjs generate plugin com.example.my-plugin
|
|
103
103
|
```
|
|
104
104
|
|
|
105
|
-
###
|
|
105
|
+
### Queue setup
|
|
106
106
|
|
|
107
|
-
Set `REDIS_URL` in your environment to
|
|
107
|
+
Set `REDIS_URL` in your environment to run queue processing (example: `redis://localhost:6379`).
|
|
108
108
|
|
|
109
109
|
## Partners
|
|
110
110
|
|
package/dist/create-project.d.ts
CHANGED
package/dist/create-project.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { existsSync, mkdirSync, readdirSync } from "node:fs";
|
|
2
|
+
import net from "node:net";
|
|
2
3
|
import { dirname, extname, join, relative, resolve, sep } from "node:path";
|
|
3
4
|
import { spawnSync } from "node:child_process";
|
|
4
5
|
import { fileURLToPath } from "node:url";
|
|
@@ -19,6 +20,92 @@ export function toPackageName(input) {
|
|
|
19
20
|
export function defaultTemplateDir() {
|
|
20
21
|
return resolve(__dirname, "../template");
|
|
21
22
|
}
|
|
23
|
+
function parseUrlHostPort(input) {
|
|
24
|
+
try {
|
|
25
|
+
const url = new URL(input);
|
|
26
|
+
if (!url.hostname)
|
|
27
|
+
return undefined;
|
|
28
|
+
const port = url.port ? Number(url.port) : undefined;
|
|
29
|
+
if (port !== undefined && (!Number.isFinite(port) || port <= 0))
|
|
30
|
+
return undefined;
|
|
31
|
+
return { host: url.hostname, port: port ?? 0 };
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
async function checkTcpConnection(host, port, timeoutMs = 1500) {
|
|
38
|
+
await new Promise((resolve, reject) => {
|
|
39
|
+
const socket = net.createConnection({ host, port });
|
|
40
|
+
const onError = (err) => {
|
|
41
|
+
socket.destroy();
|
|
42
|
+
reject(err);
|
|
43
|
+
};
|
|
44
|
+
socket.setTimeout(timeoutMs, () => onError(new Error("timeout")));
|
|
45
|
+
socket.once("error", onError);
|
|
46
|
+
socket.once("connect", () => {
|
|
47
|
+
socket.end();
|
|
48
|
+
resolve();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
async function pingRedis(urlInput, timeoutMs = 1500) {
|
|
53
|
+
const parsed = parseUrlHostPort(urlInput);
|
|
54
|
+
if (!parsed)
|
|
55
|
+
throw new Error("Invalid Redis URL");
|
|
56
|
+
const port = parsed.port || 6379;
|
|
57
|
+
await new Promise((resolve, reject) => {
|
|
58
|
+
const socket = net.createConnection({ host: parsed.host, port });
|
|
59
|
+
const onError = (err) => {
|
|
60
|
+
socket.destroy();
|
|
61
|
+
reject(err);
|
|
62
|
+
};
|
|
63
|
+
socket.setTimeout(timeoutMs, () => onError(new Error("timeout")));
|
|
64
|
+
socket.once("error", onError);
|
|
65
|
+
socket.once("connect", () => {
|
|
66
|
+
socket.write("*1\r\n$4\r\nPING\r\n");
|
|
67
|
+
});
|
|
68
|
+
let buffer = "";
|
|
69
|
+
socket.on("data", (chunk) => {
|
|
70
|
+
buffer += chunk.toString("utf8");
|
|
71
|
+
if (buffer.includes("+PONG")) {
|
|
72
|
+
socket.end();
|
|
73
|
+
resolve();
|
|
74
|
+
}
|
|
75
|
+
if (buffer.includes("-ERR")) {
|
|
76
|
+
onError(new Error("redis error"));
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async function validateDbConnectivity(database, databaseUrlInput) {
|
|
82
|
+
if (database === "sqlite")
|
|
83
|
+
return undefined;
|
|
84
|
+
const parsed = parseUrlHostPort(databaseUrlInput);
|
|
85
|
+
if (!parsed)
|
|
86
|
+
return "Invalid database URL.";
|
|
87
|
+
const port = parsed.port || (database === "postgresql" ? 5432 : 3306);
|
|
88
|
+
try {
|
|
89
|
+
await checkTcpConnection(parsed.host, port);
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return `Cannot connect to ${parsed.host}:${port}.`;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
async function validateRedisConnectivity(redisUrlInput) {
|
|
97
|
+
try {
|
|
98
|
+
await pingRedis(redisUrlInput);
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
const parsed = parseUrlHostPort(redisUrlInput);
|
|
103
|
+
if (!parsed)
|
|
104
|
+
return "Invalid Redis URL.";
|
|
105
|
+
const port = parsed.port || 6379;
|
|
106
|
+
return `Cannot connect to Redis at ${parsed.host}:${port}.`;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
22
109
|
function databaseUrl(packageName, database) {
|
|
23
110
|
if (database === "postgresql") {
|
|
24
111
|
return `postgresql://openadminjs:openadminjs@localhost:5432/${packageName}?schema=public`;
|
|
@@ -51,6 +138,15 @@ function renderTemplateFiles(targetDir, replacements) {
|
|
|
51
138
|
}
|
|
52
139
|
}
|
|
53
140
|
}
|
|
141
|
+
function runPackageManagerScript(packageManager, script, cwd) {
|
|
142
|
+
const command = packageManager === "npm" ? "npm" : packageManager;
|
|
143
|
+
const args = packageManager === "npm" ? ["run", script] : [script];
|
|
144
|
+
const result = spawnSync(command, args, { cwd, stdio: "inherit" });
|
|
145
|
+
if (result.status !== 0 || result.error) {
|
|
146
|
+
const displayCommand = packageManager === "npm" ? `npm run ${script}` : `${packageManager} ${script}`;
|
|
147
|
+
throw new Error(`Failed to run "${displayCommand}" in ${cwd}.`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
54
150
|
export function createProject(options) {
|
|
55
151
|
const appName = options.projectName;
|
|
56
152
|
const packageName = toPackageName(appName);
|
|
@@ -82,6 +178,11 @@ export function createProject(options) {
|
|
|
82
178
|
__ADMIN_ORIGIN__: options.adminOrigin,
|
|
83
179
|
__API_PORT__: options.apiPort
|
|
84
180
|
});
|
|
181
|
+
const envExamplePath = join(targetDir, ".env.example");
|
|
182
|
+
const envPath = join(targetDir, ".env");
|
|
183
|
+
if (existsSync(envExamplePath) && !existsSync(envPath)) {
|
|
184
|
+
writeFileSync(envPath, readFileSync(envExamplePath, "utf8"));
|
|
185
|
+
}
|
|
85
186
|
// Write the real .env for apps/api so the app starts and seed runs without
|
|
86
187
|
// manual intervention. Superadmin credentials are picked up by prisma/seed.ts
|
|
87
188
|
// via the SUPERADMIN_EMAIL / SUPERADMIN_PASSWORD env vars.
|
|
@@ -111,6 +212,8 @@ export function createProject(options) {
|
|
|
111
212
|
if (installResult.status !== 0 || installResult.error) {
|
|
112
213
|
throw new Error(`Dependency installation failed. Run "${options.packageManager} ${installArgs.join(" ")}" inside ${targetDir}.`);
|
|
113
214
|
}
|
|
215
|
+
runPackageManagerScript(options.packageManager, "db:migrate", targetDir);
|
|
216
|
+
runPackageManagerScript(options.packageManager, "db:seed", targetDir);
|
|
114
217
|
}
|
|
115
218
|
return {
|
|
116
219
|
appName,
|
|
@@ -120,13 +223,15 @@ export function createProject(options) {
|
|
|
120
223
|
database: options.database,
|
|
121
224
|
superadminEmail: options.superadminEmail,
|
|
122
225
|
git: options.git,
|
|
123
|
-
install: options.install
|
|
226
|
+
install: options.install,
|
|
227
|
+
dbInitialized: options.install
|
|
124
228
|
};
|
|
125
229
|
}
|
|
126
230
|
export function printNextSteps(result) {
|
|
127
231
|
const pm = result.packageManager;
|
|
128
|
-
const installStep = result.install ? "" : `\n ${pm} install`;
|
|
129
|
-
|
|
232
|
+
const installStep = result.install ? "" : `\n ${pm} install\n ${pm} db:migrate\n ${pm} db:seed`;
|
|
233
|
+
const dbStepNote = result.dbInitialized ? "\nDatabase initialized: migrations + seed completed automatically." : "";
|
|
234
|
+
outro(`${pc.green("Project created.")}\n\nNext steps:\n cd ${result.appName}${installStep}\n ${pm} dev${dbStepNote}\n\nAdmin: http://localhost:3000\nAPI: http://localhost:4000\nSwagger: http://localhost:4000/api/docs\nWeb: http://localhost:3001\n\nSuperadmin: ${result.superadminEmail} / <your password>`);
|
|
130
235
|
}
|
|
131
236
|
export async function createProjectInteractive(options = {}) {
|
|
132
237
|
intro(pc.green("Create OpenAdminJS"));
|
|
@@ -202,14 +307,70 @@ export async function createProjectInteractive(options = {}) {
|
|
|
202
307
|
cancel("Cancelled");
|
|
203
308
|
return undefined;
|
|
204
309
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
310
|
+
let checkedDatabaseUrl = String(selectedDatabaseUrl).trim();
|
|
311
|
+
while (true) {
|
|
312
|
+
const dbError = await validateDbConnectivity(database, checkedDatabaseUrl);
|
|
313
|
+
if (!dbError)
|
|
314
|
+
break;
|
|
315
|
+
const retry = await confirm({
|
|
316
|
+
message: `${dbError} Retry entering DATABASE_URL?`,
|
|
317
|
+
initialValue: true
|
|
318
|
+
});
|
|
319
|
+
if (isCancel(retry)) {
|
|
320
|
+
cancel("Cancelled");
|
|
321
|
+
return undefined;
|
|
322
|
+
}
|
|
323
|
+
if (!retry)
|
|
324
|
+
break;
|
|
325
|
+
const next = await text({
|
|
326
|
+
message: "Database URL",
|
|
327
|
+
defaultValue: checkedDatabaseUrl
|
|
328
|
+
});
|
|
329
|
+
if (isCancel(next)) {
|
|
330
|
+
cancel("Cancelled");
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
checkedDatabaseUrl = String(next).trim();
|
|
334
|
+
}
|
|
335
|
+
let checkedRedisUrl = (options.redisUrl ?? "").trim();
|
|
336
|
+
while (!checkedRedisUrl) {
|
|
337
|
+
const entered = options.redisUrl ??
|
|
338
|
+
(await text({
|
|
339
|
+
message: "Redis URL (required)",
|
|
340
|
+
defaultValue: "redis://localhost:6379",
|
|
341
|
+
validate(value) {
|
|
342
|
+
return value.trim().length > 0 ? undefined : "Redis URL is required.";
|
|
343
|
+
}
|
|
344
|
+
}));
|
|
345
|
+
if (isCancel(entered)) {
|
|
346
|
+
cancel("Cancelled");
|
|
347
|
+
return undefined;
|
|
348
|
+
}
|
|
349
|
+
checkedRedisUrl = String(entered).trim();
|
|
350
|
+
}
|
|
351
|
+
while (true) {
|
|
352
|
+
const redisError = await validateRedisConnectivity(checkedRedisUrl);
|
|
353
|
+
if (!redisError)
|
|
354
|
+
break;
|
|
355
|
+
const retry = await confirm({
|
|
356
|
+
message: `${redisError} Retry entering REDIS_URL?`,
|
|
357
|
+
initialValue: true
|
|
358
|
+
});
|
|
359
|
+
if (isCancel(retry)) {
|
|
360
|
+
cancel("Cancelled");
|
|
361
|
+
return undefined;
|
|
362
|
+
}
|
|
363
|
+
if (!retry)
|
|
364
|
+
break;
|
|
365
|
+
const next = await text({
|
|
366
|
+
message: "Redis URL (required)",
|
|
367
|
+
defaultValue: checkedRedisUrl
|
|
368
|
+
});
|
|
369
|
+
if (isCancel(next)) {
|
|
370
|
+
cancel("Cancelled");
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
checkedRedisUrl = String(next).trim();
|
|
213
374
|
}
|
|
214
375
|
const jwtSecret = options.jwtSecret ??
|
|
215
376
|
(await text({
|
|
@@ -246,8 +407,8 @@ export async function createProjectInteractive(options = {}) {
|
|
|
246
407
|
database,
|
|
247
408
|
superadminEmail: String(superadminEmail),
|
|
248
409
|
superadminPassword: String(superadminPassword),
|
|
249
|
-
databaseUrl:
|
|
250
|
-
redisUrl:
|
|
410
|
+
databaseUrl: checkedDatabaseUrl,
|
|
411
|
+
redisUrl: checkedRedisUrl,
|
|
251
412
|
jwtSecret: String(jwtSecret),
|
|
252
413
|
jwtRefreshSecret: String(jwtRefreshSecret),
|
|
253
414
|
adminOrigin: String(adminOrigin),
|
|
@@ -29,12 +29,15 @@ describe("create project", () => {
|
|
|
29
29
|
try {
|
|
30
30
|
const result = createProject({ ...BASE_OPTIONS, projectName: "my-app", cwd, templateDir: defaultTemplateDir() });
|
|
31
31
|
const envExample = readFileSync(join(result.targetDir, ".env.example"), "utf8");
|
|
32
|
+
const env = readFileSync(join(result.targetDir, ".env"), "utf8");
|
|
32
33
|
expect(envExample).toContain("DATABASE_URL=postgresql://localhost:5432/my-app?schema=public");
|
|
33
34
|
expect(envExample).toContain("REDIS_URL=redis://localhost:6379");
|
|
34
35
|
expect(envExample).toContain("JWT_SECRET=test-jwt-secret");
|
|
35
36
|
expect(envExample).toContain("JWT_REFRESH_SECRET=test-jwt-refresh-secret");
|
|
36
37
|
expect(envExample).toContain("ADMIN_ORIGIN=http://localhost:3000");
|
|
37
38
|
expect(envExample).toContain("API_PORT=4000");
|
|
39
|
+
expect(env).toBe(envExample);
|
|
40
|
+
expect(existsSync(join(result.targetDir, "tsconfig.base.json"))).toBe(true);
|
|
38
41
|
}
|
|
39
42
|
finally {
|
|
40
43
|
rmSync(cwd, { recursive: true, force: true });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "openadminjs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"bin": {
|
|
6
6
|
"openadminjs": "dist/index.js"
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
],
|
|
28
28
|
"scripts": {
|
|
29
29
|
"sync-template": "node ../../scripts/sync-template.mjs",
|
|
30
|
-
"prepack": "
|
|
30
|
+
"prepack": "pnpm run build",
|
|
31
31
|
"build": "node ../../scripts/sync-template.mjs && tsc -p tsconfig.json && node ../../scripts/make-executable.mjs dist/index.js",
|
|
32
32
|
"lint": "tsc -p tsconfig.json --noEmit",
|
|
33
33
|
"typecheck": "tsc -p tsconfig.json --noEmit",
|