vlt
Version:
The vlt CLI
176 lines (173 loc) • 4.67 kB
JavaScript
var global = globalThis;
import {Buffer} from "node:buffer";
import {setTimeout,clearTimeout,setImmediate,clearImmediate,setInterval,clearInterval} from "node:timers";
import {createRequire as _vlt_createRequire} from "node:module";
var require = _vlt_createRequire(import.meta.filename);
import {
kIndent,
kNewline,
parse,
stringify,
walkUp
} from "./chunk-SGEQHKFC.js";
import {
XDG,
error
} from "./chunk-RV3EHS4P.js";
// ../../src/vlt-json/src/index.ts
import { lstatSync, readFileSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { resolve } from "node:path";
var stringifyOptions = {};
var lstatCache = {};
var cachedLstat = (path) => {
if (path in lstatCache) return lstatCache[path];
try {
return lstatCache[path] = lstatSync(path);
} catch {
delete lstatCache[path];
return void 0;
}
};
var exists = (f) => !!cachedLstat(f);
var isRecord = (x) => !!x && typeof x === "object";
var mtimes = {
user: void 0,
project: void 0
};
var datas = {
user: void 0,
project: void 0
};
var validators = {};
var paths = {
user: new XDG("vlt").config("vlt.json"),
project: void 0
};
var maybeReadData = (path) => {
try {
const rawData = parse(readFileSync(path, "utf8"));
if (!isRecord(rawData)) return void 0;
const so = stringifyOptions[path] ?? {
[kIndent]: 2,
[kNewline]: "\n"
};
const {
[kNewline]: nl = so[kNewline],
[kIndent]: ind = so[kIndent],
...data
} = rawData;
stringifyOptions[path] = so;
stringifyOptions[path][kNewline] = nl;
stringifyOptions[path][kIndent] = ind;
return data;
} catch (er) {
throw error("Failed to parse vlt.json file", {
path,
cause: er
});
}
};
var loadFullObject = (which) => {
if (datas[which]) return datas[which];
const path = find(which);
const mtime = cachedLstat(path)?.mtime.getTime();
const data = mtime ? maybeReadData(path) : {};
if (mtime && data) {
mtimes[which] = mtime;
}
return datas[which] = data ?? {};
};
var runValidator = (v, x, file) => {
if (x !== void 0) v(x, file);
};
var unload = (which = "project") => {
const file = find(which);
delete datas[which];
delete paths[which];
delete lstatCache[file];
delete mtimes[which];
};
var reload = (field, which = "project") => {
unload(which);
const file = find(which);
const data = loadFullObject(which);
for (const [field2, validator] of Object.entries(validators)) {
const value = data[field2];
runValidator(validator, value, file);
}
return data[field];
};
var load = (field, validator, which = "project") => {
const data = loadFullObject(which);
const file = find(which);
validators[field] ??= validator;
const value = data[field];
if (value !== void 0) validator(value, file);
return value;
};
var find = (which = "project", cwd = process.cwd(), home = homedir()) => {
if (paths[which]) return paths[which];
let lastKnownRoot = cwd;
for (const dir of walkUp(cwd)) {
if (dir === home) break;
const projectConfig = resolve(dir, "vlt.json");
if (projectConfig === paths.user) break;
if (exists(projectConfig)) {
return paths[which] = projectConfig;
}
if (exists(resolve(dir, "package.json")) || exists(resolve(dir, "node_modules"))) {
lastKnownRoot = dir;
}
if (exists(resolve(dir, ".git"))) break;
}
return paths[which] = resolve(lastKnownRoot, "vlt.json");
};
var save = (field, value, which = "project") => {
const validator = validators[field];
const data = datas[which];
if (!validator || !data) {
throw error("Cannot save field before loading initially", {
name: field,
found: value
});
}
const file = find(which);
runValidator(validator, value, file);
data[field] = value;
const mtime = mtimes[which];
const path = find(which);
delete lstatCache[path];
const updatedMtime = cachedLstat(path)?.mtime.getTime();
if (updatedMtime && (!mtime || updatedMtime > mtime)) {
throw error(
"File was changed by another process, cannot safely write",
{
path,
name: field,
found: value
}
);
}
const extraStringifyOptions = stringifyOptions[path] ?? {};
if (!extraStringifyOptions[kIndent]) {
extraStringifyOptions[kIndent] = 2;
}
if (!extraStringifyOptions[kNewline]) {
extraStringifyOptions[kNewline] = "\n";
}
writeFileSync(
path,
stringify({ ...data, ...extraStringifyOptions })
);
delete lstatCache[path];
mtimes[which] = cachedLstat(path)?.mtime.getTime();
};
export {
unload,
reload,
load,
find,
save
};
//# sourceMappingURL=chunk-5UBJ3ZBM.js.map