depatcher 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present node-ecosystem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # depatcher
2
+
3
+ Dependencies patcher ~ programmatically apply `.patch` files to your dependencies.
4
+ Under the hood, it uses [diff](https://www.npmjs.com/package/diff).
5
+
6
+ ## ⚙️ Installation
7
+
8
+ Install as development dependency:
9
+
10
+ | Package Manager | Command
11
+ | - | -
12
+ | **npm** | `npm install -D depatcher`
13
+ | **yarn** | `yarn add -D depatcher`
14
+ | **pnpm** | `pnpm add -D depatcher`
15
+
16
+ ## 📖 Usage
17
+
18
+ ### `applyPatch`
19
+
20
+ Apply `.patch` files to specific files within a target dependency.
21
+
22
+ ```ts
23
+ import { applyPatch } from 'depatcher'
24
+
25
+ // applyPatch(packageName, patchMap)
26
+ await applyPatch('packageName', {
27
+ // 'target_file_in_dependency': 'path_to_patch_file'
28
+ '/dist/index.js': './packageName_index.patch'
29
+ })
30
+ ```
31
+
32
+ ### `createPatch`
33
+
34
+ Create a `.patch` file using the diff of an original file and a patched file.
35
+
36
+ ```ts
37
+ import { createPatch } from 'depatcher'
38
+
39
+ createPatch(
40
+ './original_file.js',
41
+ './patched_file.js',
42
+ './file.patch'
43
+ )
44
+ ```
45
+
46
+ ## 🔄 TODO
47
+
48
+ - `npm` patch support
49
+ - `pnpm` patch support
50
+
51
+ ## 🛠️ Contributing
52
+
53
+ Got ideas or want to add a patch string?
54
+
55
+ 1. Clone the repository.
56
+ 2. Install dependencies with `yarn install`.
57
+ 3. Build the project using `yarn build` (uses `tsdown`).
58
+ 4. Run tests with `yarn test`.
59
+
60
+ ## 📜 License
61
+
62
+ This project is licensed under the [MIT License](LICENSE).
package/dist/index.mjs ADDED
@@ -0,0 +1,36 @@
1
+ import { execSync } from "node:child_process";
2
+ import { readFileSync, writeFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { applyPatch, createPatch } from "diff";
5
+ //#region src/applyPatch.ts
6
+ function run(command, options = {}) {
7
+ console.log(`> ${command}`);
8
+ return execSync(command, {
9
+ encoding: "utf8",
10
+ stdio: "pipe",
11
+ ...options
12
+ });
13
+ }
14
+ async function applyPatch_(packageName, patchMap) {
15
+ console.log(`🔄 Start patching "${packageName}"`);
16
+ const tempDir = run(`yarn patch ${packageName}`).split("\n")[1].slice(49);
17
+ for (const [originalFile, patchPath] of Object.entries(patchMap)) {
18
+ const fileToPatch = join(tempDir, originalFile);
19
+ const patchedFile = applyPatch(readFileSync(fileToPatch, "utf8").toString(), readFileSync(patchPath, "utf8").toString());
20
+ if (!patchedFile) throw new Error("❌ Patch failed. No changes were made");
21
+ writeFileSync(fileToPatch, patchedFile, "utf8");
22
+ console.log(`💾 Patched ${fileToPatch} with ${patchPath}`);
23
+ }
24
+ run(`yarn patch-commit -s ${tempDir}`);
25
+ console.log(`📦 "${packageName}" patched`);
26
+ }
27
+ //#endregion
28
+ //#region src/createPatch.ts
29
+ function createPatch_(originalFilePath, patchedFilePath, outputPatchPath) {
30
+ let patchString = createPatch("", readFileSync(originalFilePath, "utf8"), readFileSync(patchedFilePath, "utf8"), "", "", { context: 3 });
31
+ patchString = patchString.slice(patchString.indexOf("@@"));
32
+ writeFileSync(outputPatchPath, patchString, "utf8");
33
+ console.log(`💾 Patched ${outputPatchPath}`);
34
+ }
35
+ //#endregion
36
+ export { applyPatch_ as applyPatch, createPatch_ as createPatch };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "depatcher",
3
+ "version": "0.0.1",
4
+ "description": "Dependencies patcher ~ apply patches to dependencies",
5
+ "type": "module",
6
+ "keywords": [
7
+ "dependency",
8
+ "dependencies",
9
+ "patch",
10
+ "patches"
11
+ ],
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/node-ecosystem/depatcher.git"
15
+ },
16
+ "bugs": {
17
+ "url": "https://github.com/node-ecosystem/depatcher/issues"
18
+ },
19
+ "scripts": {
20
+ "build": "tsdown",
21
+ "lint": "eslint src/**/*.ts",
22
+ "od": "yarn outdated"
23
+ },
24
+ "dependencies": {
25
+ "diff": "^9.0.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^25.6.0",
29
+ "@yarnpkg/sdks": "^3.2.3",
30
+ "eslint": "^10.2.1",
31
+ "eslint-plugin-unicorn": "^64.0.0",
32
+ "tsdown": "^0.21.10",
33
+ "typescript": "^6.0.3",
34
+ "typescript-eslint": "^8.59.1"
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "packageManager": "yarn@4.14.1"
40
+ }