Skip to content

Commit 8284988

Browse files
authored
fix: support object literal in plugin fields processing (#351)
1 parent 2035319 commit 8284988

File tree

20 files changed

+108
-19
lines changed

20 files changed

+108
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zenstack-monorepo",
3-
"version": "1.0.0-alpha.101",
3+
"version": "1.0.0-alpha.102",
44
"description": "",
55
"scripts": {
66
"build": "pnpm -r build",

packages/language/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/language",
3-
"version": "1.0.0-alpha.101",
3+
"version": "1.0.0-alpha.102",
44
"displayName": "ZenStack modeling language compiler",
55
"description": "ZenStack modeling language compiler",
66
"homepage": "https://zenstack.dev",

packages/next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/next",
3-
"version": "1.0.0-alpha.101",
3+
"version": "1.0.0-alpha.102",
44
"displayName": "ZenStack Next.js integration",
55
"description": "ZenStack Next.js integration",
66
"homepage": "https://zenstack.dev",

packages/plugins/openapi/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/openapi",
33
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
4-
"version": "1.0.0-alpha.101",
4+
"version": "1.0.0-alpha.102",
55
"description": "ZenStack plugin and runtime supporting OpenAPI",
66
"main": "index.js",
77
"repository": {

packages/plugins/react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/react",
33
"displayName": "ZenStack plugin and runtime for ReactJS",
4-
"version": "1.0.0-alpha.101",
4+
"version": "1.0.0-alpha.102",
55
"description": "ZenStack plugin and runtime for ReactJS",
66
"main": "index.js",
77
"repository": {

packages/plugins/trpc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/trpc",
33
"displayName": "ZenStack plugin for tRPC",
4-
"version": "1.0.0-alpha.101",
4+
"version": "1.0.0-alpha.102",
55
"description": "ZenStack plugin for tRPC",
66
"main": "index.js",
77
"repository": {

packages/runtime/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@zenstackhq/runtime",
33
"displayName": "ZenStack Runtime Library",
4-
"version": "1.0.0-alpha.101",
4+
"version": "1.0.0-alpha.102",
55
"description": "Runtime of ZenStack for both client-side and server-side environments.",
66
"repository": {
77
"type": "git",

packages/runtime/src/version.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export function getVersion() {
44
return require('./package.json').version;
55
} catch {
66
try {
7+
// dev environment
78
return require('../package.json').version;
89
} catch {
910
return 'unknown';

packages/schema/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"publisher": "zenstack",
44
"displayName": "ZenStack Language Tools",
55
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
6-
"version": "1.0.0-alpha.101",
6+
"version": "1.0.0-alpha.102",
77
"author": {
88
"name": "ZenStack Team"
99
},

packages/schema/src/cli/cli-util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str
157157
);
158158
}
159159
} catch {
160-
console.warn(`Unable to load plugin from ${provider}, skipping`);
160+
// noop
161161
}
162162
}
163163
}

packages/schema/src/cli/plugin-runner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ora from 'ora';
99
import path from 'path';
1010
import telemetry from '../telemetry';
1111
import type { Context } from '../types';
12+
import { getVersion } from '../utils/version-utils';
1213
import { config } from './config';
1314

1415
/**
@@ -19,7 +20,7 @@ export class PluginRunner {
1920
* Runs a series of nested generators
2021
*/
2122
async run(context: Context): Promise<void> {
22-
const version = require('../package.json').version;
23+
const version = getVersion();
2324
console.log(colors.bold(`⌛️ ZenStack CLI v${version}, running plugins`));
2425

2526
const plugins: Array<{

packages/schema/src/res/starter.zmodel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
datasource db {
99
provider = 'sqlite'
10-
url = 'file:./todo.db'
10+
url = 'file:./dev.db'
1111
}
1212

1313
generator client {

packages/schema/src/utils/version-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export function getVersion() {
33
try {
44
return require('../package.json').version;
55
} catch {
6+
// dev environment
67
return require('../../package.json').version;
78
}
89
}

packages/schema/tests/cli/command.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ describe('CLI Command Tests', () => {
9191
expect(fs.readFileSync('schema.zmodel', 'utf-8')).toEqual(fs.readFileSync('prisma/my.prisma', 'utf-8'));
9292
});
9393

94-
// eslint-disable-next-line jest/no-disabled-tests
9594
it('init project empty project', async () => {
9695
fs.writeFileSync('package.json', JSON.stringify({ name: 'my app', version: '1.0.0' }));
9796
createNpmrc();
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
/// <reference types="@types/jest" />
3+
4+
import { getWorkspaceNpmCacheFolder } from '@zenstackhq/testtools';
5+
import * as fs from 'fs';
6+
import * as path from 'path';
7+
import * as tmp from 'tmp';
8+
import { createProgram } from '../../src/cli';
9+
10+
describe('CLI Plugins Tests', () => {
11+
let projDir: string;
12+
let origDir: string;
13+
14+
beforeEach(() => {
15+
origDir = process.cwd();
16+
const r = tmp.dirSync();
17+
projDir = r.name;
18+
console.log(`Project dir: ${projDir}`);
19+
process.chdir(projDir);
20+
});
21+
22+
afterEach(() => {
23+
// fs.rmSync(projDir, { recursive: true, force: true });
24+
process.chdir(origDir);
25+
});
26+
27+
function createNpmrc() {
28+
fs.writeFileSync('.npmrc', `cache=${getWorkspaceNpmCacheFolder(__dirname)}`);
29+
}
30+
31+
const plugins = [
32+
`plugin prisma {
33+
provider = '@core/prisma'
34+
output = 'prisma/my.prisma'
35+
generateClient = true
36+
}`,
37+
`plugin meta {
38+
provider = '@core/model-meta'
39+
output = 'model-meta'
40+
}
41+
`,
42+
`plugin policy {
43+
provider = '@core/access-policy'
44+
output = 'policy'
45+
}`,
46+
`plugin zod {
47+
provider = '@core/zod'
48+
output = 'zod'
49+
}`,
50+
`plugin react {
51+
provider = '${path.join(__dirname, '../../../plugins/react/dist')}'
52+
output = 'lib/default-hooks'
53+
}`,
54+
`plugin trpc {
55+
provider = '${path.join(__dirname, '../../../plugins/trpc/dist')}'
56+
output = 'lib/trpc'
57+
}`,
58+
`plugin openapi {
59+
provider = '${path.join(__dirname, '../../../plugins/openapi/dist')}'
60+
output = 'myapi.yaml'
61+
specVersion = '3.0.0'
62+
title = 'My Awesome API'
63+
version = '1.0.0'
64+
description = 'awesome api'
65+
prefix = '/myapi'
66+
securitySchemes = {
67+
myBasic: { type: 'http', scheme: 'basic' },
68+
myBearer: { type: 'http', scheme: 'bearer', bearerFormat: 'JWT' },
69+
myApiKey: { type: 'apiKey', in: 'header', name: 'X-API-KEY' }
70+
}
71+
}`,
72+
];
73+
74+
it('all plugins', async () => {
75+
fs.writeFileSync('package.json', JSON.stringify({ name: 'my app', version: '1.0.0' }));
76+
createNpmrc();
77+
const program = createProgram();
78+
await program.parseAsync(['init', '--tag', 'latest'], { from: 'user' });
79+
80+
let schemaContent = fs.readFileSync('schema.zmodel', 'utf-8');
81+
for (const plugin of plugins) {
82+
schemaContent += `\n${plugin}`;
83+
}
84+
fs.writeFileSync('schema.zmodel', schemaContent);
85+
await program.parseAsync(['generate', '--no-dependency-check'], { from: 'user' });
86+
});
87+
});

packages/sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/sdk",
3-
"version": "1.0.0-alpha.101",
3+
"version": "1.0.0-alpha.102",
44
"description": "ZenStack plugin development SDK",
55
"main": "index.js",
66
"scripts": {

packages/sdk/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function getLiteral<T extends string | number | boolean | any = any>(
3434
expr: Expression | undefined
3535
): T | undefined {
3636
if (!isLiteralExpr(expr)) {
37-
return undefined;
37+
return getObjectLiteral<T>(expr);
3838
}
3939
return expr.value as T;
4040
}
@@ -51,7 +51,7 @@ export function getLiteralArray<
5151
if (!arr) {
5252
return undefined;
5353
}
54-
return arr.map((item) => getLiteral<T>(item) ?? getObjectLiteral<T>(item));
54+
return arr.map((item) => getLiteral<T>(item));
5555
}
5656

5757
export function getObjectLiteral<T>(expr: Expression | undefined): T | undefined {

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/server",
3-
"version": "1.0.0-alpha.101",
3+
"version": "1.0.0-alpha.102",
44
"displayName": "ZenStack Server-side Adapters",
55
"description": "ZenStack server-side adapters",
66
"homepage": "https://zenstack.dev",

packages/testtools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@zenstackhq/testtools",
3-
"version": "1.0.0-alpha.101",
3+
"version": "1.0.0-alpha.102",
44
"description": "ZenStack Test Tools",
55
"main": "index.js",
66
"publishConfig": {

tests/integration/test-run/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)