diff --git a/docs/cli-commands.md b/docs/cli-commands.md index 2730da14e..6866282dc 100644 --- a/docs/cli-commands.md +++ b/docs/cli-commands.md @@ -8,6 +8,12 @@ Set up ZenStack for an existing Next.js + Typescript project. npx zenstack init [dir] ``` +_Options_: + +``` + -p, --package-manager : package manager to use: "npm", "yarn", or "pnpm" (default: auto detect) +``` + ## `generate` Generates RESTful CRUD API and React hooks from your model. @@ -19,7 +25,9 @@ npx zenstack generate [options] _Options_: ``` - --schema schema file (with extension .zmodel) (default: "./zenstack/schema.zmodel") + --schema : schema file (with extension .zmodel) (default: "./zenstack/schema.zmodel") + + -p, --package-manager : package manager to use: "npm", "yarn", or "pnpm" (default: auto detect) ``` ## `migrate` diff --git a/package.json b/package.json index b2cc5930c..f85a235a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zenstack-monorepo", - "version": "0.3.11", + "version": "0.3.12", "description": "", "scripts": { "build": "pnpm -r build", diff --git a/packages/runtime/package.json b/packages/runtime/package.json index 02ec1d9fa..e109c116c 100644 --- a/packages/runtime/package.json +++ b/packages/runtime/package.json @@ -1,7 +1,7 @@ { "name": "@zenstackhq/runtime", "displayName": "ZenStack Runtime Library", - "version": "0.3.11", + "version": "0.3.12", "description": "Runtime of ZenStack for both client-side and server-side environments.", "repository": { "type": "git", @@ -26,13 +26,13 @@ "deepcopy": "^2.1.0", "swr": "^1.3.0", "tslib": "^2.4.1", + "@types/bcryptjs": "^2.4.2", + "bcryptjs": "^2.4.3", "zod": "^3.19.1", "zod-validation-error": "^0.2.1" }, "peerDependencies": { - "@types/bcryptjs": "^2.4.2", - "bcryptjs": "^2.4.3", - "next": "^12.3.1", + "next": "^12.3.1 || ^13", "react": "^17.0.2 || ^18", "react-dom": "^17.0.2 || ^18" }, diff --git a/packages/schema/package.json b/packages/schema/package.json index 5f0ec71a6..329c3c937 100644 --- a/packages/schema/package.json +++ b/packages/schema/package.json @@ -3,7 +3,7 @@ "publisher": "zenstack", "displayName": "ZenStack Language Tools", "description": "A toolkit for modeling data and access policies in full-stack development with Next.js and Typescript", - "version": "0.3.11", + "version": "0.3.12", "author": { "name": "ZenStack Team" }, diff --git a/packages/schema/src/cli/cli-util.ts b/packages/schema/src/cli/cli-util.ts index 2f80d5c1c..bab3b8d4a 100644 --- a/packages/schema/src/cli/cli-util.ts +++ b/packages/schema/src/cli/cli-util.ts @@ -6,7 +6,7 @@ import fs from 'fs'; import { LangiumServices } from 'langium'; import { NodeFileSystem } from 'langium/node'; import path from 'path'; -import { installPackage } from 'src/utils/pkg-utils'; +import { installPackage, PackageManagers } from '../utils/pkg-utils'; import { URI } from 'vscode-uri'; import { ZenStackGenerator } from '../generator'; import { GENERATED_CODE_PATH } from '../generator/constants'; @@ -16,7 +16,15 @@ import { CliError } from './cli-error'; /** * Initializes an existing project for ZenStack */ -export async function initProject(projectPath: string) { +export async function initProject( + projectPath: string, + packageManager: PackageManagers | undefined +) { + if (!fs.existsSync(projectPath)) { + console.error(`Path does not exist: ${projectPath}`); + throw new CliError('project path does not exist'); + } + const schema = path.join(projectPath, 'zenstack', 'schema.zmodel'); let schemaGenerated = false; @@ -96,16 +104,18 @@ model Post { schemaGenerated = true; } - installPackage('zenstack', true, undefined, projectPath); - installPackage('@zenstackhq/runtime', false, undefined, projectPath); + installPackage('zenstack', true, packageManager, projectPath); + installPackage('@zenstackhq/runtime', false, packageManager, projectPath); if (schemaGenerated) { - console.log(`Sample model generated at: ${colors.green(schema)} + console.log(`Sample model generated at: ${colors.blue(schema)} Please check the following guide on how to model your app: https://zenstack.dev/#/modeling-your-app. - `); + `); } + + console.log(colors.green('\nProject initialized successfully!')); } /** @@ -173,7 +183,7 @@ export async function loadDocument( } export async function runGenerator( - options: { schema: string; packageManager: string }, + options: { schema: string; packageManager: PackageManagers | undefined }, includedGenerators?: string[], clearOutput = true ) { diff --git a/packages/schema/src/cli/index.ts b/packages/schema/src/cli/index.ts index 37c2d1c69..40787e1e3 100644 --- a/packages/schema/src/cli/index.ts +++ b/packages/schema/src/cli/index.ts @@ -3,25 +3,31 @@ import { paramCase } from 'change-case'; import colors from 'colors'; import { Command, Option } from 'commander'; import path from 'path'; +import { PackageManagers } from '../utils/pkg-utils'; import { ZModelLanguageMetaData } from '../language-server/generated/module'; import telemetry from '../telemetry'; import { execSync } from '../utils/exec-utils'; import { CliError } from './cli-error'; import { initProject, runGenerator } from './cli-util'; -export const initAction = async (projectPath: string): Promise => { +export const initAction = async ( + projectPath: string, + options: { + packageManager: PackageManagers | undefined; + } +): Promise => { await telemetry.trackSpan( 'cli:command:start', 'cli:command:complete', 'cli:command:error', { command: 'init' }, - () => initProject(projectPath) + () => initProject(projectPath, options.packageManager) ); }; export const generateAction = async (options: { schema: string; - packageManager: string; + packageManager: PackageManagers | undefined; }): Promise => { await telemetry.trackSpan( 'cli:command:start', @@ -118,9 +124,9 @@ export default async function (): Promise { ).default('./zenstack/schema.zmodel'); const pmOption = new Option( - '--package-manager, -p', - 'package manager to use: "npm", "yarn" or "pnpm"' - ).default('auto detect'); + '-p, --package-manager ', + 'package manager to use' + ).choices(['npm', 'yarn', 'pnpm']); //#region wraps Prisma commands @@ -128,7 +134,7 @@ export default async function (): Promise { .command('init') .description('Set up a new ZenStack project.') .addOption(pmOption) - .argument('', 'project path') + .argument('[path]', 'project path', '.') .action(initAction); program diff --git a/packages/schema/src/utils/pkg-utils.ts b/packages/schema/src/utils/pkg-utils.ts index dc65f0009..9662cb59c 100644 --- a/packages/schema/src/utils/pkg-utils.ts +++ b/packages/schema/src/utils/pkg-utils.ts @@ -2,7 +2,7 @@ import fs from 'fs'; import path from 'path'; import { execSync } from './exec-utils'; -type PackageManagers = 'npm' | 'yarn' | 'pnpm'; +export type PackageManagers = 'npm' | 'yarn' | 'pnpm'; function getPackageManager(projectPath = '.'): PackageManagers { if (fs.existsSync(path.join(projectPath, 'yarn.lock'))) { @@ -25,9 +25,9 @@ export function installPackage( switch (manager) { case 'yarn': execSync( - `yarn add --cwd "${projectPath}" ${ - dev ? ' --save-dev' : '' - } ${pkg}` + `yarn --cwd "${projectPath}" add ${pkg} ${ + dev ? ' --dev' : '' + } --ignore-engines` ); break; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 86dab9d63..8c90bcdb2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -18,7 +18,7 @@ importers: cuid: ^2.1.8 decimal.js: ^10.4.2 deepcopy: ^2.1.0 - next: ^12.3.1 + next: ^12.3.1 || ^13 react: ^17.0.2 || ^18 react-dom: ^17.0.2 || ^18 rimraf: ^3.0.2 @@ -28,6 +28,7 @@ importers: zod: ^3.19.1 zod-validation-error: ^0.2.1 dependencies: + '@types/bcryptjs': 2.4.2 bcryptjs: 2.4.3 colors: 1.4.0 cuid: 2.1.8 @@ -41,7 +42,6 @@ importers: zod: 3.19.1 zod-validation-error: 0.2.1_zod@3.19.1 devDependencies: - '@types/bcryptjs': 2.4.2 '@types/jest': 29.2.0 '@types/node': 14.18.32 rimraf: 3.0.2 @@ -1903,7 +1903,6 @@ packages: /@types/bcryptjs/2.4.2: resolution: {integrity: sha512-LiMQ6EOPob/4yUL66SZzu6Yh77cbzJFYll+ZfaPiPPFswtIlA/Fs1MzdKYA7JApHU49zQTbJGX3PDmCpIdDBRQ==} - dev: true /@types/cookiejar/2.1.2: resolution: {integrity: sha512-t73xJJrvdTjXrn4jLS9VSGRbz0nUY3cl2DMGDU48lKl+HR9dbbjW2A9r3g40VA++mQpy6uuHg33gy7du2BKpog==} diff --git a/samples/todo/package-lock.json b/samples/todo/package-lock.json index 7a41c7a76..678705d5f 100644 --- a/samples/todo/package-lock.json +++ b/samples/todo/package-lock.json @@ -1,16 +1,16 @@ { "name": "todo", - "version": "0.3.11", + "version": "0.3.12", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "todo", - "version": "0.3.11", + "version": "0.3.12", "dependencies": { "@heroicons/react": "^2.0.12", "@prisma/client": "^4.4.0", - "@zenstackhq/runtime": "^0.3.11", + "@zenstackhq/runtime": "^0.3.12", "bcryptjs": "^2.4.3", "daisyui": "^2.31.0", "moment": "^2.29.4", @@ -35,7 +35,7 @@ "postcss": "^8.4.16", "tailwindcss": "^3.1.8", "typescript": "^4.6.2", - "zenstack": "^0.3.11" + "zenstack": "^0.3.12" } }, "../../packages/runtime": { @@ -752,10 +752,12 @@ } }, "node_modules/@zenstackhq/runtime": { - "version": "0.3.11", - "resolved": "https://registry.npmjs.org/@zenstackhq/runtime/-/runtime-0.3.11.tgz", - "integrity": "sha512-LUxB0QaeeLPnGdC1+H89ViNLL46FF+yGiU2IkvXfWMN5Y+r1zNL4ikVSZCkDa6vJxM8TaBzVEflbps4DJjbCww==", + "version": "0.3.12", + "resolved": "https://registry.npmjs.org/@zenstackhq/runtime/-/runtime-0.3.12.tgz", + "integrity": "sha512-akXxw4h8uHOp4XHw5y1KSjL7A1URtRandWeXU82FLYBG6DKrjBUQuNhY2M+jD6BCboNiLOBzAJ54EPaJVr27uw==", "dependencies": { + "@types/bcryptjs": "^2.4.2", + "bcryptjs": "^2.4.3", "colors": "1.4.0", "cuid": "^2.1.8", "decimal.js": "^10.4.2", @@ -766,9 +768,7 @@ "zod-validation-error": "^0.2.1" }, "peerDependencies": { - "@types/bcryptjs": "^2.4.2", - "bcryptjs": "^2.4.3", - "next": "^12.3.1", + "next": "^12.3.1 || ^13", "react": "^17.0.2 || ^18", "react-dom": "^17.0.2 || ^18" } @@ -4605,12 +4605,12 @@ } }, "node_modules/zenstack": { - "version": "0.3.11", - "resolved": "https://registry.npmjs.org/zenstack/-/zenstack-0.3.11.tgz", - "integrity": "sha512-ypohX9ijyZfzqMR3ZpCyDvuLnfIYo2CdgMZ7NjvZofkiNvlMYBEvP1mvDwM4mVGt9PZFxwkm+XaQvSCMzGnvHg==", + "version": "0.3.12", + "resolved": "https://registry.npmjs.org/zenstack/-/zenstack-0.3.12.tgz", + "integrity": "sha512-oy1LiE18x7E2gGw+N+1eDOS5LXRTTUoYOvnBcBwflSSvHKdGddsIocrlSav/3y8uIwO2JdMjvh6NB88zpc11KA==", "dev": true, "dependencies": { - "@zenstackhq/runtime": "0.3.11", + "@zenstackhq/runtime": "0.3.12", "async-exit-hook": "^2.0.1", "change-case": "^4.1.2", "chevrotain": "^9.1.0", @@ -5129,10 +5129,12 @@ } }, "@zenstackhq/runtime": { - "version": "0.3.11", - "resolved": "https://registry.npmjs.org/@zenstackhq/runtime/-/runtime-0.3.11.tgz", - "integrity": "sha512-LUxB0QaeeLPnGdC1+H89ViNLL46FF+yGiU2IkvXfWMN5Y+r1zNL4ikVSZCkDa6vJxM8TaBzVEflbps4DJjbCww==", + "version": "0.3.12", + "resolved": "https://registry.npmjs.org/@zenstackhq/runtime/-/runtime-0.3.12.tgz", + "integrity": "sha512-akXxw4h8uHOp4XHw5y1KSjL7A1URtRandWeXU82FLYBG6DKrjBUQuNhY2M+jD6BCboNiLOBzAJ54EPaJVr27uw==", "requires": { + "@types/bcryptjs": "^2.4.2", + "bcryptjs": "^2.4.3", "colors": "1.4.0", "cuid": "^2.1.8", "decimal.js": "^10.4.2", @@ -7924,12 +7926,12 @@ "integrity": "sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg==" }, "zenstack": { - "version": "0.3.11", - "resolved": "https://registry.npmjs.org/zenstack/-/zenstack-0.3.11.tgz", - "integrity": "sha512-ypohX9ijyZfzqMR3ZpCyDvuLnfIYo2CdgMZ7NjvZofkiNvlMYBEvP1mvDwM4mVGt9PZFxwkm+XaQvSCMzGnvHg==", + "version": "0.3.12", + "resolved": "https://registry.npmjs.org/zenstack/-/zenstack-0.3.12.tgz", + "integrity": "sha512-oy1LiE18x7E2gGw+N+1eDOS5LXRTTUoYOvnBcBwflSSvHKdGddsIocrlSav/3y8uIwO2JdMjvh6NB88zpc11KA==", "dev": true, "requires": { - "@zenstackhq/runtime": "0.3.11", + "@zenstackhq/runtime": "0.3.12", "async-exit-hook": "^2.0.1", "change-case": "^4.1.2", "chevrotain": "^9.1.0", diff --git a/samples/todo/package.json b/samples/todo/package.json index 32d899f17..31c6f672f 100644 --- a/samples/todo/package.json +++ b/samples/todo/package.json @@ -1,6 +1,6 @@ { "name": "todo", - "version": "0.3.11", + "version": "0.3.12", "private": true, "scripts": { "dev": "next dev", @@ -21,7 +21,7 @@ "dependencies": { "@heroicons/react": "^2.0.12", "@prisma/client": "^4.4.0", - "@zenstackhq/runtime": "^0.3.11", + "@zenstackhq/runtime": "^0.3.12", "bcryptjs": "^2.4.3", "daisyui": "^2.31.0", "moment": "^2.29.4", @@ -46,6 +46,6 @@ "postcss": "^8.4.16", "tailwindcss": "^3.1.8", "typescript": "^4.6.2", - "zenstack": "^0.3.11" + "zenstack": "^0.3.12" } }