Browse Source

update serviceman on npm

tags/v0.7.1
AJ ONeal 5 months ago
parent
commit
8b6479dc95
3 changed files with 140 additions and 60 deletions
  1. 1
    0
      npm/bin/serviceman
  2. 11
    2
      npm/package.json
  3. 128
    58
      npm/scripts/fetch-serviceman.js

+ 1
- 0
npm/bin/serviceman View File

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

+ 11
- 2
npm/package.json View File

@@ -1,10 +1,19 @@
1 1
 {
2 2
 	"name": "serviceman",
3
-	"version": "0.5.2",
3
+	"version": "0.7.0",
4 4
 	"description": "A cross-platform service manager",
5 5
 	"main": "index.js",
6
+	"homepage": "https://git.rootprojects.org/root/serviceman/src/branch/master/npm",
7
+	"files": [
8
+		"bin/",
9
+		"scripts/"
10
+	],
11
+	"bin": {
12
+		"serviceman": "bin/serviceman"
13
+	},
6 14
 	"scripts": {
7
-		"postinstall": "scripts/fetch-serviceman.js",
15
+		"serviceman": "serviceman",
16
+		"postinstall": "node scripts/fetch-serviceman.js",
8 17
 		"test": "echo \"Error: no test specified\" && exit 1"
9 18
 	},
10 19
 	"repository": {

+ 128
- 58
npm/scripts/fetch-serviceman.js View File

@@ -1,6 +1,7 @@
1 1
 #!/usr/bin/env node
2 2
 
3 3
 'use strict';
4
+var path = require('path');
4 5
 var os = require('os');
5 6
 
6 7
 // https://nodejs.org/api/os.html#os_os_arch
@@ -10,7 +11,7 @@ var arch = os.arch(); // process.arch
10 11
 // https://nodejs.org/api/os.html#os_os_platform
11 12
 // 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', 'win32'
12 13
 var platform = os.platform(); // process.platform
13
-var ext = 'windows' === platform ? '.exe' : '';
14
+var ext = /^win/i.test(platform) ? '.exe' : '';
14 15
 
15 16
 // This is _probably_ right. It's good enough for us
16 17
 // https://github.com/nodejs/node/issues/13629
@@ -53,15 +54,26 @@ var mkdirp = require('@root/mkdirp');
53 54
 
54 55
 function needsUpdate(oldVer, newVer) {
55 56
 	// "v1.0.0-pre" is BEHIND "v1.0.0"
56
-	newVer = newVer.replace(/^v/, '').split(/[\.\-\+]/);
57
-	oldVer = oldVer.replace(/^v/, '').split(/[\.\-\+]/);
58
-	//console.log(oldVer, newVer);
57
+	newVer = newVer
58
+		.replace(/^v/, '')
59
+		.split(/[\.\-\+]/)
60
+		.filter(Boolean);
61
+	oldVer = oldVer
62
+		.replace(/^v/, '')
63
+		.split(/[\.\-\+]/)
64
+		.filter(Boolean);
59 65
 
66
+	if (!oldVer.length) {
67
+		return true;
68
+	}
69
+
70
+	// ex: v1.0.0-pre vs v1.0.0
60 71
 	if (newVer[3] && !oldVer[3]) {
61 72
 		// don't install beta over stable
62 73
 		return false;
63 74
 	}
64 75
 
76
+	// ex: old is v1.0.0-pre
65 77
 	if (oldVer[3]) {
66 78
 		if (oldVer[2] > 0) {
67 79
 			oldVer[2] -= 1;
@@ -77,6 +89,8 @@ function needsUpdate(oldVer, newVer) {
77 89
 			return true;
78 90
 		}
79 91
 	}
92
+
93
+	// ex: v1.0.1 vs v1.0.0-pre
80 94
 	if (newVer[3]) {
81 95
 		if (newVer[2] > 0) {
82 96
 			newVer[2] -= 1;
@@ -93,7 +107,7 @@ function needsUpdate(oldVer, newVer) {
93 107
 		}
94 108
 	}
95 109
 
96
-	//console.log(oldVer, newVer);
110
+	// ex: v1.0.1 vs v1.0.0
97 111
 	if (oldVer[0] > newVer[0]) {
98 112
 		return false;
99 113
 	} else if (oldVer[0] < newVer[0]) {
@@ -128,72 +142,128 @@ console.log(false === needsUpdate('0.5.0', '0.5.0-pre1'));
128 142
 console.log(false === needsUpdate('0.5.1', '0.5.0'));
129 143
 */
130 144
 
131
-exec('serviceman version', { windowsHide: true }, function(err, stdout) {
132
-	var oldVer = (stdout || '').split(' ')[0];
133
-	console.log(oldVer, newVer);
134
-	if (!needsUpdate(oldVer, newVer)) {
135
-		console.info(
136
-			'Current serviceman version is new enough:',
137
-			oldVer,
138
-			newVer
139
-		);
140
-		return;
141
-		//} else {
142
-		//	console.info('Current serviceman version is older:', oldVer, newVer);
143
-	}
144
-
145
-	var url = 'https://rootprojects.org/serviceman/dist/{{ .Platform }}/{{ .Arch }}/serviceman{{ .Ext }}'
146
-		.replace(/{{ .Version }}/g, newVer)
147
-		.replace(/{{ .Platform }}/g, platform)
148
-		.replace(/{{ .Arch }}/g, arch)
149
-		.replace(/{{ .Ext }}/g, ext);
150
-
151
-	console.info('Installing from', url);
152
-	return request({ uri: url, encoding: null }, function(err, resp) {
153
-		if (err) {
154
-			console.error(err);
145
+function install(name, bindirs, getVersion, parseVersion, urlTpl) {
146
+	exec(getVersion, { windowsHide: true }, function(err, stdout) {
147
+		var oldVer = parseVersion(stdout);
148
+		//console.log('old:', oldVer, 'new:', newVer);
149
+		if (!needsUpdate(oldVer, newVer)) {
150
+			console.info(
151
+				'Current ' + name + ' version is new enough:',
152
+				oldVer,
153
+				newVer
154
+			);
155 155
 			return;
156
+			//} else {
157
+			//	console.info('Current serviceman version is older:', oldVer, newVer);
156 158
 		}
157 159
 
158
-		//console.log(resp.body.byteLength);
159
-		//console.log(typeof resp.body);
160
-		var serviceman = 'serviceman' + ext;
161
-		return fs.writeFile(serviceman, resp.body, null, function(err) {
160
+		var url = urlTpl
161
+			.replace(/{{ .Version }}/g, newVer)
162
+			.replace(/{{ .Platform }}/g, platform)
163
+			.replace(/{{ .Arch }}/g, arch)
164
+			.replace(/{{ .Ext }}/g, ext);
165
+
166
+		console.info('Installing from', url);
167
+		return request({ uri: url, encoding: null }, function(err, resp) {
162 168
 			if (err) {
163 169
 				console.error(err);
164 170
 				return;
165 171
 			}
166
-			fs.chmodSync(serviceman, parseInt('0755', 8));
167
-
168
-			var path = require('path');
169
-			var localdir = '/usr/local/bin';
170
-			fs.rename(serviceman, path.join(localdir, serviceman), function(
171
-				err
172
-			) {
173
-				if (err) {
174
-					//console.error(err);
175
-				}
176
-				// ignore
177
-			});
178
-
179
-			var homedir = require('os').homedir();
180
-			var bindir = path.join(homedir, '.local', 'bin');
181
-			return mkdirp(bindir, function(err) {
182
-				if (err) {
183
-					console.error(err);
172
+
173
+			//console.log(resp.body.byteLength);
174
+			//console.log(typeof resp.body);
175
+			var bin = name + ext;
176
+			function next() {
177
+				if (!bindirs.length) {
184 178
 					return;
185 179
 				}
186
-
187
-				var localsrv = path.join(bindir, serviceman);
188
-				return fs.writeFile(localsrv, resp.body, function(err) {
180
+				var bindir = bindirs.pop();
181
+				return mkdirp(bindir, function(err) {
189 182
 					if (err) {
190 183
 						console.error(err);
191 184
 						return;
192 185
 					}
193
-					fs.chmodSync(localsrv, parseInt('0755', 8));
194
-					console.info('Wrote', serviceman, 'to', bindir);
186
+
187
+					var localsrv = path.join(bindir, bin);
188
+					return fs.writeFile(localsrv, resp.body, function(err) {
189
+						next();
190
+						if (err) {
191
+							console.error(err);
192
+							return;
193
+						}
194
+						fs.chmodSync(localsrv, parseInt('0755', 8));
195
+						console.info('Wrote', bin, 'to', bindir);
196
+					});
195 197
 				});
196
-			});
198
+			}
199
+			next();
197 200
 		});
198 201
 	});
199
-});
202
+}
203
+
204
+function winstall(name, bindir) {
205
+	try {
206
+		fs.writeFileSync(
207
+			path.join(bindir, name),
208
+			'#!/usr/bin/env bash\n"$(dirname "$0")/serviceman.exe" "$@"\nexit $?'
209
+		);
210
+	} catch (e) {
211
+		// ignore
212
+	}
213
+
214
+	// because bugs in npm + git bash oddities, of course
215
+	// https://npm.community/t/globally-installed-package-does-not-execute-in-git-bash-on-windows/9394
216
+	try {
217
+		fs.writeFileSync(
218
+			path.join(path.join(__dirname, '../../.bin'), name),
219
+			[
220
+				'#!/bin/sh',
221
+				'# manual bugfix patch for npm on windows',
222
+				'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
223
+				'"$basedir/../' + name + '/bin/' + name + '"   "$@"',
224
+				'exit $?'
225
+			].join('\n')
226
+		);
227
+	} catch (e) {
228
+		// ignore
229
+	}
230
+	try {
231
+		fs.writeFileSync(
232
+			path.join(path.join(__dirname, '../../..'), name),
233
+			[
234
+				'#!/bin/sh',
235
+				'# manual bugfix patch for npm on windows',
236
+				'basedir=$(dirname "$(echo "$0" | sed -e \'s,\\\\,/,g\')")',
237
+				'"$basedir/node_modules/' + name + '/bin/' + name + '"   "$@"',
238
+				'exit $?'
239
+			].join('\n')
240
+		);
241
+	} catch (e) {
242
+		// ignore
243
+	}
244
+	// end bugfix
245
+}
246
+
247
+function run() {
248
+	//var homedir = require('os').homedir();
249
+	//var bindir = path.join(homedir, '.local', 'bin');
250
+	var bindir = path.resolve(__dirname, '..', 'bin');
251
+	var name = 'serviceman';
252
+	if ('.exe' === ext) {
253
+		winstall(name, bindir);
254
+	}
255
+
256
+	return install(
257
+		name,
258
+		[bindir],
259
+		'serviceman version',
260
+		function parseVersion(stdout) {
261
+			return (stdout || '').split(' ')[0];
262
+		},
263
+		'https://rootprojects.org/serviceman/dist/{{ .Platform }}/{{ .Arch }}/serviceman{{ .Ext }}'
264
+	);
265
+}
266
+
267
+if (require.main === module) {
268
+	run();
269
+}

Loading…
Cancel
Save