citty icon indicating copy to clipboard operation
citty copied to clipboard

Support `transformCommandToApi` util

Open peterroe opened this issue 2 years ago • 1 comments

Describe the feature

// build.ts
export default defineCommand({
  meta: {
    name: 'build',
    description: 'build command',
  },
  args: {
    minify: {
      type: 'boolean',
      description: 'minify file size',
    },
    mode: {
      type: 'string',
      description: 'environment variable'
    }
  }
})
// api/index.ts

import build from './build'
import { transformCommandToApi } from 'citty'

export const apiBuild = transformCommandToApi(build)

// test.ts

import { apiBuild } from './api'

apiBuild({  // directly invoke with api
  minify: true,
  mode: false
})

Reason

Because defineCommand already defines all the information related to a command, it is very easy to convert it into a direct call to API.

Additional information

  • [X] Would you be willing to help implement this feature?

peterroe avatar Oct 18 '23 11:10 peterroe

This function looks similar to the creadeMain function, but it will be much more convenient to use. If this idea is agreed, I will start a PR. 👀 @pi0

peterroe avatar Oct 19 '23 12:10 peterroe