config2ts
v2.5.2
Published
convert config to ts file.
Readme
config2ts
convert config to ts file.
install
run command: npm install -g config2ts
how to use
config2ts -d config -o dist -n csv.ts -a public
support type
| csv field | typescript type | | :--------: | :---------------: | | Index | string | | String | string | | Number | number | | Boolean | boolean | | Enum | type | | EnumIndex | type | | String[] | string[] | | Number[] | number[] | | Enum[] | type[] | | Ref | namespace.Record | | RefEnum | namespace.type | | RefEnum[] | namespace.type[] | | Object | type (Record) |
Numbersupport Infinity and NaNEnumsupport empty string typeEnum[]do not include empty string typeEnumIndexwill generate index type, and will useEnumtype to generate interfaceRef/RefEnumreference another csv's Record/type by idObjectparsekey:value,key:valueformat, auto-infer value types (number/boolean/string), generates a dedicatedtypethat merges all keys
assets2ts
Scan assets directory and generate an assets.ts index file with nested resource tree.
-a, --assets <path>set assets resource directory. default:public- Output file:
assets.ts(alongside the merged config file) - Directory/filenames are converted to camelCase keys
typefield uses file extension (lowercase), e.g.'png','mp3','svg'- Directories containing 2+ files of the same extension get a dedicated type (e.g.
PngAsset,Mp3Asset) andas Record<string, XxxAsset>annotation - Directories with ≤1 file or mixed extensions do not get a type annotation
- Nested directories are supported (e.g.
public/sub/image/)
generated example
// Auto Generated by config2ts, DO NOT EDIT
export type Mp3Asset = { path: string; type: 'mp3' };
export type PngAsset = { path: string; type: 'png' };
export type SvgAsset = { path: string; type: 'svg' };
export const ASSETS = {
public: {
image: {
adjustHorizontal: {path:'public/image/adjust-horizontal.png',type:'png'},
direction: {path:'public/image/direction.png',type:'png'}
} satisfies Record<string, PngAsset>,
music: {
effect1: {path:'public/music/effect1.mp3',type:'mp3'},
effect2: {path:'public/music/effect2.mp3',type:'mp3'}
} satisfies Record<string, Mp3Asset>,
singleFile: {
effect1: {path:'public/single-file/effect1.mp3',type:'mp3'}
}
}
};usage
import { ASSETS } from "./assets";
const meta = ASSETS.public.image.adjustHorizontal;
// meta.path → 'public/image/adjust-horizontal.png'
// meta.type → 'png'Options
Options:
-h, --help output usage information
-V, --version output the version number
-d, --dir <path> set convert path. default: ./
-o, --outDir <path> set outDir path. default: ./
-n, --name <name> output file name. default: csv.ts
-a, --assets <path> set assets resource directory for asset index. default: publicsupported file formats
| format | extension | | :----: | :-------: | | csv | .csv | | ini | .ini | | toml | .toml |
