为什么捐赠
API 浏览器
联系站长
提供 CLI 命令

Quasar CLI App Extensions 的一个强大功能是可以提供命令,让用户像使用 Quasar CLI 命令一样运行它们。

用法

让我们看看如何注册一个命令,使其可以通过 quasar run <ext-id> <cmd> [...args] 来运行。我们将编辑 Index 脚本

File: /ae/src/index.js (or .ts)

import { defineIndexScript } from '@quasar/app-vite'

export default defineIndexScript(api => {
  /**
   * @param {string} commandName
   * @param {function} fn
   *   (processArgv: string[]) => ?Promise
   */
  api.registerCommand('start', processArgv => {
    // 在这里执行操作
    // 这注册了 "start" 命令
    // 当运行以下命令时会执行此处理函数
    // $ quasar run <ext-id> start
  })
})

定义和解析参数的示例:

File: /ae/src/index.js (or .ts)

import { defineIndexScript } from '@quasar/app-vite'
import { parseArgs } from 'node:util'

export default defineIndexScript(api => {
  /**
   * 用户可以在宿主应用中运行:
   *   quasar run <ext-id> fun --name Gigi -d
   */
  api.registerCommand('fun', () => {
    try {
      const { values, positionals } = parseArgs({
        options: {
          name: { type: 'string', short: 'n' },
          debug: { type: 'boolean' }
        },
        strict: true,
        allowPositionals: true
      })

      console.log(values, positionals)
    } catch (err) {
      console.error(err.message)
    }
  })
})