initial commit
This commit is contained in:
98
node_modules/rc9/dist/index.cjs
generated
vendored
Normal file
98
node_modules/rc9/dist/index.cjs
generated
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
'use strict';
|
||||
|
||||
const node_fs = require('node:fs');
|
||||
const node_path = require('node:path');
|
||||
const node_os = require('node:os');
|
||||
const destr = require('destr');
|
||||
const flat = require('flat');
|
||||
const defu = require('defu');
|
||||
|
||||
const RE_KEY_VAL = /^\s*([^\s=]+)\s*=\s*(.*)?\s*$/;
|
||||
const RE_LINES = /\n|\r|\r\n/;
|
||||
const defaults = {
|
||||
name: ".conf",
|
||||
dir: process.cwd(),
|
||||
flat: false
|
||||
};
|
||||
function withDefaults(options) {
|
||||
if (typeof options === "string") {
|
||||
options = { name: options };
|
||||
}
|
||||
return { ...defaults, ...options };
|
||||
}
|
||||
function parse(contents, options = {}) {
|
||||
const config = {};
|
||||
const lines = contents.split(RE_LINES);
|
||||
for (const line of lines) {
|
||||
const match = line.match(RE_KEY_VAL);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const key = match[1];
|
||||
if (!key || key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const value = destr(match[2].trim());
|
||||
if (key.endsWith("[]")) {
|
||||
const nkey = key.slice(0, Math.max(0, key.length - 2));
|
||||
config[nkey] = (config[nkey] || []).concat(value);
|
||||
continue;
|
||||
}
|
||||
config[key] = value;
|
||||
}
|
||||
return options.flat ? config : flat.unflatten(config, { overwrite: true });
|
||||
}
|
||||
function parseFile(path, options) {
|
||||
if (!node_fs.existsSync(path)) {
|
||||
return {};
|
||||
}
|
||||
return parse(node_fs.readFileSync(path, "utf8"), options);
|
||||
}
|
||||
function read(options) {
|
||||
options = withDefaults(options);
|
||||
return parseFile(node_path.resolve(options.dir, options.name), options);
|
||||
}
|
||||
function readUser(options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || node_os.homedir();
|
||||
return read(options);
|
||||
}
|
||||
function serialize(config) {
|
||||
return Object.entries(flat.flatten(config)).map(([key, value]) => `${key}=${typeof value === "string" ? value : JSON.stringify(value)}`).join("\n");
|
||||
}
|
||||
function write(config, options) {
|
||||
options = withDefaults(options);
|
||||
node_fs.writeFileSync(node_path.resolve(options.dir, options.name), serialize(config), {
|
||||
encoding: "utf8"
|
||||
});
|
||||
}
|
||||
function writeUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || node_os.homedir();
|
||||
write(config, options);
|
||||
}
|
||||
function update(config, options) {
|
||||
options = withDefaults(options);
|
||||
if (!options.flat) {
|
||||
config = flat.unflatten(config, { overwrite: true });
|
||||
}
|
||||
const newConfig = defu(config, read(options));
|
||||
write(newConfig, options);
|
||||
return newConfig;
|
||||
}
|
||||
function updateUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || node_os.homedir();
|
||||
return update(config, options);
|
||||
}
|
||||
|
||||
exports.defaults = defaults;
|
||||
exports.parse = parse;
|
||||
exports.parseFile = parseFile;
|
||||
exports.read = read;
|
||||
exports.readUser = readUser;
|
||||
exports.serialize = serialize;
|
||||
exports.update = update;
|
||||
exports.updateUser = updateUser;
|
||||
exports.write = write;
|
||||
exports.writeUser = writeUser;
|
||||
18
node_modules/rc9/dist/index.d.ts
generated
vendored
Normal file
18
node_modules/rc9/dist/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
declare type RC = Record<string, any>;
|
||||
interface RCOptions {
|
||||
name?: string;
|
||||
dir?: string;
|
||||
flat?: boolean;
|
||||
}
|
||||
declare const defaults: RCOptions;
|
||||
declare function parse(contents: string, options?: RCOptions): RC;
|
||||
declare function parseFile(path: string, options?: RCOptions): RC;
|
||||
declare function read(options?: RCOptions | string): RC;
|
||||
declare function readUser(options?: RCOptions | string): RC;
|
||||
declare function serialize(config: RC): string;
|
||||
declare function write(config: RC, options?: RCOptions | string): void;
|
||||
declare function writeUser(config: RC, options?: RCOptions | string): void;
|
||||
declare function update(config: RC, options?: RCOptions | string): RC;
|
||||
declare function updateUser(config: RC, options?: RCOptions | string): RC;
|
||||
|
||||
export { defaults, parse, parseFile, read, readUser, serialize, update, updateUser, write, writeUser };
|
||||
87
node_modules/rc9/dist/index.mjs
generated
vendored
Normal file
87
node_modules/rc9/dist/index.mjs
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
import { homedir } from 'node:os';
|
||||
import destr from 'destr';
|
||||
import flat from 'flat';
|
||||
import defu from 'defu';
|
||||
|
||||
const RE_KEY_VAL = /^\s*([^\s=]+)\s*=\s*(.*)?\s*$/;
|
||||
const RE_LINES = /\n|\r|\r\n/;
|
||||
const defaults = {
|
||||
name: ".conf",
|
||||
dir: process.cwd(),
|
||||
flat: false
|
||||
};
|
||||
function withDefaults(options) {
|
||||
if (typeof options === "string") {
|
||||
options = { name: options };
|
||||
}
|
||||
return { ...defaults, ...options };
|
||||
}
|
||||
function parse(contents, options = {}) {
|
||||
const config = {};
|
||||
const lines = contents.split(RE_LINES);
|
||||
for (const line of lines) {
|
||||
const match = line.match(RE_KEY_VAL);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const key = match[1];
|
||||
if (!key || key === "__proto__" || key === "constructor") {
|
||||
continue;
|
||||
}
|
||||
const value = destr(match[2].trim());
|
||||
if (key.endsWith("[]")) {
|
||||
const nkey = key.slice(0, Math.max(0, key.length - 2));
|
||||
config[nkey] = (config[nkey] || []).concat(value);
|
||||
continue;
|
||||
}
|
||||
config[key] = value;
|
||||
}
|
||||
return options.flat ? config : flat.unflatten(config, { overwrite: true });
|
||||
}
|
||||
function parseFile(path, options) {
|
||||
if (!existsSync(path)) {
|
||||
return {};
|
||||
}
|
||||
return parse(readFileSync(path, "utf8"), options);
|
||||
}
|
||||
function read(options) {
|
||||
options = withDefaults(options);
|
||||
return parseFile(resolve(options.dir, options.name), options);
|
||||
}
|
||||
function readUser(options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
return read(options);
|
||||
}
|
||||
function serialize(config) {
|
||||
return Object.entries(flat.flatten(config)).map(([key, value]) => `${key}=${typeof value === "string" ? value : JSON.stringify(value)}`).join("\n");
|
||||
}
|
||||
function write(config, options) {
|
||||
options = withDefaults(options);
|
||||
writeFileSync(resolve(options.dir, options.name), serialize(config), {
|
||||
encoding: "utf8"
|
||||
});
|
||||
}
|
||||
function writeUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
write(config, options);
|
||||
}
|
||||
function update(config, options) {
|
||||
options = withDefaults(options);
|
||||
if (!options.flat) {
|
||||
config = flat.unflatten(config, { overwrite: true });
|
||||
}
|
||||
const newConfig = defu(config, read(options));
|
||||
write(newConfig, options);
|
||||
return newConfig;
|
||||
}
|
||||
function updateUser(config, options) {
|
||||
options = withDefaults(options);
|
||||
options.dir = process.env.XDG_CONFIG_HOME || homedir();
|
||||
return update(config, options);
|
||||
}
|
||||
|
||||
export { defaults, parse, parseFile, read, readUser, serialize, update, updateUser, write, writeUser };
|
||||
Reference in New Issue
Block a user