openadminjs 0.1.24 → 0.2.0
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 +97 -16
- package/dist/adapt-package-manager.d.ts +3 -0
- package/dist/adapt-package-manager.js +115 -0
- package/dist/adapt-package-manager.test.d.ts +1 -0
- package/dist/adapt-package-manager.test.js +76 -0
- package/dist/cli.e2e.test.d.ts +1 -0
- package/dist/cli.e2e.test.js +87 -0
- package/dist/create-project.d.ts +31 -2
- package/dist/create-project.js +296 -102
- package/dist/create-project.test.js +180 -4
- package/dist/database-providers.integration.test.d.ts +1 -0
- package/dist/database-providers.integration.test.js +64 -0
- package/dist/db-commands.d.ts +20 -0
- package/dist/db-commands.js +75 -0
- package/dist/db-commands.test.d.ts +1 -0
- package/dist/db-commands.test.js +27 -0
- package/dist/detect-package-manager.d.ts +8 -0
- package/dist/detect-package-manager.js +37 -0
- package/dist/doctor.d.ts +13 -0
- package/dist/doctor.js +203 -0
- package/dist/doctor.test.d.ts +1 -0
- package/dist/doctor.test.js +102 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +233 -141
- package/dist/index.test.js +7 -3
- package/dist/render-schema.d.ts +20 -0
- package/dist/render-schema.js +37 -0
- package/dist/render-schema.test.d.ts +1 -0
- package/dist/render-schema.test.js +57 -0
- package/dist/secrets.d.ts +24 -0
- package/dist/secrets.js +93 -0
- package/dist/secrets.test.d.ts +1 -0
- package/dist/secrets.test.js +36 -0
- package/dist/security.d.ts +13 -0
- package/dist/security.js +168 -0
- package/dist/security.test.d.ts +1 -0
- package/dist/security.test.js +54 -0
- package/dist/validate-name.d.ts +33 -0
- package/dist/validate-name.js +206 -0
- package/dist/validate-name.test.d.ts +1 -0
- package/dist/validate-name.test.js +63 -0
- package/package.json +26 -1
- package/template/README.md +22 -3
- package/template/apps/api/package.json +3 -0
- package/template/apps/api/src/common/json-field-codec.test.ts +76 -0
- package/template/apps/api/src/common/json-field-codec.ts +139 -0
- package/template/apps/api/src/common/prisma.service.test.ts +19 -0
- package/template/apps/api/src/common/prisma.service.ts +13 -0
- package/template/apps/api/src/graphql/graphql-apollo-config.test.ts +26 -0
- package/template/apps/api/src/graphql/graphql.module.ts +13 -1
- package/template/package.json +2 -0
- package/template/prisma/migrations/20260713120000_add_user_oidc_key_and_category_order/migration.sql +11 -0
|
@@ -1,8 +1,21 @@
|
|
|
1
|
-
import { existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { existsSync, mkdtempSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
2
3
|
import { join } from "node:path";
|
|
3
4
|
import { tmpdir } from "node:os";
|
|
4
5
|
import { describe, expect, it } from "vitest";
|
|
5
|
-
import { createProject, defaultTemplateDir } from "./create-project.js";
|
|
6
|
+
import { createProject, defaultTemplateDir, DEFAULT_SUPERADMIN_EMAIL, resolveSuperadminEmail, validateSuperadminEmailInput } from "./create-project.js";
|
|
7
|
+
describe("superadmin email default (press Enter)", () => {
|
|
8
|
+
it("accepts an empty input by resolving to the default", () => {
|
|
9
|
+
// Simulates pressing Enter without typing an email.
|
|
10
|
+
expect(validateSuperadminEmailInput("")).toBeUndefined();
|
|
11
|
+
expect(resolveSuperadminEmail("")).toBe(DEFAULT_SUPERADMIN_EMAIL);
|
|
12
|
+
});
|
|
13
|
+
it("validates the final value, not the raw input", () => {
|
|
14
|
+
expect(validateSuperadminEmailInput("not-an-email")).toMatch(/valid email/);
|
|
15
|
+
expect(validateSuperadminEmailInput("me@example.com")).toBeUndefined();
|
|
16
|
+
expect(resolveSuperadminEmail(" me@example.com ")).toBe("me@example.com");
|
|
17
|
+
});
|
|
18
|
+
});
|
|
6
19
|
const BASE_OPTIONS = {
|
|
7
20
|
packageManager: "pnpm",
|
|
8
21
|
database: "postgresql",
|
|
@@ -24,18 +37,119 @@ describe("create project", () => {
|
|
|
24
37
|
expect(envExample, `missing placeholder ${key}`).toContain(key);
|
|
25
38
|
}
|
|
26
39
|
});
|
|
27
|
-
it("
|
|
40
|
+
it("defaults templateDir when omitted", () => {
|
|
41
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
42
|
+
try {
|
|
43
|
+
const result = createProject({ ...BASE_OPTIONS, projectName: "default-template", cwd });
|
|
44
|
+
expect(existsSync(join(result.targetDir, "package.json"))).toBe(true);
|
|
45
|
+
expect(existsSync(join(result.targetDir, "pnpm-workspace.yaml"))).toBe(true);
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
it("adapts npm projects when packageManager is npm", () => {
|
|
52
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
53
|
+
try {
|
|
54
|
+
const result = createProject({ ...BASE_OPTIONS, packageManager: "npm", projectName: "npm-app", cwd });
|
|
55
|
+
expect(result.packageManager).toBe("npm");
|
|
56
|
+
expect(existsSync(join(result.targetDir, "pnpm-workspace.yaml"))).toBe(false);
|
|
57
|
+
const root = JSON.parse(readFileSync(join(result.targetDir, "package.json"), "utf8"));
|
|
58
|
+
expect(root.workspaces).toEqual(["apps/*", "packages/*"]);
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
it("generates a tracked .env.example but no root .env with secrets", () => {
|
|
28
65
|
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
29
66
|
try {
|
|
30
67
|
const result = createProject({ ...BASE_OPTIONS, projectName: "my-app", cwd, templateDir: defaultTemplateDir() });
|
|
31
|
-
expect(existsSync(join(result.targetDir, ".env.example"))).toBe(
|
|
68
|
+
expect(existsSync(join(result.targetDir, ".env.example"))).toBe(true);
|
|
32
69
|
expect(existsSync(join(result.targetDir, ".env"))).toBe(false);
|
|
70
|
+
const envExample = readFileSync(join(result.targetDir, ".env.example"), "utf8");
|
|
71
|
+
expect(envExample).toContain("JWT_SECRET=");
|
|
72
|
+
// placeholders only — never the real secret
|
|
73
|
+
expect(envExample).not.toContain("test-jwt-secret");
|
|
33
74
|
expect(existsSync(join(result.targetDir, "tsconfig.base.json"))).toBe(true);
|
|
34
75
|
}
|
|
35
76
|
finally {
|
|
36
77
|
rmSync(cwd, { recursive: true, force: true });
|
|
37
78
|
}
|
|
38
79
|
});
|
|
80
|
+
it("generates a .gitignore that excludes .env and matches apps/api/.env", () => {
|
|
81
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
82
|
+
try {
|
|
83
|
+
const result = createProject({ ...BASE_OPTIONS, projectName: "my-app", cwd, templateDir: defaultTemplateDir() });
|
|
84
|
+
const gitignore = readFileSync(join(result.targetDir, ".gitignore"), "utf8");
|
|
85
|
+
for (const entry of ["node_modules", ".pnpm-store", ".next", "dist", "coverage", ".env", ".env.*", "!.env.example", "*.log", ".DS_Store"]) {
|
|
86
|
+
expect(gitignore, `missing ${entry}`).toContain(entry);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
it("writes .gitignore before git init so .env is never staged", () => {
|
|
94
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
95
|
+
try {
|
|
96
|
+
const result = createProject({ ...BASE_OPTIONS, projectName: "git-app", cwd, git: true, templateDir: defaultTemplateDir() });
|
|
97
|
+
// .git exists and .gitignore present at the same time
|
|
98
|
+
expect(existsSync(join(result.targetDir, ".git"))).toBe(true);
|
|
99
|
+
expect(existsSync(join(result.targetDir, ".gitignore"))).toBe(true);
|
|
100
|
+
const status = execFileSync("git", ["status", "--porcelain", "--ignored"], {
|
|
101
|
+
cwd: result.targetDir,
|
|
102
|
+
encoding: "utf8"
|
|
103
|
+
});
|
|
104
|
+
// apps/api/.env must be ignored, never listed as untracked (??)
|
|
105
|
+
expect(status).not.toMatch(/^\?\? apps\/api\/\.env$/m);
|
|
106
|
+
expect(status).toMatch(/!! apps\/api\/\.env/);
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
it("never prints real secrets to stdout/stderr", () => {
|
|
113
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
114
|
+
const logs = [];
|
|
115
|
+
const origLog = console.log;
|
|
116
|
+
const origErr = console.error;
|
|
117
|
+
console.log = (...a) => void logs.push(a.join(" "));
|
|
118
|
+
console.error = (...a) => void logs.push(a.join(" "));
|
|
119
|
+
try {
|
|
120
|
+
createProject({
|
|
121
|
+
...BASE_OPTIONS,
|
|
122
|
+
projectName: "secret-app",
|
|
123
|
+
cwd,
|
|
124
|
+
jwtSecret: "SUPER-SECRET-JWT-VALUE-1234567890",
|
|
125
|
+
superadminPassword: "SUPER-SECRET-PASSWORD",
|
|
126
|
+
templateDir: defaultTemplateDir()
|
|
127
|
+
});
|
|
128
|
+
const output = logs.join("\n");
|
|
129
|
+
expect(output).not.toContain("SUPER-SECRET-JWT-VALUE-1234567890");
|
|
130
|
+
expect(output).not.toContain("SUPER-SECRET-PASSWORD");
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
console.log = origLog;
|
|
134
|
+
console.error = origErr;
|
|
135
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
it("cleans up the staging directory and leaves no partial project on failure", () => {
|
|
139
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
140
|
+
const badTemplate = mkdtempSync(join(tmpdir(), "openadminjs-cli-template-"));
|
|
141
|
+
try {
|
|
142
|
+
mkdirSync(join(badTemplate, "apps"), { recursive: true });
|
|
143
|
+
expect(() => createProject({ ...BASE_OPTIONS, projectName: "broken-app", cwd, templateDir: badTemplate })).toThrow(/package\.json not found/);
|
|
144
|
+
// No target and no leftover staging directory next to it
|
|
145
|
+
const leftovers = readdirSync(cwd);
|
|
146
|
+
expect(leftovers).toEqual([]);
|
|
147
|
+
}
|
|
148
|
+
finally {
|
|
149
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
150
|
+
rmSync(badTemplate, { recursive: true, force: true });
|
|
151
|
+
}
|
|
152
|
+
});
|
|
39
153
|
it("scaffolds prisma.config.ts that loads apps/api/.env for Prisma CLI", () => {
|
|
40
154
|
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
41
155
|
try {
|
|
@@ -62,6 +176,68 @@ describe("create project", () => {
|
|
|
62
176
|
rmSync(cwd, { recursive: true, force: true });
|
|
63
177
|
}
|
|
64
178
|
});
|
|
179
|
+
it("generates a SQLite project: String columns for Json/scopes, no postgres migrations", () => {
|
|
180
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
181
|
+
try {
|
|
182
|
+
const result = createProject({
|
|
183
|
+
...BASE_OPTIONS,
|
|
184
|
+
database: "sqlite",
|
|
185
|
+
projectName: "sqlite-app",
|
|
186
|
+
cwd,
|
|
187
|
+
databaseUrl: "file:./dev.db",
|
|
188
|
+
templateDir: defaultTemplateDir()
|
|
189
|
+
});
|
|
190
|
+
const schema = readFileSync(join(result.targetDir, "prisma", "schema.prisma"), "utf8");
|
|
191
|
+
expect(schema).toMatch(/datasource\s+\w+\s*\{[\s\S]*?provider\s*=\s*"sqlite"/);
|
|
192
|
+
expect(schema).not.toContain("String[]");
|
|
193
|
+
expect(schema).not.toMatch(/\bJson\b/);
|
|
194
|
+
expect(schema).toMatch(/scopes\s+String/);
|
|
195
|
+
expect(existsSync(join(result.targetDir, "prisma", "migrations"))).toBe(false);
|
|
196
|
+
const apiEnv = readFileSync(join(result.targetDir, "apps", "api", ".env"), "utf8");
|
|
197
|
+
expect(apiEnv).toContain("DATABASE_URL=file:./dev.db");
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
it("generates a MySQL project: correct provider, Json scopes, no postgres migrations", () => {
|
|
204
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
205
|
+
try {
|
|
206
|
+
const result = createProject({
|
|
207
|
+
...BASE_OPTIONS,
|
|
208
|
+
database: "mysql",
|
|
209
|
+
projectName: "mysql-app",
|
|
210
|
+
cwd,
|
|
211
|
+
databaseUrl: "mysql://openadminjs:openadminjs@localhost:3306/mysql-app",
|
|
212
|
+
templateDir: defaultTemplateDir()
|
|
213
|
+
});
|
|
214
|
+
const schema = readFileSync(join(result.targetDir, "prisma", "schema.prisma"), "utf8");
|
|
215
|
+
expect(schema).toMatch(/datasource\s+\w+\s*\{[\s\S]*?provider\s*=\s*"mysql"/);
|
|
216
|
+
// Scalar lists are unsupported on MySQL — must be converted to Json.
|
|
217
|
+
expect(schema).not.toContain("String[]");
|
|
218
|
+
expect(schema).toMatch(/scopes\s+Json/);
|
|
219
|
+
// PostgreSQL baseline migration must not be reused for MySQL.
|
|
220
|
+
expect(existsSync(join(result.targetDir, "prisma", "migrations"))).toBe(false);
|
|
221
|
+
// apps/api/.env gets the MySQL URL.
|
|
222
|
+
const apiEnv = readFileSync(join(result.targetDir, "apps", "api", ".env"), "utf8");
|
|
223
|
+
expect(apiEnv).toContain("DATABASE_URL=mysql://openadminjs:openadminjs@localhost:3306/mysql-app");
|
|
224
|
+
}
|
|
225
|
+
finally {
|
|
226
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
it("keeps the PostgreSQL baseline migration for postgresql projects", () => {
|
|
230
|
+
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
231
|
+
try {
|
|
232
|
+
const result = createProject({ ...BASE_OPTIONS, projectName: "pg-app", cwd, templateDir: defaultTemplateDir() });
|
|
233
|
+
expect(existsSync(join(result.targetDir, "prisma", "migrations", "migration_lock.toml"))).toBe(true);
|
|
234
|
+
const schema = readFileSync(join(result.targetDir, "prisma", "schema.prisma"), "utf8");
|
|
235
|
+
expect(schema).toContain("String[]");
|
|
236
|
+
}
|
|
237
|
+
finally {
|
|
238
|
+
rmSync(cwd, { recursive: true, force: true });
|
|
239
|
+
}
|
|
240
|
+
});
|
|
65
241
|
it("fails fast when template package.json is missing", () => {
|
|
66
242
|
const cwd = mkdtempSync(join(tmpdir(), "openadminjs-cli-test-"));
|
|
67
243
|
const badTemplate = mkdtempSync(join(tmpdir(), "openadminjs-cli-template-"));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { afterAll, describe, expect, it } from "vitest";
|
|
7
|
+
import { createProject, databaseUrl, defaultTemplateDir } from "./create-project.js";
|
|
8
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const repoRoot = join(here, "..", "..", "..");
|
|
10
|
+
const prismaBin = join(repoRoot, "node_modules", ".bin", "prisma");
|
|
11
|
+
const BASE = {
|
|
12
|
+
packageManager: "pnpm",
|
|
13
|
+
superadminEmail: "admin@localhost.dev",
|
|
14
|
+
superadminPassword: "password1234",
|
|
15
|
+
redisUrl: "redis://localhost:6379",
|
|
16
|
+
jwtSecret: "test-jwt-secret-01234567890123456789012",
|
|
17
|
+
jwtRefreshSecret: "test-jwt-refresh-0123456789012345678",
|
|
18
|
+
adminOrigin: "http://localhost:3000",
|
|
19
|
+
apiPort: "4000",
|
|
20
|
+
git: false,
|
|
21
|
+
install: false,
|
|
22
|
+
templateDir: defaultTemplateDir()
|
|
23
|
+
};
|
|
24
|
+
const PROVIDERS = [
|
|
25
|
+
{ database: "postgresql", url: "postgresql://localhost:5432/it-app?schema=public" },
|
|
26
|
+
{ database: "mysql", url: "mysql://openadminjs:openadminjs@localhost:3306/it-app" },
|
|
27
|
+
{ database: "sqlite", url: "file:./dev.db" }
|
|
28
|
+
];
|
|
29
|
+
function prismaValidate(schemaPath, databaseUrl) {
|
|
30
|
+
const result = spawnSync(prismaBin, ["validate", "--schema", schemaPath], {
|
|
31
|
+
env: { ...process.env, DATABASE_URL: databaseUrl },
|
|
32
|
+
encoding: "utf8"
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
ok: result.status === 0,
|
|
36
|
+
output: `${result.stdout}\n${result.stderr}`.trim()
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
describe("database provider integration", () => {
|
|
40
|
+
const cwd = mkdtempSync(join(tmpdir(), "oaj-db-int-"));
|
|
41
|
+
afterAll(() => rmSync(cwd, { recursive: true, force: true }));
|
|
42
|
+
for (const { database, url } of PROVIDERS) {
|
|
43
|
+
it(`generated ${database} schema passes prisma validate`, () => {
|
|
44
|
+
const result = createProject({
|
|
45
|
+
...BASE,
|
|
46
|
+
projectName: `${database}-validate`,
|
|
47
|
+
cwd,
|
|
48
|
+
database,
|
|
49
|
+
databaseUrl: url
|
|
50
|
+
});
|
|
51
|
+
const schemaPath = join(result.targetDir, "prisma", "schema.prisma");
|
|
52
|
+
expect(existsSync(schemaPath)).toBe(true);
|
|
53
|
+
const validation = prismaValidate(schemaPath, url);
|
|
54
|
+
expect(validation.ok, validation.output).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
describe("databaseUrl defaults", () => {
|
|
59
|
+
it("returns provider-appropriate connection strings", () => {
|
|
60
|
+
expect(databaseUrl("my-app", "postgresql")).toMatch(/^postgresql:\/\//);
|
|
61
|
+
expect(databaseUrl("my-app", "mysql")).toMatch(/^mysql:\/\//);
|
|
62
|
+
expect(databaseUrl("my-app", "sqlite")).toBe("file:./dev.db");
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { PackageManager } from "./adapt-package-manager.js";
|
|
2
|
+
export type DbAction = "migrate" | "seed" | "studio" | "reset";
|
|
3
|
+
export type DbCommand = {
|
|
4
|
+
cmd: string;
|
|
5
|
+
args: string[];
|
|
6
|
+
/** Human-readable form for --dry-run / logging. */
|
|
7
|
+
label: string;
|
|
8
|
+
/** True for destructive commands that require explicit confirmation. */
|
|
9
|
+
destructive: boolean;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Builds the process invocation for a db command using an argument array (never
|
|
13
|
+
* an interpolated shell string), so user input cannot inject shell commands.
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildDbCommand(action: DbAction, mode: string | undefined, pm: PackageManager): DbCommand;
|
|
16
|
+
/**
|
|
17
|
+
* Spawns a command inheriting stdio, forwarding SIGINT/SIGTERM to the child, and
|
|
18
|
+
* resolving with the child's exit code.
|
|
19
|
+
*/
|
|
20
|
+
export declare function runInherit(cmd: string, args: string[], cwd: string): Promise<number>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { runScriptPrefix } from "./detect-package-manager.js";
|
|
3
|
+
const DB_SCRIPTS = {
|
|
4
|
+
"migrate:dev": "db:migrate",
|
|
5
|
+
"migrate:deploy": "db:migrate:deploy",
|
|
6
|
+
seed: "db:seed",
|
|
7
|
+
studio: "db:studio",
|
|
8
|
+
reset: "db:reset"
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Builds the process invocation for a db command using an argument array (never
|
|
12
|
+
* an interpolated shell string), so user input cannot inject shell commands.
|
|
13
|
+
*/
|
|
14
|
+
export function buildDbCommand(action, mode, pm) {
|
|
15
|
+
let key;
|
|
16
|
+
let destructive = false;
|
|
17
|
+
switch (action) {
|
|
18
|
+
case "migrate": {
|
|
19
|
+
const m = (mode ?? "dev").toLowerCase();
|
|
20
|
+
if (m !== "dev" && m !== "deploy") {
|
|
21
|
+
throw new Error(`Unknown migrate mode "${mode}". Use "dev" or "deploy".`);
|
|
22
|
+
}
|
|
23
|
+
key = `migrate:${m}`;
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
case "seed":
|
|
27
|
+
key = "seed";
|
|
28
|
+
break;
|
|
29
|
+
case "studio":
|
|
30
|
+
key = "studio";
|
|
31
|
+
break;
|
|
32
|
+
case "reset":
|
|
33
|
+
key = "reset";
|
|
34
|
+
destructive = true;
|
|
35
|
+
break;
|
|
36
|
+
default:
|
|
37
|
+
throw new Error(`Unknown db action "${action}". Use: migrate, seed, studio, reset.`);
|
|
38
|
+
}
|
|
39
|
+
const script = DB_SCRIPTS[key];
|
|
40
|
+
if (!script)
|
|
41
|
+
throw new Error(`No script mapping for "${key}".`);
|
|
42
|
+
const [cmd, args] = runScriptPrefix(pm, script);
|
|
43
|
+
return { cmd, args, label: `${cmd} ${args.join(" ")}`, destructive };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Spawns a command inheriting stdio, forwarding SIGINT/SIGTERM to the child, and
|
|
47
|
+
* resolving with the child's exit code.
|
|
48
|
+
*/
|
|
49
|
+
export function runInherit(cmd, args, cwd) {
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const child = spawn(cmd, args, { cwd, stdio: "inherit" });
|
|
52
|
+
const forward = (signal) => {
|
|
53
|
+
if (!child.killed)
|
|
54
|
+
child.kill(signal);
|
|
55
|
+
};
|
|
56
|
+
process.on("SIGINT", forward);
|
|
57
|
+
process.on("SIGTERM", forward);
|
|
58
|
+
const cleanup = () => {
|
|
59
|
+
process.off("SIGINT", forward);
|
|
60
|
+
process.off("SIGTERM", forward);
|
|
61
|
+
};
|
|
62
|
+
child.on("error", (err) => {
|
|
63
|
+
cleanup();
|
|
64
|
+
reject(err);
|
|
65
|
+
});
|
|
66
|
+
child.on("close", (code, signal) => {
|
|
67
|
+
cleanup();
|
|
68
|
+
if (signal) {
|
|
69
|
+
resolve(1);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
resolve(code ?? 0);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { buildDbCommand } from "./db-commands.js";
|
|
3
|
+
describe("buildDbCommand", () => {
|
|
4
|
+
it("maps migrate dev/deploy per package manager", () => {
|
|
5
|
+
expect(buildDbCommand("migrate", "dev", "pnpm")).toMatchObject({ cmd: "pnpm", args: ["db:migrate"] });
|
|
6
|
+
expect(buildDbCommand("migrate", "deploy", "pnpm")).toMatchObject({ cmd: "pnpm", args: ["db:migrate:deploy"] });
|
|
7
|
+
expect(buildDbCommand("migrate", "dev", "npm")).toMatchObject({ cmd: "npm", args: ["run", "db:migrate"] });
|
|
8
|
+
expect(buildDbCommand("migrate", undefined, "yarn")).toMatchObject({ cmd: "yarn", args: ["db:migrate"] });
|
|
9
|
+
});
|
|
10
|
+
it("maps seed and studio", () => {
|
|
11
|
+
expect(buildDbCommand("seed", undefined, "pnpm").args).toEqual(["db:seed"]);
|
|
12
|
+
expect(buildDbCommand("studio", undefined, "npm").args).toEqual(["run", "db:studio"]);
|
|
13
|
+
});
|
|
14
|
+
it("marks reset as destructive", () => {
|
|
15
|
+
const cmd = buildDbCommand("reset", undefined, "pnpm");
|
|
16
|
+
expect(cmd.destructive).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
it("never interpolates a shell string (args are an array)", () => {
|
|
19
|
+
const cmd = buildDbCommand("migrate", "dev", "pnpm");
|
|
20
|
+
expect(Array.isArray(cmd.args)).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
it("throws on invalid action/mode", () => {
|
|
23
|
+
expect(() => buildDbCommand("migrate", "bogus", "pnpm")).toThrow(/migrate mode/);
|
|
24
|
+
// @ts-expect-error invalid action
|
|
25
|
+
expect(() => buildDbCommand("nope", undefined, "pnpm")).toThrow(/Unknown db action/);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { PackageManager } from "./adapt-package-manager.js";
|
|
2
|
+
/**
|
|
3
|
+
* Detects the package manager a generated project uses, preferring an explicit
|
|
4
|
+
* `packageManager` field, then lockfiles, then workspace config. Defaults to pnpm.
|
|
5
|
+
*/
|
|
6
|
+
export declare function detectPackageManager(cwd: string): PackageManager;
|
|
7
|
+
/** Returns [command, prefixArgs] to run an npm-style script with the given PM. */
|
|
8
|
+
export declare function runScriptPrefix(pm: PackageManager, script: string): [string, string[]];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Detects the package manager a generated project uses, preferring an explicit
|
|
5
|
+
* `packageManager` field, then lockfiles, then workspace config. Defaults to pnpm.
|
|
6
|
+
*/
|
|
7
|
+
export function detectPackageManager(cwd) {
|
|
8
|
+
const pkgPath = join(cwd, "package.json");
|
|
9
|
+
if (existsSync(pkgPath)) {
|
|
10
|
+
try {
|
|
11
|
+
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
12
|
+
const pm = pkg.packageManager ?? "";
|
|
13
|
+
if (pm.startsWith("pnpm"))
|
|
14
|
+
return "pnpm";
|
|
15
|
+
if (pm.startsWith("yarn"))
|
|
16
|
+
return "yarn";
|
|
17
|
+
if (pm.startsWith("npm"))
|
|
18
|
+
return "npm";
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// fall through to lockfile detection
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (existsSync(join(cwd, "pnpm-lock.yaml")) || existsSync(join(cwd, "pnpm-workspace.yaml")))
|
|
25
|
+
return "pnpm";
|
|
26
|
+
if (existsSync(join(cwd, "yarn.lock")))
|
|
27
|
+
return "yarn";
|
|
28
|
+
if (existsSync(join(cwd, "package-lock.json")))
|
|
29
|
+
return "npm";
|
|
30
|
+
return "pnpm";
|
|
31
|
+
}
|
|
32
|
+
/** Returns [command, prefixArgs] to run an npm-style script with the given PM. */
|
|
33
|
+
export function runScriptPrefix(pm, script) {
|
|
34
|
+
if (pm === "npm")
|
|
35
|
+
return ["npm", ["run", script]];
|
|
36
|
+
return [pm, [script]];
|
|
37
|
+
}
|
package/dist/doctor.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type CheckStatus = "pass" | "warn" | "fail";
|
|
2
|
+
export type CheckResult = {
|
|
3
|
+
name: string;
|
|
4
|
+
status: CheckStatus;
|
|
5
|
+
message: string;
|
|
6
|
+
};
|
|
7
|
+
export type DoctorReport = {
|
|
8
|
+
results: CheckResult[];
|
|
9
|
+
ok: boolean;
|
|
10
|
+
};
|
|
11
|
+
export declare function runDoctorChecks(cwd: string, opts?: {
|
|
12
|
+
skipNetwork?: boolean;
|
|
13
|
+
}): Promise<DoctorReport>;
|