UNPKG

winsvc

Version:

一个专门用来在Windows系统上以服务方式运行NodeJS脚本的工具。 A tool designed to run NodeJS scripts in service mode on Windows system. ## 安装(Installation) ``` npm install -g winsvc ``` ## 用法(Usage) 安装服务(Install Service) ``` winsvc -i ServiceName ScriptPath ``` 卸载服务(Uni

47 lines (45 loc) 1.74 kB
#!/usr/bin/env node const info = require('./package.json'); console.log(`winsvc@${info.version}`); const process = require('process'); const arguments = process.argv.splice(2); const opt = arguments[0]; const serviceName = arguments[1]; const scriptPath = arguments.slice(2).join(' '); const install = opt == '-i' && serviceName && scriptPath; const uninstall = opt == '-u' && serviceName; if (!install && !uninstall) { console.log('usage:'); console.log(' winsvc -i serviceName scriptPath'); console.log(' winsvc -u serviceName'); return; } const util = require('util'); const decoder = new util.TextDecoder('gbk'); function print(data) { console.log(decoder.decode(data)); } const { spawn } = require('child_process'); if (uninstall) { const stopCmd = spawn('sc', ['stop', serviceName]); stopCmd.stdout.on('data', print); stopCmd.stderr.on('data', print); stopCmd.on('close', () => { const deleteCmd = spawn('sc', ['delete', serviceName]); deleteCmd.stdout.on('data', print); deleteCmd.stderr.on('data', print); }); } else if (install) { const path = require('path'); const binPath = path.join(__dirname, 'bin', 'winsvc.exe'); const params = path.resolve(scriptPath); console.log('install', binPath, params); const createCmd = spawn('sc', ['create', serviceName, 'binPath=', `"${binPath}" "${params}"`, 'start=', 'auto']); createCmd.stdout.on('data', print); createCmd.stderr.on('data', print); createCmd.on('close', () => { const startCmd = spawn('sc', ['start', serviceName]); startCmd.stdout.on('data', print); startCmd.stderr.on('data', print); }); }