From b4b4b68bac058416e7a34891acaf4bdaca256609 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 10 Aug 2019 01:42:48 -0600 Subject: [PATCH] add some preinstall/postinstall scripts --- .gitignore | 4 ++++ bin-public/telebit | 3 +++ bin-public/telebit.cmd | 1 + scripts/postinstall.js | 49 ++++++++++++++++++++++++++++++++++++++++++ scripts/preinstall.js | 49 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+) create mode 100755 bin-public/telebit create mode 100644 bin-public/telebit.cmd create mode 100644 scripts/postinstall.js create mode 100644 scripts/preinstall.js diff --git a/.gitignore b/.gitignore index 319b82f..3235a44 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ +*.tar.gz +*.zip +telebit-stable-* + node_modules.* *.*.sw* etc/acme/ diff --git a/bin-public/telebit b/bin-public/telebit new file mode 100755 index 0000000..6961975 --- /dev/null +++ b/bin-public/telebit @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +"$(dirname $(dirname $0))/bin/node" "$(dirname $(dirname $0))/bin/telebit.js" "$@" +exit $? diff --git a/bin-public/telebit.cmd b/bin-public/telebit.cmd new file mode 100644 index 0000000..bbbe766 --- /dev/null +++ b/bin-public/telebit.cmd @@ -0,0 +1 @@ +"%~dp0\..\bin\node" "%~dp0\..\bin\telebit.js" %* diff --git a/scripts/postinstall.js b/scripts/postinstall.js new file mode 100644 index 0000000..462eae7 --- /dev/null +++ b/scripts/postinstall.js @@ -0,0 +1,49 @@ +'use strict'; + +// use pathman and serviceman to make telebit ready + +var spawn = require('child_process').spawn; +var os = require('os'); +var path = require('path'); +var ext = /^win/i.test(os.platform()) ? '.exe' : ''; + +function run(bin, args) { + return new Promise(function(resolve, reject) { + var runner = spawn(path.join(bin + ext), args, { + windowsHide: true + }); + runner.stdout.on('data', function(chunk) { + console.info(chunk.toString('utf8')); + }); + runner.stderr.on('data', function(chunk) { + console.error(chunk.toString('utf8')); + }); + runner.on('exit', function(code) { + if (0 !== code) { + reject( + new Error("exited with non-zero status code '" + code + "'") + ); + return; + } + resolve({ code: code }); + }); + }); +} + +run('serviceman', [ + 'add', + '--name', + 'telebit', + '--title', + 'Telebit', + '--rdns', + 'io.telebit.remote.telebit', + path.resolve(__dirname, '..', 'bin', 'telebit.js'), + 'daemon', + '--config', + path.join(os.homedir(), '.config/telebit/telebitd.yml') +]) + .then(function() {}) + .catch(function(e) { + console.error(e.message); + }); diff --git a/scripts/preinstall.js b/scripts/preinstall.js new file mode 100644 index 0000000..c416601 --- /dev/null +++ b/scripts/preinstall.js @@ -0,0 +1,49 @@ +'use strict'; + +// use pathman and serviceman to make telebit ready + +var spawn = require('child_process').spawn; +var os = require('os'); +var path = require('path'); +var ext = /^win/i.test(os.platform()) ? '.exe' : ''; + +function run(bin, args) { + return new Promise(function(resolve, reject) { + var runner = spawn(path.join(bin + ext), args, { + windowsHide: true + }); + var out = ''; + runner.stdout.on('data', function(chunk) { + var txt = chunk.toString('utf8'); + out += txt; + }); + runner.stderr.on('data', function(chunk) { + var txt = chunk.toString('utf8'); + out += txt; + }); + runner.on('exit', function(code) { + if (0 !== code && !/service matching/.test(out)) { + console.error(out); + reject( + new Error("exited with non-zero status code '" + code + "'") + ); + return; + } + resolve({ code: code }); + }); + }); +} + +var confpath = path.join(os.homedir(), '.config/telebit'); +require('mkdirp')(confpath, function(err) { + if (err) { + console.error("Error creating config path '" + confpath + "':"); + console.error(err); + } + // not having a config is fine +}); + +run('serviceman', ['stop', 'telebit']).catch(function(e) { + // TODO ignore + console.error(e.message); +});