@zenstackhq/cli 3.0.0-alpha.9 → 3.0.0-beta.10
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 +11 -11
- package/dist/index.cjs +652 -120
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -4
- package/dist/index.d.ts +1 -4
- package/dist/index.js +654 -112
- package/dist/index.js.map +1 -1
- package/package.json +15 -11
- package/scripts/post-build.ts +20 -0
- package/src/actions/action-utils.ts +69 -4
- package/src/actions/check.ts +22 -0
- package/src/actions/db.ts +9 -6
- package/src/actions/generate.ts +110 -36
- package/src/actions/index.ts +2 -1
- package/src/actions/info.ts +4 -1
- package/src/actions/migrate.ts +54 -16
- package/src/actions/templates.ts +4 -3
- package/src/constants.ts +5 -0
- package/src/index.ts +99 -28
- package/src/plugins/index.ts +2 -0
- package/src/plugins/prisma.ts +21 -0
- package/src/plugins/typescript.ts +21 -0
- package/src/telemetry.ts +139 -0
- package/src/utils/is-ci.ts +5 -0
- package/src/utils/is-container.ts +23 -0
- package/src/utils/is-docker.ts +31 -0
- package/src/utils/is-wsl.ts +18 -0
- package/src/utils/machine-id-utils.ts +76 -0
- package/src/utils/version-utils.ts +37 -0
- package/test/check.test.ts +101 -0
- package/test/generate.test.ts +12 -9
- package/test/init.test.ts +2 -1
- package/test/migrate.test.ts +33 -1
- package/test/plugins/custom-plugin.test.ts +50 -0
- package/test/plugins/prisma-plugin.test.ts +81 -0
- package/test/ts-schema-gen.test.ts +240 -1
- package/tsconfig.json +0 -3
- package/vitest.config.ts +1 -1
package/dist/index.cjs
CHANGED
|
@@ -26,16 +26,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
27
|
mod
|
|
28
28
|
));
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
29
|
|
|
31
30
|
// src/index.ts
|
|
32
|
-
var src_exports = {};
|
|
33
|
-
__export(src_exports, {
|
|
34
|
-
createProgram: () => createProgram
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(src_exports);
|
|
37
31
|
var import_language2 = require("@zenstackhq/language");
|
|
38
|
-
var
|
|
32
|
+
var import_colors7 = __toESM(require("colors"), 1);
|
|
39
33
|
var import_commander = require("commander");
|
|
40
34
|
|
|
41
35
|
// src/actions/db.ts
|
|
@@ -44,10 +38,10 @@ var import_node_fs2 = __toESM(require("fs"), 1);
|
|
|
44
38
|
// src/utils/exec-utils.ts
|
|
45
39
|
var import_child_process = require("child_process");
|
|
46
40
|
function execSync(cmd, options) {
|
|
47
|
-
const { env, ...restOptions } = options ?? {};
|
|
48
|
-
const mergedEnv =
|
|
41
|
+
const { env: env2, ...restOptions } = options ?? {};
|
|
42
|
+
const mergedEnv = env2 ? {
|
|
49
43
|
...process.env,
|
|
50
|
-
...
|
|
44
|
+
...env2
|
|
51
45
|
} : void 0;
|
|
52
46
|
(0, import_child_process.execSync)(cmd, {
|
|
53
47
|
encoding: "utf-8",
|
|
@@ -65,6 +59,7 @@ __name(execPackage, "execPackage");
|
|
|
65
59
|
|
|
66
60
|
// src/actions/action-utils.ts
|
|
67
61
|
var import_language = require("@zenstackhq/language");
|
|
62
|
+
var import_ast = require("@zenstackhq/language/ast");
|
|
68
63
|
var import_sdk = require("@zenstackhq/sdk");
|
|
69
64
|
var import_colors = __toESM(require("colors"), 1);
|
|
70
65
|
var import_node_fs = __toESM(require("fs"), 1);
|
|
@@ -85,6 +80,13 @@ function getSchemaFile(file) {
|
|
|
85
80
|
}
|
|
86
81
|
return file;
|
|
87
82
|
}
|
|
83
|
+
const pkgJsonConfig = getPkgJsonConfig(process.cwd());
|
|
84
|
+
if (pkgJsonConfig.schema) {
|
|
85
|
+
if (!import_node_fs.default.existsSync(pkgJsonConfig.schema)) {
|
|
86
|
+
throw new CliError(`Schema file not found: ${pkgJsonConfig.schema}`);
|
|
87
|
+
}
|
|
88
|
+
return pkgJsonConfig.schema;
|
|
89
|
+
}
|
|
88
90
|
if (import_node_fs.default.existsSync("./zenstack/schema.zmodel")) {
|
|
89
91
|
return "./zenstack/schema.zmodel";
|
|
90
92
|
} else if (import_node_fs.default.existsSync("./schema.zmodel")) {
|
|
@@ -97,12 +99,14 @@ __name(getSchemaFile, "getSchemaFile");
|
|
|
97
99
|
async function loadSchemaDocument(schemaFile) {
|
|
98
100
|
const loadResult = await (0, import_language.loadDocument)(schemaFile);
|
|
99
101
|
if (!loadResult.success) {
|
|
100
|
-
console.error(import_colors.default.red("Error loading schema:"));
|
|
101
102
|
loadResult.errors.forEach((err) => {
|
|
102
103
|
console.error(import_colors.default.red(err));
|
|
103
104
|
});
|
|
104
|
-
throw new CliError("
|
|
105
|
+
throw new CliError("Schema contains errors. See above for details.");
|
|
105
106
|
}
|
|
107
|
+
loadResult.warnings.forEach((warn) => {
|
|
108
|
+
console.warn(import_colors.default.yellow(warn));
|
|
109
|
+
});
|
|
106
110
|
return loadResult.model;
|
|
107
111
|
}
|
|
108
112
|
__name(loadSchemaDocument, "loadSchemaDocument");
|
|
@@ -114,14 +118,62 @@ function handleSubProcessError(err) {
|
|
|
114
118
|
}
|
|
115
119
|
}
|
|
116
120
|
__name(handleSubProcessError, "handleSubProcessError");
|
|
117
|
-
async function generateTempPrismaSchema(zmodelPath) {
|
|
121
|
+
async function generateTempPrismaSchema(zmodelPath, folder) {
|
|
118
122
|
const model = await loadSchemaDocument(zmodelPath);
|
|
123
|
+
if (!model.declarations.some(import_ast.isDataSource)) {
|
|
124
|
+
throw new CliError("Schema must define a datasource");
|
|
125
|
+
}
|
|
119
126
|
const prismaSchema = await new import_sdk.PrismaSchemaGenerator(model).generate();
|
|
120
|
-
|
|
127
|
+
if (!folder) {
|
|
128
|
+
folder = import_node_path.default.dirname(zmodelPath);
|
|
129
|
+
}
|
|
130
|
+
const prismaSchemaFile = import_node_path.default.resolve(folder, "~schema.prisma");
|
|
121
131
|
import_node_fs.default.writeFileSync(prismaSchemaFile, prismaSchema);
|
|
122
132
|
return prismaSchemaFile;
|
|
123
133
|
}
|
|
124
134
|
__name(generateTempPrismaSchema, "generateTempPrismaSchema");
|
|
135
|
+
function getPkgJsonConfig(startPath) {
|
|
136
|
+
const result = {
|
|
137
|
+
schema: void 0,
|
|
138
|
+
output: void 0
|
|
139
|
+
};
|
|
140
|
+
const pkgJsonFile = findUp([
|
|
141
|
+
"package.json"
|
|
142
|
+
], startPath, false);
|
|
143
|
+
if (!pkgJsonFile) {
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
let pkgJson = void 0;
|
|
147
|
+
try {
|
|
148
|
+
pkgJson = JSON.parse(import_node_fs.default.readFileSync(pkgJsonFile, "utf8"));
|
|
149
|
+
} catch {
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
if (pkgJson.zenstack && typeof pkgJson.zenstack === "object") {
|
|
153
|
+
result.schema = pkgJson.zenstack.schema && import_node_path.default.resolve(import_node_path.default.dirname(pkgJsonFile), pkgJson.zenstack.schema);
|
|
154
|
+
result.output = pkgJson.zenstack.output && import_node_path.default.resolve(import_node_path.default.dirname(pkgJsonFile), pkgJson.zenstack.output);
|
|
155
|
+
}
|
|
156
|
+
return result;
|
|
157
|
+
}
|
|
158
|
+
__name(getPkgJsonConfig, "getPkgJsonConfig");
|
|
159
|
+
function findUp(names, cwd = process.cwd(), multiple = false, result = []) {
|
|
160
|
+
if (!names.some((name) => !!name)) {
|
|
161
|
+
return void 0;
|
|
162
|
+
}
|
|
163
|
+
const target = names.find((name) => import_node_fs.default.existsSync(import_node_path.default.join(cwd, name)));
|
|
164
|
+
if (multiple === false && target) {
|
|
165
|
+
return import_node_path.default.join(cwd, target);
|
|
166
|
+
}
|
|
167
|
+
if (target) {
|
|
168
|
+
result.push(import_node_path.default.join(cwd, target));
|
|
169
|
+
}
|
|
170
|
+
const up = import_node_path.default.resolve(cwd, "..");
|
|
171
|
+
if (up === cwd) {
|
|
172
|
+
return multiple && result.length > 0 ? result : void 0;
|
|
173
|
+
}
|
|
174
|
+
return findUp(names, up, multiple, result);
|
|
175
|
+
}
|
|
176
|
+
__name(findUp, "findUp");
|
|
125
177
|
|
|
126
178
|
// src/actions/db.ts
|
|
127
179
|
async function run(command, options) {
|
|
@@ -136,11 +188,15 @@ async function runPush(options) {
|
|
|
136
188
|
const schemaFile = getSchemaFile(options.schema);
|
|
137
189
|
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile);
|
|
138
190
|
try {
|
|
139
|
-
const cmd =
|
|
191
|
+
const cmd = [
|
|
192
|
+
"prisma db push",
|
|
193
|
+
` --schema "${prismaSchemaFile}"`,
|
|
194
|
+
options.acceptDataLoss ? " --accept-data-loss" : "",
|
|
195
|
+
options.forceReset ? " --force-reset" : "",
|
|
196
|
+
" --skip-generate"
|
|
197
|
+
].join("");
|
|
140
198
|
try {
|
|
141
|
-
await execPackage(cmd
|
|
142
|
-
stdio: "inherit"
|
|
143
|
-
});
|
|
199
|
+
await execPackage(cmd);
|
|
144
200
|
} catch (err) {
|
|
145
201
|
handleSubProcessError(err);
|
|
146
202
|
}
|
|
@@ -154,31 +210,74 @@ __name(runPush, "runPush");
|
|
|
154
210
|
|
|
155
211
|
// src/actions/generate.ts
|
|
156
212
|
var import_common_helpers = require("@zenstackhq/common-helpers");
|
|
157
|
-
var
|
|
158
|
-
var
|
|
213
|
+
var import_ast2 = require("@zenstackhq/language/ast");
|
|
214
|
+
var import_utils = require("@zenstackhq/language/utils");
|
|
159
215
|
var import_colors2 = __toESM(require("colors"), 1);
|
|
216
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
217
|
+
var import_ora = __toESM(require("ora"), 1);
|
|
218
|
+
|
|
219
|
+
// src/plugins/index.ts
|
|
220
|
+
var plugins_exports = {};
|
|
221
|
+
__export(plugins_exports, {
|
|
222
|
+
prisma: () => prisma_default,
|
|
223
|
+
typescript: () => typescript_default
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
// src/plugins/prisma.ts
|
|
227
|
+
var import_sdk2 = require("@zenstackhq/sdk");
|
|
160
228
|
var import_node_fs3 = __toESM(require("fs"), 1);
|
|
161
229
|
var import_node_path2 = __toESM(require("path"), 1);
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
230
|
+
var plugin = {
|
|
231
|
+
name: "Prisma Schema Generator",
|
|
232
|
+
statusText: "Generating Prisma schema",
|
|
233
|
+
async generate({ model, defaultOutputPath, pluginOptions }) {
|
|
234
|
+
let outFile = import_node_path2.default.join(defaultOutputPath, "schema.prisma");
|
|
235
|
+
if (typeof pluginOptions["output"] === "string") {
|
|
236
|
+
outFile = import_node_path2.default.resolve(defaultOutputPath, pluginOptions["output"]);
|
|
237
|
+
if (!import_node_fs3.default.existsSync(import_node_path2.default.dirname(outFile))) {
|
|
238
|
+
import_node_fs3.default.mkdirSync(import_node_path2.default.dirname(outFile), {
|
|
239
|
+
recursive: true
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
}
|
|
170
243
|
const prismaSchema = await new import_sdk2.PrismaSchemaGenerator(model).generate();
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
244
|
+
import_node_fs3.default.writeFileSync(outFile, prismaSchema);
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
var prisma_default = plugin;
|
|
248
|
+
|
|
249
|
+
// src/plugins/typescript.ts
|
|
250
|
+
var import_sdk3 = require("@zenstackhq/sdk");
|
|
251
|
+
var import_node_fs4 = __toESM(require("fs"), 1);
|
|
252
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
253
|
+
var plugin2 = {
|
|
254
|
+
name: "TypeScript Schema Generator",
|
|
255
|
+
statusText: "Generating TypeScript schema",
|
|
256
|
+
async generate({ model, defaultOutputPath, pluginOptions }) {
|
|
257
|
+
let outDir = defaultOutputPath;
|
|
258
|
+
if (typeof pluginOptions["output"] === "string") {
|
|
259
|
+
outDir = import_node_path3.default.resolve(defaultOutputPath, pluginOptions["output"]);
|
|
260
|
+
if (!import_node_fs4.default.existsSync(outDir)) {
|
|
261
|
+
import_node_fs4.default.mkdirSync(outDir, {
|
|
262
|
+
recursive: true
|
|
263
|
+
});
|
|
264
|
+
}
|
|
177
265
|
}
|
|
178
|
-
|
|
266
|
+
await new import_sdk3.TsSchemaGenerator().generate(model, outDir);
|
|
179
267
|
}
|
|
268
|
+
};
|
|
269
|
+
var typescript_default = plugin2;
|
|
270
|
+
|
|
271
|
+
// src/actions/generate.ts
|
|
272
|
+
async function run2(options) {
|
|
273
|
+
const start = Date.now();
|
|
274
|
+
const schemaFile = getSchemaFile(options.schema);
|
|
275
|
+
const model = await loadSchemaDocument(schemaFile);
|
|
276
|
+
const outputPath = getOutputPath(options, schemaFile);
|
|
277
|
+
await runPlugins(schemaFile, model, outputPath, options);
|
|
180
278
|
if (!options.silent) {
|
|
181
|
-
console.log(import_colors2.default.green(
|
|
279
|
+
console.log(import_colors2.default.green(`Generation completed successfully in ${Date.now() - start}ms.
|
|
280
|
+
`));
|
|
182
281
|
console.log(`You can now create a ZenStack client with it.
|
|
183
282
|
|
|
184
283
|
\`\`\`ts
|
|
@@ -186,37 +285,113 @@ import { ZenStackClient } from '@zenstackhq/runtime';
|
|
|
186
285
|
import { schema } from '${outputPath}/schema';
|
|
187
286
|
|
|
188
287
|
const client = new ZenStackClient(schema, {
|
|
189
|
-
|
|
288
|
+
dialect: { ... }
|
|
190
289
|
});
|
|
191
290
|
\`\`\`
|
|
192
|
-
|
|
291
|
+
|
|
292
|
+
Check documentation: https://zenstack.dev/docs/3.x`);
|
|
193
293
|
}
|
|
194
294
|
}
|
|
195
295
|
__name(run2, "run");
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
296
|
+
function getOutputPath(options, schemaFile) {
|
|
297
|
+
if (options.output) {
|
|
298
|
+
return options.output;
|
|
299
|
+
}
|
|
300
|
+
const pkgJsonConfig = getPkgJsonConfig(process.cwd());
|
|
301
|
+
if (pkgJsonConfig.output) {
|
|
302
|
+
return pkgJsonConfig.output;
|
|
303
|
+
} else {
|
|
304
|
+
return import_node_path4.default.dirname(schemaFile);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
__name(getOutputPath, "getOutputPath");
|
|
308
|
+
async function runPlugins(schemaFile, model, outputPath, options) {
|
|
309
|
+
const plugins = model.declarations.filter(import_ast2.isPlugin);
|
|
310
|
+
const processedPlugins = [];
|
|
311
|
+
for (const plugin3 of plugins) {
|
|
312
|
+
const provider = getPluginProvider(plugin3);
|
|
313
|
+
let cliPlugin;
|
|
314
|
+
if (provider.startsWith("@core/")) {
|
|
315
|
+
cliPlugin = plugins_exports[provider.slice("@core/".length)];
|
|
316
|
+
if (!cliPlugin) {
|
|
317
|
+
throw new CliError(`Unknown core plugin: ${provider}`);
|
|
318
|
+
}
|
|
319
|
+
} else {
|
|
320
|
+
let moduleSpec = provider;
|
|
321
|
+
if (moduleSpec.startsWith(".")) {
|
|
322
|
+
moduleSpec = import_node_path4.default.resolve(import_node_path4.default.dirname(schemaFile), moduleSpec);
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
cliPlugin = (await import(moduleSpec)).default;
|
|
326
|
+
} catch {
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (cliPlugin) {
|
|
330
|
+
processedPlugins.push({
|
|
331
|
+
cliPlugin,
|
|
332
|
+
pluginOptions: getPluginOptions(plugin3)
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const defaultPlugins = [
|
|
337
|
+
typescript_default
|
|
338
|
+
].reverse();
|
|
339
|
+
defaultPlugins.forEach((d) => {
|
|
340
|
+
if (!processedPlugins.some((p) => p.cliPlugin === d)) {
|
|
341
|
+
processedPlugins.push({
|
|
342
|
+
cliPlugin: d,
|
|
343
|
+
pluginOptions: {}
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
for (const { cliPlugin, pluginOptions } of processedPlugins) {
|
|
348
|
+
(0, import_common_helpers.invariant)(typeof cliPlugin.generate === "function", `Plugin ${cliPlugin.name} does not have a generate function`);
|
|
349
|
+
let spinner;
|
|
350
|
+
if (!options.silent) {
|
|
351
|
+
spinner = (0, import_ora.default)(cliPlugin.statusText ?? `Running plugin ${cliPlugin.name}`).start();
|
|
352
|
+
}
|
|
353
|
+
try {
|
|
354
|
+
await cliPlugin.generate({
|
|
355
|
+
schemaFile,
|
|
356
|
+
model,
|
|
357
|
+
defaultOutputPath: outputPath,
|
|
358
|
+
pluginOptions
|
|
359
|
+
});
|
|
360
|
+
spinner?.succeed();
|
|
361
|
+
} catch (err) {
|
|
362
|
+
spinner?.fail();
|
|
363
|
+
console.error(err);
|
|
205
364
|
}
|
|
206
|
-
const generator = (await import(useProvider)).default;
|
|
207
|
-
console.log("Running generator:", provider);
|
|
208
|
-
await generator({
|
|
209
|
-
model,
|
|
210
|
-
outputPath,
|
|
211
|
-
tsSchemaFile
|
|
212
|
-
});
|
|
213
365
|
}
|
|
214
366
|
}
|
|
215
367
|
__name(runPlugins, "runPlugins");
|
|
368
|
+
function getPluginProvider(plugin3) {
|
|
369
|
+
const providerField = plugin3.fields.find((f) => f.name === "provider");
|
|
370
|
+
(0, import_common_helpers.invariant)(providerField, `Plugin ${plugin3.name} does not have a provider field`);
|
|
371
|
+
const provider = providerField.value.value;
|
|
372
|
+
return provider;
|
|
373
|
+
}
|
|
374
|
+
__name(getPluginProvider, "getPluginProvider");
|
|
375
|
+
function getPluginOptions(plugin3) {
|
|
376
|
+
const result = {};
|
|
377
|
+
for (const field of plugin3.fields) {
|
|
378
|
+
if (field.name === "provider") {
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const value = (0, import_utils.getLiteral)(field.value) ?? (0, import_utils.getLiteralArray)(field.value);
|
|
382
|
+
if (value === void 0) {
|
|
383
|
+
console.warn(`Plugin "${plugin3.name}" option "${field.name}" has unsupported value, skipping`);
|
|
384
|
+
continue;
|
|
385
|
+
}
|
|
386
|
+
result[field.name] = value;
|
|
387
|
+
}
|
|
388
|
+
return result;
|
|
389
|
+
}
|
|
390
|
+
__name(getPluginOptions, "getPluginOptions");
|
|
216
391
|
|
|
217
392
|
// src/actions/info.ts
|
|
218
393
|
var import_colors3 = __toESM(require("colors"), 1);
|
|
219
|
-
var
|
|
394
|
+
var import_node_path5 = __toESM(require("path"), 1);
|
|
220
395
|
async function run3(projectPath) {
|
|
221
396
|
const packages = await getZenStackPackages(projectPath);
|
|
222
397
|
if (!packages) {
|
|
@@ -225,11 +400,11 @@ async function run3(projectPath) {
|
|
|
225
400
|
}
|
|
226
401
|
console.log("Installed ZenStack Packages:");
|
|
227
402
|
const versions = /* @__PURE__ */ new Set();
|
|
228
|
-
for (const { pkg, version } of packages) {
|
|
229
|
-
if (
|
|
230
|
-
versions.add(
|
|
403
|
+
for (const { pkg, version: version2 } of packages) {
|
|
404
|
+
if (version2) {
|
|
405
|
+
versions.add(version2);
|
|
231
406
|
}
|
|
232
|
-
console.log(` ${import_colors3.default.green(pkg.padEnd(20))} ${
|
|
407
|
+
console.log(` ${import_colors3.default.green(pkg.padEnd(20))} ${version2}`);
|
|
233
408
|
}
|
|
234
409
|
if (versions.size > 1) {
|
|
235
410
|
console.warn(import_colors3.default.yellow("WARNING: Multiple versions of Zenstack packages detected. This may cause issues."));
|
|
@@ -238,9 +413,9 @@ async function run3(projectPath) {
|
|
|
238
413
|
__name(run3, "run");
|
|
239
414
|
async function getZenStackPackages(projectPath) {
|
|
240
415
|
let pkgJson;
|
|
241
|
-
const resolvedPath =
|
|
416
|
+
const resolvedPath = import_node_path5.default.resolve(projectPath);
|
|
242
417
|
try {
|
|
243
|
-
pkgJson = (await import(
|
|
418
|
+
pkgJson = (await import(import_node_path5.default.join(resolvedPath, "package.json"), {
|
|
244
419
|
with: {
|
|
245
420
|
type: "json"
|
|
246
421
|
}
|
|
@@ -259,6 +434,9 @@ async function getZenStackPackages(projectPath) {
|
|
|
259
434
|
type: "json"
|
|
260
435
|
}
|
|
261
436
|
})).default;
|
|
437
|
+
if (depPkgJson.private) {
|
|
438
|
+
return void 0;
|
|
439
|
+
}
|
|
262
440
|
return {
|
|
263
441
|
pkg,
|
|
264
442
|
version: depPkgJson.version
|
|
@@ -270,15 +448,15 @@ async function getZenStackPackages(projectPath) {
|
|
|
270
448
|
};
|
|
271
449
|
}
|
|
272
450
|
}));
|
|
273
|
-
return result;
|
|
451
|
+
return result.filter((p) => !!p);
|
|
274
452
|
}
|
|
275
453
|
__name(getZenStackPackages, "getZenStackPackages");
|
|
276
454
|
|
|
277
455
|
// src/actions/init.ts
|
|
278
456
|
var import_colors4 = __toESM(require("colors"), 1);
|
|
279
|
-
var
|
|
280
|
-
var
|
|
281
|
-
var
|
|
457
|
+
var import_node_fs5 = __toESM(require("fs"), 1);
|
|
458
|
+
var import_node_path6 = __toESM(require("path"), 1);
|
|
459
|
+
var import_ora2 = __toESM(require("ora"), 1);
|
|
282
460
|
var import_package_manager_detector = require("package-manager-detector");
|
|
283
461
|
|
|
284
462
|
// src/actions/templates.ts
|
|
@@ -340,7 +518,7 @@ async function run4(projectPath) {
|
|
|
340
518
|
if (!resolved) {
|
|
341
519
|
throw new CliError(`Unable to determine how to install package "${pkg.name}". Please install it manually.`);
|
|
342
520
|
}
|
|
343
|
-
const spinner = (0,
|
|
521
|
+
const spinner = (0, import_ora2.default)(`Installing "${pkg.name}"`).start();
|
|
344
522
|
try {
|
|
345
523
|
execSync(`${resolved.command} ${resolved.args.join(" ")}`, {
|
|
346
524
|
cwd: projectPath
|
|
@@ -352,11 +530,11 @@ async function run4(projectPath) {
|
|
|
352
530
|
}
|
|
353
531
|
}
|
|
354
532
|
const generationFolder = "zenstack";
|
|
355
|
-
if (!
|
|
356
|
-
|
|
533
|
+
if (!import_node_fs5.default.existsSync(import_node_path6.default.join(projectPath, generationFolder))) {
|
|
534
|
+
import_node_fs5.default.mkdirSync(import_node_path6.default.join(projectPath, generationFolder));
|
|
357
535
|
}
|
|
358
|
-
if (!
|
|
359
|
-
|
|
536
|
+
if (!import_node_fs5.default.existsSync(import_node_path6.default.join(projectPath, generationFolder, "schema.zmodel"))) {
|
|
537
|
+
import_node_fs5.default.writeFileSync(import_node_path6.default.join(projectPath, generationFolder, "schema.zmodel"), STARTER_ZMODEL);
|
|
360
538
|
} else {
|
|
361
539
|
console.log(import_colors4.default.yellow("Schema file already exists. Skipping generation of sample."));
|
|
362
540
|
}
|
|
@@ -367,10 +545,12 @@ async function run4(projectPath) {
|
|
|
367
545
|
__name(run4, "run");
|
|
368
546
|
|
|
369
547
|
// src/actions/migrate.ts
|
|
370
|
-
var
|
|
548
|
+
var import_node_fs6 = __toESM(require("fs"), 1);
|
|
549
|
+
var import_node_path7 = __toESM(require("path"), 1);
|
|
371
550
|
async function run5(command, options) {
|
|
372
551
|
const schemaFile = getSchemaFile(options.schema);
|
|
373
|
-
const
|
|
552
|
+
const prismaSchemaDir = options.migrations ? import_node_path7.default.dirname(options.migrations) : void 0;
|
|
553
|
+
const prismaSchemaFile = await generateTempPrismaSchema(schemaFile, prismaSchemaDir);
|
|
374
554
|
try {
|
|
375
555
|
switch (command) {
|
|
376
556
|
case "dev":
|
|
@@ -385,19 +565,27 @@ async function run5(command, options) {
|
|
|
385
565
|
case "status":
|
|
386
566
|
await runStatus(prismaSchemaFile, options);
|
|
387
567
|
break;
|
|
568
|
+
case "resolve":
|
|
569
|
+
await runResolve(prismaSchemaFile, options);
|
|
570
|
+
break;
|
|
388
571
|
}
|
|
389
572
|
} finally {
|
|
390
|
-
if (
|
|
391
|
-
|
|
573
|
+
if (import_node_fs6.default.existsSync(prismaSchemaFile)) {
|
|
574
|
+
import_node_fs6.default.unlinkSync(prismaSchemaFile);
|
|
392
575
|
}
|
|
393
576
|
}
|
|
394
577
|
}
|
|
395
578
|
__name(run5, "run");
|
|
396
579
|
async function runDev(prismaSchemaFile, options) {
|
|
397
580
|
try {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
581
|
+
const cmd = [
|
|
582
|
+
"prisma migrate dev",
|
|
583
|
+
` --schema "${prismaSchemaFile}"`,
|
|
584
|
+
" --skip-generate",
|
|
585
|
+
options.name ? ` --name ${options.name}` : "",
|
|
586
|
+
options.createOnly ? " --create-only" : ""
|
|
587
|
+
].join("");
|
|
588
|
+
await execPackage(cmd);
|
|
401
589
|
} catch (err) {
|
|
402
590
|
handleSubProcessError2(err);
|
|
403
591
|
}
|
|
@@ -405,9 +593,13 @@ async function runDev(prismaSchemaFile, options) {
|
|
|
405
593
|
__name(runDev, "runDev");
|
|
406
594
|
async function runReset(prismaSchemaFile, options) {
|
|
407
595
|
try {
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
596
|
+
const cmd = [
|
|
597
|
+
"prisma migrate reset",
|
|
598
|
+
` --schema "${prismaSchemaFile}"`,
|
|
599
|
+
" --skip-generate",
|
|
600
|
+
options.force ? " --force" : ""
|
|
601
|
+
].join("");
|
|
602
|
+
await execPackage(cmd);
|
|
411
603
|
} catch (err) {
|
|
412
604
|
handleSubProcessError2(err);
|
|
413
605
|
}
|
|
@@ -415,9 +607,11 @@ async function runReset(prismaSchemaFile, options) {
|
|
|
415
607
|
__name(runReset, "runReset");
|
|
416
608
|
async function runDeploy(prismaSchemaFile, _options) {
|
|
417
609
|
try {
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
610
|
+
const cmd = [
|
|
611
|
+
"prisma migrate deploy",
|
|
612
|
+
` --schema "${prismaSchemaFile}"`
|
|
613
|
+
].join("");
|
|
614
|
+
await execPackage(cmd);
|
|
421
615
|
} catch (err) {
|
|
422
616
|
handleSubProcessError2(err);
|
|
423
617
|
}
|
|
@@ -425,14 +619,29 @@ async function runDeploy(prismaSchemaFile, _options) {
|
|
|
425
619
|
__name(runDeploy, "runDeploy");
|
|
426
620
|
async function runStatus(prismaSchemaFile, _options) {
|
|
427
621
|
try {
|
|
428
|
-
await execPackage(`prisma migrate status --schema "${prismaSchemaFile}"
|
|
429
|
-
stdio: "inherit"
|
|
430
|
-
});
|
|
622
|
+
await execPackage(`prisma migrate status --schema "${prismaSchemaFile}"`);
|
|
431
623
|
} catch (err) {
|
|
432
624
|
handleSubProcessError2(err);
|
|
433
625
|
}
|
|
434
626
|
}
|
|
435
627
|
__name(runStatus, "runStatus");
|
|
628
|
+
async function runResolve(prismaSchemaFile, options) {
|
|
629
|
+
if (!options.applied && !options.rolledBack) {
|
|
630
|
+
throw new CliError("Either --applied or --rolled-back option must be provided");
|
|
631
|
+
}
|
|
632
|
+
try {
|
|
633
|
+
const cmd = [
|
|
634
|
+
"prisma migrate resolve",
|
|
635
|
+
` --schema "${prismaSchemaFile}"`,
|
|
636
|
+
options.applied ? ` --applied ${options.applied}` : "",
|
|
637
|
+
options.rolledBack ? ` --rolled-back ${options.rolledBack}` : ""
|
|
638
|
+
].join("");
|
|
639
|
+
await execPackage(cmd);
|
|
640
|
+
} catch (err) {
|
|
641
|
+
handleSubProcessError2(err);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
__name(runResolve, "runResolve");
|
|
436
645
|
function handleSubProcessError2(err) {
|
|
437
646
|
if (err instanceof Error && "status" in err && typeof err.status === "number") {
|
|
438
647
|
process.exit(err.status);
|
|
@@ -442,62 +651,385 @@ function handleSubProcessError2(err) {
|
|
|
442
651
|
}
|
|
443
652
|
__name(handleSubProcessError2, "handleSubProcessError");
|
|
444
653
|
|
|
654
|
+
// src/actions/check.ts
|
|
655
|
+
var import_colors5 = __toESM(require("colors"), 1);
|
|
656
|
+
async function run6(options) {
|
|
657
|
+
const schemaFile = getSchemaFile(options.schema);
|
|
658
|
+
try {
|
|
659
|
+
await loadSchemaDocument(schemaFile);
|
|
660
|
+
console.log(import_colors5.default.green("\u2713 Schema validation completed successfully."));
|
|
661
|
+
} catch (error) {
|
|
662
|
+
console.error(import_colors5.default.red("\u2717 Schema validation failed."));
|
|
663
|
+
throw error;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
__name(run6, "run");
|
|
667
|
+
|
|
668
|
+
// src/telemetry.ts
|
|
669
|
+
var import_mixpanel = require("mixpanel");
|
|
670
|
+
var import_node_crypto2 = require("crypto");
|
|
671
|
+
var import_node_fs11 = __toESM(require("fs"), 1);
|
|
672
|
+
var os2 = __toESM(require("os"), 1);
|
|
673
|
+
|
|
674
|
+
// src/constants.ts
|
|
675
|
+
var TELEMETRY_TRACKING_TOKEN = "74944eb779d7d3b4ce185be843fde9fc";
|
|
676
|
+
|
|
677
|
+
// src/utils/is-ci.ts
|
|
678
|
+
var import_node_process = require("process");
|
|
679
|
+
var isInCi = import_node_process.env["CI"] !== "0" && import_node_process.env["CI"] !== "false" && ("CI" in import_node_process.env || "CONTINUOUS_INTEGRATION" in import_node_process.env || Object.keys(import_node_process.env).some((key) => key.startsWith("CI_")));
|
|
680
|
+
|
|
681
|
+
// src/utils/is-container.ts
|
|
682
|
+
var import_node_fs8 = __toESM(require("fs"), 1);
|
|
683
|
+
|
|
684
|
+
// src/utils/is-docker.ts
|
|
685
|
+
var import_node_fs7 = __toESM(require("fs"), 1);
|
|
686
|
+
var isDockerCached;
|
|
687
|
+
function hasDockerEnv() {
|
|
688
|
+
try {
|
|
689
|
+
import_node_fs7.default.statSync("/.dockerenv");
|
|
690
|
+
return true;
|
|
691
|
+
} catch {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
__name(hasDockerEnv, "hasDockerEnv");
|
|
696
|
+
function hasDockerCGroup() {
|
|
697
|
+
try {
|
|
698
|
+
return import_node_fs7.default.readFileSync("/proc/self/cgroup", "utf8").includes("docker");
|
|
699
|
+
} catch {
|
|
700
|
+
return false;
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
__name(hasDockerCGroup, "hasDockerCGroup");
|
|
704
|
+
function isDocker() {
|
|
705
|
+
if (isDockerCached === void 0) {
|
|
706
|
+
isDockerCached = hasDockerEnv() || hasDockerCGroup();
|
|
707
|
+
}
|
|
708
|
+
return isDockerCached;
|
|
709
|
+
}
|
|
710
|
+
__name(isDocker, "isDocker");
|
|
711
|
+
|
|
712
|
+
// src/utils/is-container.ts
|
|
713
|
+
var cachedResult;
|
|
714
|
+
var hasContainerEnv = /* @__PURE__ */ __name(() => {
|
|
715
|
+
try {
|
|
716
|
+
import_node_fs8.default.statSync("/run/.containerenv");
|
|
717
|
+
return true;
|
|
718
|
+
} catch {
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
}, "hasContainerEnv");
|
|
722
|
+
function isInContainer() {
|
|
723
|
+
if (cachedResult === void 0) {
|
|
724
|
+
cachedResult = hasContainerEnv() || isDocker();
|
|
725
|
+
}
|
|
726
|
+
return cachedResult;
|
|
727
|
+
}
|
|
728
|
+
__name(isInContainer, "isInContainer");
|
|
729
|
+
|
|
730
|
+
// src/utils/is-wsl.ts
|
|
731
|
+
var import_node_process2 = __toESM(require("process"), 1);
|
|
732
|
+
var import_node_os = __toESM(require("os"), 1);
|
|
733
|
+
var import_node_fs9 = __toESM(require("fs"), 1);
|
|
734
|
+
var isWsl = /* @__PURE__ */ __name(() => {
|
|
735
|
+
if (import_node_process2.default.platform !== "linux") {
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
if (import_node_os.default.release().toLowerCase().includes("microsoft")) {
|
|
739
|
+
return true;
|
|
740
|
+
}
|
|
741
|
+
try {
|
|
742
|
+
return import_node_fs9.default.readFileSync("/proc/version", "utf8").toLowerCase().includes("microsoft");
|
|
743
|
+
} catch {
|
|
744
|
+
return false;
|
|
745
|
+
}
|
|
746
|
+
}, "isWsl");
|
|
747
|
+
|
|
748
|
+
// src/utils/machine-id-utils.ts
|
|
749
|
+
var import_child_process2 = require("child_process");
|
|
750
|
+
var import_node_crypto = require("crypto");
|
|
751
|
+
var { platform } = process;
|
|
752
|
+
var win32RegBinPath = {
|
|
753
|
+
native: "%windir%\\System32",
|
|
754
|
+
mixed: "%windir%\\sysnative\\cmd.exe /c %windir%\\System32"
|
|
755
|
+
};
|
|
756
|
+
var guid = {
|
|
757
|
+
darwin: "ioreg -rd1 -c IOPlatformExpertDevice",
|
|
758
|
+
win32: `${win32RegBinPath[isWindowsProcessMixedOrNativeArchitecture()]}\\REG.exe QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography /v MachineGuid`,
|
|
759
|
+
linux: "( cat /var/lib/dbus/machine-id /etc/machine-id 2> /dev/null || hostname 2> /dev/null) | head -n 1 || :",
|
|
760
|
+
freebsd: "kenv -q smbios.system.uuid || sysctl -n kern.hostuuid"
|
|
761
|
+
};
|
|
762
|
+
function isWindowsProcessMixedOrNativeArchitecture() {
|
|
763
|
+
if (process.arch === "ia32" && process.env.hasOwnProperty("PROCESSOR_ARCHITEW6432")) {
|
|
764
|
+
return "mixed";
|
|
765
|
+
}
|
|
766
|
+
return "native";
|
|
767
|
+
}
|
|
768
|
+
__name(isWindowsProcessMixedOrNativeArchitecture, "isWindowsProcessMixedOrNativeArchitecture");
|
|
769
|
+
function hash(guid2) {
|
|
770
|
+
return (0, import_node_crypto.createHash)("sha256").update(guid2).digest("hex");
|
|
771
|
+
}
|
|
772
|
+
__name(hash, "hash");
|
|
773
|
+
function expose(result) {
|
|
774
|
+
switch (platform) {
|
|
775
|
+
case "darwin":
|
|
776
|
+
return result.split("IOPlatformUUID")[1]?.split("\n")[0]?.replace(/=|\s+|"/gi, "").toLowerCase();
|
|
777
|
+
case "win32":
|
|
778
|
+
return result.toString().split("REG_SZ")[1]?.replace(/\r+|\n+|\s+/gi, "").toLowerCase();
|
|
779
|
+
case "linux":
|
|
780
|
+
return result.toString().replace(/\r+|\n+|\s+/gi, "").toLowerCase();
|
|
781
|
+
case "freebsd":
|
|
782
|
+
return result.toString().replace(/\r+|\n+|\s+/gi, "").toLowerCase();
|
|
783
|
+
default:
|
|
784
|
+
throw new Error(`Unsupported platform: ${process.platform}`);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
__name(expose, "expose");
|
|
788
|
+
function getMachineId() {
|
|
789
|
+
if (!(platform in guid)) {
|
|
790
|
+
return (0, import_node_crypto.randomUUID)();
|
|
791
|
+
}
|
|
792
|
+
try {
|
|
793
|
+
const value = (0, import_child_process2.execSync)(guid[platform]);
|
|
794
|
+
const id = expose(value.toString());
|
|
795
|
+
if (!id) {
|
|
796
|
+
return (0, import_node_crypto.randomUUID)();
|
|
797
|
+
}
|
|
798
|
+
return hash(id);
|
|
799
|
+
} catch {
|
|
800
|
+
return (0, import_node_crypto.randomUUID)();
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
__name(getMachineId, "getMachineId");
|
|
804
|
+
|
|
445
805
|
// src/utils/version-utils.ts
|
|
446
|
-
var
|
|
447
|
-
var
|
|
806
|
+
var import_colors6 = __toESM(require("colors"), 1);
|
|
807
|
+
var import_node_fs10 = __toESM(require("fs"), 1);
|
|
808
|
+
var import_node_path8 = __toESM(require("path"), 1);
|
|
448
809
|
var import_node_url = require("url");
|
|
810
|
+
var import_semver = __toESM(require("semver"), 1);
|
|
449
811
|
var import_meta = {};
|
|
812
|
+
var CHECK_VERSION_TIMEOUT = 2e3;
|
|
813
|
+
var VERSION_CHECK_TAG = "next";
|
|
450
814
|
function getVersion() {
|
|
451
815
|
try {
|
|
452
|
-
const _dirname = typeof __dirname !== "undefined" ? __dirname :
|
|
453
|
-
return JSON.parse(
|
|
816
|
+
const _dirname = typeof __dirname !== "undefined" ? __dirname : import_node_path8.default.dirname((0, import_node_url.fileURLToPath)(import_meta.url));
|
|
817
|
+
return JSON.parse(import_node_fs10.default.readFileSync(import_node_path8.default.join(_dirname, "../package.json"), "utf8")).version;
|
|
454
818
|
} catch {
|
|
455
819
|
return void 0;
|
|
456
820
|
}
|
|
457
821
|
}
|
|
458
822
|
__name(getVersion, "getVersion");
|
|
823
|
+
async function checkNewVersion() {
|
|
824
|
+
const currVersion = getVersion();
|
|
825
|
+
let latestVersion;
|
|
826
|
+
try {
|
|
827
|
+
latestVersion = await getLatestVersion();
|
|
828
|
+
} catch {
|
|
829
|
+
return;
|
|
830
|
+
}
|
|
831
|
+
if (latestVersion && currVersion && import_semver.default.gt(latestVersion, currVersion)) {
|
|
832
|
+
console.log(`A newer version ${import_colors6.default.cyan(latestVersion)} is available.`);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
__name(checkNewVersion, "checkNewVersion");
|
|
836
|
+
async function getLatestVersion() {
|
|
837
|
+
const fetchResult = await fetch(`https://registry.npmjs.org/@zenstackhq/cli/${VERSION_CHECK_TAG}`, {
|
|
838
|
+
headers: {
|
|
839
|
+
accept: "application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*"
|
|
840
|
+
},
|
|
841
|
+
signal: AbortSignal.timeout(CHECK_VERSION_TIMEOUT)
|
|
842
|
+
});
|
|
843
|
+
if (fetchResult.ok) {
|
|
844
|
+
const data = await fetchResult.json();
|
|
845
|
+
const latestVersion = data?.version;
|
|
846
|
+
if (typeof latestVersion === "string" && import_semver.default.valid(latestVersion)) {
|
|
847
|
+
return latestVersion;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
throw new Error("invalid npm registry response");
|
|
851
|
+
}
|
|
852
|
+
__name(getLatestVersion, "getLatestVersion");
|
|
853
|
+
|
|
854
|
+
// src/telemetry.ts
|
|
855
|
+
var import_meta2 = {};
|
|
856
|
+
var Telemetry = class {
|
|
857
|
+
static {
|
|
858
|
+
__name(this, "Telemetry");
|
|
859
|
+
}
|
|
860
|
+
mixpanel;
|
|
861
|
+
hostId = getMachineId();
|
|
862
|
+
sessionid = (0, import_node_crypto2.randomUUID)();
|
|
863
|
+
_os_type = os2.type();
|
|
864
|
+
_os_release = os2.release();
|
|
865
|
+
_os_arch = os2.arch();
|
|
866
|
+
_os_version = os2.version();
|
|
867
|
+
_os_platform = os2.platform();
|
|
868
|
+
version = getVersion();
|
|
869
|
+
prismaVersion = this.getPrismaVersion();
|
|
870
|
+
isDocker = isDocker();
|
|
871
|
+
isWsl = isWsl();
|
|
872
|
+
isContainer = isInContainer();
|
|
873
|
+
isCi = isInCi;
|
|
874
|
+
constructor() {
|
|
875
|
+
if (process.env["DO_NOT_TRACK"] !== "1" && TELEMETRY_TRACKING_TOKEN) {
|
|
876
|
+
this.mixpanel = (0, import_mixpanel.init)(TELEMETRY_TRACKING_TOKEN, {
|
|
877
|
+
geolocate: true
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
get isTracking() {
|
|
882
|
+
return !!this.mixpanel;
|
|
883
|
+
}
|
|
884
|
+
track(event, properties = {}) {
|
|
885
|
+
if (this.mixpanel) {
|
|
886
|
+
const payload = {
|
|
887
|
+
distinct_id: this.hostId,
|
|
888
|
+
session: this.sessionid,
|
|
889
|
+
time: /* @__PURE__ */ new Date(),
|
|
890
|
+
$os: this._os_type,
|
|
891
|
+
osType: this._os_type,
|
|
892
|
+
osRelease: this._os_release,
|
|
893
|
+
osPlatform: this._os_platform,
|
|
894
|
+
osArch: this._os_arch,
|
|
895
|
+
osVersion: this._os_version,
|
|
896
|
+
nodeVersion: process.version,
|
|
897
|
+
version: this.version,
|
|
898
|
+
prismaVersion: this.prismaVersion,
|
|
899
|
+
isDocker: this.isDocker,
|
|
900
|
+
isWsl: this.isWsl,
|
|
901
|
+
isContainer: this.isContainer,
|
|
902
|
+
isCi: this.isCi,
|
|
903
|
+
...properties
|
|
904
|
+
};
|
|
905
|
+
this.mixpanel.track(event, payload);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
trackError(err) {
|
|
909
|
+
this.track("cli:error", {
|
|
910
|
+
message: err.message,
|
|
911
|
+
stack: err.stack
|
|
912
|
+
});
|
|
913
|
+
}
|
|
914
|
+
async trackSpan(startEvent, completeEvent, errorEvent, properties, action) {
|
|
915
|
+
this.track(startEvent, properties);
|
|
916
|
+
const start = Date.now();
|
|
917
|
+
let success = true;
|
|
918
|
+
try {
|
|
919
|
+
return await action();
|
|
920
|
+
} catch (err) {
|
|
921
|
+
this.track(errorEvent, {
|
|
922
|
+
message: err.message,
|
|
923
|
+
stack: err.stack,
|
|
924
|
+
...properties
|
|
925
|
+
});
|
|
926
|
+
success = false;
|
|
927
|
+
throw err;
|
|
928
|
+
} finally {
|
|
929
|
+
this.track(completeEvent, {
|
|
930
|
+
duration: Date.now() - start,
|
|
931
|
+
success,
|
|
932
|
+
...properties
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
async trackCommand(command, action) {
|
|
937
|
+
await this.trackSpan("cli:command:start", "cli:command:complete", "cli:command:error", {
|
|
938
|
+
command
|
|
939
|
+
}, action);
|
|
940
|
+
}
|
|
941
|
+
async trackCli(action) {
|
|
942
|
+
await this.trackSpan("cli:start", "cli:complete", "cli:error", {}, action);
|
|
943
|
+
}
|
|
944
|
+
getPrismaVersion() {
|
|
945
|
+
try {
|
|
946
|
+
const packageJsonPath = import_meta2.resolve("prisma/package.json");
|
|
947
|
+
const packageJsonUrl = new URL(packageJsonPath);
|
|
948
|
+
const packageJson = JSON.parse(import_node_fs11.default.readFileSync(packageJsonUrl, "utf8"));
|
|
949
|
+
return packageJson.version;
|
|
950
|
+
} catch {
|
|
951
|
+
return void 0;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
var telemetry = new Telemetry();
|
|
459
956
|
|
|
460
957
|
// src/index.ts
|
|
461
958
|
var generateAction = /* @__PURE__ */ __name(async (options) => {
|
|
462
|
-
await run2(options);
|
|
959
|
+
await telemetry.trackCommand("generate", () => run2(options));
|
|
463
960
|
}, "generateAction");
|
|
464
|
-
var migrateAction = /* @__PURE__ */ __name(async (
|
|
465
|
-
await run5(
|
|
961
|
+
var migrateAction = /* @__PURE__ */ __name(async (subCommand, options) => {
|
|
962
|
+
await telemetry.trackCommand(`migrate ${subCommand}`, () => run5(subCommand, options));
|
|
466
963
|
}, "migrateAction");
|
|
467
|
-
var dbAction = /* @__PURE__ */ __name(async (
|
|
468
|
-
await run(
|
|
964
|
+
var dbAction = /* @__PURE__ */ __name(async (subCommand, options) => {
|
|
965
|
+
await telemetry.trackCommand(`db ${subCommand}`, () => run(subCommand, options));
|
|
469
966
|
}, "dbAction");
|
|
470
967
|
var infoAction = /* @__PURE__ */ __name(async (projectPath) => {
|
|
471
|
-
await run3(projectPath);
|
|
968
|
+
await telemetry.trackCommand("info", () => run3(projectPath));
|
|
472
969
|
}, "infoAction");
|
|
473
970
|
var initAction = /* @__PURE__ */ __name(async (projectPath) => {
|
|
474
|
-
await run4(projectPath);
|
|
971
|
+
await telemetry.trackCommand("init", () => run4(projectPath));
|
|
475
972
|
}, "initAction");
|
|
973
|
+
var checkAction = /* @__PURE__ */ __name(async (options) => {
|
|
974
|
+
await telemetry.trackCommand("check", () => run6(options));
|
|
975
|
+
}, "checkAction");
|
|
476
976
|
function createProgram() {
|
|
477
|
-
const
|
|
478
|
-
|
|
977
|
+
const program = new import_commander.Command("zen");
|
|
978
|
+
program.version(getVersion(), "-v --version", "display CLI version");
|
|
479
979
|
const schemaExtensions = import_language2.ZModelLanguageMetaData.fileExtensions.join(", ");
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
Documentation: https://zenstack.dev
|
|
483
|
-
const schemaOption = new import_commander.Option("--schema <file>", `schema file (with extension ${schemaExtensions}). Defaults to "schema.zmodel" unless specified in package.json.`);
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
migrateCommand
|
|
487
|
-
|
|
488
|
-
migrateCommand.command("
|
|
489
|
-
migrateCommand.command("
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
980
|
+
program.description(`${import_colors7.default.bold.blue("\u03B6")} ZenStack is the data layer for modern TypeScript apps.
|
|
981
|
+
|
|
982
|
+
Documentation: https://zenstack.dev/docs/3.x`).showHelpAfterError().showSuggestionAfterError();
|
|
983
|
+
const schemaOption = new import_commander.Option("--schema <file>", `schema file (with extension ${schemaExtensions}). Defaults to "zenstack/schema.zmodel" unless specified in package.json.`);
|
|
984
|
+
const noVersionCheckOption = new import_commander.Option("--no-version-check", "do not check for new version");
|
|
985
|
+
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("--silent", "suppress all output except errors").default(false)).action(generateAction);
|
|
986
|
+
const migrateCommand = program.command("migrate").description("Run database schema migration related tasks.");
|
|
987
|
+
const migrationsOption = new import_commander.Option("--migrations <path>", 'path that contains the "migrations" directory');
|
|
988
|
+
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));
|
|
989
|
+
migrateCommand.command("reset").addOption(schemaOption).addOption(new import_commander.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));
|
|
990
|
+
migrateCommand.command("deploy").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).description("Deploy your pending migrations to your production/staging database.").action((options) => migrateAction("deploy", options));
|
|
991
|
+
migrateCommand.command("status").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).description("Check the status of your database migrations.").action((options) => migrateAction("status", options));
|
|
992
|
+
migrateCommand.command("resolve").addOption(schemaOption).addOption(noVersionCheckOption).addOption(migrationsOption).addOption(new import_commander.Option("--applied <migration>", "record a specific migration as applied")).addOption(new import_commander.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));
|
|
993
|
+
const dbCommand = program.command("db").description("Manage your database schema during development.");
|
|
994
|
+
dbCommand.command("push").description("Push the state from your schema to your database.").addOption(schemaOption).addOption(noVersionCheckOption).addOption(new import_commander.Option("--accept-data-loss", "ignore data loss warnings")).addOption(new import_commander.Option("--force-reset", "force a reset of the database before push")).action((options) => dbAction("push", options));
|
|
995
|
+
program.command("info").description("Get information of installed ZenStack packages.").argument("[path]", "project path", ".").addOption(noVersionCheckOption).action(infoAction);
|
|
996
|
+
program.command("init").description("Initialize an existing project for ZenStack.").argument("[path]", "project path", ".").addOption(noVersionCheckOption).action(initAction);
|
|
997
|
+
program.command("check").description("Check a ZModel schema for syntax or semantic errors.").addOption(schemaOption).addOption(noVersionCheckOption).action(checkAction);
|
|
998
|
+
program.hook("preAction", async (_thisCommand, actionCommand) => {
|
|
999
|
+
if (actionCommand.getOptionValue("versionCheck") !== false) {
|
|
1000
|
+
await checkNewVersion();
|
|
1001
|
+
}
|
|
1002
|
+
});
|
|
1003
|
+
return program;
|
|
495
1004
|
}
|
|
496
1005
|
__name(createProgram, "createProgram");
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
1006
|
+
async function main() {
|
|
1007
|
+
let exitCode = 0;
|
|
1008
|
+
const program = createProgram();
|
|
1009
|
+
program.exitOverride();
|
|
1010
|
+
try {
|
|
1011
|
+
await telemetry.trackCli(async () => {
|
|
1012
|
+
await program.parseAsync();
|
|
1013
|
+
});
|
|
1014
|
+
} catch (e) {
|
|
1015
|
+
if (e instanceof import_commander.CommanderError) {
|
|
1016
|
+
exitCode = e.exitCode;
|
|
1017
|
+
} else if (e instanceof CliError) {
|
|
1018
|
+
console.error(import_colors7.default.red(e.message));
|
|
1019
|
+
exitCode = 1;
|
|
1020
|
+
} else {
|
|
1021
|
+
console.error(import_colors7.default.red(`Unhandled error: ${e}`));
|
|
1022
|
+
exitCode = 1;
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
if (telemetry.isTracking) {
|
|
1026
|
+
setTimeout(() => {
|
|
1027
|
+
process.exit(exitCode);
|
|
1028
|
+
}, 200);
|
|
1029
|
+
} else {
|
|
1030
|
+
process.exit(exitCode);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
__name(main, "main");
|
|
1034
|
+
main();
|
|
503
1035
|
//# sourceMappingURL=index.cjs.map
|