7 个版本
0.3.4 | 2023 年 10 月 22 日 |
---|---|
0.3.3 | 2023 年 8 月 5 日 |
0.3.1 | 2023 年 7 月 29 日 |
0.2.0 | 2023 年 7 月 18 日 |
0.1.0 | 2023 年 7 月 16 日 |
1026 在 命令行工具
每月 49 次下载
49KB
1.5K SLoC
ntro
一个用于检查配置文件并输出 TypeScript 类型声明的 cli 工具。
功能
.yaml -> .d.ts
# filename: test.multiple.yaml
calling-birds:
- huey
- dewey
- louie
- fred
doe: "a deer, a female deer"
french-hens: 3
pi: 3.14159
ray: "a drop of golden sun"
xmas: true
xmas-fifth-day:
calling-birds: four
french-hens: 3
golden-rings: 5
partridges:
count: 1
location: "a pear tree"
turtle-doves: two
---
calling-birds:
- huey
- dewey
- louie
- fred
doe: "a deer, a female deer"
---
hello: world
变为
declare namespace TestMultiple {
export type Document0 = {
"calling-birds": ["huey", "dewey", "louie", "fred"];
doe: "a deer, a female deer";
"french-hens": 3;
pi: 3.14159;
ray: "a drop of golden sun";
xmas: true;
"xmas-fifth-day": {
"calling-birds": "four";
"french-hens": 3;
"golden-rings": 5;
partridges: { count: 1; location: "a pear tree" };
"turtle-doves": "two";
};
};
export type Document1 = {
"calling-birds": ["huey", "dewey", "louie", "fred"];
doe: "a deer, a female deer";
};
export type Document2 = { hello: "world" };
export type All = [Document0, Document1, Document2];
}
用法
Usage: ntro yaml [OPTIONS] <SOURCE_FILE>
Arguments:
<SOURCE_FILE> Path to a yaml file
Options:
-o <OUTPUT_DIR> Set the output directory, to where to save the *.d.ts file
-q, --quiet Disable logs
-h, --help Print help
.env -> .ts
NAME="value"
NEXT_PUBLIC_KEY="yoa",
# notice the type hint below
# @type 'a' | 'b'
NAME2=value
KEY = "value" # asdfa
keys= 'city'
# another type hint
# @type string
keys2 ='city'
*.d.ts
declare namespace NodeJS {
interface ProcessEnv {
KEY?: string;
NAME?: string;
NAME2?: string;
NEXT_PUBLIC_KEY: string;
keys?: string;
keys2?: string;
}
}
您还可以使用 zod 生成一个解析环境变量的 TypeScript 模块,具有额外的类型安全性。优点是类型提示注释被收集并用于定义 zod 模式。
import z, { ZodTypeAny } from "zod";
const clientEnvSchemas = {
NEXT_PUBLIC_KEY: z.string(),
};
const serverEnvSchemas = {
...clientEnvSchemas,
NAME: z.string(),
NAME2: z.enum(["a", "b"]),
keys: z.string(),
keys2: z.string(),
};
//... more implementation details are generated
用法
Usage: ntro dotenv [OPTIONS] [SOURCE_FILES]...
Arguments:
[SOURCE_FILES]... Path(s) to some .env files
Options:
-o <OUTPUT_DIR> Set the output directory, to where to save the env.d.ts file
-q, --quiet Disable logs
-z, --zod Generate a typescript module implementing a zod schema for env variables
-w, --watch Wath for changes in the source files and rerun
-p, --set-ts-config-path-alias Update the project's tsconfig.json to include a path alias to the env.parsed.ts module that holds the zod schemas
-h, --help Print help
依赖关系
~6–16MB
~215K SLoC