@zenstackhq/cli 3.0.0-alpha.4 → 3.0.0-alpha.7

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.
@@ -0,0 +1,44 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { describe, expect, it } from 'vitest';
4
+ import { createProject, runCli } from './utils';
5
+
6
+ const model = `
7
+ model User {
8
+ id String @id @default(cuid())
9
+ }
10
+ `;
11
+
12
+ describe('CLI generate command test', () => {
13
+ it('should generate a TypeScript schema', () => {
14
+ const workDir = createProject(model);
15
+ runCli('generate', workDir);
16
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
17
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(false);
18
+ });
19
+
20
+ it('should respect custom output directory', () => {
21
+ const workDir = createProject(model);
22
+ runCli('generate --output ./zen', workDir);
23
+ expect(fs.existsSync(path.join(workDir, 'zen/schema.ts'))).toBe(true);
24
+ });
25
+
26
+ it('should respect custom schema location', () => {
27
+ const workDir = createProject(model);
28
+ fs.renameSync(path.join(workDir, 'zenstack/schema.zmodel'), path.join(workDir, 'zenstack/foo.zmodel'));
29
+ runCli('generate --schema ./zenstack/foo.zmodel', workDir);
30
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
31
+ });
32
+
33
+ it('should respect save prisma schema option', () => {
34
+ const workDir = createProject(model);
35
+ runCli('generate --save-prisma-schema', workDir);
36
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema.prisma'))).toBe(true);
37
+ });
38
+
39
+ it('should respect save prisma schema custom path option', () => {
40
+ const workDir = createProject(model);
41
+ runCli('generate --save-prisma-schema "../prisma/schema.prisma"', workDir);
42
+ expect(fs.existsSync(path.join(workDir, 'prisma/schema.prisma'))).toBe(true);
43
+ });
44
+ });
@@ -0,0 +1,13 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import tmp from 'tmp';
4
+ import { describe, expect, it } from 'vitest';
5
+ import { runCli } from './utils';
6
+
7
+ describe('Cli init command tests', () => {
8
+ it('should create a new project', () => {
9
+ const { name: workDir } = tmp.dirSync({ unsafeCleanup: true });
10
+ runCli('init', workDir);
11
+ expect(fs.existsSync(path.join(workDir, 'zenstack/schema.zmodel'))).toBe(true);
12
+ });
13
+ });
@@ -0,0 +1,41 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { describe, expect, it } from 'vitest';
4
+ import { createProject, runCli } from './utils';
5
+
6
+ const model = `
7
+ model User {
8
+ id String @id @default(cuid())
9
+ }
10
+ `;
11
+
12
+ describe('CLI migrate commands test', () => {
13
+ it('should generate a database with migrate dev', () => {
14
+ const workDir = createProject(model);
15
+ runCli('migrate dev --name init', workDir);
16
+ expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
17
+ expect(fs.existsSync(path.join(workDir, 'zenstack/migrations'))).toBe(true);
18
+ });
19
+
20
+ it('should reset the database with migrate reset', () => {
21
+ const workDir = createProject(model);
22
+ runCli('db push', workDir);
23
+ expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
24
+ runCli('migrate reset --force', workDir);
25
+ expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
26
+ });
27
+
28
+ it('should reset the database with migrate deploy', () => {
29
+ const workDir = createProject(model);
30
+ runCli('migrate dev --name init', workDir);
31
+ fs.rmSync(path.join(workDir, 'zenstack/dev.db'));
32
+ runCli('migrate deploy', workDir);
33
+ expect(fs.existsSync(path.join(workDir, 'zenstack/dev.db'))).toBe(true);
34
+ });
35
+
36
+ it('supports migrate status', () => {
37
+ const workDir = createProject(model);
38
+ runCli('migrate dev --name init', workDir);
39
+ runCli('migrate status', workDir);
40
+ });
41
+ });
package/test/utils.ts ADDED
@@ -0,0 +1,23 @@
1
+ import { createTestProject } from '@zenstackhq/testtools';
2
+ import { execSync } from 'node:child_process';
3
+ import fs from 'node:fs';
4
+ import path from 'node:path';
5
+
6
+ const ZMODEL_PRELUDE = `datasource db {
7
+ provider = "sqlite"
8
+ url = "file:./dev.db"
9
+ }
10
+ `;
11
+
12
+ export function createProject(zmodel: string, addPrelude = true) {
13
+ const workDir = createTestProject();
14
+ fs.mkdirSync(path.join(workDir, 'zenstack'), { recursive: true });
15
+ const schemaPath = path.join(workDir, 'zenstack/schema.zmodel');
16
+ fs.writeFileSync(schemaPath, addPrelude ? `${ZMODEL_PRELUDE}\n\n${zmodel}` : zmodel);
17
+ return workDir;
18
+ }
19
+
20
+ export function runCli(command: string, cwd: string) {
21
+ const cli = path.join(__dirname, '../dist/index.js');
22
+ execSync(`node ${cli} ${command}`, { cwd });
23
+ }
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "extends": "../../tsconfig.json",
2
+ "extends": "@zenstackhq/typescript-config/base.json",
3
3
  "compilerOptions": {
4
4
  "outDir": "dist"
5
5
  },
@@ -1,18 +0,0 @@
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
- =============