Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 56f0d4ed3c
Fetching contributors…

Cannot retrieve contributors at this time

executable file 121 lines (111 sloc) 4.239 kb
#!/usr/bin/env node
var fs = require("fs"),
exec = require("child_process").exec,
sequence = require("sequence")(),
program = require("commander"),
path = require("path");
String.prototype.supplant = function (o) {
return this.replace(/{([^{}]*)}/g,
function (a, b) {
var r = o[b];
return typeof r === "string" || typeof r === "number" ? r : a;
}
);
};
program
.version(require("../package.json").version)
.option("-i, --install", "install the node application as a windows service")
.option("-r, --remove", "remove the windows service for the node application")
.option("-x, --stop", "stop the service before uninstalling")
.option("-s, --silent", "supress any information in the console")
.option("-c, --confirmation", "ask for confirmation before installing/uninstalling")
.option("-p, --path [path]", "path to the node application you want to install as a service [current directory]", process.cwd())
.parse(process.argv);
function log(message){
!program.silent && console.log(message);
}
if (!program.install && !program.remove){
process.stdout.write(program.helpInformation());
process.exit();
return;
}
if (process.platform !== "win32"){
log("Winser can only install services on windows.");
process.exit();
return;
}
if (!(fs.existsSync||path.existsSync)(path.join(program.path, "package.json"))){
log(program.path + " doesn't seems to be a node application path.\nIt doesn't contains a package.json file.");
process.exit();
return;
}
var appName = require(path.join(process.cwd(), "package.json")).name;
sequence
.then(function(next){
exec("whoami /groups | findstr /b BUILTIN\\Administrators | findstr /c:\"Enabled group\"", function(err, r){
if(r.length === 0){
log("You must run this tool as an administrator");
process.exit();
}else{
next();
}
});
})
.then(function(next){
if (!program.confirmation) {
next();
}else{
var message = program.install ? "continue installing " + appName + " as a service? " :
"continue uninstalling the " + appName + " service? ";
program.confirm(message, function(ok){
process.stdin.pause(); //i think this program.confirm thing doesnt close the stdin properly
if(ok){
next();
}else{
process.exit();
}
});
}
})
.then(function(next){
if(!program.stop || program.install){
next();
return;
}
exec("net stop " + appName, next);
})
.then(function(next){
exec("where npm.cmd", function(err, r){
var npmPath = r.trim().split("\r\n")[0];
next(err, npmPath);
});
})
.then(function(next, err, npmLocation){
if(program.install){
exec('\"{binfolder}\\nssm.exe\" install {serviceName} "{npmLocation}" start "\\"{appFolder}\\""'.supplant({
binfolder: __dirname,
serviceName: appName,
npmLocation: npmLocation,
appFolder: fs.realpathSync(".")
}), function(err,stdout, stderr){
if(stderr){
!program.silent && console.error(stderr);
}else{
log("The program {serviceName} was installed as a service.".supplant({serviceName: appName}));
}
next();
});
}else if(program.remove){
exec("\"{binfolder}\\nssm.exe\" remove {serviceName} confirm".supplant({
binfolder: __dirname,
serviceName: appName
}), function(err,stdout, stderr){
if(stderr){
!program.silent && console.error(stderr);
}else{
log("The service for {serviceName} was removed.".supplant({serviceName: appName}));
}
next();
});
}
});
Jump to Line
Something went wrong with that request. Please try again.