lockfile-hasher 1.0.0
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 +21 -0
- package/README.md +19 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Wei Wang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# lockfile-hasher
|
|
2
|
+
|
|
3
|
+
Generates a combined hash of all the lockfiles in a repo. Supports `package-lock.json`, `pnpm-lock.yaml`, and `yarn.lock` files.
|
|
4
|
+
|
|
5
|
+
This can be used as a cache key for CI/CD pipelines in repositories that have multiple lock files.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
npm install lockfile-hasher
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { generateCombinedLockFileHash } from 'lockfile-hasher';
|
|
17
|
+
|
|
18
|
+
const combinedHash = generateCombinedLockfileHash(pathToRepositoryRoot);
|
|
19
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
type LockFileName = 'package-lock.json' | 'pnpm-lock.yaml' | 'yarn.lock';
|
|
2
|
+
/**
|
|
3
|
+
* Generate a combined hash of all lock files in a repo.
|
|
4
|
+
* @param {string} rootPath - Root path of the repository
|
|
5
|
+
* @param {LockFileName} lockFileName - Name of the lock file to search for.
|
|
6
|
+
* Currently supports 'package-lock.json', 'pnpm-lock.yaml', and 'yarn.lock'
|
|
7
|
+
* @returns {Promise<string>} - Combined hash string
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateCombinedLockFileHash(rootPath: string, lockFileName: LockFileName): Promise<string>;
|
|
10
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// hash-package-locks.js
|
|
2
|
+
import crypto from 'node:crypto';
|
|
3
|
+
import { readFile } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { glob } from 'glob';
|
|
6
|
+
/**
|
|
7
|
+
* Generate a combined hash of all lock files in a repo.
|
|
8
|
+
* @param {string} rootPath - Root path of the repository
|
|
9
|
+
* @param {LockFileName} lockFileName - Name of the lock file to search for.
|
|
10
|
+
* Currently supports 'package-lock.json', 'pnpm-lock.yaml', and 'yarn.lock'
|
|
11
|
+
* @returns {Promise<string>} - Combined hash string
|
|
12
|
+
*/
|
|
13
|
+
export async function generateCombinedLockFileHash(rootPath, lockFileName) {
|
|
14
|
+
const pattern = path.join(rootPath, `**/${lockFileName}`);
|
|
15
|
+
const files = await glob(pattern, { ignore: '**/node_modules/**' });
|
|
16
|
+
const fileContents = await Promise.all(files.map((file) => readFile(file)));
|
|
17
|
+
// Generate hash for each file
|
|
18
|
+
const hashesMap = {};
|
|
19
|
+
files.forEach((file, i) => {
|
|
20
|
+
const relativePath = path.relative(rootPath, file);
|
|
21
|
+
const fileContent = fileContents[i];
|
|
22
|
+
const hash = crypto.createHash('sha256').update(fileContent).digest('hex');
|
|
23
|
+
hashesMap[relativePath] = hash;
|
|
24
|
+
});
|
|
25
|
+
// Sort the entries to ensure consistent hash
|
|
26
|
+
const sortedEntries = Object.entries(hashesMap).sort(([pathA], [pathB]) => {
|
|
27
|
+
return pathA.localeCompare(pathB);
|
|
28
|
+
});
|
|
29
|
+
// Create a string representation of all hashes
|
|
30
|
+
const combinedString = sortedEntries.map(([relPath, hash]) => `${relPath}:${hash}`).join('|');
|
|
31
|
+
// Generate a new hash from the combined string
|
|
32
|
+
return crypto.createHash('sha256').update(combinedString).digest('hex');
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wBAAwB;AACxB,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAI5B;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,4BAA4B,CAChD,QAAgB,EAChB,YAA0B;IAE1B,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,MAAM,YAAY,EAAE,CAAC,CAAC;IAC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AM,CAAC,KAAK,CAAC,CAAC;AAC1E,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lockfile-hasher",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Generates a combined hash of all the lockfiles in a repository",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"packageManager": "pnpm@10.6.2",
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "rm -rf dist && tsc",
|
|
14
|
+
"check-format": "biome check .",
|
|
15
|
+
"ci": "pnpm run test && pnpm run check-format",
|
|
16
|
+
"format": "biome check --write .",
|
|
17
|
+
"prepublishOnly": "pnpm run ci",
|
|
18
|
+
"test": "node --experimental-strip-types --test"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/onlywei/lockfile-hasher.git"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"gitlab"
|
|
26
|
+
],
|
|
27
|
+
"author": "Wei Wang <wei@fastmail.com>",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/onlywei/lockfile-hasher/issues"
|
|
31
|
+
},
|
|
32
|
+
"homepage": "https://github.com/onlywei/lockfile-hasher#readme",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@biomejs/biome": "^1.9.4",
|
|
35
|
+
"@changesets/cli": "^2.28.0",
|
|
36
|
+
"@types/node": "^22.13.4",
|
|
37
|
+
"typescript": "^5.8.2"
|
|
38
|
+
},
|
|
39
|
+
"pnpm": {
|
|
40
|
+
"onlyBuiltDependencies": [
|
|
41
|
+
"@biomejs/biome"
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"glob": "^11.0.1"
|
|
46
|
+
}
|
|
47
|
+
}
|