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: auto copy egg meta properties #3

Merged
merged 1 commit into from
Dec 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
3 changes: 1 addition & 2 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

Expand All @@ -13,4 +12,4 @@ jobs:
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, windows-latest'
version: '16, 18, 20, 22'
version: '16, 18, 20, 22, 23'
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# tshy-after

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/node-modules/tshy-after/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/tshy-after/actions/workflows/nodejs.yml)
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/tshy-after.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/tshy-after.svg?style=flat-square
[npm-url]: https://npmjs.org/package/tshy-after
[download-image]: https://img.shields.io/npm/dm/tshy-after.svg?style=flat-square
[download-url]: https://npmjs.org/package/tshy-after

Auto set package.json after [tshy](https://github.com/isaacs/tshy) run

## keep `types`
Expand Down Expand Up @@ -28,3 +38,17 @@ export function getDirname() {
return path.dirname(fileURLToPath(import.meta.url));
}
```

## Auto copy egg meta properties package.json

Copy `eggPlugin`, `egg` to `dist/package.json`

## License

[MIT](LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=node-modules/tshy-after)](https://github.com/node-modules/tshy-after/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"homepage": "https://github.com/node-modules/tshy-after#readme",
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^20.6.2",
"@types/node": "^22.10.2",
"coffee": "^5.5.0",
"egg-bin": "^6.5.2",
"tshy": "^2.0.0"
"tshy": "^3.0.2"
},
"type": "module",
"tshy": {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ writeFileSync('package.json', JSON.stringify(pkg, null, 2) + '\n')
writeFileSync('dist/package.json', JSON.stringify({
name: pkg.name,
version: pkg.version,
eggPlugin: pkg.eggPlugin,
egg: pkg.egg,
Comment on lines +16 to +17
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation for egg meta properties

The code directly copies eggPlugin and egg properties without checking if they exist in the source package.json. This could lead to undefined properties being copied to dist/package.json.

Consider adding validation:

writeFileSync('dist/package.json', JSON.stringify({
  name: pkg.name,
  version: pkg.version,
-  eggPlugin: pkg.eggPlugin,
-  egg: pkg.egg,
+  ...(pkg.eggPlugin && { eggPlugin: pkg.eggPlugin }),
+  ...(pkg.egg && { egg: pkg.egg }),
}, null, 2) + '\n')
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
eggPlugin: pkg.eggPlugin,
egg: pkg.egg,
...(pkg.eggPlugin && { eggPlugin: pkg.eggPlugin }),
...(pkg.egg && { egg: pkg.egg }),

}, null, 2) + '\n')

// Replace all `( import.meta.url )` into `('import_meta_url_placeholder_by_tshy_after')` on commonjs.
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/demo/package-init.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
"url": "https://github.com/node-modules/tshy-after/issues"
},
"homepage": "https://github.com/node-modules/tshy-after#readme",
"eggPlugin": {
"name": "egg-mock",
"exports": {
"import": "./dist/esm",
"require": "./dist/commonjs"
}
},
"egg": {
"framework": true
},
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^20.6.2",
Expand Down
10 changes: 10 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,15 @@ describe('test/index.test.ts', () => {
const distPkg = JSON.parse(fs.readFileSync(distPackageFile, 'utf-8'));
assert.equal(distPkg.name, 'tshy-after');
assert.equal(distPkg.version, '1.0.0');
assert.deepEqual(distPkg.eggPlugin, {
"name": "egg-mock",
"exports": {
"import": "./dist/esm",
"require": "./dist/commonjs"
},
});
assert.deepEqual(distPkg.egg, {
framework: true,
});
});
});
Loading