@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.cjs CHANGED
@@ -28,7 +28,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  ));
29
29
 
30
30
  // src/index.ts
31
- var import_language3 = require("@zenstackhq/language");
31
+ var import_language4 = require("@zenstackhq/language");
32
32
  var import_colors9 = __toESM(require("colors"), 1);
33
33
  var import_commander = require("commander");
34
34
 
@@ -289,6 +289,7 @@ __name(run3, "run");
289
289
 
290
290
  // src/actions/generate.ts
291
291
  var import_common_helpers = require("@zenstackhq/common-helpers");
292
+ var import_language3 = require("@zenstackhq/language");
292
293
  var import_ast2 = require("@zenstackhq/language/ast");
293
294
  var import_utils = require("@zenstackhq/language/utils");
294
295
  var import_colors4 = __toESM(require("colors"), 1);
@@ -296,6 +297,7 @@ var import_jiti = require("jiti");
296
297
  var import_node_fs6 = __toESM(require("fs"), 1);
297
298
  var import_node_path4 = __toESM(require("path"), 1);
298
299
  var import_node_url = require("url");
300
+ var import_chokidar = require("chokidar");
299
301
  var import_ora = __toESM(require("ora"), 1);
300
302
 
301
303
  // src/plugins/index.ts
@@ -363,6 +365,81 @@ var typescript_default = plugin2;
363
365
 
364
366
  // src/actions/generate.ts
365
367
  async function run4(options) {
368
+ const model = await pureGenerate(options, false);
369
+ if (options.watch) {
370
+ const logsEnabled = !options.silent;
371
+ if (logsEnabled) {
372
+ console.log(import_colors4.default.green(`
373
+ Enabled watch mode!`));
374
+ }
375
+ const schemaExtensions = import_language3.ZModelLanguageMetaData.fileExtensions;
376
+ 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");
377
+ const watchedPaths = getRootModelWatchPaths(model);
378
+ if (logsEnabled) {
379
+ const logPaths = [
380
+ ...watchedPaths
381
+ ].map((at) => `- ${at}`).join("\n");
382
+ console.log(`Watched file paths:
383
+ ${logPaths}`);
384
+ }
385
+ const watcher = (0, import_chokidar.watch)([
386
+ ...watchedPaths
387
+ ], {
388
+ alwaysStat: false,
389
+ ignoreInitial: true,
390
+ ignorePermissionErrors: true,
391
+ ignored: /* @__PURE__ */ __name((at) => !schemaExtensions.some((ext) => at.endsWith(ext)), "ignored")
392
+ });
393
+ const reGenerateSchema = (0, import_common_helpers.singleDebounce)(async () => {
394
+ if (logsEnabled) {
395
+ console.log("Got changes, run generation!");
396
+ }
397
+ try {
398
+ const newModel = await pureGenerate(options, true);
399
+ const allModelsPaths = getRootModelWatchPaths(newModel);
400
+ const newModelPaths = [
401
+ ...allModelsPaths
402
+ ].filter((at) => !watchedPaths.has(at));
403
+ const removeModelPaths = [
404
+ ...watchedPaths
405
+ ].filter((at) => !allModelsPaths.has(at));
406
+ if (newModelPaths.length) {
407
+ if (logsEnabled) {
408
+ const logPaths = newModelPaths.map((at) => `- ${at}`).join("\n");
409
+ console.log(`Added file(s) to watch:
410
+ ${logPaths}`);
411
+ }
412
+ newModelPaths.forEach((at) => watchedPaths.add(at));
413
+ watcher.add(newModelPaths);
414
+ }
415
+ if (removeModelPaths.length) {
416
+ if (logsEnabled) {
417
+ const logPaths = removeModelPaths.map((at) => `- ${at}`).join("\n");
418
+ console.log(`Removed file(s) from watch:
419
+ ${logPaths}`);
420
+ }
421
+ removeModelPaths.forEach((at) => watchedPaths.delete(at));
422
+ watcher.unwatch(removeModelPaths);
423
+ }
424
+ } catch (e) {
425
+ console.error(e);
426
+ }
427
+ }, 500, true);
428
+ watcher.on("unlink", (pathAt) => {
429
+ if (logsEnabled) {
430
+ console.log(`Removed file from watch: ${pathAt}`);
431
+ }
432
+ watchedPaths.delete(pathAt);
433
+ watcher.unwatch(pathAt);
434
+ reGenerateSchema();
435
+ });
436
+ watcher.on("change", () => {
437
+ reGenerateSchema();
438
+ });
439
+ }
440
+ }
441
+ __name(run4, "run");
442
+ async function pureGenerate(options, fromWatch) {
366
443
  const start = Date.now();
367
444
  const schemaFile = getSchemaFile(options.schema);
368
445
  const model = await loadSchemaDocument(schemaFile);
@@ -371,7 +448,8 @@ async function run4(options) {
371
448
  if (!options.silent) {
372
449
  console.log(import_colors4.default.green(`Generation completed successfully in ${Date.now() - start}ms.
373
450
  `));
374
- console.log(`You can now create a ZenStack client with it.
451
+ if (!fromWatch) {
452
+ console.log(`You can now create a ZenStack client with it.
375
453
 
376
454
  \`\`\`ts
377
455
  import { ZenStackClient } from '@zenstackhq/orm';
@@ -383,9 +461,11 @@ const client = new ZenStackClient(schema, {
383
461
  \`\`\`
384
462
 
385
463
  Check documentation: https://zenstack.dev/docs/`);
464
+ }
386
465
  }
466
+ return model;
387
467
  }
