@vltpkg/package-json 0.0.0-0.1730239248325

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,15 @@
1
+ Copyright (c) vlt technology, Inc.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+ Subject to the terms and conditions of this license, each copyright holder and contributor hereby grants to those receiving rights under this license a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except for failure to satisfy the conditions of this license) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer this software, where such license applies only to those patent claims, already acquired or hereafter acquired, licensable by such copyright holder or contributor that are necessarily infringed by:
8
+
9
+ (a) their Contribution(s) (the licensed copyrights of copyright holders and non-copyrightable additions of contributors, in source or binary form) alone; or
10
+ (b) combination of their Contribution(s) with the work of authorship to which such Contribution(s) was added by such copyright holder or contributor, if, at the time the Contribution is added, such addition causes such combination to be necessarily infringed. The patent license shall not apply to any other combinations which include the Contribution.
11
+ Except as expressly stated above, no rights or licenses from any copyright holder or contributor is granted under this license, whether expressly, by implication, estoppel or otherwise.
12
+
13
+ DISCLAIMER
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # `@vltpkg/package-json`
2
+
3
+ Manages reading `package.json` files.
4
+
5
+ Caches previously seen files and augments error messages for clarity.
6
+
7
+ ## USAGE
8
+
9
+ ```js
10
+ import { PackageJson } from '@vltpkg/package-json'
11
+
12
+ const packageJson = new PackageJson()
13
+ const dir = process.cwd()
14
+ const pkg = await packageJson.read(dir)
15
+ ```
@@ -0,0 +1,14 @@
1
+ import { Manifest } from '@vltpkg/types';
2
+ export declare class PackageJson {
3
+ #private;
4
+ /**
5
+ * Reads and parses contents of a `package.json` file at a directory `dir`.
6
+ * `reload` will optionally skip reading from the cache when set to `true`.
7
+ */
8
+ read(dir: string, { reload }?: {
9
+ reload?: boolean;
10
+ }): Manifest;
11
+ write(dir: string, manifest: Manifest): void;
12
+ save(manifest: Manifest): void;
13
+ }
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAc,QAAQ,EAAE,MAAM,eAAe,CAAA;AAKpD,qBAAa,WAAW;;IAgBtB;;;OAGG;IACH,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,GAAE;QAAE,MAAM,CAAC,EAAE,OAAO,CAAA;KAAO,GAAG,QAAQ;IAiClE,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI;IAyB5C,IAAI,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;CAa/B"}
@@ -0,0 +1,79 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { asManifest } from '@vltpkg/types';
3
+ import { readFileSync, writeFileSync } from 'node:fs';
4
+ import { resolve } from 'node:path';
5
+ import { parse, stringify } from 'polite-json';
6
+ export class PackageJson {
7
+ /**
8
+ * cache of `package.json` loads
9
+ */
10
+ #cache = new Map();
11
+ /**
12
+ * cache of `package.json` paths by manifest
13
+ */
14
+ #pathCache = new Map();
15
+ /**
16
+ * cache of load errors
17
+ */
18
+ #errCache = new Map();
19
+ /**
20
+ * Reads and parses contents of a `package.json` file at a directory `dir`.
21
+ * `reload` will optionally skip reading from the cache when set to `true`.
22
+ */
23
+ read(dir, { reload } = {}) {
24
+ const cachedPackageJson = !reload && this.#cache.get(dir);
25
+ if (cachedPackageJson) {
26
+ return cachedPackageJson;
27
+ }
28
+ const filename = resolve(dir, 'package.json');
29
+ const fail = (err) => error('Could not read package.json file', err, this.read);
30
+ const cachedError = !reload && this.#errCache.get(dir);
31
+ if (cachedError) {
32
+ throw fail(cachedError);
33
+ }
34
+ try {
35
+ const res = asManifest(parse(readFileSync(filename, { encoding: 'utf8' })));
36
+ this.#cache.set(dir, res);
37
+ this.#pathCache.set(res, dir);
38
+ return res;
39
+ }
40
+ catch (err) {
41
+ const ec = {
42
+ path: filename,
43
+ cause: err,
44
+ };
45
+ this.#errCache.set(dir, ec);
46
+ throw fail(ec);
47
+ }
48
+ }
49
+ write(dir, manifest) {
50
+ const filename = resolve(dir, 'package.json');
51
+ try {
52
+ // This assumes kIndent and kNewline are already present on the manifest because we would
53
+ // only write a package.json after reading it which will set those properties.
54
+ writeFileSync(filename, stringify(manifest));
55
+ this.#cache.set(dir, manifest);
56
+ this.#pathCache.set(manifest, dir);
57
+ }
58
+ catch (err) {
59
+ // If there was an error writing to this package.json then also delete it from our cache
60
+ // just in case a future read would get stale data.
61
+ this.#cache.delete(dir);
62
+ this.#pathCache.delete(manifest);
63
+ throw error('Could not write package.json file', {
64
+ path: filename,
65
+ cause: err,
66
+ }, this.write);
67
+ }
68
+ }
69
+ save(manifest) {
70
+ const dir = this.#pathCache.get(manifest);
71
+ if (!dir) {
72
+ throw error('Could not save manifest', {
73
+ manifest,
74
+ }, this.save);
75
+ }
76
+ this.write(dir, manifest);
77
+ }
78
+ }
79
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAoB,MAAM,qBAAqB,CAAA;AAC7D,OAAO,EAAE,UAAU,EAAY,MAAM,eAAe,CAAA;AACpD,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AACnC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAE9C,MAAM,OAAO,WAAW;IACtB;;OAEG;IACH,MAAM,GAAG,IAAI,GAAG,EAAoB,CAAA;IAEpC;;OAEG;IACH,UAAU,GAAG,IAAI,GAAG,EAAoB,CAAA;IAExC;;OAEG;IACH,SAAS,GAAG,IAAI,GAAG,EAA4B,CAAA;IAE/C;;;OAGG;IACH,IAAI,CAAC,GAAW,EAAE,EAAE,MAAM,KAA2B,EAAE;QACrD,MAAM,iBAAiB,GAAG,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QACzD,IAAI,iBAAiB,EAAE,CAAC;YACtB,OAAO,iBAAiB,CAAA;QAC1B,CAAC;QAED,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,EAAE,cAAc,CAAC,CAAA;QAE7C,MAAM,IAAI,GAAG,CAAC,GAAqB,EAAE,EAAE,CACrC,KAAK,CAAC,kCAAkC,EAAE,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAA;QAE3D,MAAM,WAAW,GAAG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CAAA;YACvB,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;YAChC,MAAM,KAAK,CACT,mCAAmC,EACnC;gBACE,IAAI,EAAE,QAAQ;gBACd,KAAK,EAAE,GAAY;aACpB,EACD,IAAI,CAAC,KAAK,CACX,CAAA;QACH,CAAC;IACH,CAAC;IAED,IAAI,CAAC,QAAkB;QACrB,MAAM,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAA;QACzC,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,KAAK,CACT,yBAAyB,EACzB;gBACE,QAAQ;aACT,EACD,IAAI,CAAC,IAAI,CACV,CAAA;QACH,CAAC;QACD,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAA;IAC3B,CAAC;CACF","sourcesContent":["import { error, ErrorCauseObject } from '@vltpkg/error-cause'\nimport { asManifest, Manifest } from '@vltpkg/types'\nimport { readFileSync, writeFileSync } from 'node:fs'\nimport { resolve } from 'node:path'\nimport { parse, stringify } from 'polite-json'\n\nexport class PackageJson {\n /**\n * cache of `package.json` loads\n */\n #cache = new Map<string, Manifest>()\n\n /**\n * cache of `package.json` paths by manifest\n */\n #pathCache = new Map<Manifest, string>()\n\n /**\n * cache of load errors\n */\n #errCache = new Map<string, ErrorCauseObject>()\n\n /**\n * Reads and parses contents of a `package.json` file at a directory `dir`.\n * `reload` will optionally skip reading from the cache when set to `true`.\n */\n read(dir: string, { reload }: { reload?: boolean } = {}): Manifest {\n const cachedPackageJson = !reload && this.#cache.get(dir)\n if (cachedPackageJson) {\n return cachedPackageJson\n }\n\n const filename = resolve(dir, 'package.json')\n\n const fail = (err: ErrorCauseObject) =>\n error('Could not read package.json file', err, this.read)\n\n const cachedError = !reload && this.#errCache.get(dir)\n if (cachedError) {\n throw fail(cachedError)\n }\n\n try {\n const res: Manifest = asManifest(\n parse(readFileSync(filename, { encoding: 'utf8' })),\n )\n this.#cache.set(dir, res)\n this.#pathCache.set(res, dir)\n return res\n } catch (err) {\n const ec: ErrorCauseObject = {\n path: filename,\n cause: err as Error,\n }\n this.#errCache.set(dir, ec)\n throw fail(ec)\n }\n }\n\n write(dir: string, manifest: Manifest): void {\n const filename = resolve(dir, 'package.json')\n\n try {\n // This assumes kIndent and kNewline are already present on the manifest because we would\n // only write a package.json after reading it which will set those properties.\n writeFileSync(filename, stringify(manifest))\n this.#cache.set(dir, manifest)\n this.#pathCache.set(manifest, dir)\n } catch (err) {\n // If there was an error writing to this package.json then also delete it from our cache\n // just in case a future read would get stale data.\n this.#cache.delete(dir)\n this.#pathCache.delete(manifest)\n throw error(\n 'Could not write package.json file',\n {\n path: filename,\n cause: err as Error,\n },\n this.write,\n )\n }\n }\n\n save(manifest: Manifest): void {\n const dir = this.#pathCache.get(manifest)\n if (!dir) {\n throw error(\n 'Could not save manifest',\n {\n manifest,\n },\n this.save,\n )\n }\n this.write(dir, manifest)\n }\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@vltpkg/package-json",
3
+ "description": "Manage reading package.json files",
4
+ "version": "0.0.0-0.1730239248325",
5
+ "tshy": {
6
+ "selfLink": false,
7
+ "dialects": [
8
+ "esm"
9
+ ],
10
+ "exports": {
11
+ "./package.json": "./package.json",
12
+ ".": "./src/index.ts"
13
+ }
14
+ },
15
+ "dependencies": {
16
+ "polite-json": "^5.0.0",
17
+ "@vltpkg/error-cause": "0.0.0-0.1730239248325",
18
+ "@vltpkg/types": "0.0.0-0.1730239248325"
19
+ },
20
+ "devDependencies": {
21
+ "@eslint/js": "^9.8.0",
22
+ "@types/eslint__js": "^8.42.3",
23
+ "@types/node": "^22.4.1",
24
+ "eslint": "^9.8.0",
25
+ "prettier": "^3.3.2",
26
+ "tap": "^21.0.1",
27
+ "tshy": "^3.0.2",
28
+ "typescript": "^5.5.4",
29
+ "typescript-eslint": "^8.0.1"
30
+ },
31
+ "license": "BSD-2-Clause-Patent",
32
+ "engines": {
33
+ "node": "20 || >=22"
34
+ },
35
+ "tap": {
36
+ "extends": "../../tap-config.yaml"
37
+ },
38
+ "prettier": "../../.prettierrc.js",
39
+ "module": "./dist/esm/index.js",
40
+ "type": "module",
41
+ "exports": {
42
+ "./package.json": "./package.json",
43
+ ".": {
44
+ "import": {
45
+ "types": "./dist/esm/index.d.ts",
46
+ "default": "./dist/esm/index.js"
47
+ }
48
+ }
49
+ },
50
+ "files": [
51
+ "dist"
52
+ ],
53
+ "scripts": {
54
+ "format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
55
+ "format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
56
+ "lint": "eslint . --fix",
57
+ "lint:check": "eslint .",
58
+ "presnap": "tshy",
59
+ "snap": "tap",
60
+ "pretest": "tshy",
61
+ "test": "tap"
62
+ }
63
+ }