@zenstackhq/cli 3.1.1 → 3.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.
Files changed (46) hide show
  1. package/dist/index.cjs +88 -5
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.js +89 -6
  4. package/dist/index.js.map +1 -1
  5. package/package.json +17 -9
  6. package/.turbo/turbo-build.log +0 -22
  7. package/eslint.config.js +0 -4
  8. package/scripts/post-build.ts +0 -20
  9. package/src/actions/action-utils.ts +0 -146
  10. package/src/actions/check.ts +0 -22
  11. package/src/actions/db.ts +0 -51
  12. package/src/actions/format.ts +0 -27
  13. package/src/actions/generate.ts +0 -226
  14. package/src/actions/index.ts +0 -10
  15. package/src/actions/info.ts +0 -71
  16. package/src/actions/init.ts +0 -61
  17. package/src/actions/migrate.ts +0 -149
  18. package/src/actions/seed.ts +0 -38
  19. package/src/actions/templates.ts +0 -58
  20. package/src/cli-error.ts +0 -4
  21. package/src/constants.ts +0 -5
  22. package/src/index.ts +0 -233
  23. package/src/plugins/index.ts +0 -2
  24. package/src/plugins/prisma.ts +0 -21
  25. package/src/plugins/typescript.ts +0 -40
  26. package/src/telemetry.ts +0 -139
  27. package/src/utils/exec-utils.ts +0 -61
  28. package/src/utils/is-ci.ts +0 -5
  29. package/src/utils/is-container.ts +0 -23
  30. package/src/utils/is-docker.ts +0 -31
  31. package/src/utils/is-wsl.ts +0 -18
  32. package/src/utils/machine-id-utils.ts +0 -76
  33. package/src/utils/version-utils.ts +0 -50
  34. package/test/check.test.ts +0 -101
  35. package/test/db.test.ts +0 -61
  36. package/test/format.test.ts +0 -33
  37. package/test/generate.test.ts +0 -76
  38. package/test/init.test.ts +0 -14
  39. package/test/migrate.test.ts +0 -72
  40. package/test/plugins/custom-plugin.test.ts +0 -50
  41. package/test/plugins/prisma-plugin.test.ts +0 -81
  42. package/test/ts-schema-gen.test.ts +0 -445
  43. package/test/utils.ts +0 -23
  44. package/tsconfig.json +0 -4
  45. package/tsup.config.ts +0 -13
  46. package/vitest.config.ts +0 -4
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ var __export = (target, all) => {
12
12
  };
13
13
 
14
14
  // src/index.ts
15
- import { ZModelLanguageMetaData } from "@zenstackhq/language";
15
+ import { ZModelLanguageMetaData as ZModelLanguageMetaData2 } from "@zenstackhq/language";
16
16
  import colors9 from "colors";
17
17
  import { Command, CommanderError, Option } from "commander";
18
18
 
