Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问whistle该怎么整合到项目里面去,也就是项目启动就启动,项目关闭就关闭,还有就是分组设置,api的形式该如何设置 #1164

Open
zifer123 opened this issue Nov 1, 2024 · 2 comments

Comments

@zifer123
Copy link

zifer123 commented Nov 1, 2024

  1. 我想写一个whistle.js,想要npm run dev的时候,同时启动whistle,同时当ctrl + C关闭项目的时候,whistle服务也关闭,或者其他不通过写一个js的方式也行
  2. 对于这种api形式,该如何添加分组,并在分组下新增对应的规则
const startWhistle = require('whistle')
const pkg = require('./package.json')
const kill = require('kill-port')

;(async () => {
  await kill(8899, 'tcp') // 保证运行前8899不被占用

  // 配置whistle启动参数
  const options = {
    port: 8899, // 代理端口,
    rules: {
      [`\nadmin`]: {
        rules: [
          'aa bb',
          'cc dd',
          'gg ee',
          'gh ty',
          'aa z',
        ]
      },
    },
  }

  // 启动whistle服务
  startWhistle(options, () => {
    console.log(`whistle started, you can access: 
      http://127.0.0.1:8899/
    `)
  })
})()

我写了个大概,但都不行,源码部分只看到\n是增加分组

@DiamondYuan
Copy link

import { dirname, join } from 'path'
import whistle from 'whistle'
import fs from 'fs/promises'

interface IPluginOption {
  resolveConfig: (option: { port: number }) => Promise<{
    port: number
    uat: string
    pre: string
  }>
}

export default function vitePluginWhistle(options: IPluginOption) {
  return {
    name: 'vite-plugin-whistle',
    async configureServer(server) {
      server.httpServer.on('listening', async () => {
        const baseDir = join(process.cwd(), 'whistle')
        const port = server.httpServer.address().port

        const pluginConfig = await options.resolveConfig({ port })

        const whistleOptions = {
          name: 'whistle-project',
          port: pluginConfig.port,
          rules: {
            uat: pluginConfig.uat,
            pre: pluginConfig.pre
          },
          baseDir
        }

        const backupFiles = [
          '.whistle/rules/properties',
          '.whistle/rules/files/0.uat',
          '.whistle/rules/files/1.pre'
        ]

        const cache = new Map()
        for (const element of backupFiles) {
          try {
            const content = await fs.readFile(join(baseDir, element), 'utf-8')
            cache.set(element, content)
          } catch (error) {
            //
          }
        }
        await fs.rm(baseDir, { recursive: true, force: true })
        for (const [key, value] of cache) {
          await fs.mkdir(dirname(join(baseDir, key)), { recursive: true })
          await fs.writeFile(join(baseDir, key), value)
        }
        whistle(whistleOptions)
      })
    }
  }
}

可以参考这个 vite 插件

@zifer123
Copy link
Author

zifer123 commented Nov 1, 2024

@DiamondYuan 好的,非常感谢,我先参考研究一下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants