Skip to content

Commit

Permalink
fix: support object literal in plugin fields processing (zenstackhq#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Apr 14, 2023
1 parent 2035319 commit 8284988
Show file tree
Hide file tree
Showing 20 changed files with 108 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/next",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"displayName": "ZenStack Next.js integration",
"description": "ZenStack Next.js integration",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/react",
"displayName": "ZenStack plugin and runtime for ReactJS",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"description": "ZenStack plugin and runtime for ReactJS",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export function getVersion() {
return require('./package.json').version;
} catch {
try {
// dev environment
return require('../package.json').version;
} catch {
return 'unknown';
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"author": {
"name": "ZenStack Team"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export async function getPluginDocuments(services: ZModelServices, fileName: str
);
}
} catch {
console.warn(`Unable to load plugin from ${provider}, skipping`);
// noop
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/schema/src/cli/plugin-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ora from 'ora';
import path from 'path';
import telemetry from '../telemetry';
import type { Context } from '../types';
import { getVersion } from '../utils/version-utils';
import { config } from './config';

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

const plugins: Array<{
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/res/starter.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
datasource db {
provider = 'sqlite'
url = 'file:./todo.db'
url = 'file:./dev.db'
}

generator client {
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/utils/version-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export function getVersion() {
try {
return require('../package.json').version;
} catch {
// dev environment
return require('../../package.json').version;
}
}
1 change: 0 additions & 1 deletion packages/schema/tests/cli/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ describe('CLI Command Tests', () => {
expect(fs.readFileSync('schema.zmodel', 'utf-8')).toEqual(fs.readFileSync('prisma/my.prisma', 'utf-8'));
});

// eslint-disable-next-line jest/no-disabled-tests
it('init project empty project', async () => {
fs.writeFileSync('package.json', JSON.stringify({ name: 'my app', version: '1.0.0' }));
createNpmrc();
Expand Down
87 changes: 87 additions & 0 deletions packages/schema/tests/cli/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/// <reference types="@types/jest" />

import { getWorkspaceNpmCacheFolder } from '@zenstackhq/testtools';
import * as fs from 'fs';
import * as path from 'path';
import * as tmp from 'tmp';
import { createProgram } from '../../src/cli';

describe('CLI Plugins Tests', () => {
let projDir: string;
let origDir: string;

beforeEach(() => {
origDir = process.cwd();
const r = tmp.dirSync();
projDir = r.name;
console.log(`Project dir: ${projDir}`);
process.chdir(projDir);
});

afterEach(() => {
// fs.rmSync(projDir, { recursive: true, force: true });
process.chdir(origDir);
});

function createNpmrc() {
fs.writeFileSync('.npmrc', `cache=${getWorkspaceNpmCacheFolder(__dirname)}`);
}

const plugins = [
`plugin prisma {
provider = '@core/prisma'
output = 'prisma/my.prisma'
generateClient = true
}`,
`plugin meta {
provider = '@core/model-meta'
output = 'model-meta'
}
`,
`plugin policy {
provider = '@core/access-policy'
output = 'policy'
}`,
`plugin zod {
provider = '@core/zod'
output = 'zod'
}`,
`plugin react {
provider = '${path.join(__dirname, '../../../plugins/react/dist')}'
output = 'lib/default-hooks'
}`,
`plugin trpc {
provider = '${path.join(__dirname, '../../../plugins/trpc/dist')}'
output = 'lib/trpc'
}`,
`plugin openapi {
provider = '${path.join(__dirname, '../../../plugins/openapi/dist')}'
output = 'myapi.yaml'
specVersion = '3.0.0'
title = 'My Awesome API'
version = '1.0.0'
description = 'awesome api'
prefix = '/myapi'
securitySchemes = {
myBasic: { type: 'http', scheme: 'basic' },
myBearer: { type: 'http', scheme: 'bearer', bearerFormat: 'JWT' },
myApiKey: { type: 'apiKey', in: 'header', name: 'X-API-KEY' }
}
}`,
];

it('all plugins', async () => {
fs.writeFileSync('package.json', JSON.stringify({ name: 'my app', version: '1.0.0' }));
createNpmrc();
const program = createProgram();
await program.parseAsync(['init', '--tag', 'latest'], { from: 'user' });

let schemaContent = fs.readFileSync('schema.zmodel', 'utf-8');
for (const plugin of plugins) {
schemaContent += `\n${plugin}`;
}
fs.writeFileSync('schema.zmodel', schemaContent);
await program.parseAsync(['generate', '--no-dependency-check'], { from: 'user' });
});
});
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function getLiteral<T extends string | number | boolean | any = any>(
expr: Expression | undefined
): T | undefined {
if (!isLiteralExpr(expr)) {
return undefined;
return getObjectLiteral<T>(expr);
}
return expr.value as T;
}
Expand All @@ -51,7 +51,7 @@ export function getLiteralArray<
if (!arr) {
return undefined;
}
return arr.map((item) => getLiteral<T>(item) ?? getObjectLiteral<T>(item));
return arr.map((item) => getLiteral<T>(item));
}

export function getObjectLiteral<T>(expr: Expression | undefined): T | undefined {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/server",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"displayName": "ZenStack Server-side Adapters",
"description": "ZenStack server-side adapters",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "1.0.0-alpha.101",
"version": "1.0.0-alpha.102",
"description": "ZenStack Test Tools",
"main": "index.js",
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test-run/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8284988

Please sign in to comment.