1

我想启动一个 vue-cli 开发服务器,然后在此构建完成后从节点运行命令。

有没有办法vue-cli-service serve从节点脚本运行,以便在开发服务器启动并运行后我可以接收回调?

4

1 回答 1

4

vue-cli-service.js不返回服务承诺(这将允许您设置完成回调),但您可以复制脚本(25 行)来执行此操作:

我的-vue-cli-service.js:

#!/usr/bin/env node
const semver = require('semver')
const { error } = require('@vue/cli-shared-utils')
const requiredVersion = require('@vue/cli-service/package.json').engines.node

if (!semver.satisfies(process.version, requiredVersion)) {
  error(
    `You are using Node ${process.version}, but vue-cli-service ` +
    `requires Node ${requiredVersion}.\nPlease upgrade your Node version.`
  )
  process.exit(1)
}

const Service = require('@vue/cli-service')
const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd())

const rawArgv = process.argv.slice(2)
const args = require('minimist')(rawArgv)
const command = args._[0]

module.exports = () =>  // <--- add this line to return the service promise
  service.run(command, args, rawArgv).catch(err => {
    error(err)
    process.exit(1)
  })

然后,您可以创建另一个使用此副本的脚本,添加一个then在服务器成功启动时调用的回调:

我的服务.js:

#!/usr/bin/env node
const service = require('./my-vue-cli-service')

service().then(result => {
  if (result && result.server) {  // <--- server started successfully if result.server exists
    const {server, url} = result;
    console.log('ready');
  }
})

请务必更新package.json以使用新脚本:

包.json:

{
  "scripts": {
    "serve": "./my-service.js serve"
  }
}
于 2018-09-02T15:25:31.693 回答