generated from krauters/typescript-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3d0f7d1
Showing
17 changed files
with
8,116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# This auto assigns the following teams to pull requests | ||
* @krauters/reviewers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Node Publish | ||
run-name: Node Publish [${{ github.ref_name }}] triggered by [${{ github.event_name }}/${{ github.actor }}] | ||
|
||
on: | ||
release: | ||
types: published | ||
push: | ||
branches: '*' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
publish: | ||
uses: krauters/shared-workflows/.github/workflows/node-publish.yaml@main | ||
secrets: | ||
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }} | ||
with: | ||
node_install: true | ||
dry_run: ${{ github.event_name != 'release' }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: Node Release | ||
run-name: Node Release [${{ github.ref_name }}] triggered by [${{ github.event_name }}/${{ github.actor }}] | ||
|
||
on: | ||
push: | ||
branches: main | ||
workflow_dispatch: | ||
|
||
jobs: | ||
publish: | ||
uses: krauters/shared-workflows/.github/workflows/node-release.yaml@main | ||
secrets: | ||
GH_TOKEN_RELEASES: ${{ secrets.GH_TOKEN_RELEASES }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Node.js dependencies | ||
node_modules/ | ||
|
||
# Output directories | ||
dist/ | ||
|
||
# IDE and editor files | ||
.idea/ | ||
.vscode/ | ||
|
||
# System files | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# Coverage directories | ||
coverage/ | ||
|
||
# Environment variable files | ||
dev.env | ||
.env | ||
.env.local | ||
.env.*.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
|
||
MAIN_DIR=./node_modules/@krauters/utils/scripts/pre-commit | ||
. $MAIN_DIR/index.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
registry=https://registry.npmjs.org/ | ||
//registry.npmjs.org/:_authToken=${NPM_TOKEN} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
ISC License | ||
|
||
Copyright (c) 2024 Krauters | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Monkey Patcher | ||
|
||
**MonkeyPatcher** is a powerful TypeScript utility designed to help developers extend class prototypes by adding new methods under a specified namespace. This approach enhances the flexibility and extensibility of codebases without altering the original class implementations. By encapsulating additional functionalities within a clear namespace, **MonkeyPatcher** promotes organized and maintainable code patching. | ||
|
||
While monkey patching can be incredibly useful, it's crucial to avoid falling into common anti-patterns. Always ensure that the added functionalities are well-organized and do not interfere with existing methods, and please group them into reasonable namespaces to make their usage more intuitive. | ||
|
||
```zsh | ||
npm install @krauters/monkey-patcher | ||
``` | ||
|
||
## Usage | ||
|
||
To use **MonkeyPatcher**, first define the new methods you want to add to a target class and extend the class's interface with a unique namespace. Then, create a `MonkeyPatcher` instance with the class prototype and namespace, apply the patch, and access the new methods through the designated namespace on class instances. | ||
|
||
```ts | ||
// Usage Example: Patching String with MonkeyPatcher | ||
|
||
import { MonkeyPatcher } from 'monkey-patcher' | ||
|
||
// 1. Define new methods to add to String | ||
const stringMethods = { | ||
reverse(this: String): string { | ||
return this.split('').reverse().join('') | ||
}, | ||
capitalize(this: String): string { | ||
if (this.length === 0) return '' | ||
return this.charAt(0).toUpperCase() + this.slice(1) | ||
}, | ||
} | ||
|
||
// 2. Extend the String interface to include the new namespace | ||
declare global { | ||
interface String { | ||
utils: { | ||
reverse(): string | ||
capitalize(): string | ||
} | ||
} | ||
} | ||
|
||
// 3. Create a MonkeyPatcher instance with the desired namespace | ||
const stringPatcher = new MonkeyPatcher(String.prototype, 'utils') | ||
|
||
// 4. Patch the String class with the new methods | ||
stringPatcher.patch(stringMethods) | ||
|
||
// 5. Use the patched methods | ||
const str = new String('hello world') | ||
|
||
// Access existing method | ||
console.log(str.toUpperCase()) // Output: HELLO WORLD | ||
|
||
// Use patched methods under the 'utils' namespace | ||
console.log(str.utils.reverse()) // Output: dlrow olleh | ||
console.log(str.utils.capitalize()) // Output: Hello world | ||
``` | ||
|
||
## Husky | ||
|
||
Husky helps manage Git hooks easily, automating things like running tests or linting before a commit is made. This ensures your code is in good shape. | ||
|
||
Pre-commit hooks run scripts before a commit is finalized to catch issues or enforce standards. With Husky, setting up these hooks across your team becomes easy, keeping your codebase clean and consistent. | ||
|
||
### Our Custom Pre-Commit Hook | ||
|
||
This project uses a custom pre-commit hook to run `npm run bundle`. This ensures that our bundled assets are always up to date before any commit (which is especially important for TypeScript GitHub Actions). Husky automates this, so no commits will go through without a fresh bundle, keeping everything streamlined. | ||
|
||
## Contributing | ||
|
||
The goal of this project is to continually evolve and improve its core features, making it more efficient and easier to use. Development happens openly here on GitHub, and we’re thankful to the community for contributing bug fixes, enhancements, and fresh ideas. Whether you're fixing a small bug or suggesting a major improvement, your input is invaluable. | ||
|
||
## License | ||
|
||
This project is licensed under the ISC License. Please see the [LICENSE](./LICENSE) file for more details. | ||
|
||
## 🥂 Thanks Contributors | ||
|
||
Thanks for spending time on this project. | ||
|
||
<a href="https://github.com/krauters/monkey-patcher/graphs/contributors"> | ||
<img src="https://contrib.rocks/image?repo=krauters/monkey-patcher" /> | ||
</a> | ||
|
||
<br /> | ||
<br /> | ||
<a href="https://www.buymeacoffee.com/coltenkrauter"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=coltenkrauter&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff" /></a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* eslint-disable filenames/match-exported */ | ||
const eslintConfig = require('@krauters/eslint-config') | ||
|
||
module.exports = eslintConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
module.exports = { | ||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
coverageThreshold: { | ||
global: { | ||
branches: 60, | ||
functions: 60, | ||
lines: 60, | ||
statements: 60, | ||
}, | ||
}, | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
testMatch: ['**/test/**/*.test.{ts,tsx}'], | ||
transform: { | ||
'^.+\\.tsx?$': ['ts-jest', { tsconfig: 'tsconfig.jest.json' }], | ||
}, | ||
} |
Oops, something went wrong.