Skip to content

Commit

Permalink
feat: ignore ts error in dalgen (#264)
Browse files Browse the repository at this point in the history
<!--
Thank you for your pull request. Please review below requirements.
Bug fixes and new features should include tests and possibly benchmarks.
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md

感谢您贡献代码。请确认下列 checklist 的完成情况。
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
Contributors guide:
https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
-->

##### Checklist
<!-- Remove items that do not apply. For completed items, change [ ] to
[x]. -->

- [ ] `npm test` passes
- [ ] tests and/or benchmarks are included
- [ ] documentation is changed or added
- [ ] commit message follows commit guidelines

##### Affected core subsystem(s)
<!-- Provide affected core subsystem(s). -->


##### Description of change
<!-- Provide a description of the change below this comment. -->
  • Loading branch information
killagu committed Apr 19, 2024
1 parent 9cb89a8 commit b33f309
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/cmd/dal/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/dal-with-ts-error/app/modules/dal/Bar.ts
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions test/fixtures/dal-with-ts-error/app/modules/dal/Foo.ts
Original file line number Diff line number Diff line change
@@ -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;
}
11 changes: 11 additions & 0 deletions test/fixtures/dal-with-ts-error/app/modules/dal/FooService.ts
Original file line number Diff line number Diff line change
@@ -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';
}
}
6 changes: 6 additions & 0 deletions test/fixtures/dal-with-ts-error/app/modules/dal/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dal",
"eggModule": {
"name": "dal"
}
}
10 changes: 10 additions & 0 deletions test/fixtures/dal-with-ts-error/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "dal",
"egg": {
"typescript": true
},
"repository": "[email protected]:eggjs/egg-bin.git",
"devDependencies": {
"@eggjs/tsconfig": "^1.3.3"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/dal-with-ts-error/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"baseUrl": "."
}
}
35 changes: 35 additions & 0 deletions test/lib/cmd/dal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down Expand Up @@ -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));
});
});
});

0 comments on commit b33f309

Please sign in to comment.