Skip to content

Commit

Permalink
fix: not overwrite extension file (#218)
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. -->

<!--
- any feature?
- close https://github.com/eggjs/egg/ISSUE_URL
-->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Enhanced code generation logic to dynamically determine paths,
improving flexibility in project structure.
- Introduced a new custom SQL query for `FooExtension` to perform
specific database operations.
- Added a new TypeScript class `Foo` representing a database table with
comprehensive data type support and indexing.

- **Bug Fixes**
- Updated code generation settings to prevent overwriting existing
codes, ensuring data integrity.

- **Tests**
- Added test cases to validate the new code generation logic and
directory-specific operations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Apr 28, 2024
1 parent 68c1a9c commit f915f08
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ jobs:
run: brew install [email protected]

- name: Start Mysql
run: /usr/local/opt/[email protected]/bin/mysql.server start
## arm64/x86 homebrew mysql path different
run: /usr/local/opt/[email protected]/bin/mysql.server start || /opt/homebrew/Cellar/[email protected]/5.7.44_1/bin/mysql.server start

- name: Install Npm
run: npm i -g npm@9
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ plugin/**/dist
plugin/oneapi/test/fixtures/modules/*/oneapi
plugin/dal/test/fixtures/modules/*/dal/
core/dal-runtime/test/fixtures/modules/*/dal
core/dal-runtime/test/fixtures/modules/*/src/dal/
!core/dal-runtime/test/fixtures/modules/generate_codes_not_overwrite_dao/dal/extension/FooExtension.ts
!core/dal-runtime/test/fixtures/modules/generate_codes_not_overwrite_dao/dal/dao/FooDAO.ts

plugin/tegg/test/fixtures/apps/**/*.js
Expand Down
10 changes: 8 additions & 2 deletions core/dal-runtime/src/CodeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export class CodeGenerator {
}

async generate(tableModel: TableModel) {
const dalDir = path.join(this.moduleDir, 'dal');
let dalDir: string;
try {
await fs.access(path.join(this.moduleDir, 'src'));
dalDir = path.join(this.moduleDir, 'src/dal');
} catch {
dalDir = path.join(this.moduleDir, 'dal');
}

// const tableName = tableModel.name;
// const clazzName = tableModel.clazz.name;
Expand Down Expand Up @@ -104,7 +110,7 @@ export class CodeGenerator {
templates: Templates.EXTENSION,
filePath: paths.extension,
beautify: false,
overwrite: true,
overwrite: false,
}];
for (const { templates, filePath, beautify, overwrite } of codes) {
await fs.mkdir(path.dirname(filePath), {
Expand Down
16 changes: 16 additions & 0 deletions core/dal-runtime/test/CodeGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,21 @@ describe('test/CodeGenerator.test.ts', () => {
await generator.generate(multiPrimaryKeyTableModel);
const daoFile = await fs.readFile(path.join(__dirname, './fixtures/modules/generate_codes_not_overwrite_dao/dal/dao/FooDAO.ts'), 'utf8');
assert(/customFind/.test(daoFile));

const extensionFile = await fs.readFile(path.join(__dirname, './fixtures/modules/generate_codes_not_overwrite_dao/dal/extension/FooExtension.ts'), 'utf8');
assert(/customFind/.test(extensionFile));
});

it('should generate to src first', async () => {
const generator = new CodeGenerator({
moduleDir: path.join(__dirname, './fixtures/modules/generate_codes_to_src'),
moduleName: 'dal',
dalPkg: '@eggjs/dal-decorator',
});
const fooModel = TableModel.build(Foo);
await generator.generate(fooModel);

const daoFile = await fs.readFile(path.join(__dirname, './fixtures/modules/generate_codes_to_src/src/dal/dao/FooDAO.ts'), 'utf8');
assert(daoFile);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { SqlMap } from '@eggjs/dal-decorator';

/**
* Define Custom SQLs
*
* import { SqlMap, SqlType } from '@eggjs/dal-decorator';
*
* export default {
* findByName: {
* type: SqlType.SELECT,
* sql: 'SELECT from foo where name = '
* },
* }
*/
export default {
customFind: {
type: 'SELECT',
sql: `SELECT now()`,
},
} as Record<string, SqlMap>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dal",
"eggModule": {
"name": "dal"
}
}
Loading

0 comments on commit f915f08

Please sign in to comment.