Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 54f68c6fad
Fetching contributors…

Cannot retrieve contributors at this time

90 lines (84 sloc) 3.015 kB
#!/usr/bin/env node
var fs = require("fs"),
exec = require('child_process').exec,
sequence = require('sequence')(),
program = require('commander'),
os = require("os");
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('0.0.4')
.option('-i, --install', 'install the node application as a windows service')
.option('-r, --remove', 'remove the windows service for the node application')
.parse(process.argv);
if(!program.install && !program.remove){
process.stdout.write(program.helpInformation());
process.exit(0);
return;
}
sequence
.then(function(next){
exec("whoami /groups | findstr /b BUILTIN\\Administrators | findstr /c:\"Enabled group\"", function(err, r){
if(r.length === 0){
console.error("You must run this tool as an administrator");
process.exit();
}else{
next();
}
});
})
.then(function(next){
if(os.platform() !== "win32"){
console.error("This program only works on win32 platforms.");
process.exit();
return;
}
next();
})
.then(fs.readFile.bind(fs,'package.json'))
.then(function(next, err, buffer){
next(null, JSON.parse(buffer.toString()));
})
.then(function(next, err, pkg){
exec("where npm", function(err, r){
var npmPath = r.trim().split("\r\n").filter(function(npm){
return npm.substr(-4) === ".cmd";
})[0];
next(err, pkg, npmPath);
});
})
.then(function(next, err, pkg, npmLocation){
if(program.install){
exec("{binfolder}\\nssm.exe install {serviceName} \"{npmLocation}\" start \"{appFolder}\"".supplant({
binfolder: __dirname,
serviceName: pkg.name,
npmLocation: npmLocation,
appFolder: fs.realpathSync(".")
}), function(err,stdout, stderr){
if(stderr){
console.error(stderr);
}else{
console.log("The program {serviceName} was installed as a service.".supplant({serviceName: pkg.name}));
}
next();
});
}else if(program.remove){
exec("{binfolder}\\nssm.exe remove {serviceName} confirm".supplant({
binfolder: __dirname,
serviceName: pkg.name
}), function(err,stdout, stderr){
if(stderr){
console.error(stderr);
}else{
console.log("The service for {serviceName} was removed.".supplant({serviceName: pkg.name}));
}
next();
});
}
});
Jump to Line
Something went wrong with that request. Please try again.