@zenstackhq/cli 3.0.0-beta.22 → 3.0.0-beta.24
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/.turbo/turbo-build.log +8 -8
- package/dist/index.cjs +207 -142
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +208 -138
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
- package/src/actions/action-utils.ts +13 -5
- package/src/actions/db.ts +3 -3
- package/src/actions/format.ts +27 -0
- package/src/actions/generate.ts +13 -1
- package/src/actions/index.ts +3 -2
- package/src/actions/migrate.ts +18 -22
- package/src/index.ts +28 -14
- package/src/plugins/typescript.ts +12 -1
- package/src/utils/exec-utils.ts +27 -0
- package/test/format.test.ts +33 -0
- package/test/generate.test.ts +15 -0
- package/test/migrate.test.ts +1 -2
package/dist/index.js
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
|
+
}) : x)(function(x) {
|
|
6
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
+
});
|
|
3
9
|
var __export = (target, all) => {
|
|
4
10
|
for (var name in all)
|
|
5
11
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -7,33 +13,11 @@ var __export = (target, all) => {
|
|
|
7
13
|
|
|
8
14
|
// src/index.ts
|
|
9
15
|
import { ZModelLanguageMetaData } from "@zenstackhq/language";
|
|
10
|
-
import
|
|
16
|
+
import colors8 from "colors";
|
|
11
17
|
import { Command, CommanderError, Option } from "commander";
|
|
12
18
|
|
|
13
|
-
// src/actions/
|
|
14
|
-
import
|
|
15
|
-
|
|
16
|
-
// src/utils/exec-utils.ts
|
|
17
|
-
import { execSync as _exec } from "child_process";
|
|
18
|
-
function execSync(cmd, options) {
|
|
19
|
-
const { env: env2, ...restOptions } = options ?? {};
|
|
20
|
-
const mergedEnv = env2 ? {
|
|
21
|
-
...process.env,
|
|
22
|
-
...env2
|
|
23
|
-
} : void 0;
|
|
24
|
-
_exec(cmd, {
|
|
25
|
-
encoding: "utf-8",
|
|
26
|
-
stdio: options?.stdio ?? "inherit",
|
|
27
|
-
env: mergedEnv,
|
|
28
|
-
...restOptions
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
__name(execSync, "execSync");
|
|
32
|
-
function execPackage(cmd, options) {
|
|
33
|
-
const packageManager = process?.versions?.["bun"] ? "bunx" : "npx";
|
|
34
|
-
execSync(`${packageManager} ${cmd}`, options);
|
|
35
|
-
}
|
|
36
|
-
__name(execPackage, "execPackage");
|
|
19
|
+
// src/actions/check.ts
|
|
20
|
+
import colors2 from "colors";
|
|
37
21
|
|
|
38
22
|
// src/actions/action-utils.ts
|
|
39
23
|
import { loadDocument } from "@zenstackhq/language";
|
|
@@ -63,14 +47,22 @@ function getSchemaFile(file) {
|
|
|
63
47
|
if (!fs.existsSync(pkgJsonConfig.schema)) {
|
|
64
48
|
throw new CliError(`Schema file not found: ${pkgJsonConfig.schema}`);
|
|
65
49
|
}
|
|
66
|
-
|
|
50
|
+
if (fs.statSync(pkgJsonConfig.schema).isDirectory()) {
|
|
51
|
+
const schemaPath = path.join(pkgJsonConfig.schema, "schema.zmodel");
|
|
52
|
+
if (!fs.existsSync(schemaPath)) {
|
|
53
|
+
throw new CliError(`Schema file not found: ${schemaPath}`);
|
|
54
|
+
}
|
|
55
|
+
return schemaPath;
|
|
56
|
+
} else {
|
|
57
|
+
return pkgJsonConfig.schema;
|
|
58
|
+
}
|
|
67
59
|
}
|
|
68
|
-
if (fs.existsSync("./
|
|
69
|
-
return "./zenstack/schema.zmodel";
|
|
70
|
-
} else if (fs.existsSync("./schema.zmodel")) {
|
|
60
|
+
if (fs.existsSync("./schema.zmodel")) {
|
|
71
61
|
return "./schema.zmodel";
|
|
62
|
+
} else if (fs.existsSync("./zenstack/schema.zmodel")) {
|
|
63
|
+
return "./zenstack/schema.zmodel";
|
|
72
64
|
} else {
|
|
73
|
-
throw new CliError('Schema file not found in default locations ("./
|
|
65
|
+
throw new CliError('Schema file not found in default locations ("./schema.zmodel" or "./zenstack/schema.zmodel").');
|
|
74
66
|
}
|
|
75
67
|
}
|
|
76
68
|
__name(getSchemaFile, "getSchemaFile");
|
|
@@ -153,28 +145,84 @@ function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
|
|
|
153
145
|
}
|
|
154
146
|
__name(findUp, "findUp");
|
|
155
147
|
|
|
148
|
+
// src/actions/check.ts
|
|
149
|
+
async function run(options) {
|
|
150
|
+
const schemaFile = getSchemaFile(options.schema);
|
|
151
|
+
try {
|
|
152
|
+
await loadSchemaDocument(schemaFile);
|
|
153
|
+
console.log(colors2.green("\u2713 Schema validation completed successfully."));
|
|
154
|
+
} catch (error) {
|
|
155
|
+
console.error(colors2.red("\u2717 Schema validation failed."));
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
__name(run, "run");
|
|
160
|
+
|
|
161
|
+
// src/actions/db.ts
|
|
162
|
+
import fs2 from "fs";
|
|
163
|
+
|
|
164
|
+
// src/utils/exec-utils.ts
|
|
165
|
+
import { execSync as _exec } from "child_process";
|
|
166
|
+
import { fileURLToPath } from "url";
|
|
167
|
+
function execSync(cmd, options) {
|
|
168
|
+
const { env: env2, ...restOptions } = options ?? {};
|
|
169
|
+
const mergedEnv = env2 ? {
|
|
170
|
+
...process.env,
|
|
171
|
+
...env2
|
|
172
|
+
} : void 0;
|
|
173
|
+
_exec(cmd, {
|
|
174
|
+
encoding: "utf-8",
|
|
175
|
+
stdio: options?.stdio ?? "inherit",
|
|
176
|
+
env: mergedEnv,
|
|
177
|
+
...restOptions
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
__name(execSync, "execSync");
|
|
181
|
+
function execPackage(cmd, options) {
|
|
182
|
+
const packageManager = process?.versions?.["bun"] ? "bunx" : "npx";
|
|
183
|
+
execSync(`${packageManager} ${cmd}`, options);
|
|
184
|
+
}
|
|
185
|
+
__name(execPackage, "execPackage");
|
|
186
|
+
function execPrisma(args, options) {
|
|
187
|
+
let prismaPath;
|
|
188
|
+
try {
|
|
189
|
+
if (typeof import.meta.resolve === "function") {
|
|
190
|
+
prismaPath = fileURLToPath(import.meta.resolve("prisma/build/index.js"));
|
|
191
|
+
} else {
|
|
192
|
+
prismaPath = __require.resolve("prisma/build/index.js");
|
|
193
|
+
}
|
|
194
|
+
} catch {
|
|
195
|
+
}
|
|
196
|
+
if (!prismaPath) {
|
|
197
|
+
execPackage(`prisma ${args}`, options);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
execSync(`node ${prismaPath} ${args}`, options);
|
|
201
|
+
}
|
|
202
|
+
__name(execPrisma, "execPrisma");
|
|
203
|
+
|
|
156
204
|
// src/actions/db.ts
|
|
157
|
-
async function
|
|
205
|
+
async function run2(command, options) {
|
|
158
206
|
switch (command) {
|
|
159
207
|
case "push":
|
|
160
208
|
await runPush(options);
|
|
161
209
|
break;
|
|
162
210
|
}
|
|
163
211
|
}
|
|
164
|
-
__name(
|
|
212
|
+
__name(run2, "run");
|
|
165
213
|
async function runPush(options) {
|
|
166
214
|
const schemaFile = getSchemaFile(options.schema);
|
|
167
215
|
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile);
|
|
168
216
|
try {
|
|
169
217
|
const cmd = [
|
|
170
|
-
"
|
|
218
|
+
"db push",
|
|
171
219
|
` --schema "${prismaSchemaFile}"`,
|
|
172
220
|
options.acceptDataLoss ? " --accept-data-loss" : "",
|
|
173
221
|
options.forceReset ? " --force-reset" : "",
|
|
174
222
|
" --skip-generate"
|
|
175
223
|
].join("");
|
|
176
224
|
try {
|
|
177
|
-
|
|
225
|
+
execPrisma(cmd);
|
|
178
226
|
} catch (err) {
|
|
179
227
|
handleSubProcessError(err);
|
|
180
228
|
}
|
|
@@ -186,11 +234,29 @@ async function runPush(options) {
|
|
|
186
234
|
}
|
|
187
235
|
__name(runPush, "runPush");
|
|
188
236
|
|
|
237
|
+
// src/actions/format.ts
|
|
238
|
+
import { formatDocument } from "@zenstackhq/language";
|
|
239
|
+
import colors3 from "colors";
|
|
240
|
+
import fs3 from "fs";
|
|
241
|
+
async function run3(options) {
|
|
242
|
+
const schemaFile = getSchemaFile(options.schema);
|
|
243
|
+
let formattedContent;
|
|
244
|
+
try {
|
|
245
|
+
formattedContent = await formatDocument(fs3.readFileSync(schemaFile, "utf-8"));
|
|
246
|
+
} catch (error) {
|
|
247
|
+
console.error(colors3.red("\u2717 Schema formatting failed."));
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
fs3.writeFileSync(schemaFile, formattedContent, "utf-8");
|
|
251
|
+
console.log(colors3.green("\u2713 Schema formatting completed successfully."));
|
|
252
|
+
}
|
|
253
|
+
__name(run3, "run");
|
|
254
|
+
|
|
189
255
|
// src/actions/generate.ts
|
|
190
256
|
import { invariant } from "@zenstackhq/common-helpers";
|
|
191
257
|
import { isPlugin } from "@zenstackhq/language/ast";
|
|
192
258
|
import { getLiteral, getLiteralArray } from "@zenstackhq/language/utils";
|
|
193
|
-
import
|
|
259
|
+
import colors4 from "colors";
|
|
194
260
|
import path4 from "path";
|
|
195
261
|
import ora from "ora";
|
|
196
262
|
|
|
@@ -203,7 +269,7 @@ __export(plugins_exports, {
|
|
|
203
269
|
|
|
204
270
|
// src/plugins/prisma.ts
|
|
205
271
|
import { PrismaSchemaGenerator as PrismaSchemaGenerator2 } from "@zenstackhq/sdk";
|
|
206
|
-
import
|
|
272
|
+
import fs4 from "fs";
|
|
207
273
|
import path2 from "path";
|
|
208
274
|
var plugin = {
|
|
209
275
|
name: "Prisma Schema Generator",
|
|
@@ -212,21 +278,21 @@ var plugin = {
|
|
|
212
278
|
let outFile = path2.join(defaultOutputPath, "schema.prisma");
|
|
213
279
|
if (typeof pluginOptions["output"] === "string") {
|
|
214
280
|
outFile = path2.resolve(defaultOutputPath, pluginOptions["output"]);
|
|
215
|
-
if (!
|
|
216
|
-
|
|
281
|
+
if (!fs4.existsSync(path2.dirname(outFile))) {
|
|
282
|
+
fs4.mkdirSync(path2.dirname(outFile), {
|
|
217
283
|
recursive: true
|
|
218
284
|
});
|
|
219
285
|
}
|
|
220
286
|
}
|
|
221
287
|
const prismaSchema = await new PrismaSchemaGenerator2(model).generate();
|
|
222
|
-
|
|
288
|
+
fs4.writeFileSync(outFile, prismaSchema);
|
|
223
289
|
}
|
|
224
290
|
};
|
|
225
291
|
var prisma_default = plugin;
|
|
226
292
|
|
|
227
293
|
// src/plugins/typescript.ts
|
|
228
294
|
import { TsSchemaGenerator } from "@zenstackhq/sdk";
|
|
229
|
-
import
|
|
295
|
+
import fs5 from "fs";
|
|
230
296
|
import path3 from "path";
|
|
231
297
|
var plugin2 = {
|
|
232
298
|
name: "TypeScript Schema Generator",
|
|
@@ -235,32 +301,37 @@ var plugin2 = {
|
|
|
235
301
|
let outDir = defaultOutputPath;
|
|
236
302
|
if (typeof pluginOptions["output"] === "string") {
|
|
237
303
|
outDir = path3.resolve(defaultOutputPath, pluginOptions["output"]);
|
|
238
|
-
if (!
|
|
239
|
-
|
|
304
|
+
if (!fs5.existsSync(outDir)) {
|
|
305
|
+
fs5.mkdirSync(outDir, {
|
|
240
306
|
recursive: true
|
|
241
307
|
});
|
|
242
308
|
}
|
|
243
309
|
}
|
|
244
310
|
const lite = pluginOptions["lite"] === true;
|
|
245
311
|
const liteOnly = pluginOptions["liteOnly"] === true;
|
|
312
|
+
const importWithFileExtension = pluginOptions["importWithFileExtension"];
|
|
313
|
+
if (importWithFileExtension && typeof importWithFileExtension !== "string") {
|
|
314
|
+
throw new Error('The "importWithFileExtension" option must be a string if specified.');
|
|
315
|
+
}
|
|
246
316
|
await new TsSchemaGenerator().generate(model, {
|
|
247
317
|
outDir,
|
|
248
318
|
lite,
|
|
249
|
-
liteOnly
|
|
319
|
+
liteOnly,
|
|
320
|
+
importWithFileExtension
|
|
250
321
|
});
|
|
251
322
|
}
|
|
252
323
|
};
|
|
253
324
|
var typescript_default = plugin2;
|
|
254
325
|
|
|
255
326
|
// src/actions/generate.ts
|
|
256
|
-
async function
|
|
327
|
+
async function run4(options) {
|
|
257
328
|
const start = Date.now();
|
|
258
329
|
const schemaFile = getSchemaFile(options.schema);
|
|
259
330
|
const model = await loadSchemaDocument(schemaFile);
|
|
260
331
|
const outputPath = getOutputPath(options, schemaFile);
|
|
261
332
|
await runPlugins(schemaFile, model, outputPath, options);
|
|
262
333
|
if (!options.silent) {
|
|
263
|
-
console.log(
|
|
334
|
+
console.log(colors4.green(`Generation completed successfully in ${Date.now() - start}ms.
|
|
264
335
|
`));
|
|
265
336
|
console.log(`You can now create a ZenStack client with it.
|
|
266
337
|
|
|
@@ -276,7 +347,7 @@ const client = new ZenStackClient(schema, {
|
|
|
276
347
|
Check documentation: https://zenstack.dev/docs/3.x`);
|
|
277
348
|
}
|
|
278
349
|
}
|
|
279
|
-
__name(
|
|
350
|
+
__name(run4, "run");
|
|
280
351
|
function getOutputPath(options, schemaFile) {
|
|
281
352
|
if (options.output) {
|
|
282
353
|
return options.output;
|
|
@@ -311,9 +382,18 @@ async function runPlugins(schemaFile, model, outputPath, options) {
|
|
|
311
382
|
}
|
|
312
383
|
}
|
|
313
384
|
if (cliPlugin) {
|
|
385
|
+
const pluginOptions = getPluginOptions(plugin3);
|
|
386
|
+
if (provider === "@core/typescript") {
|
|
387
|
+
if (pluginOptions["lite"] === void 0) {
|
|
388
|
+
pluginOptions["lite"] = options.lite;
|
|
389
|
+
}
|
|
390
|
+
if (pluginOptions["liteOnly"] === void 0) {
|
|
391
|
+
pluginOptions["liteOnly"] = options.liteOnly;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
314
394
|
processedPlugins.push({
|
|
315
395
|
cliPlugin,
|
|
316
|
-
pluginOptions
|
|
396
|
+
pluginOptions
|
|
317
397
|
});
|
|
318
398
|
}
|
|
319
399
|
}
|
|
@@ -380,9 +460,9 @@ function getPluginOptions(plugin3) {
|
|
|
380
460
|
__name(getPluginOptions, "getPluginOptions");
|
|
381
461
|
|
|
382
462
|
// src/actions/info.ts
|
|
383
|
-
import
|
|
463
|
+
import colors5 from "colors";
|
|
384
464
|
import path5 from "path";
|
|
385
|
-
async function
|
|
465
|
+
async function run5(projectPath) {
|
|
386
466
|
const packages = await getZenStackPackages(projectPath);
|
|
387
467
|
if (!packages) {
|
|
388
468
|
console.error("Unable to locate package.json. Are you in a valid project directory?");
|
|
@@ -394,13 +474,13 @@ async function run3(projectPath) {
|
|
|
394
474
|
if (version2) {
|
|
395
475
|
versions.add(version2);
|
|
396
476
|
}
|
|
397
|
-
console.log(` ${
|
|
477
|
+
console.log(` ${colors5.green(pkg.padEnd(20))} ${version2}`);
|
|
398
478
|
}
|
|
399
479
|
if (versions.size > 1) {
|
|
400
|
-
console.warn(
|
|
480
|
+
console.warn(colors5.yellow("WARNING: Multiple versions of Zenstack packages detected. This may cause issues."));
|
|
401
481
|
}
|
|
402
482
|
}
|
|
403
|
-
__name(
|
|
483
|
+
__name(run5, "run");
|
|
404
484
|
async function getZenStackPackages(projectPath) {
|
|
405
485
|
let pkgJson;
|
|
406
486
|
const resolvedPath = path5.resolve(projectPath);
|
|
@@ -443,8 +523,8 @@ async function getZenStackPackages(projectPath) {
|
|
|
443
523
|
__name(getZenStackPackages, "getZenStackPackages");
|
|
444
524
|
|
|
445
525
|
// src/actions/init.ts
|
|
446
|
-
import
|
|
447
|
-
import
|
|
526
|
+
import colors6 from "colors";
|
|
527
|
+
import fs6 from "fs";
|
|
448
528
|
import path6 from "path";
|
|
449
529
|
import ora2 from "ora";
|
|
450
530
|
import { detect, resolveCommand } from "package-manager-detector";
|
|
@@ -479,7 +559,7 @@ model Post {
|
|
|
479
559
|
`;
|
|
480
560
|
|
|
481
561
|
// src/actions/init.ts
|
|
482
|
-
async function
|
|
562
|
+
async function run6(projectPath) {
|
|
483
563
|
const packages = [
|
|
484
564
|
{
|
|
485
565
|
name: "@zenstackhq/cli@next",
|
|
@@ -497,7 +577,7 @@ async function run4(projectPath) {
|
|
|
497
577
|
name: "npm"
|
|
498
578
|
};
|
|
499
579
|
}
|
|
500
|
-
console.log(
|
|
580
|
+
console.log(colors6.gray(`Using package manager: ${pm.agent}`));
|
|
501
581
|
for (const pkg of packages) {
|
|
502
582
|
const resolved = resolveCommand(pm.agent, "install", [
|
|
503
583
|
pkg.name,
|
|
@@ -520,24 +600,24 @@ async function run4(projectPath) {
|
|
|
520
600
|
}
|
|
521
601
|
}
|
|
522
602
|
const generationFolder = "zenstack";
|
|
523
|
-
if (!
|
|
524
|
-
|
|
603
|
+
if (!fs6.existsSync(path6.join(projectPath, generationFolder))) {
|
|
604
|
+
fs6.mkdirSync(path6.join(projectPath, generationFolder));
|
|
525
605
|
}
|
|
526
|
-
if (!
|
|
527
|
-
|
|
606
|
+
if (!fs6.existsSync(path6.join(projectPath, generationFolder, "schema.zmodel"))) {
|
|
607
|
+
fs6.writeFileSync(path6.join(projectPath, generationFolder, "schema.zmodel"), STARTER_ZMODEL);
|
|
528
608
|
} else {
|
|
529
|
-
console.log(
|
|
609
|
+
console.log(colors6.yellow("Schema file already exists. Skipping generation of sample."));
|
|
530
610
|
}
|
|
531
|
-
console.log(
|
|
532
|
-
console.log(
|
|
533
|
-
console.log(
|
|
611
|
+
console.log(colors6.green("ZenStack project initialized successfully!"));
|
|
612
|
+
console.log(colors6.gray(`See "${generationFolder}/schema.zmodel" for your database schema.`));
|
|
613
|
+
console.log(colors6.gray("Run `zenstack generate` to compile the the schema into a TypeScript file."));
|
|
534
614
|
}
|
|
535
|
-
__name(
|
|
615
|
+
__name(run6, "run");
|
|
536
616
|
|
|
537
617
|
// src/actions/migrate.ts
|
|
538
|
-
import
|
|
618
|
+
import fs7 from "fs";
|
|
539
619
|
import path7 from "path";
|
|
540
|
-
async function
|
|
620
|
+
async function run7(command, options) {
|
|
541
621
|
const schemaFile = getSchemaFile(options.schema);
|
|
542
622
|
const prismaSchemaDir = options.migrations ? path7.dirname(options.migrations) : void 0;
|
|
543
623
|
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile, prismaSchemaDir);
|
|
@@ -560,73 +640,73 @@ async function run5(command, options) {
|
|
|
560
640
|
break;
|
|
561
641
|
}
|
|
562
642
|
} finally {
|
|
563
|
-
if (
|
|
564
|
-
|
|
643
|
+
if (fs7.existsSync(prismaSchemaFile)) {
|
|
644
|
+
fs7.unlinkSync(prismaSchemaFile);
|
|
565
645
|
}
|
|
566
646
|
}
|
|
567
647
|
}
|
|
568
|
-
__name(
|
|
569
|
-
|
|
648
|
+
__name(run7, "run");
|
|
649
|
+
function runDev(prismaSchemaFile, options) {
|
|
570
650
|
try {
|
|
571
651
|
const cmd = [
|
|
572
|
-
"
|
|
652
|
+
"migrate dev",
|
|
573
653
|
` --schema "${prismaSchemaFile}"`,
|
|
574
654
|
" --skip-generate",
|
|
575
|
-
options.name ? ` --name ${options.name}` : "",
|
|
655
|
+
options.name ? ` --name "${options.name}"` : "",
|
|
576
656
|
options.createOnly ? " --create-only" : ""
|
|
577
657
|
].join("");
|
|
578
|
-
|
|
658
|
+
execPrisma(cmd);
|
|
579
659
|
} catch (err) {
|
|
580
660
|
handleSubProcessError2(err);
|
|
581
661
|
}
|
|
582
662
|
}
|
|
583
663
|
__name(runDev, "runDev");
|
|
584
|
-
|
|
664
|
+
function runReset(prismaSchemaFile, options) {
|
|
585
665
|
try {
|
|
586
666
|
const cmd = [
|
|
587
|
-
"
|
|
667
|
+
"migrate reset",
|
|
588
668
|
` --schema "${prismaSchemaFile}"`,
|
|
589
669
|
" --skip-generate",
|
|
590
670
|
options.force ? " --force" : ""
|
|
591
671
|
].join("");
|
|
592
|
-
|
|
672
|
+
execPrisma(cmd);
|
|
593
673
|
} catch (err) {
|
|
594
674
|
handleSubProcessError2(err);
|
|
595
675
|
}
|
|
596
676
|
}
|
|
597
677
|
__name(runReset, "runReset");
|
|
598
|
-
|
|
678
|
+
function runDeploy(prismaSchemaFile, _options) {
|
|
599
679
|
try {
|
|
600
680
|
const cmd = [
|
|
601
|
-
"
|
|
681
|
+
"migrate deploy",
|
|
602
682
|
` --schema "${prismaSchemaFile}"`
|
|
603
683
|
].join("");
|
|
604
|
-
|
|
684
|
+
execPrisma(cmd);
|
|
605
685
|
} catch (err) {
|
|
606
686
|
handleSubProcessError2(err);
|
|
607
687
|
}
|
|
608
688
|
}
|
|
609
689
|
__name(runDeploy, "runDeploy");
|
|
610
|
-
|
|
690
|
+
function runStatus(prismaSchemaFile, _options) {
|
|
611
691
|
try {
|
|
612
|
-
|
|
692
|
+
execPrisma(`migrate status --schema "${prismaSchemaFile}"`);
|
|
613
693
|
} catch (err) {
|
|
614
694
|
handleSubProcessError2(err);
|
|
615
695
|
}
|
|
616
696
|
}
|
|
617
697
|
__name(runStatus, "runStatus");
|
|
618
|
-
|
|
698
|
+
function runResolve(prismaSchemaFile, options) {
|
|
619
699
|
if (!options.applied && !options.rolledBack) {
|
|
620
700
|
throw new CliError("Either --applied or --rolled-back option must be provided");
|
|
621
701
|
}
|
|
622
702
|
try {
|
|
623
703
|
const cmd = [
|
|
624
|
-
"
|
|
704
|
+
"migrate resolve",
|
|
625
705
|
` --schema "${prismaSchemaFile}"`,
|
|
626
|
-
options.applied ? ` --applied ${options.applied}` : "",
|
|
627
|
-
options.rolledBack ? ` --rolled-back ${options.rolledBack}` : ""
|
|
706
|
+
options.applied ? ` --applied "${options.applied}"` : "",
|
|
707
|
+
options.rolledBack ? ` --rolled-back "${options.rolledBack}"` : ""
|
|
628
708
|
].join("");
|
|
629
|
-
|
|
709
|
+
execPrisma(cmd);
|
|
630
710
|
} catch (err) {
|
|
631
711
|
handleSubProcessError2(err);
|
|
632
712
|
}
|
|
@@ -641,24 +721,10 @@ function handleSubProcessError2(err) {
|
|
|
641
721
|
}
|
|
642
722
|
__name(handleSubProcessError2, "handleSubProcessError");
|
|
643
723
|
|
|
644
|
-
// src/actions/check.ts
|
|
645
|
-
import colors5 from "colors";
|
|
646
|
-
async function run6(options) {
|
|
647
|
-
const schemaFile = getSchemaFile(options.schema);
|
|
648
|
-
try {
|
|
649
|
-
await loadSchemaDocument(schemaFile);
|
|
650
|
-
console.log(colors5.green("\u2713 Schema validation completed successfully."));
|
|
651
|
-
} catch (error) {
|
|
652
|
-
console.error(colors5.red("\u2717 Schema validation failed."));
|
|
653
|
-
throw error;
|
|
654
|
-
}
|
|
655
|
-
}
|
|
656
|
-
__name(run6, "run");
|
|
657
|
-
|
|
658
724
|
// src/telemetry.ts
|
|
659
725
|
import { init } from "mixpanel";
|
|
660
726
|
import { randomUUID as randomUUID2 } from "crypto";
|
|
661
|
-
import
|
|
727
|
+
import fs12 from "fs";
|
|
662
728
|
import * as os2 from "os";
|
|
663
729
|
|
|
664
730
|
// src/constants.ts
|
|
@@ -669,14 +735,14 @@ import { env } from "process";
|
|
|
669
735
|
var isInCi = env["CI"] !== "0" && env["CI"] !== "false" && ("CI" in env || "CONTINUOUS_INTEGRATION" in env || Object.keys(env).some((key) => key.startsWith("CI_")));
|
|
670
736
|
|
|
671
737
|
// src/utils/is-container.ts
|
|
672
|
-
import
|
|
738
|
+
import fs9 from "fs";
|
|
673
739
|
|
|
674
740
|
// src/utils/is-docker.ts
|
|
675
|
-
import
|
|
741
|
+
import fs8 from "fs";
|
|
676
742
|
var isDockerCached;
|
|
677
743
|
function hasDockerEnv() {
|
|
678
744
|
try {
|
|
679
|
-
|
|
745
|
+
fs8.statSync("/.dockerenv");
|
|
680
746
|
return true;
|
|
681
747
|
} catch {
|
|
682
748
|
return false;
|
|
@@ -685,7 +751,7 @@ function hasDockerEnv() {
|
|
|
685
751
|
__name(hasDockerEnv, "hasDockerEnv");
|
|
686
752
|
function hasDockerCGroup() {
|
|
687
753
|
try {
|
|
688
|
-
return
|
|
754
|
+
return fs8.readFileSync("/proc/self/cgroup", "utf8").includes("docker");
|
|
689
755
|
} catch {
|
|
690
756
|
return false;
|
|
691
757
|
}
|
|
@@ -703,7 +769,7 @@ __name(isDocker, "isDocker");
|
|
|
703
769
|
var cachedResult;
|
|
704
770
|
var hasContainerEnv = /* @__PURE__ */ __name(() => {
|
|
705
771
|
try {
|
|
706
|
-
|
|
772
|
+
fs9.statSync("/run/.containerenv");
|
|
707
773
|
return true;
|
|
708
774
|
} catch {
|
|
709
775
|
return false;
|
|
@@ -720,7 +786,7 @@ __name(isInContainer, "isInContainer");
|
|
|
720
786
|
// src/utils/is-wsl.ts
|
|
721
787
|
import process2 from "process";
|
|
722
788
|
import os from "os";
|
|
723
|
-
import
|
|
789
|
+
import fs10 from "fs";
|
|
724
790
|
var isWsl = /* @__PURE__ */ __name(() => {
|
|
725
791
|
if (process2.platform !== "linux") {
|
|
726
792
|
return false;
|
|
@@ -729,7 +795,7 @@ var isWsl = /* @__PURE__ */ __name(() => {
|
|
|
729
795
|
return true;
|
|
730
796
|
}
|
|
731
797
|
try {
|
|
732
|
-
return
|
|
798
|
+
return fs10.readFileSync("/proc/version", "utf8").toLowerCase().includes("microsoft");
|
|
733
799
|
} catch {
|
|
734
800
|
return false;
|
|
735
801
|
}
|
|
@@ -793,17 +859,17 @@ function getMachineId() {
|
|
|
793
859
|
__name(getMachineId, "getMachineId");
|
|
794
860
|
|
|
795
861
|
// src/utils/version-utils.ts
|
|
796
|
-
import
|
|
797
|
-
import
|
|
862
|
+
import colors7 from "colors";
|
|
863
|
+
import fs11 from "fs";
|
|
798
864
|
import path8 from "path";
|
|
799
|
-
import { fileURLToPath } from "url";
|
|
865
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
800
866
|
import semver from "semver";
|
|
801
867
|
var CHECK_VERSION_TIMEOUT = 2e3;
|
|
802
868
|
var VERSION_CHECK_TAG = "next";
|
|
803
869
|
function getVersion() {
|
|
804
870
|
try {
|
|
805
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname : path8.dirname(
|
|
806
|
-
return JSON.parse(
|
|
871
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : path8.dirname(fileURLToPath2(import.meta.url));
|
|
872
|
+
return JSON.parse(fs11.readFileSync(path8.join(_dirname, "../package.json"), "utf8")).version;
|
|
807
873
|
} catch {
|
|
808
874
|
return void 0;
|
|
809
875
|
}
|
|
@@ -818,7 +884,7 @@ async function checkNewVersion() {
|
|
|
818
884
|
return;
|
|
819
885
|
}
|
|
820
886
|
if (latestVersion && currVersion && semver.gt(latestVersion, currVersion)) {
|
|
821
|
-
console.log(`A newer version ${
|
|
887
|
+
console.log(`A newer version ${colors7.cyan(latestVersion)} is available.`);
|
|
822
888
|
}
|
|
823
889
|
}
|
|
824
890
|
__name(checkNewVersion, "checkNewVersion");
|
|
@@ -933,7 +999,7 @@ var Telemetry = class {
|
|
|
933
999
|
try {
|
|
934
1000
|
const packageJsonPath = import.meta.resolve("prisma/package.json");
|
|
935
1001
|
const packageJsonUrl = new URL(packageJsonPath);
|
|
936
|
-
const packageJson = JSON.parse(
|
|
1002
|
+
const packageJson = JSON.parse(fs12.readFileSync(packageJsonUrl, "utf8"));
|
|
937
1003
|
return packageJson.version;
|
|
938
1004
|
} catch {
|
|
939
1005
|
return void 0;
|
|
@@ -944,45 +1010,49 @@ var telemetry = new Telemetry();
|
|
|
944
1010
|
|
|
945
1011
|
// src/index.ts
|
|
946
1012
|
var generateAction = /* @__PURE__ */ __name(async (options) => {
|
|
947
|
-
await telemetry.trackCommand("generate", () =>
|
|
1013
|
+
await telemetry.trackCommand("generate", () => run4(options));
|
|
948
1014
|
}, "generateAction");
|
|
949
1015
|
var migrateAction = /* @__PURE__ */ __name(async (subCommand, options) => {
|
|
950
|
-
await telemetry.trackCommand(`migrate ${subCommand}`, () =>
|
|
1016
|
+
await telemetry.trackCommand(`migrate ${subCommand}`, () => run7(subCommand, options));
|
|
951
1017
|
}, "migrateAction");
|
|
952
1018
|
var dbAction = /* @__PURE__ */ __name(async (subCommand, options) => {
|
|
953
|
-
await telemetry.trackCommand(`db ${subCommand}`, () =>
|
|
1019
|
+
await telemetry.trackCommand(`db ${subCommand}`, () => run2(subCommand, options));
|
|
954
1020
|
}, "dbAction");
|
|
955
1021
|
var infoAction = /* @__PURE__ */ __name(async (projectPath) => {
|
|
956
|
-
await telemetry.trackCommand("info", () =>
|
|
1022
|
+
await telemetry.trackCommand("info", () => run5(projectPath));
|
|
957
1023
|
}, "infoAction");
|
|
958
1024
|
var initAction = /* @__PURE__ */ __name(async (projectPath) => {
|
|
959
|
-
await telemetry.trackCommand("init", () =>
|
|
1025
|
+
await telemetry.trackCommand("init", () => run6(projectPath));
|
|
960
1026
|
}, "initAction");
|
|
961
1027
|
var checkAction = /* @__PURE__ */ __name(async (options) => {
|
|
962
|
-
await telemetry.trackCommand("check", () =>
|
|
1028
|
+
await telemetry.trackCommand("check", () => run(options));
|
|
963
1029
|
}, "checkAction");
|
|
1030
|
+
var formatAction = /* @__PURE__ */ __name(async (options) => {
|
|
1031
|
+
await telemetry.trackCommand("format", () => run3(options));
|
|
1032
|
+
}, "formatAction");
|
|
964
1033
|
function createProgram() {
|
|
965
|
-
const program = new Command("zen");
|
|
966
|
-
program.version(getVersion(), "-v --version", "display CLI version");
|
|
1034
|
+
const program = new Command("zen").alias("zenstack").helpOption("-h, --help", "Show this help message").version(getVersion(), "-v --version", "Show CLI version");
|
|
967
1035
|
const schemaExtensions = ZModelLanguageMetaData.fileExtensions.join(", ");
|
|
968
|
-
program.description(`${
|
|
1036
|
+
program.description(`${colors8.bold.blue("\u03B6")} ZenStack is the data layer for modern TypeScript apps.
|
|
969
1037
|
|
|
970
1038
|
Documentation: https://zenstack.dev/docs/3.x`).showHelpAfterError().showSuggestionAfterError();
|
|
971
1039
|
const schemaOption = new Option("--schema <file>", `schema file (with extension ${schemaExtensions}). Defaults to "zenstack/schema.zmodel" unless specified in package.json.`);
|
|
972
1040
|
const noVersionCheckOption = new Option("--no-version-check", "do not check for new version");
|
|
973
|
-
program.command("generate").description("Run code generation plugins
|
|
1041
|
+
program.command("generate").description("Run code generation plugins").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new Option("-o, --output <path>", "default output directory for code generation")).addOption(new Option("--lite", "also generate a lite version of schema without attributes").default(false)).addOption(new Option("--lite-only", "only generate lite version of schema without attributes").default(false)).addOption(new Option("--silent", "suppress all output except errors").default(false)).action(generateAction);
|
|
974
1042
|
const migrateCommand = program.command("migrate").description("Run database schema migration related tasks.");
|
|
975
1043
|
const migrationsOption = new Option("--migrations <path>", 'path that contains the "migrations" directory');
|
|
976
|
-
migrateCommand.command("dev").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new Option("-n, --name <name>", "migration name")).addOption(new Option("--create-only", "only create migration, do not apply")).addOption(migrationsOption).description("Create a migration from changes in schema and apply it to the database
|
|
977
|
-
migrateCommand.command("reset").addOption(schemaOption).addOption(new Option("--force", "skip the confirmation prompt")).addOption(migrationsOption).addOption(noVersionCheckOption).description("Reset your database and apply all migrations, all data will be lost
|
|
978
|
-
migrateCommand.command("deploy").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).description("Deploy your pending migrations to your production/staging database
|
|
979
|
-
migrateCommand.command("status").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).description("Check the status of your database migrations
|
|
980
|
-
migrateCommand.command("resolve").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).addOption(new Option("--applied <migration>", "record a specific migration as applied")).addOption(new Option("--rolled-back <migration>", "record a specific migration as rolled back")).description("Resolve issues with database migrations in deployment databases
|
|
981
|
-
const dbCommand = program.command("db").description("Manage your database schema during development
|
|
982
|
-
dbCommand.command("push").description("Push the state from your schema to your database
|
|
983
|
-
program.command("info").description("Get information of installed ZenStack packages
|
|
984
|
-
program.command("init").description("Initialize an existing project for ZenStack
|
|
985
|
-
program.command("check").description("Check a ZModel schema for syntax or semantic errors
|
|
1044
|
+
migrateCommand.command("dev").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new Option("-n, --name <name>", "migration name")).addOption(new Option("--create-only", "only create migration, do not apply")).addOption(migrationsOption).description("Create a migration from changes in schema and apply it to the database").action((options) => migrateAction("dev", options));
|
|
1045
|
+
migrateCommand.command("reset").addOption(schemaOption).addOption(new Option("--force", "skip the confirmation prompt")).addOption(migrationsOption).addOption(noVersionCheckOption).description("Reset your database and apply all migrations, all data will be lost").action((options) => migrateAction("reset", options));
|
|
1046
|
+
migrateCommand.command("deploy").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).description("Deploy your pending migrations to your production/staging database").action((options) => migrateAction("deploy", options));
|
|
1047
|
+
migrateCommand.command("status").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).description("Check the status of your database migrations").action((options) => migrateAction("status", options));
|
|
1048
|
+
migrateCommand.command("resolve").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).addOption(new Option("--applied <migration>", "record a specific migration as applied")).addOption(new Option("--rolled-back <migration>", "record a specific migration as rolled back")).description("Resolve issues with database migrations in deployment databases").action((options) => migrateAction("resolve", options));
|
|
1049
|
+
const dbCommand = program.command("db").description("Manage your database schema during development");
|
|
1050
|
+
dbCommand.command("push").description("Push the state from your schema to your database").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new Option("--accept-data-loss", "ignore data loss warnings")).addOption(new Option("--force-reset", "force a reset of the database before push")).action((options) => dbAction("push", options));
|
|
1051
|
+
program.command("info").description("Get information of installed ZenStack packages").argument("[path]", "project path", ".").addOption(noVersionCheckOption).action(infoAction);
|
|
1052
|
+
program.command("init").description("Initialize an existing project for ZenStack").argument("[path]", "project path", ".").addOption(noVersionCheckOption).action(initAction);
|
|
1053
|
+
program.command("check").description("Check a ZModel schema for syntax or semantic errors").addOption(schemaOption).addOption(noVersionCheckOption).action(checkAction);
|
|
1054
|
+
program.command("format").description("Format a ZModel schema file").addOption(schemaOption).addOption(noVersionCheckOption).action(formatAction);
|
|
1055
|
+
program.addHelpCommand("help [command]", "Display help for a command");
|
|
986
1056
|
program.hook("preAction", async (_thisCommand, actionCommand) => {
|
|
987
1057
|
if (actionCommand.getOptionValue("versionCheck") !== false) {
|
|
988
1058
|
await checkNewVersion();
|
|
@@ -1003,10 +1073,10 @@ async function main() {
|
|
|
1003
1073
|
if (e instanceof CommanderError) {
|
|
1004
1074
|
exitCode = e.exitCode;
|
|
1005
1075
|
} else if (e instanceof CliError) {
|
|
1006
|
-
console.error(
|
|
1076
|
+
console.error(colors8.red(e.message));
|
|
1007
1077
|
exitCode = 1;
|
|
1008
1078
|
} else {
|
|
1009
|
-
console.error(
|
|
1079
|
+
console.error(colors8.red(`Unhandled error: ${e}`));
|
|
1010
1080
|
exitCode = 1;
|
|
1011
1081
|
}
|
|
1012
1082
|
}
|