update serviceman on npm

This commit is contained in:
AJ ONeal 2019-08-09 01:20:17 -06:00
parent 3513b64aa7
commit 8b6479dc95
3 changed files with 139 additions and 59 deletions

1
npm/bin/serviceman Normal file
View File

@ -0,0 +1 @@
# this will be replaced by the postinstall script

View File

@ -1,10 +1,19 @@
{ {
"name": "serviceman", "name": "serviceman",
"version": "0.5.2", "version": "0.7.0",
"description": "A cross-platform service manager", "description": "A cross-platform service manager",
"main": "index.js", "main": "index.js",
"homepage": "https://git.rootprojects.org/root/serviceman/src/branch/master/npm",
"files": [
"bin/",
"scripts/"
],
"bin": {
"serviceman": "bin/serviceman"
},
"scripts": { "scripts": {
"postinstall": "scripts/fetch-serviceman.js", "serviceman": "serviceman",
"postinstall": "node scripts/fetch-serviceman.js",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {

View File

@ -1,6 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; 'use strict';
var path = require('path');
var os = require('os'); var os = require('os');
// https://nodejs.org/api/os.html#os_os_arch // https://nodejs.org/api/os.html#os_os_arch
@ -10,7 +11,7 @@ var arch = os.arch(); // process.arch
// https://nodejs.org/api/os.html#os_os_platform // https://nodejs.org/api/os.html#os_os_platform
// 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32' // 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32'
var platform = os.platform(); // process.platform var platform = os.platform(); // process.platform
var ext = 'windows' === platform ? '.exe' : ''; var ext = /^win/i.test(platform) ? '.exe' : '';
// This is _probably_ right. It's good enough for us // This is _probably_ right. It's good enough for us
// https://github.com/nodejs/node/issues/13629 // https://github.com/nodejs/node/issues/13629
@ -53,15 +54,26 @@ var mkdirp = require('@root/mkdirp');
function needsUpdate(oldVer, newVer) { function needsUpdate(oldVer, newVer) {
// "v1.0.0-pre" is BEHIND "v1.0.0" // "v1.0.0-pre" is BEHIND "v1.0.0"
newVer = newVer.replace(/^v/, '').split(/[\.\-\+]/); newVer = newVer
oldVer = oldVer.replace(/^v/, '').split(/[\.\-\+]/); .replace(/^v/, '')
//console.log(oldVer, newVer); .split(/[\.\-\+]/)
.filter(Boolean);
oldVer = oldVer
.replace(/^v/, '')
.split(/[\.\-\+]/)
.filter(Boolean);
if (!oldVer.length) {
return true;
}
// ex: v1.0.0-pre vs v1.0.0
if (newVer[3] && !oldVer[3]) { if (newVer[3] && !oldVer[3]) {
// don't install beta over stable // don't install beta over stable
return false; return false;
} }
// ex: old is v1.0.0-pre
if (oldVer[3]) { if (oldVer[3]) {
if (oldVer[2] > 0) { if (oldVer[2] > 0) {
oldVer[2] -= 1; oldVer[2] -= 1;
@ -77,6 +89,8 @@ function needsUpdate(oldVer, newVer) {
return true; return true;
} }
} }
// ex: v1.0.1 vs v1.0.0-pre
if (newVer[3]) { if (newVer[3]) {
if (newVer[2] > 0) { if (newVer[2] > 0) {
newVer[2] -= 1; newVer[2] -= 1;
@ -93,7 +107,7 @@ function needsUpdate(oldVer, newVer) {
} }
} }
//console.log(oldVer, newVer); // ex: v1.0.1 vs v1.0.0
if (oldVer[0] > newVer[0]) { if (oldVer[0] > newVer[0]) {
return false; return false;
} else if (oldVer[0] < newVer[0]) { } else if (oldVer[0] < newVer[0]) {
@ -128,12 +142,13 @@ console.log(false === needsUpdate('0.5.0', '0.5.0-pre1'));
console.log(false === needsUpdate('0.5.1', '0.5.0')); console.log(false === needsUpdate('0.5.1', '0.5.0'));
*/ */
exec('serviceman version', { windowsHide: true }, function(err, stdout) { function install(name, bindirs, getVersion, parseVersion, urlTpl) {
var oldVer = (stdout || '').split(' ')[0]; exec(getVersion, { windowsHide: true }, function(err, stdout) {
console.log(oldVer, newVer); var oldVer = parseVersion(stdout);
//console.log('old:', oldVer, 'new:', newVer);
if (!needsUpdate(oldVer, newVer)) { if (!needsUpdate(oldVer, newVer)) {
console.info( console.info(
'Current serviceman version is new enough:', 'Current ' + name + ' version is new enough:',
oldVer, oldVer,
newVer newVer
); );
@ -142,7 +157,7 @@ exec('serviceman version', { windowsHide: true }, function(err, stdout) {
// console.info('Current serviceman version is older:', oldVer, newVer); // console.info('Current serviceman version is older:', oldVer, newVer);
} }
var url = 'https://rootprojects.org/serviceman/dist/{{ .Platform }}/{{ .Arch }}/serviceman{{ .Ext }}' var url = urlTpl
.replace(/{{ .Version }}/g, newVer) .replace(/{{ .Version }}/g, newVer)
.replace(/{{ .Platform }}/g, platform) .replace(/{{ .Platform }}/g, platform)
.replace(/{{ .Arch }}/g, arch) .replace(/{{ .Arch }}/g, arch)
@ -157,43 +172,98 @@ exec('serviceman version', { windowsHide: true }, function(err, stdout) {
//console.log(resp.body.byteLength); //console.log(resp.body.byteLength);
//console.log(typeof resp.body); //console.log(typeof resp.body);
var serviceman = 'serviceman' + ext; var bin = name + ext;
return fs.writeFile(serviceman, resp.body, null, function(err) { function next() {
if (err) { if (!bindirs.length) {
console.error(err);
return; return;
} }
fs.chmodSync(serviceman, parseInt('0755', 8)); var bindir = bindirs.pop();
var path = require('path');
var localdir = '/usr/local/bin';
fs.rename(serviceman, path.join(localdir, serviceman), function(
err
) {
if (err) {
//console.error(err);
}
// ignore
});
var homedir = require('os').homedir();
var bindir = path.join(homedir, '.local', 'bin');
return mkdirp(bindir, function(err) { return mkdirp(bindir, function(err) {
if (err) { if (err) {
console.error(err); console.error(err);
return; return;
} }
var localsrv = path.join(bindir, serviceman); var localsrv = path.join(bindir, bin);
return fs.writeFile(localsrv, resp.body, function(err) { return fs.writeFile(localsrv, resp.body, function(err) {
next();
if (err) { if (err) {
console.error(err); console.error(err);
return; return;
} }
fs.chmodSync(localsrv, parseInt('0755', 8)); fs.chmodSync(localsrv, parseInt('0755', 8));
console.info('Wrote', serviceman, 'to', bindir); console.info('Wrote', bin, 'to', bindir);
}); });
}); });
}
next();
}); });
}); });
}); }
function winstall(name, bindir) {
try {
fs.writeFileSync(
path.join(bindir, name),
'#!/usr/bin/env bash\n"$(dirname "$0")/serviceman.exe" "$@"\nexit $?'
);
} catch (e) {
// ignore
}
// because bugs in npm + git bash oddities, of course
// https://npm.community/t/globally-installed-package-does-not-execute-in-git-bash-on-windows/9394
try {
fs.writeFileSync(
path.join(path.join(__dirname, '../../.bin'), name),
[
'#!/bin/sh',
'# manual bugfix patch for npm on windows',
'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
'"$basedir/../' + name + '/bin/' + name + '" "$@"',
'exit $?'
].join('\n')
);
} catch (e) {
// ignore
}
try {
fs.writeFileSync(
path.join(path.join(__dirname, '../../..'), name),
[
'#!/bin/sh',
'# manual bugfix patch for npm on windows',
'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
'"$basedir/node_modules/' + name + '/bin/' + name + '" "$@"',
'exit $?'
].join('\n')
);
} catch (e) {
// ignore
}
// end bugfix
}
function run() {
//var homedir = require('os').homedir();
//var bindir = path.join(homedir, '.local', 'bin');
var bindir = path.resolve(__dirname, '..', 'bin');
var name = 'serviceman';
if ('.exe' === ext) {
winstall(name, bindir);
}
return install(
name,
[bindir],
'serviceman version',
function parseVersion(stdout) {
return (stdout || '').split(' ')[0];
},
'https://rootprojects.org/serviceman/dist/{{ .Platform }}/{{ .Arch }}/serviceman{{ .Ext }}'
);
}
if (require.main === module) {
run();
}