From e1ab4336a0ab6955cfd0c167cf115415928fc83e Mon Sep 17 00:00:00 2001 From: killagu Date: Fri, 22 Mar 2024 23:29:48 +0800 Subject: [PATCH] feat: impl egg-bin dal gen --- .gitignore | 1 + README.md | 10 +++++ lib/cmd/dal.js | 17 ++++++++ lib/cmd/dal/gen.js | 39 +++++++++++++++++ lib/dal-gen.js | 36 ++++++++++++++++ package.json | 9 +++- test/fixtures/dal/app/modules/dal/Bar.ts | 19 +++++++++ test/fixtures/dal/app/modules/dal/Foo.ts | 23 ++++++++++ .../fixtures/dal/app/modules/dal/package.json | 6 +++ test/fixtures/dal/package.json | 10 +++++ test/fixtures/dal/tsconfig.json | 6 +++ test/lib/cmd/dal.test.js | 42 +++++++++++++++++++ 12 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 lib/cmd/dal.js create mode 100644 lib/cmd/dal/gen.js create mode 100644 lib/dal-gen.js create mode 100644 test/fixtures/dal/app/modules/dal/Bar.ts create mode 100644 test/fixtures/dal/app/modules/dal/Foo.ts create mode 100644 test/fixtures/dal/app/modules/dal/package.json create mode 100644 test/fixtures/dal/package.json create mode 100644 test/fixtures/dal/tsconfig.json create mode 100644 test/lib/cmd/dal.test.js diff --git a/.gitignore b/.gitignore index 787f51b5..44179156 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ package-lock.json yarn.lock .c8_output .idea +.eslintcache diff --git a/README.md b/README.md index d683b1a7..5a24c683 100644 --- a/README.md +++ b/README.md @@ -334,6 +334,16 @@ $ my-egg-bin nsp run nsp check at /foo/bar with {} ``` +### dal + +Generate code for @eggjs/tegg-dal-plugin + +```bash +egg-bin dal gen +``` + +dal document please read [tegg doc](https://github.com/eggjs/tegg/tree/master/plugin/dal). + ## License [MIT](LICENSE) diff --git a/lib/cmd/dal.js b/lib/cmd/dal.js new file mode 100644 index 00000000..061bf128 --- /dev/null +++ b/lib/cmd/dal.js @@ -0,0 +1,17 @@ +'use strict'; + +const Command = require('../command'); +const path = require('node:path'); + +class DalCommand extends Command { + constructor(rawArgv) { + super(rawArgv); + this.load(path.join(__dirname, 'dal')); + } + + get description() { + return '生成 dal DAO、extensions、structure 代码'; + } +} + +module.exports = DalCommand; diff --git a/lib/cmd/dal/gen.js b/lib/cmd/dal/gen.js new file mode 100644 index 00000000..830c7011 --- /dev/null +++ b/lib/cmd/dal/gen.js @@ -0,0 +1,39 @@ +const path = require('node:path'); +const { ModuleConfigUtil } = require('@eggjs/tegg-common-util'); +const Command = require('../../command'); + +class DalGenCommand extends Command { + constructor(rawArgv) { + super(rawArgv); + this.usage = 'Usage: egg-bin dal gen'; + + this.options = { + baseDir: { + description: 'directory of application, default to `process.cwd()`', + type: 'string', + }, + }; + this.genBin = path.join(__dirname, '../../dal-gen'); + } + + async run(context) { + const { cwd, argv } = context; + const baseDir = argv.baseDir || cwd; + + const options = { + execArgv: context.execArgv, + env: context.env, + }; + + const moduleReferences = ModuleConfigUtil.readModuleReference(baseDir, {}); + console.log('[egg-bin] dal gen get modules %j', moduleReferences); + for (const moduleReference of moduleReferences) { + await this.helper.forkNode(this.genBin, [ + moduleReference.path, + moduleReference.name, + ], options); + } + } +} + +module.exports = DalGenCommand; diff --git a/lib/dal-gen.js b/lib/dal-gen.js new file mode 100644 index 00000000..aad0461a --- /dev/null +++ b/lib/dal-gen.js @@ -0,0 +1,36 @@ +const assert = require('node:assert'); +const { TableModel, TableInfoUtil } = require('@eggjs/dal-decorator'); +const { CodeGenerator } = require('@eggjs/dal-runtime'); +const { LoaderFactory } = require('@eggjs/tegg-loader'); + +const moduleDir = process.argv[2]; +assert(moduleDir, 'miss module dir'); + +const moduleName = process.argv[3]; +assert(moduleName, 'miss module name'); + +(async () => { + try { + console.log('[egg-bin] start dal gen for %s', moduleName); + const generator = new CodeGenerator({ + moduleDir, + moduleName, + }); + const loader = LoaderFactory.createLoader(moduleDir, 'MODULE'); + const clazzList = loader.load(); + for (const clazz of clazzList) { + if (TableInfoUtil.getIsTable(clazz)) { + const tableModel = TableModel.build(clazz); + console.log('[egg-bin] generate code for %s', clazz.name); + await generator.generate(tableModel); + } + } + console.log('[egg-bin] dal generate done'); + process.exit(0); + } catch (e) { + e.message = `[egg-bin] generate dal code ${moduleDir} failed: ` + e.message; + console.error(e); + process.exit(1); + } +})(); + diff --git a/package.json b/package.json index 010acd59..79c8c955 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,12 @@ "test": "^3.0.0", "ts-node": "^10.8.0", "tsconfig-paths": "^4.1.1", - "ypkgfiles": "^1.6.0" + "ypkgfiles": "^1.6.0", + "@eggjs/tegg-common-util": "^3.33.0", + "@eggjs/dal-runtime": "^3.33.0", + "@eggjs/dal-decorator": "^3.33.0", + "@eggjs/tegg-loader": "^3.33.0", + "@eggjs/tegg": "^3.33.0" }, "peerDependencies": { "egg-mock": ">=5.8.3" @@ -56,7 +61,7 @@ "eslint-config-egg": "^12.0.0", "git-contributor": "2", "mm": "^3.2.0", - "typescript": "^4.7.2" + "typescript": "^5.0.4" }, "repository": { "type": "git", diff --git a/test/fixtures/dal/app/modules/dal/Bar.ts b/test/fixtures/dal/app/modules/dal/Bar.ts new file mode 100644 index 00000000..a209784a --- /dev/null +++ b/test/fixtures/dal/app/modules/dal/Bar.ts @@ -0,0 +1,19 @@ +import { Table, Column, ColumnType } from '@eggjs/tegg/dal'; + +@Table({ + comment: 'foo table', +}) +export class Bar { + @Column({ + type: ColumnType.INT, + }, { + primaryKey: true, + }) + id: number; + + @Column({ + type: ColumnType.VARCHAR, + length: 100, + }) + name: string; +} diff --git a/test/fixtures/dal/app/modules/dal/Foo.ts b/test/fixtures/dal/app/modules/dal/Foo.ts new file mode 100644 index 00000000..3fbd6f49 --- /dev/null +++ b/test/fixtures/dal/app/modules/dal/Foo.ts @@ -0,0 +1,23 @@ +import { Table, Index, Column, ColumnType, IndexType } from '@eggjs/tegg/dal'; + +@Table({ + comment: 'foo table', +}) +@Index({ + keys: [ 'name' ], + type: IndexType.UNIQUE, +}) +export class Foo { + @Column({ + type: ColumnType.INT, + }, { + primaryKey: true, + }) + id: number; + + @Column({ + type: ColumnType.VARCHAR, + length: 100, + }) + name: string; +} diff --git a/test/fixtures/dal/app/modules/dal/package.json b/test/fixtures/dal/app/modules/dal/package.json new file mode 100644 index 00000000..1769916d --- /dev/null +++ b/test/fixtures/dal/app/modules/dal/package.json @@ -0,0 +1,6 @@ +{ + "name": "dal", + "eggModule": { + "name": "dal" + } +} diff --git a/test/fixtures/dal/package.json b/test/fixtures/dal/package.json new file mode 100644 index 00000000..e8dae28d --- /dev/null +++ b/test/fixtures/dal/package.json @@ -0,0 +1,10 @@ +{ + "name": "dal", + "egg": { + "typescript": true + }, + "repository": "git@github.com:eggjs/egg-bin.git", + "devDependencies": { + "@eggjs/tsconfig": "^1.3.3" + } +} diff --git a/test/fixtures/dal/tsconfig.json b/test/fixtures/dal/tsconfig.json new file mode 100644 index 00000000..2d5bb407 --- /dev/null +++ b/test/fixtures/dal/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "@eggjs/tsconfig", + "compilerOptions": { + "baseUrl": "." + } +} diff --git a/test/lib/cmd/dal.test.js b/test/lib/cmd/dal.test.js new file mode 100644 index 00000000..1a336065 --- /dev/null +++ b/test/lib/cmd/dal.test.js @@ -0,0 +1,42 @@ +const path = require('node:path'); +const coffee = require('coffee'); +const mm = require('mm'); +const fs = require('node:fs/promises'); +const assert = require('assert'); + +describe('test/lib/cmd/dal.test.js', () => { + const eggBin = require.resolve('../../../bin/egg-bin.js'); + const cwd = path.join(__dirname, '../../fixtures/dal'); + + afterEach(mm.restore); + + describe('egg-bin dal gen', () => { + after(async () => { + await fs.rm(path.join(cwd, 'app/modules/dal/dal'), { + recursive: true, + }); + }); + + it('egg-bin dal gen should work', async () => { + await coffee.fork(eggBin, [ 'dal', 'gen' ], { cwd }) + .debug() + .expect('code', 0) + .end(); + + for (const file of [ + 'app/modules/dal/dal/dao/BarDAO.ts', + 'app/modules/dal/dal/dao/FooDAO.ts', + 'app/modules/dal/dal/dao/base/BaseBarDAO.ts', + 'app/modules/dal/dal/dao/base/BaseFooDAO.ts', + 'app/modules/dal/dal/extension/BarExtension.ts', + 'app/modules/dal/dal/extension/FooExtension.ts', + 'app/modules/dal/dal/structure/Bar.json', + 'app/modules/dal/dal/structure/Bar.sql', + 'app/modules/dal/dal/structure/Foo.json', + 'app/modules/dal/dal/structure/Foo.sql', + ]) { + assert.ok(fs.stat(path.join(cwd, file))); + } + }); + }); +});