@unraven/env 1.0.0
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/README.md +1 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.js +6 -0
- package/package.json +38 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# Env
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
interface EnvFlags {
|
|
3
|
+
override: boolean;
|
|
4
|
+
ignore: boolean;
|
|
5
|
+
commented: boolean;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/formatters/types.d.ts
|
|
9
|
+
interface Groupable {
|
|
10
|
+
name: string;
|
|
11
|
+
}
|
|
12
|
+
interface Formattable {
|
|
13
|
+
name: string;
|
|
14
|
+
value: string | null;
|
|
15
|
+
flags?: EnvFlags;
|
|
16
|
+
comments?: string[];
|
|
17
|
+
}
|
|
18
|
+
interface FormatterContract {
|
|
19
|
+
format(items: Formattable[], options?: object): string;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/formatters/base.formatter.d.ts
|
|
23
|
+
declare abstract class BaseFormatter {
|
|
24
|
+
protected type: string;
|
|
25
|
+
constructor(type: string);
|
|
26
|
+
static sort<T extends Groupable>(items: T[]): T[];
|
|
27
|
+
protected toSorted<T extends Groupable>(items: T[]): T[];
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/formatters/dotenv.formatter.d.ts
|
|
31
|
+
interface EnvFormatterOptions {
|
|
32
|
+
headers?: string[];
|
|
33
|
+
eol?: string;
|
|
34
|
+
}
|
|
35
|
+
declare class DotenvFormatter extends BaseFormatter implements FormatterContract {
|
|
36
|
+
group<T extends Groupable>(items: T[]): T[][];
|
|
37
|
+
format(items: Formattable[], options?: EnvFormatterOptions): string;
|
|
38
|
+
private resolvePrefix;
|
|
39
|
+
private escapeValue;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/formatters/json.formatter.d.ts
|
|
43
|
+
declare class JsonFormatter extends BaseFormatter implements FormatterContract {
|
|
44
|
+
format(items: Formattable[]): string;
|
|
45
|
+
private asObject;
|
|
46
|
+
private asArray;
|
|
47
|
+
}
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region src/formatters/yaml.formatter.d.ts
|
|
50
|
+
declare class YamlFormatter extends BaseFormatter implements FormatterContract {
|
|
51
|
+
format(items: Formattable[]): string;
|
|
52
|
+
private escapeValue;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/formatter.d.ts
|
|
56
|
+
declare class Formatter<T extends Record<string, new (type: string) => FormatterContract>, Format extends Extract<keyof T, string> = Extract<keyof T, string>> {
|
|
57
|
+
private formatters;
|
|
58
|
+
constructor(formatters: T);
|
|
59
|
+
use<Type extends Format>(type: Type): InstanceType<T[Type]>;
|
|
60
|
+
detect(file: string): string;
|
|
61
|
+
format<Type extends Format>(type: Type, items: Formattable[], options?: Parameters<InstanceType<T[Type]>['format']>[1]): string;
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/index.d.ts
|
|
65
|
+
declare const formatter: Formatter<{
|
|
66
|
+
dotenv: typeof DotenvFormatter;
|
|
67
|
+
json: typeof JsonFormatter;
|
|
68
|
+
json_array: typeof JsonFormatter;
|
|
69
|
+
yaml: typeof YamlFormatter;
|
|
70
|
+
}, "dotenv" | "json" | "json_array" | "yaml">;
|
|
71
|
+
//#endregion
|
|
72
|
+
export { formatter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
var e=class{constructor(e){this.formatters=e}use(e){if(!this.formatters[e])throw Error(`Invalid format "${e}".`);return new this.formatters[e](e)}detect(e){switch(e.split(`.`).slice(1).pop()?.toLowerCase()??``){case`json`:return`json`;case`yml`:case`yaml`:return`yaml`;default:return`dotenv`}}format(e,t,n={}){return this.use(e).format(t,n)}},t=class e{constructor(e){this.type=e}static sort(e){return e.toSorted((e,t)=>e.name.localeCompare(t.name))}toSorted(t){return e.sort(t)}};const n=[`NUXT`,`NUXT_PUBLIC`,`NEXT`,`NEXT_PUBLIC`,`VITE`];var r=class extends t{group(e){let t={},n=this.toSorted(e);for(let e of n){let{common:n,prefix:r}=this.resolvePrefix(e.name),i=t[r];i||(i={items:[],common:n,name:r},t[r]=i),i.items.push(e)}return Object.values(t).sort((e,t)=>e.common.localeCompare(t.common)).map(e=>e.items)}format(e,t={}){let n=[],r=t.eol??`
|
|
2
|
+
`,i=t.headers??[];i.length>0&&n.push(...i,``);let a=this.group(e);for(let e of a){for(let t of e){n.push(...t.comments??[]);let e=Object.entries(t.flags??{}).filter(([e,t])=>t&&e!==`commented`);if(e.length>0){let t=e.map(([e])=>`@${e}`).join(` `);n.push(`# ${t}`)}let r=`${t.name}=${this.escapeValue(t.value??``)}`;t.flags?.commented&&(r=`# ${r}`),n.push(r)}n.push(``)}return n.join(r)}resolvePrefix(e){let t=e.split(`_`),r=[];do r.push(t.shift());while(t.length>0&&n.includes(r.join(`_`)));return{prefix:r.join(`_`),common:r.slice(0,r.length-1).join(`_`),name:r.slice(r.length-1).join(`_`)}}escapeValue(e){return/[ \n\r\t"'\\]/.test(e)?`"${e.replace(/\\/g,`\\\\`).replace(/"/g,`\\"`).replace(/\n/g,`\\n`).replace(/\r/g,`\\r`).replace(/\t/g,`\\t`)}"`:e}},i=class extends t{format(e){let t=this.toSorted(e),n;switch(this.type){case`json_array`:n=this.asArray(t);break;default:n=this.asObject(t);break}return JSON.stringify(n,null,2)}asObject(e){return e.reduce((e,t)=>(e[t.name]=t.value,e),{})}asArray(e){return e.map(e=>({name:e.name,value:e.value}))}};const a=new e({dotenv:r,json:i,json_array:i,yaml:class extends t{format(e){return this.toSorted(e).map(e=>`${e.name}: ${this.escapeValue(e.value??``)}`).join(`
|
|
3
|
+
`)}escapeValue(e){return e.includes(`
|
|
4
|
+
`)?`|\n${e.split(`
|
|
5
|
+
`).map(e=>` ${e}`).join(`
|
|
6
|
+
`)}`:/^\d/.test(e)||/[\n\r\t:#[\]{}&*!|>'"@`]/.test(e)||e.trim()!==e?`"${e.replace(/"/g,`\\"`)}"`:e}}});export{a as formatter};
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@unraven/env",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Environment utilities for Unraven projects.",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"source": "./src/index.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"imports": {
|
|
19
|
+
"#/*": "./src/*.js"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@outloud/eslint-config": "^3.0.0",
|
|
23
|
+
"@types/node": "^24.10.0",
|
|
24
|
+
"eslint": "^10.1.0",
|
|
25
|
+
"release-it": "^19.0.5",
|
|
26
|
+
"tsdown": "^0.15.12",
|
|
27
|
+
"typescript": "~5.8",
|
|
28
|
+
"vitest": "^1.5.0"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"dev": "vitest dev",
|
|
32
|
+
"test": "vitest --config .config/vitest.config.ts",
|
|
33
|
+
"build": "tsdown --config .config/tsdown.config.ts",
|
|
34
|
+
"lint": "eslint .",
|
|
35
|
+
"typecheck": "tsc --project tsconfig.build.json --noEmit",
|
|
36
|
+
"release": "release-it --config .config/release-it.json"
|
|
37
|
+
}
|
|
38
|
+
}
|