Compare commits
2 Commits
3e412d5090
...
647dc9a2ec
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 647dc9a2ec | |
Derrick Hammer | 7c631f3af5 |
|
@ -5,80 +5,69 @@
|
|||
*/
|
||||
/// <reference types="node" />
|
||||
export interface Options {
|
||||
suffix?: string;
|
||||
fallback?: string;
|
||||
alias?: object;
|
||||
suffix?: string;
|
||||
fallback?: string;
|
||||
alias?: object;
|
||||
}
|
||||
export interface LoadOptions {
|
||||
hash?: string | boolean;
|
||||
query?: string | boolean;
|
||||
env?: object | boolean;
|
||||
argv?: string[] | boolean;
|
||||
config?: string | boolean;
|
||||
hash?: string | boolean;
|
||||
query?: string | boolean;
|
||||
env?: object | boolean;
|
||||
argv?: string[] | boolean;
|
||||
config?: string | boolean;
|
||||
}
|
||||
/**
|
||||
* Config Parser
|
||||
*/
|
||||
export default class Config {
|
||||
private module;
|
||||
private prefix;
|
||||
private suffix?;
|
||||
private fallback?;
|
||||
private options;
|
||||
private alias;
|
||||
private data;
|
||||
private env;
|
||||
private args;
|
||||
private argv;
|
||||
private pass;
|
||||
private query;
|
||||
private hash;
|
||||
constructor(module: string, options?: Options);
|
||||
private init;
|
||||
inject(options: object): void;
|
||||
load(options: LoadOptions): void;
|
||||
open(file: string): void;
|
||||
openDir(dir: string): void;
|
||||
openJson(file: string): void;
|
||||
saveConfigJson(file: string, data: object): void;
|
||||
filter(name: string): Config;
|
||||
set(key: string, value: any): void;
|
||||
has(key: string): boolean;
|
||||
get(key: string, fallback?: any): any;
|
||||
typeOf(
|
||||
key: string
|
||||
):
|
||||
| "string"
|
||||
| "number"
|
||||
| "bigint"
|
||||
| "boolean"
|
||||
| "symbol"
|
||||
| "undefined"
|
||||
| "object"
|
||||
| "function"
|
||||
| "null";
|
||||
str(key: string, fallback?: any): any;
|
||||
int(key: any, fallback?: any): any;
|
||||
uint(key: any, fallback?: any): any;
|
||||
float(key: any, fallback?: any): any;
|
||||
ufloat(key: any, fallback?: any): any;
|
||||
fixed(key: any, exp: any, fallback?: any): any;
|
||||
ufixed(key: any, exp: any, fallback?: any): any;
|
||||
bool(key: any, fallback?: any): any;
|
||||
buf(key: string, fallback?: any, enc?: BufferEncoding): any;
|
||||
array(key: string, fallback?: any): any;
|
||||
obj(key: string, fallback?: any): any;
|
||||
func(key: string, fallback?: any): any;
|
||||
path(key: string, fallback?: any): any;
|
||||
mb(key: string, fallback?: any): any;
|
||||
getSuffix(): any;
|
||||
getPrefix(): string;
|
||||
getFile(file: string): any;
|
||||
location(file: string): string;
|
||||
parseConfig(text: string): void;
|
||||
parseArg(argv?: string[]): void;
|
||||
parseEnv(env?: object): void;
|
||||
parseQuery(query: string): void | {};
|
||||
parseHash(hash: string): void | {};
|
||||
parseForm(query: string, ch: string, map: object): void;
|
||||
private module;
|
||||
private prefix;
|
||||
private suffix?;
|
||||
private fallback?;
|
||||
private options;
|
||||
private alias;
|
||||
private data;
|
||||
private env;
|
||||
private args;
|
||||
private argv;
|
||||
private pass;
|
||||
private query;
|
||||
private hash;
|
||||
constructor(module: string, options?: Options);
|
||||
private init;
|
||||
inject(options: object): void;
|
||||
load(options: LoadOptions): void;
|
||||
open(file: string): void;
|
||||
openDir(dir: string): void;
|
||||
openJson(file: string): void;
|
||||
saveConfigJson(file: string, data: object): void;
|
||||
filter(name: string): Config;
|
||||
set(key: string, value: any): void;
|
||||
has(key: string): boolean;
|
||||
get(key: string, fallback?: any): any;
|
||||
typeOf(key: string): "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null";
|
||||
str(key: string, fallback?: any): any;
|
||||
int(key: any, fallback?: any): any;
|
||||
uint(key: any, fallback?: any): any;
|
||||
float(key: any, fallback?: any): any;
|
||||
ufloat(key: any, fallback?: any): any;
|
||||
fixed(key: any, exp: any, fallback?: any): any;
|
||||
ufixed(key: any, exp: any, fallback?: any): any;
|
||||
bool(key: any, fallback?: any): any;
|
||||
buf(key: string, fallback?: any, enc?: BufferEncoding): any;
|
||||
array(key: string, fallback?: any): any;
|
||||
obj(key: string, fallback?: any): any;
|
||||
func(key: string, fallback?: any): any;
|
||||
path(key: string, fallback?: any): any;
|
||||
mb(key: string, fallback?: any): any;
|
||||
getSuffix(): any;
|
||||
getPrefix(): string;
|
||||
getFile(file: string): any;
|
||||
location(file: string): string;
|
||||
parseConfig(text: string): void;
|
||||
parseArg(argv?: string[]): void;
|
||||
parseEnv(env?: object): void;
|
||||
parseQuery(query: string): void | {};
|
||||
parseHash(hash: string): void | {};
|
||||
parseForm(query: string, ch: string, map: object): void;
|
||||
}
|
||||
|
|
|
@ -135,6 +135,9 @@ class Config {
|
|||
(0, bsert_1.default)(typeof data === "object");
|
||||
(0, bsert_1.default)(!Array.isArray(data));
|
||||
const fullPath = path_1.default.join(this.str("configdir"), file);
|
||||
if (!fs_1.default.existsSync(fullPath)) {
|
||||
fs_1.default.mkdirSync(fullPath, { recursive: true });
|
||||
}
|
||||
fs_1.default.writeFileSync(fullPath, JSON.stringify(data));
|
||||
this.openJson(fullPath);
|
||||
}
|
||||
|
|
10
src/index.ts
10
src/index.ts
|
@ -80,6 +80,7 @@ export default class Config {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public inject(options: object) {
|
||||
for (const key of Object.keys(options)) {
|
||||
const value = options[key];
|
||||
|
@ -174,6 +175,10 @@ export default class Config {
|
|||
|
||||
const fullPath = Path.join(this.str("configdir"), file);
|
||||
|
||||
if (!fs.existsSync(fullPath)) {
|
||||
fs.mkdirSync(fullPath, { recursive: true });
|
||||
}
|
||||
|
||||
fs.writeFileSync(fullPath, JSON.stringify(data));
|
||||
this.openJson(fullPath);
|
||||
}
|
||||
|
@ -317,6 +322,7 @@ export default class Config {
|
|||
|
||||
return value;
|
||||
}
|
||||
|
||||
int(key, fallback = null) {
|
||||
const value = this.get(key);
|
||||
|
||||
|
@ -398,6 +404,7 @@ export default class Config {
|
|||
|
||||
return num;
|
||||
}
|
||||
|
||||
ufloat(key, fallback = null) {
|
||||
const value = this.float(key);
|
||||
if (value === null) {
|
||||
|
@ -424,6 +431,7 @@ export default class Config {
|
|||
throw new Error(`${fmt(key)} must be a fixed number.`);
|
||||
}
|
||||
}
|
||||
|
||||
ufixed(key, exp, fallback = null) {
|
||||
const value = this.fixed(key, exp);
|
||||
|
||||
|
@ -473,6 +481,7 @@ export default class Config {
|
|||
|
||||
throw new Error(`${fmt(key)} must be a boolean.`);
|
||||
}
|
||||
|
||||
buf(key: string, fallback = null, enc: BufferEncoding = "hex") {
|
||||
const value = this.get(key);
|
||||
|
||||
|
@ -875,6 +884,7 @@ export default class Config {
|
|||
|
||||
return this.parseForm(query, "?", this.query);
|
||||
}
|
||||
|
||||
public parseHash(hash: string) {
|
||||
if (typeof hash !== "string") {
|
||||
if (!global.location) {
|
||||
|
|
Loading…
Reference in New Issue