388
- __name(run4, "run");
468
+ __name(pureGenerate, "pureGenerate");
389
469
  function getOutputPath(options, schemaFile) {
390
470
  if (options.output) {
391
471
  return options.output;
@@ -1165,13 +1245,13 @@ var seedAction = /* @__PURE__ */ __name(async (options, args) => {
1165
1245
  }, "seedAction");
1166
1246
  function createProgram() {
1167
1247
  const program = new import_commander.Command("zen").alias("zenstack").helpOption("-h, --help", "Show this help message").version(getVersion(), "-v --version", "Show CLI version");
1168
- const schemaExtensions = import_language3.ZModelLanguageMetaData.fileExtensions.join(", ");
1248
+ const schemaExtensions = import_language4.ZModelLanguageMetaData.fileExtensions.join(", ");
1169
1249
  program.description(`${import_colors9.default.bold.blue("\u03B6")} ZenStack is the modern data layer for TypeScript apps.
1170
1250
 
1171
1251
  Documentation: https://zenstack.dev/docs`).showHelpAfterError().showSuggestionAfterError();
1172
1252
  const schemaOption = new import_commander.Option("--schema <file>", `schema file (with extension ${schemaExtensions}). Defaults to "zenstack/schema.zmodel" unless specified in package.json.`);
1173
1253
  const noVersionCheckOption = new import_commander.Option("--no-version-check", "do not check for new version");
1174
- program.command("generate").description("Run code generation plugins").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new import_commander.Option("-o, --output <path>", "default output directory for code generation")).addOption(new import_commander.Option("--lite", "also generate a lite version of schema without attributes").default(false)).addOption(new import_commander.Option("--lite-only", "only generate lite version of schema without attributes").default(false)).addOption(new import_commander.Option("--silent", "suppress all output except errors").default(false)).action(generateAction);
1254
+ program.command("generate").description("Run code generation plugins").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new import_commander.Option("-o, --output <path>", "default output directory for code generation")).addOption(new import_commander.Option("-w, --watch", "enable watch mode").default(false)).addOption(new import_commander.Option("--lite", "also generate a lite version of schema without attributes").default(false)).addOption(new import_commander.Option("--lite-only", "only generate lite version of schema without attributes").default(false)).addOption(new import_commander.Option("--silent", "suppress all output except errors").default(false)).action(generateAction);
1175
1255
  const migrateCommand = program.command("migrate").description("Run database schema migration related tasks.");
1176
1256
  const migrationsOption = new import_commander.Option("--migrations <path>", 'path that contains the "migrations" directory');
1177
1257
  migrateCommand.command("dev").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new import_commander.Option("-n, --name <name>", "migration name")).addOption(new import_commander.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));
@@ -1223,6 +1303,9 @@ async function main() {
1223
1303
  exitCode = 1;
1224
1304
  }
1225
1305
  }
1306
+ if (program.args.includes("generate") && (program.args.includes("-w") || program.args.includes("--watch"))) {
1307
+ return;
1308
+ }
1226
1309
  if (telemetry.isTracking) {
1227
1310
  setTimeout(() => {
1228
1311
  process.exit(exitCode);