@zenstackhq/cli 3.0.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +23 -0
- package/.turbo/turbo-lint.log +18 -0
- package/LICENSE +21 -0
- package/bin/cli +3 -0
- package/dist/index.cjs +543 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +517 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
- package/src/actions/action-utils.ts +47 -0
- package/src/actions/db.ts +46 -0
- package/src/actions/generate.ts +70 -0
- package/src/actions/index.ts +7 -0
- package/src/actions/info.ts +77 -0
- package/src/actions/init.ts +82 -0
- package/src/actions/migrate.ts +111 -0
- package/src/actions/templates.ts +52 -0
- package/src/cli-error.ts +4 -0
- package/src/index.ts +138 -0
- package/src/utils/exec-utils.ts +29 -0
- package/src/utils/version-utils.ts +13 -0
- package/test/ts-schema-gen.test.ts +185 -0
- package/tsconfig.json +7 -0
- package/tsup.config.ts +13 -0
- package/vitest.config.ts +4 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @zenstackhq/cli@3.0.0-alpha.1 build /Users/yiming/git/zenstack/zenstack-v3/packages/cli
|
|
4
|
+
> tsup-node
|
|
5
|
+
|
|
6
|
+
[34mCLI[39m Building entry: {"index":"src/index.ts"}
|
|
7
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
|
+
[34mCLI[39m tsup v8.3.5
|
|
9
|
+
[34mCLI[39m Using tsup config: /Users/yiming/git/zenstack/zenstack-v3/packages/cli/tsup.config.ts
|
|
10
|
+
[34mCLI[39m Target: esnext
|
|
11
|
+
[34mCLI[39m Cleaning output folder
|
|
12
|
+
[34mESM[39m Build start
|
|
13
|
+
[34mCJS[39m Build start
|
|
14
|
+
[32mESM[39m [1mdist/index.js [22m[32m14.02 KB[39m
|
|
15
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m27.76 KB[39m
|
|
16
|
+
[32mESM[39m ⚡️ Build success in 51ms
|
|
17
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m15.95 KB[39m
|
|
18
|
+
[32mCJS[39m [1mdist/index.cjs.map [22m[32m27.84 KB[39m
|
|
19
|
+
[32mCJS[39m ⚡️ Build success in 51ms
|
|
20
|
+
DTS Build start
|
|
21
|
+
DTS ⚡️ Build success in 1226ms
|
|
22
|
+
DTS dist/index.d.ts 108.00 B
|
|
23
|
+
DTS dist/index.d.cts 108.00 B
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> zenstack@3.0.0-alpha.1 lint /Users/yiming/git/zenstack/zenstack-v3/packages/cli
|
|
4
|
+
> eslint src --ext ts
|
|
5
|
+
|
|
6
|
+
=============
|
|
7
|
+
|
|
8
|
+
WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.
|
|
9
|
+
|
|
10
|
+
You may find that it works just fine, or you may not.
|
|
11
|
+
|
|
12
|
+
SUPPORTED TYPESCRIPT VERSIONS: >=4.7.4 <5.5.0
|
|
13
|
+
|
|
14
|
+
YOUR TYPESCRIPT VERSION: 5.7.3
|
|
15
|
+
|
|
16
|
+
Please only submit bug reports when using the officially supported version.
|
|
17
|
+
|
|
18
|
+
=============
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 ZenStack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/bin/cli
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
};
|
|
12
|
+
var __export = (target, all) => {
|
|
13
|
+
for (var name in all)
|
|
14
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
15
|
+
};
|
|
16
|
+
var __copyProps = (to, from, except, desc) => {
|
|
17
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
18
|
+
for (let key of __getOwnPropNames(from))
|
|
19
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
20
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
30
|
+
mod
|
|
31
|
+
));
|
|
32
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
33
|
+
|
|
34
|
+
// package.json
|
|
35
|
+
var require_package = __commonJS({
|
|
36
|
+
"package.json"(exports2, module2) {
|
|
37
|
+
module2.exports = {
|
|
38
|
+
name: "@zenstackhq/cli",
|
|
39
|
+
publisher: "zenstack",
|
|
40
|
+
displayName: "ZenStack CLI",
|
|
41
|
+
description: "FullStack database toolkit with built-in access control and automatic API generation.",
|
|
42
|
+
version: "3.0.0-alpha.0",
|
|
43
|
+
type: "module",
|
|
44
|
+
author: {
|
|
45
|
+
name: "ZenStack Team"
|
|
46
|
+
},
|
|
47
|
+
homepage: "https://zenstack.dev",
|
|
48
|
+
license: "MIT",
|
|
49
|
+
keywords: [
|
|
50
|
+
"orm",
|
|
51
|
+
"fullstack",
|
|
52
|
+
"react",
|
|
53
|
+
"typescript",
|
|
54
|
+
"data modeling"
|
|
55
|
+
],
|
|
56
|
+
bin: {
|
|
57
|
+
zenstack: "bin/cli"
|
|
58
|
+
},
|
|
59
|
+
scripts: {
|
|
60
|
+
build: "tsup-node",
|
|
61
|
+
watch: "tsup-node --watch",
|
|
62
|
+
lint: "eslint src --ext ts",
|
|
63
|
+
test: "vitest",
|
|
64
|
+
pack: "pnpm pack"
|
|
65
|
+
},
|
|
66
|
+
dependencies: {
|
|
67
|
+
"@types/node": "^20.0.0",
|
|
68
|
+
"@zenstackhq/language": "workspace:*",
|
|
69
|
+
"@zenstackhq/sdk": "workspace:*",
|
|
70
|
+
"async-exit-hook": "^2.0.1",
|
|
71
|
+
colors: "1.4.0",
|
|
72
|
+
commander: "^8.3.0",
|
|
73
|
+
langium: "~3.3.0",
|
|
74
|
+
ora: "^5.4.1",
|
|
75
|
+
"package-manager-detector": "^1.3.0",
|
|
76
|
+
"tiny-invariant": "^1.3.3",
|
|
77
|
+
"ts-pattern": "^4.3.0"
|
|
78
|
+
},
|
|
79
|
+
peerDependencies: {
|
|
80
|
+
prisma: "^6.0.0",
|
|
81
|
+
typescript: "^5.0.0"
|
|
82
|
+
},
|
|
83
|
+
devDependencies: {
|
|
84
|
+
"@types/async-exit-hook": "^2.0.0",
|
|
85
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
86
|
+
"@types/semver": "^7.3.13",
|
|
87
|
+
"@types/tmp": "^0.2.6",
|
|
88
|
+
"@zenstackhq/runtime": "workspace:*",
|
|
89
|
+
"@zenstackhq/testtools": "workspace:*",
|
|
90
|
+
"better-sqlite3": "^11.8.1",
|
|
91
|
+
tmp: "^0.2.3"
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// src/index.ts
|
|
98
|
+
var src_exports = {};
|
|
99
|
+
__export(src_exports, {
|
|
100
|
+
createProgram: () => createProgram
|
|
101
|
+
});
|
|
102
|
+
module.exports = __toCommonJS(src_exports);
|
|
103
|
+
var import_language2 = require("@zenstackhq/language");
|
|
104
|
+
var import_colors5 = __toESM(require("colors"), 1);
|
|
105
|
+
var import_commander = require("commander");
|
|
106
|
+
|
|
107
|
+
// src/actions/db.ts
|
|
108
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
109
|
+
|
|
110
|
+
// src/utils/exec-utils.ts
|
|
111
|
+
var import_child_process = require("child_process");
|
|
112
|
+
function execSync(cmd, options) {
|
|
113
|
+
const { env, ...restOptions } = options ?? {};
|
|
114
|
+
const mergedEnv = env ? {
|
|
115
|
+
...process.env,
|
|
116
|
+
...env
|
|
117
|
+
} : void 0;
|
|
118
|
+
(0, import_child_process.execSync)(cmd, {
|
|
119
|
+
encoding: "utf-8",
|
|
120
|
+
stdio: options?.stdio ?? "inherit",
|
|
121
|
+
env: mergedEnv,
|
|
122
|
+
...restOptions
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
__name(execSync, "execSync");
|
|
126
|
+
function execPackage(cmd, options) {
|
|
127
|
+
const packageManager = process?.versions?.["bun"] ? "bunx" : "npx";
|
|
128
|
+
execSync(`${packageManager} ${cmd}`, options);
|
|
129
|
+
}
|
|
130
|
+
__name(execPackage, "execPackage");
|
|
131
|
+
|
|
132
|
+
// src/actions/action-utils.ts
|
|
133
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
|
134
|
+
|
|
135
|
+
// src/cli-error.ts
|
|
136
|
+
var CliError = class extends Error {
|
|
137
|
+
static {
|
|
138
|
+
__name(this, "CliError");
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// src/actions/action-utils.ts
|
|
143
|
+
var import_language = require("@zenstackhq/language");
|
|
144
|
+
var import_colors = __toESM(require("colors"), 1);
|
|
145
|
+
function getSchemaFile(file) {
|
|
146
|
+
if (file) {
|
|
147
|
+
if (!import_node_fs.default.existsSync(file)) {
|
|
148
|
+
throw new CliError(`Schema file not found: ${file}`);
|
|
149
|
+
}
|
|
150
|
+
return file;
|
|
151
|
+
}
|
|
152
|
+
if (import_node_fs.default.existsSync("./zenstack/schema.zmodel")) {
|
|
153
|
+
return "./zenstack/schema.zmodel";
|
|
154
|
+
} else if (import_node_fs.default.existsSync("./schema.zmodel")) {
|
|
155
|
+
return "./schema.zmodel";
|
|
156
|
+
} else {
|
|
157
|
+
throw new CliError('Schema file not found in default locations ("./zenstack/schema.zmodel" or "./schema.zmodel").');
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
__name(getSchemaFile, "getSchemaFile");
|
|
161
|
+
async function loadSchemaDocument(schemaFile) {
|
|
162
|
+
const loadResult = await (0, import_language.loadDocument)(schemaFile);
|
|
163
|
+
if (!loadResult.success) {
|
|
164
|
+
console.error(import_colors.default.red("Error loading schema:"));
|
|
165
|
+
loadResult.errors.forEach((err) => {
|
|
166
|
+
console.error(import_colors.default.red(err));
|
|
167
|
+
});
|
|
168
|
+
throw new CliError("Failed to load schema");
|
|
169
|
+
}
|
|
170
|
+
return loadResult.model;
|
|
171
|
+
}
|
|
172
|
+
__name(loadSchemaDocument, "loadSchemaDocument");
|
|
173
|
+
function handleSubProcessError(err) {
|
|
174
|
+
if (err instanceof Error && "status" in err && typeof err.status === "number") {
|
|
175
|
+
process.exit(err.status);
|
|
176
|
+
} else {
|
|
177
|
+
process.exit(1);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
__name(handleSubProcessError, "handleSubProcessError");
|
|
181
|
+
|
|
182
|
+
// src/actions/generate.ts
|
|
183
|
+
var import_ast = require("@zenstackhq/language/ast");
|
|
184
|
+
var import_sdk = require("@zenstackhq/sdk");
|
|
185
|
+
var import_colors2 = __toESM(require("colors"), 1);
|
|
186
|
+
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
187
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
188
|
+
var import_tiny_invariant = __toESM(require("tiny-invariant"), 1);
|
|
189
|
+
async function run(options) {
|
|
190
|
+
const schemaFile = getSchemaFile(options.schema);
|
|
191
|
+
const model = await loadSchemaDocument(schemaFile);
|
|
192
|
+
const outputPath = options.output ?? import_node_path.default.dirname(schemaFile);
|
|
193
|
+
const tsSchemaFile = import_node_path.default.join(outputPath, "schema.ts");
|
|
194
|
+
await new import_sdk.TsSchemaGenerator().generate(schemaFile, [], tsSchemaFile);
|
|
195
|
+
await runPlugins(model, outputPath, tsSchemaFile);
|
|
196
|
+
const prismaSchema = await new import_sdk.PrismaSchemaGenerator(model).generate();
|
|
197
|
+
import_node_fs2.default.writeFileSync(import_node_path.default.join(outputPath, "schema.prisma"), prismaSchema);
|
|
198
|
+
if (!options.silent) {
|
|
199
|
+
console.log(import_colors2.default.green("Generation completed successfully."));
|
|
200
|
+
console.log(`You can now create a ZenStack client with it.
|
|
201
|
+
|
|
202
|
+
\`\`\`
|
|
203
|
+
import { ZenStackClient } from '@zenstackhq/runtime';
|
|
204
|
+
import { schema } from '${outputPath}/schema';
|
|
205
|
+
|
|
206
|
+
const client = new ZenStackClient(schema);
|
|
207
|
+
\`\`\`
|
|
208
|
+
`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
__name(run, "run");
|
|
212
|
+
async function runPlugins(model, outputPath, tsSchemaFile) {
|
|
213
|
+
const plugins = model.declarations.filter(import_ast.isPlugin);
|
|
214
|
+
for (const plugin of plugins) {
|
|
215
|
+
const providerField = plugin.fields.find((f) => f.name === "provider");
|
|
216
|
+
(0, import_tiny_invariant.default)(providerField, `Plugin ${plugin.name} does not have a provider field`);
|
|
217
|
+
const provider = providerField.value.value;
|
|
218
|
+
let useProvider = provider;
|
|
219
|
+
if (useProvider.startsWith("@core/")) {
|
|
220
|
+
useProvider = `@zenstackhq/runtime/plugins/${useProvider.slice(6)}`;
|
|
221
|
+
}
|
|
222
|
+
const generator = (await import(useProvider)).default;
|
|
223
|
+
console.log("Running generator:", provider);
|
|
224
|
+
await generator({
|
|
225
|
+
model,
|
|
226
|
+
outputPath,
|
|
227
|
+
tsSchemaFile
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
__name(runPlugins, "runPlugins");
|
|
232
|
+
|
|
233
|
+
// src/actions/db.ts
|
|
234
|
+
async function run2(command, options) {
|
|
235
|
+
const schemaFile = getSchemaFile(options.schema);
|
|
236
|
+
await run({
|
|
237
|
+
schema: schemaFile,
|
|
238
|
+
silent: true
|
|
239
|
+
});
|
|
240
|
+
const prismaSchemaFile = import_node_path2.default.join(import_node_path2.default.dirname(schemaFile), "schema.prisma");
|
|
241
|
+
switch (command) {
|
|
242
|
+
case "push":
|
|
243
|
+
await runPush(prismaSchemaFile, options);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
__name(run2, "run");
|
|
248
|
+
async function runPush(prismaSchemaFile, options) {
|
|
249
|
+
const cmd = `prisma db push --schema "${prismaSchemaFile}"${options.acceptDataLoss ? " --accept-data-loss" : ""}${options.forceReset ? " --force-reset" : ""} --skip-generate`;
|
|
250
|
+
try {
|
|
251
|
+
await execPackage(cmd, {
|
|
252
|
+
stdio: "inherit"
|
|
253
|
+
});
|
|
254
|
+
} catch (err) {
|
|
255
|
+
handleSubProcessError(err);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
__name(runPush, "runPush");
|
|
259
|
+
|
|
260
|
+
// src/actions/info.ts
|
|
261
|
+
var import_colors3 = __toESM(require("colors"), 1);
|
|
262
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
263
|
+
async function run3(projectPath) {
|
|
264
|
+
const packages = await getZenStackPackages(projectPath);
|
|
265
|
+
if (!packages) {
|
|
266
|
+
console.error("Unable to locate package.json. Are you in a valid project directory?");
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
console.log("Installed ZenStack Packages:");
|
|
270
|
+
const versions = /* @__PURE__ */ new Set();
|
|
271
|
+
for (const { pkg, version } of packages) {
|
|
272
|
+
if (version) {
|
|
273
|
+
versions.add(version);
|
|
274
|
+
}
|
|
275
|
+
console.log(` ${import_colors3.default.green(pkg.padEnd(20))} ${version}`);
|
|
276
|
+
}
|
|
277
|
+
if (versions.size > 1) {
|
|
278
|
+
console.warn(import_colors3.default.yellow("WARNING: Multiple versions of Zenstack packages detected. This may cause issues."));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
__name(run3, "run");
|
|
282
|
+
async function getZenStackPackages(projectPath) {
|
|
283
|
+
let pkgJson;
|
|
284
|
+
const resolvedPath = import_node_path3.default.resolve(projectPath);
|
|
285
|
+
try {
|
|
286
|
+
pkgJson = (await import(import_node_path3.default.join(resolvedPath, "package.json"), {
|
|
287
|
+
with: {
|
|
288
|
+
type: "json"
|
|
289
|
+
}
|
|
290
|
+
})).default;
|
|
291
|
+
} catch (err) {
|
|
292
|
+
return [];
|
|
293
|
+
}
|
|
294
|
+
const packages = Array.from(new Set([
|
|
295
|
+
...Object.keys(pkgJson.dependencies ?? {}),
|
|
296
|
+
...Object.keys(pkgJson.devDependencies ?? {})
|
|
297
|
+
].filter((p) => p.startsWith("@zenstackhq/") || p === "zenstack"))).sort();
|
|
298
|
+
const result = await Promise.all(packages.map(async (pkg) => {
|
|
299
|
+
try {
|
|
300
|
+
const depPkgJson = (await import(`${pkg}/package.json`, {
|
|
301
|
+
with: {
|
|
302
|
+
type: "json"
|
|
303
|
+
}
|
|
304
|
+
})).default;
|
|
305
|
+
return {
|
|
306
|
+
pkg,
|
|
307
|
+
version: depPkgJson.version
|
|
308
|
+
};
|
|
309
|
+
} catch {
|
|
310
|
+
return {
|
|
311
|
+
pkg,
|
|
312
|
+
version: void 0
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
}));
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
__name(getZenStackPackages, "getZenStackPackages");
|
|
319
|
+
|
|
320
|
+
// src/actions/init.ts
|
|
321
|
+
var import_colors4 = __toESM(require("colors"), 1);
|
|
322
|
+
var import_node_fs3 = __toESM(require("fs"), 1);
|
|
323
|
+
var import_node_path4 = __toESM(require("path"), 1);
|
|
324
|
+
var import_ora = __toESM(require("ora"), 1);
|
|
325
|
+
var import_package_manager_detector = require("package-manager-detector");
|
|
326
|
+
|
|
327
|
+
// src/actions/templates.ts
|
|
328
|
+
var STARTER_ZMODEL = `// This is a sample model to get you started.
|
|
329
|
+
|
|
330
|
+
/// A sample data source using local sqlite db.
|
|
331
|
+
datasource db {
|
|
332
|
+
provider = 'sqlite'
|
|
333
|
+
url = 'file:./dev.db'
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/// User model
|
|
337
|
+
model User {
|
|
338
|
+
id String @id @default(cuid())
|
|
339
|
+
email String @unique @email @length(6, 32)
|
|
340
|
+
posts Post[]
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/// Post model
|
|
344
|
+
model Post {
|
|
345
|
+
id String @id @default(cuid())
|
|
346
|
+
createdAt DateTime @default(now())
|
|
347
|
+
updatedAt DateTime @updatedAt
|
|
348
|
+
title String @length(1, 256)
|
|
349
|
+
content String
|
|
350
|
+
published Boolean @default(false)
|
|
351
|
+
author User @relation(fields: [authorId], references: [id])
|
|
352
|
+
authorId String
|
|
353
|
+
}
|
|
354
|
+
`;
|
|
355
|
+
|
|
356
|
+
// src/actions/init.ts
|
|
357
|
+
async function run4(projectPath) {
|
|
358
|
+
const packages = [
|
|
359
|
+
{
|
|
360
|
+
name: "@zenstackhq/cli",
|
|
361
|
+
dev: true
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
name: "@zenstackhq/runtime",
|
|
365
|
+
dev: false
|
|
366
|
+
}
|
|
367
|
+
];
|
|
368
|
+
let pm = await (0, import_package_manager_detector.detect)();
|
|
369
|
+
if (!pm) {
|
|
370
|
+
pm = {
|
|
371
|
+
agent: "npm",
|
|
372
|
+
name: "npm"
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
console.log(import_colors4.default.gray(`Using package manager: ${pm.agent}`));
|
|
376
|
+
for (const pkg of packages) {
|
|
377
|
+
const resolved = (0, import_package_manager_detector.resolveCommand)(pm.agent, "install", [
|
|
378
|
+
pkg.name,
|
|
379
|
+
...pkg.dev ? [
|
|
380
|
+
pm.agent === "yarn" ? "--dev" : "--save-dev"
|
|
381
|
+
] : []
|
|
382
|
+
]);
|
|
383
|
+
if (!resolved) {
|
|
384
|
+
throw new CliError(`Unable to determine how to install package "${pkg.name}". Please install it manually.`);
|
|
385
|
+
}
|
|
386
|
+
const spinner = (0, import_ora.default)(`Installing "${pkg.name}"`).start();
|
|
387
|
+
try {
|
|
388
|
+
execSync(`${resolved.command} ${resolved.args.join(" ")}`, {
|
|
389
|
+
cwd: projectPath
|
|
390
|
+
});
|
|
391
|
+
spinner.succeed();
|
|
392
|
+
} catch (e) {
|
|
393
|
+
spinner.fail();
|
|
394
|
+
throw e;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const generationFolder = "zenstack";
|
|
398
|
+
if (!import_node_fs3.default.existsSync(import_node_path4.default.join(projectPath, generationFolder))) {
|
|
399
|
+
import_node_fs3.default.mkdirSync(import_node_path4.default.join(projectPath, generationFolder));
|
|
400
|
+
}
|
|
401
|
+
if (!import_node_fs3.default.existsSync(import_node_path4.default.join(projectPath, generationFolder, "schema.zmodel"))) {
|
|
402
|
+
import_node_fs3.default.writeFileSync(import_node_path4.default.join(projectPath, generationFolder, "schema.zmodel"), STARTER_ZMODEL);
|
|
403
|
+
} else {
|
|
404
|
+
console.log(import_colors4.default.yellow("Schema file already exists. Skipping generation of sample."));
|
|
405
|
+
}
|
|
406
|
+
console.log(import_colors4.default.green("ZenStack project initialized successfully!"));
|
|
407
|
+
console.log(import_colors4.default.gray(`See "${generationFolder}/schema.zmodel" for your database schema.`));
|
|
408
|
+
console.log(import_colors4.default.gray("Run `zenstack generate` to compile the the schema into a TypeScript file."));
|
|
409
|
+
}
|
|
410
|
+
__name(run4, "run");
|
|
411
|
+
|
|
412
|
+
// src/actions/migrate.ts
|
|
413
|
+
var import_node_path5 = __toESM(require("path"), 1);
|
|
414
|
+
async function run5(command, options) {
|
|
415
|
+
const schemaFile = getSchemaFile(options.schema);
|
|
416
|
+
await run({
|
|
417
|
+
schema: schemaFile,
|
|
418
|
+
silent: true
|
|
419
|
+
});
|
|
420
|
+
const prismaSchemaFile = import_node_path5.default.join(import_node_path5.default.dirname(schemaFile), "schema.prisma");
|
|
421
|
+
switch (command) {
|
|
422
|
+
case "dev":
|
|
423
|
+
await runDev(prismaSchemaFile, options);
|
|
424
|
+
break;
|
|
425
|
+
case "reset":
|
|
426
|
+
await runReset(prismaSchemaFile, options);
|
|
427
|
+
break;
|
|
428
|
+
case "deploy":
|
|
429
|
+
await runDeploy(prismaSchemaFile, options);
|
|
430
|
+
break;
|
|
431
|
+
case "status":
|
|
432
|
+
await runStatus(prismaSchemaFile, options);
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
__name(run5, "run");
|
|
437
|
+
async function runDev(prismaSchemaFile, _options) {
|
|
438
|
+
try {
|
|
439
|
+
await execPackage(`prisma migrate dev --schema "${prismaSchemaFile}" --skip-generate`, {
|
|
440
|
+
stdio: "inherit"
|
|
441
|
+
});
|
|
442
|
+
} catch (err) {
|
|
443
|
+
handleSubProcessError2(err);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
__name(runDev, "runDev");
|
|
447
|
+
async function runReset(prismaSchemaFile, options) {
|
|
448
|
+
try {
|
|
449
|
+
await execPackage(`prisma migrate reset --schema "${prismaSchemaFile}"${options.force ? " --force" : ""}`, {
|
|
450
|
+
stdio: "inherit"
|
|
451
|
+
});
|
|
452
|
+
} catch (err) {
|
|
453
|
+
handleSubProcessError2(err);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
__name(runReset, "runReset");
|
|
457
|
+
async function runDeploy(prismaSchemaFile, _options) {
|
|
458
|
+
try {
|
|
459
|
+
await execPackage(`prisma migrate deploy --schema "${prismaSchemaFile}"`, {
|
|
460
|
+
stdio: "inherit"
|
|
461
|
+
});
|
|
462
|
+
} catch (err) {
|
|
463
|
+
handleSubProcessError2(err);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
__name(runDeploy, "runDeploy");
|
|
467
|
+
async function runStatus(prismaSchemaFile, _options) {
|
|
468
|
+
try {
|
|
469
|
+
await execPackage(`prisma migrate status --schema "${prismaSchemaFile}"`, {
|
|
470
|
+
stdio: "inherit"
|
|
471
|
+
});
|
|
472
|
+
} catch (err) {
|
|
473
|
+
handleSubProcessError2(err);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
__name(runStatus, "runStatus");
|
|
477
|
+
function handleSubProcessError2(err) {
|
|
478
|
+
if (err instanceof Error && "status" in err && typeof err.status === "number") {
|
|
479
|
+
process.exit(err.status);
|
|
480
|
+
} else {
|
|
481
|
+
process.exit(1);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
__name(handleSubProcessError2, "handleSubProcessError");
|
|
485
|
+
|
|
486
|
+
// src/utils/version-utils.ts
|
|
487
|
+
function getVersion() {
|
|
488
|
+
try {
|
|
489
|
+
return require("../package.json").version;
|
|
490
|
+
} catch {
|
|
491
|
+
try {
|
|
492
|
+
return require_package().version;
|
|
493
|
+
} catch {
|
|
494
|
+
return void 0;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
__name(getVersion, "getVersion");
|
|
499
|
+
|
|
500
|
+
// src/index.ts
|
|
501
|
+
var generateAction = /* @__PURE__ */ __name(async (options) => {
|
|
502
|
+
await run(options);
|
|
503
|
+
}, "generateAction");
|
|
504
|
+
var migrateAction = /* @__PURE__ */ __name(async (command, options) => {
|
|
505
|
+
await run5(command, options);
|
|
506
|
+
}, "migrateAction");
|
|
507
|
+
var dbAction = /* @__PURE__ */ __name(async (command, options) => {
|
|
508
|
+
await run2(command, options);
|
|
509
|
+
}, "dbAction");
|
|
510
|
+
var infoAction = /* @__PURE__ */ __name(async (projectPath) => {
|
|
511
|
+
await run3(projectPath);
|
|
512
|
+
}, "infoAction");
|
|
513
|
+
var initAction = /* @__PURE__ */ __name(async (projectPath) => {
|
|
514
|
+
await run4(projectPath);
|
|
515
|
+
}, "initAction");
|
|
516
|
+
function createProgram() {
|
|
517
|
+
const program2 = new import_commander.Command("zenstack");
|
|
518
|
+
program2.version(getVersion(), "-v --version", "display CLI version");
|
|
519
|
+
const schemaExtensions = import_language2.ZModelLanguageMetaData.fileExtensions.join(", ");
|
|
520
|
+
program2.description(`${import_colors5.default.bold.blue("\u03B6")} ZenStack is a Prisma power pack for building full-stack apps.
|
|
521
|
+
|
|
522
|
+
Documentation: https://zenstack.dev.`).showHelpAfterError().showSuggestionAfterError();
|
|
523
|
+
const schemaOption = new import_commander.Option("--schema <file>", `schema file (with extension ${schemaExtensions}). Defaults to "schema.zmodel" unless specified in package.json.`);
|
|
524
|
+
program2.command("generate").description("Run code generation.").addOption(schemaOption).addOption(new import_commander.Option("-o, --output <path>", "default output directory for core plugins")).action(generateAction);
|
|
525
|
+
const migrateCommand = program2.command("migrate").description("Update the database schema with migrations.");
|
|
526
|
+
migrateCommand.command("dev").addOption(schemaOption).addOption(new import_commander.Option("-n, --name <name>", "migration name")).addOption(new import_commander.Option("--create-only", "only create migration, do not apply")).description("Create a migration from changes in schema and apply it to the database.").action((options) => migrateAction("dev", options));
|
|
527
|
+
migrateCommand.command("reset").addOption(schemaOption).addOption(new import_commander.Option("--force", "skip the confirmation prompt")).description("Reset your database and apply all migrations, all data will be lost.").action((options) => migrateAction("reset", options));
|
|
528
|
+
migrateCommand.command("deploy").addOption(schemaOption).description("Deploy your pending migrations to your production/staging database.").action((options) => migrateAction("deploy", options));
|
|
529
|
+
migrateCommand.command("status").addOption(schemaOption).description("check the status of your database migrations.").action((options) => migrateAction("status", options));
|
|
530
|
+
const dbCommand = program2.command("db").description("Manage your database schema during development.");
|
|
531
|
+
dbCommand.command("push").description("Push the state from your schema to your database").addOption(schemaOption).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));
|
|
532
|
+
program2.command("info").description("Get information of installed ZenStack and related packages.").argument("[path]", "project path", ".").action(infoAction);
|
|
533
|
+
program2.command("init").description("Initialize an existing project for ZenStack.").argument("[path]", "project path", ".").action(initAction);
|
|
534
|
+
return program2;
|
|
535
|
+
}
|
|
536
|
+
__name(createProgram, "createProgram");
|
|
537
|
+
var program = createProgram();
|
|
538
|
+
program.parse(process.argv);
|
|
539
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
540
|
+
0 && (module.exports = {
|
|
541
|
+
createProgram
|
|
542
|
+
});
|
|
543
|
+
//# sourceMappingURL=index.cjs.map
|