Пример #1
0
  return new Promise((resolve, reject) => {
    const verbose = argv.verbose
    const context = argv.context
    const cwd = context.cwd
    const staticPaths = context.staticPaths || {}
    const appInfo = context.appInfo
    const existsPackageJson = fs.existsSync(path.join(cwd, './package.json'))
    let hasEntry = false
    let pathToApp
    let serviceName
    let customError

    console.log('Platform is ' + platform)

    if (platform !== 'win32') {
      console.log('Installing app as windows service only works on windows platforms..')
      console.log('Installing jsreport as startup service for your platform should be described at http://jsreport.net/downloads')

      return resolve({
        installed: false,
        serviceName: null
      })
    }

    if (!existsPackageJson && !appInfo) {
      customError = new Error('To install app as windows service you need a package.json file')
      customError.cleanState = true
      return reject(customError)
    }

    if (appInfo) {
      if (!appInfo.path) {
        customError = new Error('To install app as windows service you need to pass "path" in appInfo')
        customError.cleanState = true
        return reject(customError)
      }

      pathToApp = appInfo.path

      if (!appInfo.name) {
        customError = new Error('To install app as windows service you need to pass "name" in appInfo')
        customError.cleanState = true
        return reject(customError)
      }

      serviceName = appInfo.name

      if (appInfo.startcmd) {
        hasEntry = true
      }

      if (!hasEntry) {
        customError = new Error('To install app as windows service you need to pass "startcmd" in appInfo')
        customError.cleanState = true
        return reject(customError)
      }
    } else {
      pathToApp = cwd

      const userPkg = require(path.join(pathToApp, './package.json'))

      if (!userPkg.name) {
        customError = new Error('To install app as windows service you need a "name" field in package.json file')
        customError.cleanState = true
        return reject(customError)
      }

      serviceName = userPkg.name

      if (userPkg.scripts && userPkg.scripts.start) {
        hasEntry = true
      } else if (userPkg.main) {
        hasEntry = true
      }

      if (!hasEntry) {
        customError = new Error('To install app as windows service you need to have a "start" script or a "main" field in package.json file')
        customError.cleanState = true
        return reject(customError)
      }
    }

    console.log('Installing windows service "' + serviceName + '" for app..')

    let winser

    if (appInfo) {
      winser = Winser({ silent: !verbose, nssmPath: staticPaths.nssm, app: appInfo })
    } else {
      winser = Winser({ silent: !verbose, nssmPath: staticPaths.nssm })
    }

    winser.install({
      path: pathToApp,
      env: ['NODE_ENV=' + (process.env.NODE_ENV != null ? process.env.NODE_ENV : 'development')],
      autostart: true
    }, (error) => {
      if (error) {
        error.message = `Error while trying to install windows service: ${error.message}`
        return reject(error)
      }

      console.log('Service "' + serviceName + '" is running.')

      resolve({
        installed: true,
        serviceName: serviceName
      })
    })
  })
Пример #2
0
  return new Promise((resolve, reject) => {
    const verbose = argv.verbose
    const context = argv.context
    const cwd = context.cwd
    const staticPaths = context.staticPaths || {}
    const appInfo = context.appInfo
    const existsPackageJson = fs.existsSync(path.join(cwd, './package.json'))
    let pathToApp
    let serviceName
    let customError

    console.log('Platform is ' + platform)

    if (platform !== 'win32') {
      console.log('Unstalling windows service for app only works on windows platforms..')

      return resolve({
        uninstalled: false,
        serviceName: null
      })
    }

    if (!existsPackageJson && !appInfo) {
      customError = new Error('To uninstall windows service for app you need a package.json file')
      customError.cleanState = true
      return reject(customError)
    }

    if (appInfo) {
      if (!appInfo.path) {
        customError = new Error('To uninstall windows service for app you need to pass "path" in appInfo')
        customError.cleanState = true
        return reject(customError)
      }

      pathToApp = appInfo.path

      if (!appInfo.name) {
        customError = new Error('To uninstall windows service for app you need to pass "name" in appInfo')
        customError.cleanState = true
        return reject(customError)
      }

      serviceName = appInfo.name
    } else {
      pathToApp = cwd

      const userPkg = require(path.join(pathToApp, './package.json'))

      if (!userPkg.name) {
        customError = new Error('To uninstall windows service for app you need a "name" field in package.json file')
        customError.cleanState = true
        return reject(customError)
      }

      serviceName = userPkg.name
    }

    console.log('Uninstalling windows service "' + serviceName + '".')

    let winser

    if (appInfo) {
      winser = Winser({ silent: !verbose, nssmPath: staticPaths.nssm, app: appInfo })
    } else {
      winser = Winser({ silent: !verbose, nssmPath: staticPaths.nssm })
    }

    winser.remove({
      path: pathToApp,
      stop: true
    }, (error) => {
      if (error) {
        error.message = `Error while trying to uninstall windows service: ${error.message}`
        return reject(error)
      }

      console.log('Windows service "' + serviceName + '" uninstalled')

      resolve({
        uninstalled: true,
        serviceName: serviceName
      })
    })
  })