2.0.0 โ€ข Published 5 months ago

path-typegen v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

path-typegen

CI Status npm version codecov

path-typegen is a library that converts file paths to TypeScript types

๐Ÿ“ฆ Installation

npm i path-typegen

๐Ÿš€ Usage

๐Ÿ”ฐ ph

Provides flexible path type processing using method chaining with map and filter.

Basic

// ๐Ÿ“ฆ src/components/
//  โ”ฃ ๐Ÿ“‚Button
//  โ”ƒ โ”— ๐Ÿ“œindex.tsx
//  โ”ฃ ๐Ÿ“‚Card
//  โ”ƒ โ”— ๐Ÿ“œindex.tsx

ph('./src/components', './src/types/components.ts')
  .write()

// ./src/types/components.ts
export type PathType = './src/components/Button/index.tsx' | './src/components/Card/index.tsx'

Processing

Uses method chaining with map() and filter() for path transformations.

  • All operations are lazily evaluated until write() is called.
  • Use typed tagged template function for object type transformation
// ๐Ÿ“ฆ src/pages/
//  โ”ฃ ๐Ÿ“œposts/[id].tsx
//  โ”— ๐Ÿ“œusers/[id]/settings.tsx

const paths = ph('./src/pages', './src/types/routes.ts')
  .filter(path => path.endsWith('.tsx')) // Lazy evaluation, Iterable is not consumed
  .map(path => typed`{
    path: ${path},
    params: {
      id: number,
    }
  }`) // Lazy evaluation, Iterable is still not consumed
  .write(prettier.format) // Iterable is consumed

// ./src/types/routes.ts
export type PathType = 
  | {
      path: './src/pages/posts/[id].tsx',
      params: {
        id: number
      }
    } 
  | {
      path: './src/pages/users/[id]/settings.tsx',
      params: {
        id: number
      }
    }

Configuration

ph('./src/components', './src/types/components.ts')
  .setConfig({
    typeName: 'ComponentPaths',
    annotation: '/** @generated This is auto-generated type */\n'
  })
  .write()

// ./src/types/components.ts
/** @generated This is auto-generated type */
export type ComponentPaths = './src/components/Button/index.tsx' | './src/components/Card/index.tsx'

๐Ÿ’ป 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 from your input files.

Options:

  • -i, --input โ€” Input file path
  • -o, --output โ€” Output file path
# Generate TypeScript types
path-typegen generate -i ./assets/images -o ./ImageType.ts
2.0.0

5 months ago

2.0.0-alpha.3

5 months ago

2.0.0-alpha.2

5 months ago

2.0.0-alpha.1

5 months ago

2.0.0-alpha.0

5 months ago

1.2.0

7 months ago

1.1.0

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago