@vltpkg/vlt-json 1.0.0-rc.22 → 1.0.0-rc.24
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/dist/index.d.ts +7 -0
- package/dist/index.js +169 -0
- package/package.json +5 -5
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type WhichConfig = 'user' | 'project';
|
|
2
|
+
export type Validator<T> = (x: unknown, file: string) => asserts x is T;
|
|
3
|
+
export declare const unload: (which?: WhichConfig) => void;
|
|
4
|
+
export declare const reload: (field: string, which?: WhichConfig) => unknown;
|
|
5
|
+
export declare const load: <T>(field: string, validator: Validator<T>, which?: WhichConfig) => T | undefined;
|
|
6
|
+
export declare const find: (which?: WhichConfig, cwd?: string, home?: string) => string;
|
|
7
|
+
export declare const save: (field: string, value: unknown, which?: WhichConfig) => void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { error } from '@vltpkg/error-cause';
|
|
2
|
+
import { XDG } from '@vltpkg/xdg';
|
|
3
|
+
import { lstatSync, mkdirSync, readFileSync, writeFileSync, } from 'node:fs';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { dirname, 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
|
+
export const unload = (which = 'project') => {
|
|
74
|
+
const file = find(which);
|
|
75
|
+
delete datas[which];
|
|
76
|
+
delete paths[which];
|
|
77
|
+
delete lstatCache[file];
|
|
78
|
+
delete mtimes[which];
|
|
79
|
+
};
|
|
80
|
+
export const reload = (field, which = 'project') => {
|
|
81
|
+
unload(which);
|
|
82
|
+
const file = find(which);
|
|
83
|
+
const data = loadFullObject(which);
|
|
84
|
+
for (const [field, validator] of Object.entries(validators)) {
|
|
85
|
+
const value = data[field];
|
|
86
|
+
runValidator(validator, value, file);
|
|
87
|
+
}
|
|
88
|
+
return data[field];
|
|
89
|
+
};
|
|
90
|
+
export const load = (field, validator, which = 'project') => {
|
|
91
|
+
const data = loadFullObject(which);
|
|
92
|
+
const file = find(which);
|
|
93
|
+
validators[field] ??= validator;
|
|
94
|
+
const value = data[field];
|
|
95
|
+
if (value !== undefined)
|
|
96
|
+
validator(value, file);
|
|
97
|
+
return value;
|
|
98
|
+
};
|
|
99
|
+
export const find = (which = 'project', cwd = process.cwd(), home = homedir()) => {
|
|
100
|
+
// always resolve the user config path to the XDG location
|
|
101
|
+
// if caches were unloaded, reinitialize it instead of walking the project tree
|
|
102
|
+
if (which === 'user') {
|
|
103
|
+
return (paths.user ??= new XDG('vlt').config('vlt.json'));
|
|
104
|
+
}
|
|
105
|
+
if (paths[which])
|
|
106
|
+
return paths[which];
|
|
107
|
+
let lastKnownRoot = cwd;
|
|
108
|
+
for (const dir of walkUp(cwd)) {
|
|
109
|
+
// don't look in ~
|
|
110
|
+
if (dir === home)
|
|
111
|
+
break;
|
|
112
|
+
const projectConfig = resolve(dir, 'vlt.json');
|
|
113
|
+
// don't let it match user config
|
|
114
|
+
if (projectConfig === paths.user)
|
|
115
|
+
break;
|
|
116
|
+
// these mean we're done looking
|
|
117
|
+
if (exists(projectConfig)) {
|
|
118
|
+
return (paths[which] = projectConfig);
|
|
119
|
+
}
|
|
120
|
+
// these are likely candidates, come back if nothing else matches
|
|
121
|
+
if (exists(resolve(dir, 'package.json')) ||
|
|
122
|
+
exists(resolve(dir, 'node_modules'))) {
|
|
123
|
+
lastKnownRoot = dir;
|
|
124
|
+
}
|
|
125
|
+
if (exists(resolve(dir, '.git')))
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
return (paths[which] = resolve(lastKnownRoot, 'vlt.json'));
|
|
129
|
+
};
|
|
130
|
+
export const save = (field, value, which = 'project') => {
|
|
131
|
+
const validator = validators[field];
|
|
132
|
+
const data = datas[which];
|
|
133
|
+
if (!validator || !data) {
|
|
134
|
+
throw error('Cannot save field before loading initially', {
|
|
135
|
+
name: field,
|
|
136
|
+
found: value,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const file = find(which);
|
|
140
|
+
runValidator(validator, value, file);
|
|
141
|
+
data[field] = value;
|
|
142
|
+
const mtime = mtimes[which];
|
|
143
|
+
const path = find(which);
|
|
144
|
+
delete lstatCache[path];
|
|
145
|
+
const updatedMtime = cachedLstat(path)?.mtime.getTime();
|
|
146
|
+
// if we didn't have a file, and now do, or if we had a file and
|
|
147
|
+
// it's been changed since we read it, no go.
|
|
148
|
+
if (updatedMtime && (!mtime || updatedMtime > mtime)) {
|
|
149
|
+
throw error('File was changed by another process, cannot safely write', {
|
|
150
|
+
path,
|
|
151
|
+
name: field,
|
|
152
|
+
found: value,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
// defines a default indentation and newline in case
|
|
156
|
+
// a user config file has not yet been read before
|
|
157
|
+
const extraStringifyOptions = stringifyOptions[path] ?? {};
|
|
158
|
+
if (!extraStringifyOptions[kIndent]) {
|
|
159
|
+
extraStringifyOptions[kIndent] = 2;
|
|
160
|
+
}
|
|
161
|
+
if (!extraStringifyOptions[kNewline]) {
|
|
162
|
+
extraStringifyOptions[kNewline] = '\n';
|
|
163
|
+
}
|
|
164
|
+
// Ensure the directory exists before writing the file
|
|
165
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
166
|
+
writeFileSync(path, jsonStringify({ ...data, ...extraStringifyOptions }));
|
|
167
|
+
delete lstatCache[path];
|
|
168
|
+
mtimes[which] = cachedLstat(path)?.mtime.getTime();
|
|
169
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/vlt-json",
|
|
3
3
|
"description": "utility for finding, reading, and updating the vlt.json file",
|
|
4
|
-
"version": "1.0.0-rc.
|
|
4
|
+
"version": "1.0.0-rc.24",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"email": "support@vlt.sh"
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@vltpkg/error-cause": "1.0.0-rc.
|
|
16
|
-
"@vltpkg/xdg": "1.0.0-rc.
|
|
15
|
+
"@vltpkg/error-cause": "1.0.0-rc.24",
|
|
16
|
+
"@vltpkg/xdg": "1.0.0-rc.24",
|
|
17
17
|
"polite-json": "^5.0.0",
|
|
18
18
|
"walk-up-path": "^4.0.0"
|
|
19
19
|
},
|
|
@@ -46,13 +46,13 @@
|
|
|
46
46
|
"extends": "../../tap-config.yaml"
|
|
47
47
|
},
|
|
48
48
|
"prettier": "../../.prettierrc.js",
|
|
49
|
-
"module": "./
|
|
49
|
+
"module": "./dist/index.js",
|
|
50
50
|
"type": "module",
|
|
51
51
|
"exports": {
|
|
52
52
|
"./package.json": "./package.json",
|
|
53
53
|
".": {
|
|
54
54
|
"import": {
|
|
55
|
-
"default": "./
|
|
55
|
+
"default": "./dist/index.js"
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
},
|