@ilokesto/path-codegen
v0.0.2
Published
A CLI tool to generate path constants from a directory structure.
Readme
path-codegen
A simple CLI tool to generate a nested path object from a directory structure.
Description
This tool scans a given directory and creates a TypeScript/JavaScript file that exports a constant object containing all the paths within that directory. This allows you to reference your project's assets and files in a type-safe way, preventing typos and errors from broken paths.
The generated object mirrors the directory structure, and the keys are derived from the file and directory names.
Usage
You can run this tool directly using npx without any installation.
npx path-codegen <source-directory> [options]Arguments
<source-directory>: (Required) The path to the directory you want to scan.
Options
-o, --output <file>: The name for the generated output file. (Default:path-codegen.ts)
Example
Given a directory structure like this:
assets/
├── images/
│ ├── logo.png
│ └── icons/
│ └── home.svg
└── README.mdRunning the following command:
npx path-codegen assets -o src/generated/paths.tsWill generate the following file at src/generated/paths.ts:
// This file is auto-generated by path-codegen. Do not edit.
export const ASSETS = {
README: "/README.md",
images: {
icons: {
home: "/icons/home.svg"
},
logo: "/images/logo.png"
}
} as const;