path-typegen
v1.2.0
Published
Typegen for path - Powered by JSON Schema
Downloads
91
Maintainers
Readme
path-typegen
path-typegen is a library that converts file paths to TypeScript types and JSON Schema
Installation
npm i path-typegen
Usage
writeTS
- Generates path structure to Typescript
Basic
// 📦 src/assets/
// ┣ 📂bar
// ┃ ┗ 📜a.png
// ┣ 📂foo
// ┃ ┗ 📜b.jpeg
writeTS('./src/assets', './src/imageType.ts');
// ./src/imageType.ts
export type ImageType = './src/assets/bar/a.png' | './src/assets/foo/b.jpeg'
With options
// 📦 src/pages/posts/
// ┣ 📂[id]
// ┃ ┗ 📜index.tsx
writeTS('./src/pages/posts', './src/pathType.ts', {
description: 'Wrap your navigation Component',
typeName: 'PathType',
replacer: (path: string) => path.split('/').slice(1, -1).join('/'),
output: {
type: 'object',
pattern: /(?<=\[)[^\]]*(?=\])/g; // [<PICK>]
},
});
// ./src/pathType.ts
/**
* Wrap your navigation Component
*/
export type PathType = {
path: "src/pages/posts/[id]";
params: {
id: string;
};
};
writeSchema
- Generates path structure to JSON Schema
- You can use the same Options type that was used in writeTS.
// 📦 src/assets/
// ┣ 📂bar
// ┃ ┗ 📜a.png
// ┣ 📂foo
// ┃ ┗ 📜b.jpeg
writeSchema('./src/assets', './src/imageSchema.json');
// ./src/imageSchema.json
{
"title": "ImageSchema",
"type": "string",
"enum": [
"./src/assets/bar/a.png",
"./src/assets/foo/b.jpeg",
],
"description": "@summary this file was automatically generated by path-typegen"
}
CLI
Options
- -h, --help — Prints help information
- -V, --version — Prints version information
Command
init
command
Creates a new configuration file (path-typegen.config.cjs) in your project root with default settings. Types are provided through JSDoc annotations for better DX
path-typegen init
generate
command
Generates TypeScript types or JSON Schema from your input files.
Options:
- -i, --input — Input file path
- -o, --output — Output file path
- -s, --schema — Generate JSON Schema instead of TypeScript (default: false)
# Generate TypeScript types
path-typegen generate -i ./assets/images -o ./ImageType.ts
# Generate JSON Schema
path-typegen generate -i ./assets/images -o ./ImageSchema.json --schema