@zenstackhq/cli 3.0.0-beta.21 → 3.0.0-beta.23
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 +26 -8
- package/dist/index.cjs +43 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +46 -26
- package/dist/index.js.map +1 -1
- package/package.json +9 -9
- package/src/actions/action-utils.ts +9 -1
- package/src/actions/db.ts +3 -3
- package/src/actions/migrate.ts +18 -22
- package/src/utils/exec-utils.ts +16 -0
- package/test/generate.test.ts +15 -0
- package/test/migrate.test.ts +1 -2
package/src/actions/db.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
|
-
import {
|
|
2
|
+
import { execPrisma } from '../utils/exec-utils';
|
|
3
3
|
import { generateTempPrismaSchema, getSchemaFile, handleSubProcessError } from './action-utils';
|
|
4
4
|
|
|
5
5
|
type Options = {
|
|
@@ -27,7 +27,7 @@ async function runPush(options: Options) {
|
|
|
27
27
|
try {
|
|
28
28
|
// run prisma db push
|
|
29
29
|
const cmd = [
|
|
30
|
-
'
|
|
30
|
+
'db push',
|
|
31
31
|
` --schema "${prismaSchemaFile}"`,
|
|
32
32
|
options.acceptDataLoss ? ' --accept-data-loss' : '',
|
|
33
33
|
options.forceReset ? ' --force-reset' : '',
|
|
@@ -35,7 +35,7 @@ async function runPush(options: Options) {
|
|
|
35
35
|
].join('');
|
|
36
36
|
|
|
37
37
|
try {
|
|
38
|
-
|
|
38
|
+
execPrisma(cmd);
|
|
39
39
|
} catch (err) {
|
|
40
40
|
handleSubProcessError(err);
|
|
41
41
|
}
|
package/src/actions/migrate.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { CliError } from '../cli-error';
|
|
4
|
-
import {
|
|
4
|
+
import { execPrisma } from '../utils/exec-utils';
|
|
5
5
|
import { generateTempPrismaSchema, getSchemaFile } from './action-utils';
|
|
6
6
|
|
|
7
7
|
type CommonOptions = {
|
|
@@ -64,69 +64,65 @@ export async function run(command: string, options: CommonOptions) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
67
|
+
function runDev(prismaSchemaFile: string, options: DevOptions) {
|
|
68
68
|
try {
|
|
69
69
|
const cmd = [
|
|
70
|
-
'
|
|
70
|
+
'migrate dev',
|
|
71
71
|
` --schema "${prismaSchemaFile}"`,
|
|
72
72
|
' --skip-generate',
|
|
73
|
-
options.name ? ` --name ${options.name}` : '',
|
|
73
|
+
options.name ? ` --name "${options.name}"` : '',
|
|
74
74
|
options.createOnly ? ' --create-only' : '',
|
|
75
75
|
].join('');
|
|
76
|
-
|
|
77
|
-
await execPackage(cmd);
|
|
76
|
+
execPrisma(cmd);
|
|
78
77
|
} catch (err) {
|
|
79
78
|
handleSubProcessError(err);
|
|
80
79
|
}
|
|
81
80
|
}
|
|
82
81
|
|
|
83
|
-
|
|
82
|
+
function runReset(prismaSchemaFile: string, options: ResetOptions) {
|
|
84
83
|
try {
|
|
85
84
|
const cmd = [
|
|
86
|
-
'
|
|
85
|
+
'migrate reset',
|
|
87
86
|
` --schema "${prismaSchemaFile}"`,
|
|
88
87
|
' --skip-generate',
|
|
89
88
|
options.force ? ' --force' : '',
|
|
90
89
|
].join('');
|
|
91
|
-
|
|
92
|
-
await execPackage(cmd);
|
|
90
|
+
execPrisma(cmd);
|
|
93
91
|
} catch (err) {
|
|
94
92
|
handleSubProcessError(err);
|
|
95
93
|
}
|
|
96
94
|
}
|
|
97
95
|
|
|
98
|
-
|
|
96
|
+
function runDeploy(prismaSchemaFile: string, _options: DeployOptions) {
|
|
99
97
|
try {
|
|
100
|
-
const cmd = ['
|
|
101
|
-
|
|
102
|
-
await execPackage(cmd);
|
|
98
|
+
const cmd = ['migrate deploy', ` --schema "${prismaSchemaFile}"`].join('');
|
|
99
|
+
execPrisma(cmd);
|
|
103
100
|
} catch (err) {
|
|
104
101
|
handleSubProcessError(err);
|
|
105
102
|
}
|
|
106
103
|
}
|
|
107
104
|
|
|
108
|
-
|
|
105
|
+
function runStatus(prismaSchemaFile: string, _options: StatusOptions) {
|
|
109
106
|
try {
|
|
110
|
-
|
|
107
|
+
execPrisma(`migrate status --schema "${prismaSchemaFile}"`);
|
|
111
108
|
} catch (err) {
|
|
112
109
|
handleSubProcessError(err);
|
|
113
110
|
}
|
|
114
111
|
}
|
|
115
112
|
|
|
116
|
-
|
|
113
|
+
function runResolve(prismaSchemaFile: string, options: ResolveOptions) {
|
|
117
114
|
if (!options.applied && !options.rolledBack) {
|
|
118
115
|
throw new CliError('Either --applied or --rolled-back option must be provided');
|
|
119
116
|
}
|
|
120
117
|
|
|
121
118
|
try {
|
|
122
119
|
const cmd = [
|
|
123
|
-
'
|
|
120
|
+
'migrate resolve',
|
|
124
121
|
` --schema "${prismaSchemaFile}"`,
|
|
125
|
-
options.applied ? ` --applied ${options.applied}` : '',
|
|
126
|
-
options.rolledBack ? ` --rolled-back ${options.rolledBack}` : '',
|
|
122
|
+
options.applied ? ` --applied "${options.applied}"` : '',
|
|
123
|
+
options.rolledBack ? ` --rolled-back "${options.rolledBack}"` : '',
|
|
127
124
|
].join('');
|
|
128
|
-
|
|
129
|
-
await execPackage(cmd);
|
|
125
|
+
execPrisma(cmd);
|
|
130
126
|
} catch (err) {
|
|
131
127
|
handleSubProcessError(err);
|
|
132
128
|
}
|
package/src/utils/exec-utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { execSync as _exec, type ExecSyncOptions } from 'child_process';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Utility for executing command synchronously and prints outputs on current console
|
|
@@ -24,3 +25,18 @@ export function execPackage(
|
|
|
24
25
|
const packageManager = process?.versions?.['bun'] ? 'bunx' : 'npx';
|
|
25
26
|
execSync(`${packageManager} ${cmd}`, options);
|
|
26
27
|
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Utility for running prisma commands
|
|
31
|
+
*/
|
|
32
|
+
export function execPrisma(args: string, options?: Omit<ExecSyncOptions, 'env'> & { env?: Record<string, string> }) {
|
|
33
|
+
let prismaPath: string;
|
|
34
|
+
if (typeof import.meta.resolve === 'function') {
|
|
35
|
+
// esm
|
|
36
|
+
prismaPath = fileURLToPath(import.meta.resolve('prisma/build/index.js'));
|
|
37
|
+
} else {
|
|
38
|
+
// cjs
|
|
39
|
+
prismaPath = require.resolve('prisma/build/index.js');
|
|
40
|
+
}
|
|
41
|
+
execSync(`node ${prismaPath} ${args}`, options);
|
|
42
|
+
}
|
package/test/generate.test.ts
CHANGED
|
@@ -45,6 +45,21 @@ describe('CLI generate command test', () => {
|
|
|
45
45
|
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
+
it('should respect package.json schema dir config', () => {
|
|
49
|
+
const workDir = createProject(model);
|
|
50
|
+
fs.mkdirSync(path.join(workDir, 'foo'));
|
|
51
|
+
fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'foo/schema.zmodel'));
|
|
52
|
+
fs.rmdirSync(path.join(workDir, 'zenstack'));
|
|
53
|
+
const pkgJson = JSON.parse(fs.readFileSync(path.join(workDir, 'package.json'), 'utf8'));
|
|
54
|
+
pkgJson.zenstack = {
|
|
55
|
+
schema: './foo',
|
|
56
|
+
output: './bar',
|
|
57
|
+
};
|
|
58
|
+
fs.writeFileSync(path.join(workDir, 'package.json'), JSON.stringify(pkgJson, null, 2));
|
|
59
|
+
runCli('generate', workDir);
|
|
60
|
+
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
48
63
|
it('should respect lite option', () => {
|
|
49
64
|
const workDir = createProject(model);
|
|
50
65
|
runCli('generate --lite', workDir);
|
package/test/migrate.test.ts
CHANGED
|
@@ -9,8 +9,7 @@ model User {
|
|
|
9
9
|
}
|
|
10
10
|
`;
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
describe.skip('CLI migrate commands test', () => {
|
|
12
|
+
describe('CLI migrate commands test', () => {
|
|
14
13
|
it('should generate a database with migrate dev', () => {
|
|
15
14
|
const workDir = createProject(model);
|
|
16
15
|
runCli('migrate dev --name init', workDir);
|