From b33f3099c705429e4810af228bedef3507d01e77 Mon Sep 17 00:00:00 2001 From: killa Date: Fri, 19 Apr 2024 09:17:06 +0800 Subject: [PATCH] feat: ignore ts error in dalgen (#264) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ##### Checklist - [ ] `npm test` passes - [ ] tests and/or benchmarks are included - [ ] documentation is changed or added - [ ] commit message follows commit guidelines ##### Affected core subsystem(s) ##### Description of change --- lib/cmd/dal/gen.js | 6 +++- .../dal-with-ts-error/app/modules/dal/Bar.ts | 19 ++++++++++ .../dal-with-ts-error/app/modules/dal/Foo.ts | 23 ++++++++++++ .../app/modules/dal/FooService.ts | 11 ++++++ .../app/modules/dal/package.json | 6 ++++ test/fixtures/dal-with-ts-error/package.json | 10 ++++++ test/fixtures/dal-with-ts-error/tsconfig.json | 6 ++++ test/lib/cmd/dal.test.js | 35 +++++++++++++++++++ 8 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/dal-with-ts-error/app/modules/dal/Bar.ts create mode 100644 test/fixtures/dal-with-ts-error/app/modules/dal/Foo.ts create mode 100644 test/fixtures/dal-with-ts-error/app/modules/dal/FooService.ts create mode 100644 test/fixtures/dal-with-ts-error/app/modules/dal/package.json create mode 100644 test/fixtures/dal-with-ts-error/package.json create mode 100644 test/fixtures/dal-with-ts-error/tsconfig.json diff --git a/lib/cmd/dal/gen.js b/lib/cmd/dal/gen.js index 48dbbe2..2b54ed6 100644 --- a/lib/cmd/dal/gen.js +++ b/lib/cmd/dal/gen.js @@ -41,7 +41,11 @@ class DalGenCommand extends Command { const options = { execArgv: context.execArgv, - env: context.env, + env: { + ...context.env, + // Dal table class modified may cause ts error, so we use transpile only + TS_NODE_TRANSPILE_ONLY: 'true', + }, cwd: baseDir, }; diff --git a/test/fixtures/dal-with-ts-error/app/modules/dal/Bar.ts b/test/fixtures/dal-with-ts-error/app/modules/dal/Bar.ts new file mode 100644 index 0000000..a209784 --- /dev/null +++ b/test/fixtures/dal-with-ts-error/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-with-ts-error/app/modules/dal/Foo.ts b/test/fixtures/dal-with-ts-error/app/modules/dal/Foo.ts new file mode 100644 index 0000000..b691360 --- /dev/null +++ b/test/fixtures/dal-with-ts-error/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.INT, + length: 100, + }) + name: number; +} diff --git a/test/fixtures/dal-with-ts-error/app/modules/dal/FooService.ts b/test/fixtures/dal-with-ts-error/app/modules/dal/FooService.ts new file mode 100644 index 0000000..3a7af45 --- /dev/null +++ b/test/fixtures/dal-with-ts-error/app/modules/dal/FooService.ts @@ -0,0 +1,11 @@ +import { SingletonProto } from '@eggjs/tegg'; +import { Foo } from './Foo'; + +@SingletonProto() +export class FooService { + foo() { + const foo = new Foo(); + foo.id = '233'; + foo.name = '233'; + } +} diff --git a/test/fixtures/dal-with-ts-error/app/modules/dal/package.json b/test/fixtures/dal-with-ts-error/app/modules/dal/package.json new file mode 100644 index 0000000..1769916 --- /dev/null +++ b/test/fixtures/dal-with-ts-error/app/modules/dal/package.json @@ -0,0 +1,6 @@ +{ + "name": "dal", + "eggModule": { + "name": "dal" + } +} diff --git a/test/fixtures/dal-with-ts-error/package.json b/test/fixtures/dal-with-ts-error/package.json new file mode 100644 index 0000000..e8dae28 --- /dev/null +++ b/test/fixtures/dal-with-ts-error/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-with-ts-error/tsconfig.json b/test/fixtures/dal-with-ts-error/tsconfig.json new file mode 100644 index 0000000..2d5bb40 --- /dev/null +++ b/test/fixtures/dal-with-ts-error/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 index 1dc734a..22806b9 100644 --- a/test/lib/cmd/dal.test.js +++ b/test/lib/cmd/dal.test.js @@ -7,12 +7,18 @@ 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'); + const cwd2 = path.join(__dirname, '../../fixtures/dal-with-ts-error'); afterEach(mm.restore); describe('egg-bin dal gen', () => { after(async () => { await fs.rm(path.join(cwd, 'app/modules/dal/dal'), { + force: true, + recursive: true, + }); + await fs.rm(path.join(cwd2, 'app/modules/dal/dal'), { + force: true, recursive: true, }); }); @@ -42,5 +48,34 @@ describe('test/lib/cmd/dal.test.js', () => { assert(/import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs\/xianyadan\/dal';/.test(content)); assert(/import { SingletonProto, AccessLevel, Inject } from '@eggjs\/xianyadan';/.test(content)); }); + + it('egg-bin dal gen with ts error should work', async () => { + const cwd = path.join(__dirname, '../../fixtures/dal-with-ts-error'); + await coffee.fork(eggBin, [ 'dal', 'gen', '--teggPkgName', '@eggjs/xianyadan', '--teggDalPkgName', '@eggjs/xianyadan/dal' ], { + cwd: cwd2, + }) + .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(await fs.stat(path.join(cwd, file))); + } + + const content = await fs.readFile(path.join(cwd, 'app/modules/dal/dal/dao/base/BaseFooDAO.ts'), 'utf8'); + assert(/import type { InsertResult, UpdateResult, DeleteResult } from '@eggjs\/xianyadan\/dal';/.test(content)); + assert(/import { SingletonProto, AccessLevel, Inject } from '@eggjs\/xianyadan';/.test(content)); + }); }); });