Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: impl egg-bin dal gen #257

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ package-lock.json
yarn.lock
.c8_output
.idea
.eslintcache
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Comment on lines +337 to +345
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new section on the dal command is clear and concise, providing straightforward instructions on how to use the egg-bin dal gen command and where to find further documentation. However, to maintain consistency with the rest of the document, consider starting the description with an uppercase letter and adding a comma after "dal document please read" for better readability.

- dal document please read [tegg doc](https://github.com/eggjs/tegg/tree/master/plugin/dal).
+ Dal document, please read [tegg doc](https://github.com/eggjs/tegg/tree/master/plugin/dal).

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
### 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).
### dal
Generate code for @eggjs/tegg-dal-plugin
```bash
egg-bin dal gen

Dal document, please read tegg doc.


</details>
<!-- suggestion_end -->

<!-- This is an auto-generated comment by CodeRabbit -->


## License

[MIT](LICENSE)
Expand Down
17 changes: 17 additions & 0 deletions lib/cmd/dal.js
Original file line number Diff line number Diff line change
@@ -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;
39 changes: 39 additions & 0 deletions lib/cmd/dal/gen.js
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 36 additions & 0 deletions lib/dal-gen.js
Original file line number Diff line number Diff line change
@@ -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);
}
})();

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/dal/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/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.VARCHAR,
length: 100,
})
name: string;
}
6 changes: 6 additions & 0 deletions test/fixtures/dal/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/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "dal",
"egg": {
"typescript": true
},
"repository": "git@github.com:eggjs/egg-bin.git",
"devDependencies": {
"@eggjs/tsconfig": "^1.3.3"
}
}
6 changes: 6 additions & 0 deletions test/fixtures/dal/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"baseUrl": "."
}
}
42 changes: 42 additions & 0 deletions test/lib/cmd/dal.test.js
Original file line number Diff line number Diff line change
@@ -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)));
}
});
});
});
Comment on lines +7 to +42
Copy link

@coderabbitai coderabbitai bot Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test suite for egg-bin dal gen is well-implemented. However, ensure that the fs.stat promise is properly awaited or handled to accurately test for the existence of generated files.

- assert.ok(fs.stat(path.join(cwd, file)));
+ assert.ok(await fs.stat(path.join(cwd, file)));

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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)));
}
});
});
});
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(await fs.stat(path.join(cwd, file)));
}
});
});
});

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

厉害了。。。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fengmk2, 谢谢您的认可!如果您有任何问题或需要进一步的帮助,请随时告诉我。 😊

(\__/) 
(•ㅅ•) CodeRabbit is happy!
/   づ

Loading