diff --git a/packages/cli/generator/package.json b/packages/cli/generator/package.json new file mode 100644 index 0000000..7188674 --- /dev/null +++ b/packages/cli/generator/package.json @@ -0,0 +1,40 @@ +{ + "name": "@unioc/generator", + "type": "module", + "version": "0.0.9", + "description": "Template generator engine for unioc.", + "author": "Naily Zero (https://naily.cc)", + "license": "MIT", + "homepage": "https://unioc.dev", + "repository": { + "type": "git", + "url": "https://github.com/uniocjs/core.git", + "directory": "packages/cli/generator" + }, + "bugs": { + "url": "https://github.com/uniocjs/core/issues" + }, + "keywords": [ + "unioc", + "generator", + "template" + ], + "files": [ + "dist", + "templates" + ], + "scripts": { + "build": "tsdown", + "watch": "tsdown -w", + "prepublishOnly": "tsdown" + }, + "publishConfig": { + "access": "public" + }, + "dependencies": { + "@types/prompts": "^2.4.9", + "@unioc/shared": "workspace:*", + "hbs": "^4.2.0", + "prompts": "^2.4.2" + } +} diff --git a/packages/cli/generator/src/index.ts b/packages/cli/generator/src/index.ts new file mode 100644 index 0000000..58f2038 --- /dev/null +++ b/packages/cli/generator/src/index.ts @@ -0,0 +1,38 @@ +export interface IGeneratorTemplateCollection { + files: string[] | string + baseDir: string +} + +export interface IGeneratorOptions { + templateCollections: IGeneratorTemplateCollection[] +} + +export interface IGeneratorDestination { + baseDir: string + outDir: string +} + +export interface IGenerateOptions { + destinations: IGeneratorDestination[] +} + +export interface IEjectOptions {} + +export interface IGeneratorService { + generate(generateOptions: IGenerateOptions): Promise + eject(ejectOptions: IEjectOptions): Promise +} + +export async function createGenerator(_generatorOptions: IGeneratorOptions): Promise { + async function generate(_generateOptions: IGenerateOptions): Promise { + + } + + async function eject(_ejectOptions: IEjectOptions): Promise { + } + + return { + generate, + eject, + } +} diff --git a/packages/cli/generator/templates/vite-nestjs/package.json.hbs b/packages/cli/generator/templates/vite-nestjs/package.json.hbs new file mode 100644 index 0000000..451560a --- /dev/null +++ b/packages/cli/generator/templates/vite-nestjs/package.json.hbs @@ -0,0 +1,16 @@ +{ + "name": "{{name}}", + "version": "0.0.1", + "private": true, + "description": "", + "type": "module", + "scripts": { + "dev": "vite build --watch", + "build": "vite build", + "preview": "node ./dist/main.js" + }, + "devDependencies": { + "vite": "^6.3.3", + "unioc": "latest" + } +} \ No newline at end of file diff --git a/packages/cli/generator/templates/vite-nestjs/src/app.controller.ts.hbs b/packages/cli/generator/templates/vite-nestjs/src/app.controller.ts.hbs new file mode 100644 index 0000000..dfc7be6 --- /dev/null +++ b/packages/cli/generator/templates/vite-nestjs/src/app.controller.ts.hbs @@ -0,0 +1,12 @@ +import { Controller, Get } from '@nestjs/common' +import { AppService } from './app.service' + +@Controller() +export class AppController { + constructor(private readonly appService: AppService) {} + + @Get() + getHello(): string { + return this.appService.getHello() + } +} \ No newline at end of file diff --git a/packages/cli/generator/templates/vite-nestjs/src/app.module.ts.hbs b/packages/cli/generator/templates/vite-nestjs/src/app.module.ts.hbs new file mode 100644 index 0000000..2550331 --- /dev/null +++ b/packages/cli/generator/templates/vite-nestjs/src/app.module.ts.hbs @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common' +import { AppController } from './app.controller' +import { AppService } from './app.service' + +@Module({ + imports: [], + controllers: [AppController], + providers: [AppService], +}) +export class AppModule {} \ No newline at end of file diff --git a/packages/cli/generator/templates/vite-nestjs/src/app.service.ts.hbs b/packages/cli/generator/templates/vite-nestjs/src/app.service.ts.hbs new file mode 100644 index 0000000..bc13055 --- /dev/null +++ b/packages/cli/generator/templates/vite-nestjs/src/app.service.ts.hbs @@ -0,0 +1,8 @@ +import { Injectable } from '@nestjs/common' + +@Injectable() +export class AppService { + getHello(): string { + return 'Hello World!' + } +} diff --git a/packages/cli/generator/templates/vite-nestjs/src/main.ts.hbs b/packages/cli/generator/templates/vite-nestjs/src/main.ts.hbs new file mode 100644 index 0000000..179c344 --- /dev/null +++ b/packages/cli/generator/templates/vite-nestjs/src/main.ts.hbs @@ -0,0 +1,15 @@ +import { ExpressApp } from 'unioc/web-express' +import process from 'node:process' +import { NestJS } from 'unioc/adapter-nestjs' +import { AppModule } from './app.module' + +async function main(): Promise { + const app = new ExpressApp() + + app.use(NestJS, { + imports: [AppModule], + }) + + await app.run(process.env.PORT ?? 3000) +} +main() \ No newline at end of file diff --git a/packages/cli/generator/templates/vite-nestjs/tsconfig.json.hbs b/packages/cli/generator/templates/vite-nestjs/tsconfig.json.hbs new file mode 100644 index 0000000..6ae5742 --- /dev/null +++ b/packages/cli/generator/templates/vite-nestjs/tsconfig.json.hbs @@ -0,0 +1,13 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "strict": true, + "moduleResolution": "bundler", + "skipLibCheck": true, + "skipDefaultLibCheck": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true + }, + "include": ["src/**/*.ts"] +} \ No newline at end of file diff --git a/packages/cli/generator/templates/vite-nestjs/vite.config.ts.hbs b/packages/cli/generator/templates/vite-nestjs/vite.config.ts.hbs new file mode 100644 index 0000000..3153af8 --- /dev/null +++ b/packages/cli/generator/templates/vite-nestjs/vite.config.ts.hbs @@ -0,0 +1,11 @@ +import { defineConfig } from 'vite' +import { NodeRunner } from 'vite-plugin-node-runner' + +export default defineConfig({ + plugins: [ + NodeRunner({ + entry: './dist/main.js', + sourceMap: true, + }), + ], +}) \ No newline at end of file diff --git a/packages/cli/generator/tsconfig.json b/packages/cli/generator/tsconfig.json new file mode 100644 index 0000000..a476cab --- /dev/null +++ b/packages/cli/generator/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "target": "ES2022", + "lib": ["ESNext"], + "module": "ES2022", + "moduleResolution": "Bundler", + "strict": true, + "strictPropertyInitialization": false + }, + "include": ["src", "test"] +} diff --git a/packages/cli/generator/tsdown.config.ts b/packages/cli/generator/tsdown.config.ts new file mode 100644 index 0000000..0a3f597 --- /dev/null +++ b/packages/cli/generator/tsdown.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from 'tsdown' +import swc from 'unplugin-swc' + +export default defineConfig({ + entry: { + index: './src/index.ts', + }, + sourcemap: true, + clean: true, + format: ['esm', 'cjs'], + plugins: [ + swc.rolldown(), + ], +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e2c40de..387adec 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -435,6 +435,21 @@ importers: specifier: ^4.0.1 version: 4.0.1 + packages/cli/generator: + dependencies: + '@types/prompts': + specifier: ^2.4.9 + version: 2.4.9 + '@unioc/shared': + specifier: workspace:* + version: link:../../core/shared + hbs: + specifier: ^4.2.0 + version: 4.2.0 + prompts: + specifier: ^2.4.2 + version: 2.4.2 + packages/core/core: dependencies: '@unioc/decorator': @@ -2673,6 +2688,9 @@ packages: '@types/object-path@0.11.4': resolution: {integrity: sha512-4tgJ1Z3elF/tOMpA8JLVuR9spt9Ynsf7+JjqsQ2IqtiPJtcLoHoXcT6qU4E10cPFqyXX5HDm9QwIzZhBSkLxsw==} + '@types/prompts@2.4.9': + resolution: {integrity: sha512-qTxFi6Buiu8+50/+3DGIWLHM6QuWsEKugJnnP6iv2Mc4ncxE4A/OJkjuVOA+5X0X1S/nq5VJRa8Lu+nwcvbrKA==} + '@types/qs@6.9.18': resolution: {integrity: sha512-kK7dgTYDyGqS+e2Q4aK9X3D7q234CIZ1Bv0q/7Z5IwRDoADNU81xXJK/YVyLbLTZCoIwUoDoffFeF+p/eIklAA==} @@ -4849,6 +4867,9 @@ packages: resolution: {integrity: sha512-0OABksIGrxKK8K4kynWkQ7y1zounQxP+CWnyclVwj81KW3vlLlGUx57DKGcP/LH216GzqnstnPocF16Nxs0Ycg==} engines: {node: '>=0.10.0'} + foreachasync@3.0.0: + resolution: {integrity: sha512-J+ler7Ta54FwwNcx6wQRDhTIbNeyDcARMkOcguEqnEdtm0jKvN3Li3PDAb2Du3ubJYEWfYL83XMROXdsXAXycw==} + foreground-child@3.3.1: resolution: {integrity: sha512-gIXjKqtFuWEgzFRJA9WCQeSJLZDjgJUOMCMzxtvFq/37KojM1BFGufqsCy0r4qSQmYLsZYMeyRqzIWOMup03sw==} engines: {node: '>=14'} @@ -5044,6 +5065,11 @@ packages: hachure-fill@0.5.2: resolution: {integrity: sha512-3GKBOn+m2LX9iq+JC1064cSFprJY4jL1jCXTcpnfER5HYE2l/4EfWSGzkPa/ZDBmYI0ZOEj5VHV/eKnPGkHuOg==} + handlebars@4.7.7: + resolution: {integrity: sha512-aAcXm5OAfE/8IXkcZvCepKU3VzW1/39Fb5ZuqMtgI/hT8X2YgoMvBY5dLhq/cpOvw7Lk1nK/UF71aLG/ZnVYRA==} + engines: {node: '>=0.4.7'} + hasBin: true + handlebars@4.7.8: resolution: {integrity: sha512-vafaFqs8MZkRrSX7sFVUdo3ap/eNiLnb4IakshzvP56X5Nr1iGKAIqdX6tMlm6HcNRIkr6AxO5jFEoJzzpT8aQ==} engines: {node: '>=0.4.7'} @@ -5083,6 +5109,10 @@ packages: hast-util-whitespace@3.0.0: resolution: {integrity: sha512-88JUN06ipLwsnv+dVn+OIYOvAuvBMy/Qoi6O7mQHxdPXpjy+Cd6xRkWwux7DKO+4sYILtLBRIKgsdpS2gQc7qw==} + hbs@4.2.0: + resolution: {integrity: sha512-dQwHnrfWlTk5PvG9+a45GYpg0VpX47ryKF8dULVd6DtwOE6TEcYQXQ5QM6nyOx/h7v3bvEQbdn19EDAcfUAgZg==} + engines: {node: '>= 0.8', npm: 1.2.8000 || >= 1.4.16} + he@1.2.0: resolution: {integrity: sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==} hasBin: true @@ -8119,6 +8149,9 @@ packages: typescript: optional: true + walk@2.3.15: + resolution: {integrity: sha512-4eRTBZljBfIISK1Vnt69Gvr2w/wc3U6Vtrw7qiN5iqYJPH7LElcYh/iU4XWhdCy2dZqv1ToMyYlybDylfG/5Vg==} + walker@1.0.8: resolution: {integrity: sha512-ts/8E8l5b7kY0vlWLewOkDXMmPdLcVV4GmOQLyxuSswIJsweeFZtAsMF7k1Nszz+TYBQrlYRmzOnr398y1JemQ==} @@ -10573,6 +10606,11 @@ snapshots: '@types/object-path@0.11.4': {} + '@types/prompts@2.4.9': + dependencies: + '@types/node': 22.15.2 + kleur: 3.0.3 + '@types/qs@6.9.18': {} '@types/range-parser@1.2.7': {} @@ -13163,6 +13201,8 @@ snapshots: dependencies: for-in: 1.0.2 + foreachasync@3.0.0: {} + foreground-child@3.3.1: dependencies: cross-spawn: 7.0.6 @@ -13400,6 +13440,15 @@ snapshots: hachure-fill@0.5.2: {} + handlebars@4.7.7: + dependencies: + minimist: 1.2.8 + neo-async: 2.6.2 + source-map: 0.6.1 + wordwrap: 1.0.0 + optionalDependencies: + uglify-js: 3.19.3 + handlebars@4.7.8: dependencies: minimist: 1.2.8 @@ -13445,6 +13494,11 @@ snapshots: dependencies: '@types/hast': 3.0.4 + hbs@4.2.0: + dependencies: + handlebars: 4.7.7 + walk: 2.3.15 + he@1.2.0: {} header-case@2.0.4: @@ -17096,6 +17150,10 @@ snapshots: optionalDependencies: typescript: 5.8.3 + walk@2.3.15: + dependencies: + foreachasync: 3.0.0 + walker@1.0.8: dependencies: makeerror: 1.0.12