@@ -271,7 +271,8 @@ async function run3(options) {
271
271
  __name(run3, "run");
272
272
 
273
273
  // src/actions/generate.ts
274
- import { invariant } from "@zenstackhq/common-helpers";
274
+ import { invariant, singleDebounce } from "@zenstackhq/common-helpers";
275
+ import { ZModelLanguageMetaData } from "@zenstackhq/language";
275
276
  import { isPlugin } from "@zenstackhq/language/ast";
276
277
  import { getLiteral, getLiteralArray } from "@zenstackhq/language/utils";
277
278
  import colors4 from "colors";
@@ -279,6 +280,7 @@ import { createJiti } from "jiti";
279
280
  import fs6 from "fs";
280
281
  import path4 from "path";
281
282
  import { pathToFileURL } from "url";
283
+ import { watch } from "chokidar";
282
284
  import ora from "ora";
283
285
 
284
286
  // src/plugins/index.ts
@@ -346,6 +348,81 @@ var typescript_default = plugin2;
346
348
 
347
349
  // src/actions/generate.ts
348
350
  async function run4(options) {
351
+ const model = await pureGenerate(options, false);
352
+ if (options.watch) {
353
+ const logsEnabled = !options.silent;
354
+ if (logsEnabled) {
355
+ console.log(colors4.green(`
356
+ Enabled watch mode!`));
357
+ }
358
+ const schemaExtensions = ZModelLanguageMetaData.fileExtensions;
359
+ const getRootModelWatchPaths = /* @__PURE__ */ __name((model2) => new Set(model2.declarations.filter((v) => v.$cstNode?.parent?.element.$type === "Model" && !!v.$cstNode.parent.element.$document?.uri?.fsPath).map((v) => v.$cstNode.parent.element.$document.uri.fsPath)), "getRootModelWatchPaths");
360
+ const watchedPaths = getRootModelWatchPaths(model);
361
+ if (logsEnabled) {
362
+ const logPaths = [
363
+ ...watchedPaths
364
+ ].map((at) => `- ${at}`).join("\n");
365
+ console.log(`Watched file paths:
366
+ ${logPaths}`);
367
+ }
368
+ const watcher = watch([
369
+ ...watchedPaths
370
+ ], {
371
+ alwaysStat: false,
372
+ ignoreInitial: true,
373
+ ignorePermissionErrors: true,
374
+ ignored: /* @__PURE__ */ __name((at) => !schemaExtensions.some((ext) => at.endsWith(ext)), "ignored")
375
+ });
376
+ const reGenerateSchema = singleDebounce(async () => {
377
+ if (logsEnabled) {
378
+ console.log("Got changes, run generation!");
379
+ }
380
+ try {
381
+ const newModel = await pureGenerate(options, true);
382
+ const allModelsPaths = getRootModelWatchPaths(newModel);
383
+ const newModelPaths = [
384
+ ...allModelsPaths
385
+ ].filter((at) => !watchedPaths.has(at));
386
+ const removeModelPaths = [
387
+ ...watchedPaths
388
+ ].filter((at) => !allModelsPaths.has(at));
389
+ if (newModelPaths.length) {
390
+ if (logsEnabled) {
391
+ const logPaths = newModelPaths.map((at) => `- ${at}`).join("\n");
392
+ console.log(`Added file(s) to watch:
393
+ ${logPaths}`);
394
+ }
395
+ newModelPaths.forEach((at) => watchedPaths.add(at));
396
+ watcher.add(newModelPaths);
397
+ }
398
+ if (removeModelPaths.length) {
399
+ if (logsEnabled) {
400
+ const logPaths = removeModelPaths.map((at) => `- ${at}`).join("\n");
401
+ console.log(`Removed file(s) from watch:
402
+ ${logPaths}`);
403
+ }
404
+ removeModelPaths.forEach((at) => watchedPaths.delete(at));
405
+ watcher.unwatch(removeModelPaths);
406
+ }
407
+ } catch (e) {
408
+ console.error(e);
409
+ }
410
+ }, 500, true);
411
+ watcher.on("unlink", (pathAt) => {
412
+ if (logsEnabled) {
413
+ console.log(`Removed file from watch: ${pathAt}`);
414
+ }
415
+ watchedPaths.delete(pathAt);
416
+ watcher.unwatch(pathAt);
417
+ reGenerateSchema();
418
+ });
419
+ watcher.on("change", () => {
420
+ reGenerateSchema();
421
+ });
422
+ }
423
+ }
424
+ __name(run4, "run");
425
+ async function pureGenerate(options, fromWatch) {
349
426
  const start = Date.now();
350
427
  const schemaFile = getSchemaFile(options.schema);
351
428
  const model = await loadSchemaDocument(schemaFile);
@@ -354,7 +431,8 @@ async function run4(options) {
354
431
  if (!options.silent) {
355
432
  console.log(colors4.green(`Generation completed successfully in ${Date.now() - start}ms.
356
433
  `));
357
- console.log(`You can now create a ZenStack client with it.
434
+ if (!fromWatch) {
435
+ console.log(`You can now create a ZenStack client with it.
358
436
 
359
437
  \`\`\`ts
360
438
  import { ZenStackClient } from '@zenstackhq/orm';
@@ -366,9 +444,11 @@ const client = new ZenStackClient(schema, {
366
444
  \`\`\`
367
445
 
368
446
  Check documentation: https://zenstack.dev/docs/`);
447
+ }
369
448
  }
449
+ return model;
370
450
  }
371
- __name(run4, "run");
451
+ __name(pureGenerate, "pureGenerate");
372
452
  function getOutputPath(options, schemaFile) {
373
453
  if (options.output) {
374
454
  return options.output;
@@ -1146,13 +1226,13 @@ var seedAction = /* @__PURE__ */ __name(async (options, args) => {
1146
1226
  }, "seedAction");
1147
1227
  function createProgram() {
1148
1228
  const program = new Command("zen").alias("zenstack").helpOption("-h, --help", "Show this help message").version(getVersion(), "-v --version", "Show CLI version");
1149
- const schemaExtensions = ZModelLanguageMetaData.fileExtensions.join(", ");
1229
+ const schemaExtensions = ZModelLanguageMetaData2.fileExtensions.join(", ");
1150
1230
  program.description(`${colors9.bold.blue("\u03B6")} ZenStack is the modern data layer for TypeScript apps.
1151
1231
 
1152
1232
  Documentation: https://zenstack.dev/docs`).showHelpAfterError().showSuggestionAfterError();
1153
1233
  const schemaOption = new Option("--schema <file>", `schema file (with extension ${schemaExtensions}). Defaults to "zenstack/schema.zmodel" unless specified in package.json.`);
1154
1234
  const noVersionCheckOption = new Option("--no-version-check", "do not check for new version");
1155
- 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);
1235
+ 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("-w, --watch", "enable watch mode").default(false)).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);
1156
1236
  const migrateCommand = program.command("migrate").description("Run database schema migration related tasks.");
1157
1237
  const migrationsOption = new Option("--migrations <path>", 'path that contains the "migrations" directory');
1158
1238
  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));
@@ -1204,6 +1284,9 @@ async function main() {
1204
1284
  exitCode = 1;
1205
1285
  }
1206
1286
  }
1287
+ if (program.args.includes("generate") && (program.args.includes("-w") || program.args.includes("--watch"))) {
1288
+ return;
1289
+ }
1207
1290
  if (telemetry.isTracking) {
1208
1291
  setTimeout(() => {
1209
1292
  process.exit(exitCode);