@vltpkg/vlt-json 0.0.0-14

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,38 @@
1
+ # @vltpkg/vlt-json
2
+
3
+ Facilities for finding, loading, and updating the `vlt.json` vlt
4
+ project configuration file.
5
+
6
+ ## Usage
7
+
8
+ ```js
9
+ import { find, load, save } from '@vltpkg/xdg'
10
+
11
+ // finds the project config file.
12
+ const projectRoot = find()
13
+
14
+ // Load a bit of data, providing a typedef check for it
15
+ // If a matching field is not found, then `undefined` is returned.
16
+
17
+ const isWorkspaceConfig = (x: unknown): x is WorkspaceConfig => { ... }
18
+ const workspaceOptions = load('workspaces', isWorkspaceConfig)
19
+ // now workspaceOptions is WorkspaceConfig | undefined
20
+
21
+ // do whatever and then save it back. This organizes writes so
22
+ // that we do not clobber the file if there are multiple parts
23
+ // of vlt all trying to write to it. Once the validator function
24
+ // is established, it'll be re-used on that field when saving.
25
+ save('workspaces', workspaceConfig)
26
+
27
+ // If you need a user-level instead of project-level file, use
28
+ // the third argument to specify that file instead.
29
+ const userConfig = load('config', isValidConfig, 'user')
30
+ const projectConfig = load('config', isValidConfig)
31
+ userConfig.color = true
32
+ save('config', userConfig, 'user')
33
+
34
+ // Note that save() ALWAYS clobbers, so if you want to do
35
+ // a merge, load() first, make the change, and then save back.
36
+ // If the file was edited since it was opened, then this will
37
+ // fail.
38
+ ```
@@ -0,0 +1,9 @@
1
+ export type WhichConfig = 'user' | 'project';
2
+ export type Validator<T> = (x: unknown, file: string) => asserts x is T;
3
+ /** This should only be used in tests */
4
+ export declare const unload: (which?: WhichConfig) => void;
5
+ export declare const reload: (field: string, which?: WhichConfig) => unknown;
6
+ export declare const load: <T>(field: string, validator: Validator<T>, which?: WhichConfig) => T | undefined;
7
+ export declare const find: (which?: WhichConfig, cwd?: string, home?: string) => string;
8
+ export declare const save: (field: string, value: unknown, which?: WhichConfig) => void;
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAuBA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,SAAS,CAAA;AA8E5C,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,CACzB,CAAC,EAAE,OAAO,EACV,IAAI,EAAE,MAAM,KACT,OAAO,CAAC,CAAC,IAAI,CAAC,CAAA;AAcnB,wCAAwC;AACxC,eAAO,MAAM,MAAM,WAAW,WAAW,SAMxC,CAAA;AAED,eAAO,MAAM,MAAM,UACV,MAAM,UACN,WAAW,KACjB,OASF,CAAA;AAED,eAAO,MAAM,IAAI,GAAI,CAAC,SACb,MAAM,aACF,SAAS,CAAC,CAAC,CAAC,UAChB,WAAW,KACjB,CAAC,GAAG,SAON,CAAA;AAED,eAAO,MAAM,IAAI,WACR,WAAW,kCAGjB,MA6BF,CAAA;AAED,eAAO,MAAM,IAAI,UACR,MAAM,SACN,OAAO,UACP,WAAW,KACjB,IAkCF,CAAA"}
@@ -0,0 +1,155 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { XDG } from '@vltpkg/xdg';
3
+ import { lstatSync, readFileSync, writeFileSync } from 'node:fs';
4
+ import { homedir } from 'node:os';
5
+ import { resolve } from 'node:path';
6
+ import { walkUp } from 'walk-up-path';
7
+ import { parse as jsonParse, stringify as jsonStringify, kIndent, kNewline, } from 'polite-json';
8
+ const stringifyOptions = {};
9
+ const lstatCache = {};
10
+ const cachedLstat = (path) => {
11
+ if (path in lstatCache)
12
+ return lstatCache[path];
13
+ try {
14
+ return (lstatCache[path] = lstatSync(path));
15
+ }
16
+ catch {
17
+ delete lstatCache[path];
18
+ return undefined;
19
+ }
20
+ };
21
+ const exists = (f) => !!cachedLstat(f);
22
+ const isRecord = (x) => !!x && typeof x === 'object';
23
+ const mtimes = {
24
+ user: undefined,
25
+ project: undefined,
26
+ };
27
+ const datas = {
28
+ user: undefined,
29
+ project: undefined,
30
+ };
31
+ const validators = {};
32
+ const paths = {
33
+ user: new XDG('vlt').config('vlt.json'),
34
+ project: undefined,
35
+ };
36
+ const maybeReadData = (path) => {
37
+ try {
38
+ const rawData = jsonParse(readFileSync(path, 'utf8'));
39
+ if (!isRecord(rawData))
40
+ return undefined;
41
+ const so = stringifyOptions[path] ?? {
42
+ [kIndent]: 2,
43
+ [kNewline]: '\n',
44
+ };
45
+ const { [kNewline]: nl = so[kNewline], [kIndent]: ind = so[kIndent], ...data } = rawData;
46
+ stringifyOptions[path] = so;
47
+ stringifyOptions[path][kNewline] = nl;
48
+ stringifyOptions[path][kIndent] = ind;
49
+ return data;
50
+ }
51
+ catch (er) {
52
+ throw error('Failed to parse vlt.json file', {
53
+ path,
54
+ cause: er,
55
+ });
56
+ }
57
+ };
58
+ const loadFullObject = (which) => {
59
+ if (datas[which])
60
+ return datas[which];
61
+ const path = find(which);
62
+ const mtime = cachedLstat(path)?.mtime.getTime();
63
+ const data = mtime ? maybeReadData(path) : {};
64
+ if (mtime && data) {
65
+ mtimes[which] = mtime;
66
+ }
67
+ return (datas[which] = data ?? {});
68
+ };
69
+ const runValidator = (v, x, file) => {
70
+ if (x !== undefined)
71
+ v(x, file);
72
+ };
73
+ /** This should only be used in tests */
74
+ export const unload = (which = 'project') => {
75
+ const file = find(which);
76
+ delete datas[which];
77
+ delete paths[which];
78
+ delete lstatCache[file];
79
+ delete mtimes[which];
80
+ };
81
+ export const reload = (field, which = 'project') => {
82
+ unload(which);
83
+ const file = find(which);
84
+ const data = loadFullObject(which);
85
+ for (const [field, validator] of Object.entries(validators)) {
86
+ const value = data[field];
87
+ runValidator(validator, value, file);
88
+ }
89
+ return data[field];
90
+ };
91
+ export const load = (field, validator, which = 'project') => {
92
+ const data = loadFullObject(which);
93
+ const file = find(which);
94
+ validators[field] ??= validator;
95
+ const value = data[field];
96
+ if (value !== undefined)
97
+ validator(value, file);
98
+ return value;
99
+ };
100
+ export const find = (which = 'project', cwd = process.cwd(), home = homedir()) => {
101
+ if (paths[which])
102
+ return paths[which];
103
+ let lastKnownRoot = cwd;
104
+ for (const dir of walkUp(cwd)) {
105
+ // don't look in ~
106
+ if (dir === home)
107
+ break;
108
+ const projectConfig = resolve(dir, 'vlt.json');
109
+ // don't let it match user config
110
+ if (projectConfig === paths.user)
111
+ break;
112
+ // these mean we're done looking
113
+ if (exists(projectConfig)) {
114
+ return (paths[which] = projectConfig);
115
+ }
116
+ // these are likely candidates, come back if nothing else matches
117
+ if (exists(resolve(dir, 'package.json')) ||
118
+ exists(resolve(dir, 'node_modules'))) {
119
+ lastKnownRoot = dir;
120
+ }
121
+ if (exists(resolve(dir, '.git')))
122
+ break;
123
+ }
124
+ return (paths[which] = resolve(lastKnownRoot, 'vlt.json'));
125
+ };
126
+ export const save = (field, value, which = 'project') => {
127
+ const validator = validators[field];
128
+ const data = datas[which];
129
+ if (!validator || !data) {
130
+ throw error('Cannot save field before loading initially', {
131
+ name: field,
132
+ found: value,
133
+ });
134
+ }
135
+ const file = find(which);
136
+ runValidator(validator, value, file);
137
+ data[field] = value;
138
+ const mtime = mtimes[which];
139
+ const path = find(which);
140
+ delete lstatCache[path];
141
+ const updatedMtime = cachedLstat(path)?.mtime.getTime();
142
+ // if we didn't have a file, and now do, or if we had a file and
143
+ // it's been changed since we read it, no go.
144
+ if (updatedMtime && (!mtime || updatedMtime > mtime)) {
145
+ throw error('File was changed by another process, cannot safely write', {
146
+ path,
147
+ name: field,
148
+ found: value,
149
+ });
150
+ }
151
+ writeFileSync(path, jsonStringify({ ...data, ...stringifyOptions[path] }));
152
+ delete lstatCache[path];
153
+ mtimes[which] = cachedLstat(path)?.mtime.getTime();
154
+ };
155
+ //# sourceMappingURL=index.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,CAAC,EAAE,KAAK,CAAC,OAAO,EAAE,CAAA;IACvD,gEAAgE;IAChE,6CAA6C;IAC7C,IAAI,YAAY,IAAI,CAAC,CAAC,KAAK,IAAI,YAAY,GAAG,KAAK,CAAC,EAAE,CAAC;QACrD,MAAM,KAAK,CACT,0DAA0D,EAC1D;YACE,IAAI;YACJ,IAAI,EAAE,KAAK;YACX,KAAK,EAAE,KAAK;SACb,CACF,CAAA;IACH,CAAC;IACD,aAAa,CACX,IAAI,EACJ,aAAa,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC,CACtD,CAAA;IACD,OAAO,UAAU,CAAC,IAAI,CAAC,CAAA;IACvB,MAAM,CAAC,KAAK,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,EAAE,KAAK,CAAC,OAAO,EAAE,CAAA;AACpD,CAAC,CAAA","sourcesContent":["import { error } from '@vltpkg/error-cause'\nimport { XDG } from '@vltpkg/xdg'\nimport type { Stats } from 'node:fs'\nimport { lstatSync, readFileSync, writeFileSync } from 'node:fs'\nimport { homedir } from 'node:os'\nimport { resolve } from 'node:path'\nimport { walkUp } from 'walk-up-path'\n\nimport {\n parse as jsonParse,\n stringify as jsonStringify,\n kIndent,\n kNewline,\n} from 'polite-json'\n\nconst stringifyOptions: Record<\n string,\n {\n [kIndent]: number | string\n [kNewline]: string\n }\n> = {}\n\nexport type WhichConfig = 'user' | 'project'\n\nconst lstatCache: Record<string, Stats> = {}\nconst cachedLstat = (path: string): undefined | Stats => {\n if (path in lstatCache) return lstatCache[path]\n try {\n return (lstatCache[path] = lstatSync(path))\n } catch {\n delete lstatCache[path]\n return undefined\n }\n}\n\nconst exists = (f: string) => !!cachedLstat(f)\n\nconst isRecord = (x: unknown): x is Record<string, unknown> =>\n !!x && typeof x === 'object'\n\nconst mtimes: Record<WhichConfig, undefined | number> = {\n user: undefined,\n project: undefined,\n}\n\nconst datas: Record<\n WhichConfig,\n undefined | Record<string, unknown>\n> = {\n user: undefined,\n project: undefined,\n}\n\nconst validators: Record<string, Validator<unknown>> = {}\n\nconst paths: Record<WhichConfig, undefined | string> = {\n user: new XDG('vlt').config('vlt.json'),\n project: undefined,\n}\n\nconst maybeReadData = (\n path: string,\n): undefined | Record<string, unknown> => {\n try {\n const rawData = jsonParse(readFileSync(path, 'utf8'))\n if (!isRecord(rawData)) return undefined\n const so = stringifyOptions[path] ?? {\n [kIndent]: 2,\n [kNewline]: '\\n',\n }\n const {\n [kNewline]: nl = so[kNewline],\n [kIndent]: ind = so[kIndent],\n ...data\n } = rawData\n stringifyOptions[path] = so\n stringifyOptions[path][kNewline] = nl\n stringifyOptions[path][kIndent] = ind\n return data\n } catch (er) {\n throw error('Failed to parse vlt.json file', {\n path,\n cause: er,\n })\n }\n}\n\nconst loadFullObject = (\n which: WhichConfig,\n): Record<string, unknown> => {\n if (datas[which]) return datas[which]\n const path = find(which)\n const mtime = cachedLstat(path)?.mtime.getTime()\n const data = mtime ? maybeReadData(path) : {}\n if (mtime && data) {\n mtimes[which] = mtime\n }\n return (datas[which] = data ?? {})\n}\n\nexport type Validator<T> = (\n x: unknown,\n file: string,\n) => asserts x is T\n\nconst runValidator: <T>(\n v: Validator<T>,\n x: unknown,\n file: string,\n) => void = <T>(\n v: Validator<T>,\n x: unknown,\n file: string,\n): asserts x is T | undefined => {\n if (x !== undefined) v(x, file)\n}\n\n/** This should only be used in tests */\nexport const unload = (which: WhichConfig = 'project') => {\n const file = find(which)\n delete datas[which]\n delete paths[which]\n delete lstatCache[file]\n delete mtimes[which]\n}\n\nexport const reload = (\n field: string,\n which: WhichConfig = 'project',\n): unknown => {\n unload(which)\n const file = find(which)\n const data = loadFullObject(which)\n for (const [field, validator] of Object.entries(validators)) {\n const value = data[field]\n runValidator(validator, value, file)\n }\n return data[field]\n}\n\nexport const load = <T>(\n field: string,\n validator: Validator<T>,\n which: WhichConfig = 'project',\n): T | undefined => {\n const data = loadFullObject(which)\n const file = find(which)\n validators[field] ??= validator\n const value = data[field]\n if (value !== undefined) validator(value, file)\n return value\n}\n\nexport const find = (\n which: WhichConfig = 'project',\n cwd = process.cwd(),\n home = homedir(),\n): string => {\n if (paths[which]) return paths[which]\n let lastKnownRoot = cwd\n for (const dir of walkUp(cwd)) {\n // don't look in ~\n if (dir === home) break\n\n const projectConfig = resolve(dir, 'vlt.json')\n\n // don't let it match user config\n if (projectConfig === paths.user) break\n\n // these mean we're done looking\n if (exists(projectConfig)) {\n return (paths[which] = projectConfig)\n }\n\n // these are likely candidates, come back if nothing else matches\n if (\n exists(resolve(dir, 'package.json')) ||\n exists(resolve(dir, 'node_modules'))\n ) {\n lastKnownRoot = dir\n }\n\n if (exists(resolve(dir, '.git'))) break\n }\n\n return (paths[which] = resolve(lastKnownRoot, 'vlt.json'))\n}\n\nexport const save = (\n field: string,\n value: unknown,\n which: WhichConfig = 'project',\n): void => {\n const validator = validators[field]\n const data = datas[which]\n if (!validator || !data) {\n throw error('Cannot save field before loading initially', {\n name: field,\n found: value,\n })\n }\n const file = find(which)\n runValidator(validator, value, file)\n data[field] = value\n const mtime = mtimes[which]\n const path = find(which)\n delete lstatCache[path]\n const updatedMtime = cachedLstat(path)?.mtime.getTime()\n // if we didn't have a file, and now do, or if we had a file and\n // it's been changed since we read it, no go.\n if (updatedMtime && (!mtime || updatedMtime > mtime)) {\n throw error(\n 'File was changed by another process, cannot safely write',\n {\n path,\n name: field,\n found: value,\n },\n )\n }\n writeFileSync(\n path,\n jsonStringify({ ...data, ...stringifyOptions[path] }),\n )\n delete lstatCache[path]\n mtimes[which] = cachedLstat(path)?.mtime.getTime()\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@vltpkg/vlt-json",
3
+ "description": "utility for finding, reading, and updating the vlt.json file",
4
+ "version": "0.0.0-14",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/vltpkg/vltpkg.git",
8
+ "directory": "src/vlt-json"
9
+ },
10
+ "tshy": {
11
+ "selfLink": false,
12
+ "liveDev": true,
13
+ "dialects": [
14
+ "esm"
15
+ ],
16
+ "exports": {
17
+ "./package.json": "./package.json",
18
+ ".": "./src/index.ts"
19
+ }
20
+ },
21
+ "dependencies": {
22
+ "polite-json": "^5.0.0",
23
+ "walk-up-path": "^4.0.0",
24
+ "@vltpkg/error-cause": "0.0.0-14",
25
+ "@vltpkg/xdg": "0.0.0-14"
26
+ },
27
+ "devDependencies": {
28
+ "@eslint/js": "^9.28.0",
29
+ "@types/node": "^22.15.29",
30
+ "eslint": "^9.28.0",
31
+ "prettier": "^3.5.3",
32
+ "tap": "^21.1.0",
33
+ "tshy": "^3.0.2",
34
+ "typedoc": "~0.27.9",
35
+ "typescript": "5.7.3",
36
+ "typescript-eslint": "^8.33.1"
37
+ },
38
+ "license": "BSD-2-Clause-Patent",
39
+ "engines": {
40
+ "node": ">=22"
41
+ },
42
+ "tap": {
43
+ "extends": "../../tap-config.yaml"
44
+ },
45
+ "prettier": "../../.prettierrc.js",
46
+ "module": "./dist/esm/index.js",
47
+ "type": "module",
48
+ "exports": {
49
+ "./package.json": "./package.json",
50
+ ".": {
51
+ "import": {
52
+ "types": "./dist/esm/index.d.ts",
53
+ "default": "./dist/esm/index.js"
54
+ }
55
+ }
56
+ },
57
+ "files": [
58
+ "dist"
59
+ ],
60
+ "scripts": {
61
+ "format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
62
+ "format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
63
+ "lint": "eslint . --fix",
64
+ "lint:check": "eslint .",
65
+ "snap": "tap",
66
+ "test": "tap",
67
+ "posttest": "tsc --noEmit",
68
+ "tshy": "tshy",
69
+ "typecheck": "tsc --noEmit"
70
+ }
71
+ }