Skip to content

Commit b371b67

Browse files
committed
update bc version for callback property name change
- added general type checking functions - update readme - corrections
1 parent a8033d4 commit b371b67

File tree

5 files changed

+296
-35
lines changed

5 files changed

+296
-35
lines changed

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ console.log('To fully install a `pandoc` package run: ' + sys.installer + ' pand
4242

4343
### Install `vim` package onto host, using system's default package manager
4444

45-
* Returns a Promise
45+
* Returns a `Promise`
4646

4747
```js
4848
import { installer } from 'node-sys';
49-
installer('vim')
49+
50+
// Progress callback for any output doing installation.
51+
// Any value returned in `callback` will be the final resolved output result.
52+
const onprogress = (object) => {
53+
console.log(object.output);
54+
}
55+
56+
installer('vim', onprogress)
5057
.then(function(data){
5158
// returns installation output
5259
console.log(data);
@@ -56,7 +63,9 @@ installer('vim')
5663
});
5764
```
5865

59-
## API - spawning(command, arguments, progressOptions, options)
66+
## API - `spawning`(command, arguments, progressOptions, options)
67+
68+
`import { spawning } from 'node-sys';`
6069

6170
`Spawning` takes an additional argument, `progressOptions`, its [`options`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) are the same as those of `child_process.spawn` plus:
6271

@@ -72,17 +81,36 @@ installer('vim')
7281

7382
*The progress callback will receive an object with these properties:*
7483

75-
* `handle:` *Object* - Spawned child process instance handler.
84+
* `spawn:` *Object* - Spawned child process instance handle.
7685
* Access the child process object.
7786

7887
* `output:` *String* - Output from stdout.
7988
* Output can be altered and if returned will replace the otherwise resolved result.
8089

81-
* `fork:` *Object* - An additional [forked](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options) Node Js process handler, IPC communication channel.
90+
* `fork:` *Object* - An additional [forked](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options) Node Js process handle, IPC communication channel.
8291
* Execute additional processing base off of sub child process output, with module a script.
8392

8493
If there's an error running the child process, received data on stderr, or errors in progress callback, `spawning` rejects the returned promise.
8594

95+
### General type `check` functions
96+
97+
```js
98+
import {
99+
isArray,
100+
isUndefined,
101+
isBuffer,
102+
isArrayBuffer,
103+
isString,
104+
isNumber,
105+
isObject,
106+
isObjectOnly,
107+
isBlob,
108+
isFunction,
109+
isDate,
110+
isStream
111+
} from 'node-sys';
112+
```
113+
86114
### CLI Usage
87115

88116
```s

index.js

Lines changed: 147 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,26 +153,18 @@ export const where = Sys.where = function (executable) {
153153
}
154154

155155
/**
156-
* Determine if a value is a Function
157-
*
158-
* @param {Object} value The value to test
159-
* @returns {boolean} True if value is a Function, otherwise false
160-
*/
161-
function isFunction(value) {
162-
return Object.prototype.toString.call(value) === '[object Function]';
163-
}
164-
165-
/**
166-
* Spawn subprocess with `Promise` features, and `progress` callback for `stdout.on('data') `event.
156+
* Spawn subprocess with `Promise` features, pass callbacks for `.on('data')` events, with ability to run as admin.
167157
*
168158
* @param {String} command - platform command
169159
* @param {Array} argument - command arguments
170-
* @param {Function|Object} progressOptions - either callback for `stdout.on('data')` event or spawn options.
160+
* @param {Function|Object} progressOptions - either callback for `stdout.on('data')` event or `options`.
161+
* - the callback will received an object:
171162
*```js
172-
* { handle: object, output: string }
163+
* spawn: object, // child process **spawn** `instance`.
164+
* output: string, // any **output** data.
165+
* fork: object, // if created, child process **fork** `instance`.
173166
*```
174-
* - the callback will received an object, child process `instance` **handle**, and any **output** data.
175-
* - any returns will be the **`resolve()` .then()** handler.
167+
* - any `return` is the **`resolve()` .then()** result.
176168
* @param {Object} options - Any child process `spawn` options, defaults: stdio: 'pipe'.
177169
* - Additionally:
178170
*```js
@@ -194,8 +186,10 @@ export const spawning = Sys.spawning = function (command, argument, progressOpti
194186
onmessage: null,
195187
};
196188

189+
options.stdio = options.stdio || 'pipe';
190+
const forked = isString(options.fork) ? fork(options.fork) : null;
197191
let progress = progressOptions;
198-
if (typeof progressOptions == 'object' && !isFunction(progressOptions))
192+
if (isObjectOnly(progressOptions))
199193
options = Object.assign(options, progressOptions);
200194

201195
if (isFunction(options.onprogress))
@@ -204,13 +198,9 @@ export const spawning = Sys.spawning = function (command, argument, progressOpti
204198
let error = null;
205199
let output = null;
206200
let sudo = options.sudo || false;
207-
let forking = options.fork || null;
208201
let onerror = options.onerror || null;
209202
let onmessage = options.onmessage || null;
210203

211-
if (typeof forking == 'string')
212-
var forked = fork(forking);
213-
214204
delete options.sudo;
215205
delete options.fork;
216206
delete options.onerror;
@@ -236,21 +226,25 @@ export const spawning = Sys.spawning = function (command, argument, progressOpti
236226
return reject(error, code);
237227
});
238228

239-
spawned.on('exit', () => {
229+
spawned.on('exit', (code) => {
240230
if (forked)
241231
setTimeout(() => {
242232
forked.kill();
243233
}, 1000);
244234

245-
return resolve(output);
235+
if (code === 0) {
236+
return resolve(output);
237+
}
238+
239+
return reject(error, code);
246240
});
247241

248242
spawned.stdout.on('data', (data) => {
249243
let input = data.toString();
250244
output += input;
251245
try {
252246
if (isFunction(progress)) {
253-
output = progress({ handle: spawned, output: input, fork: forked }) || output;
247+
output = progress({ spawn: spawned, output: input, fork: forked }) || output;
254248
}
255249
} catch (e) {
256250
return reject(e.toString());
@@ -283,6 +277,136 @@ export const spawning = Sys.spawning = function (command, argument, progressOpti
283277
});
284278
}
285279

280+
let toString = Object.prototype.toString;
281+
282+
/**
283+
* Determine if a value is an Array.
284+
*
285+
* @param {Object} value The value to test.
286+
* @returns {boolean} True if value is an Array, otherwise false.
287+
*/
288+
export const isArray = Sys.isArray = function (value) {
289+
return toString.call(value) === '[object Array]';
290+
}
291+
292+
/**
293+
* Determine if a value is undefined.
294+
*
295+
* @param {Object} value The value to test.
296+
* @returns {boolean} True if the value is undefined, otherwise false.
297+
*/
298+
export const isUndefined = Sys.isUndefined = function (value) {
299+
return typeof value === 'undefined';
300+
}
301+
302+
/**
303+
* Determine if a value is a Buffer.
304+
*
305+
* @param {Object} value The value to test.
306+
* @returns {boolean} True if value is a Buffer, otherwise false.
307+
*/
308+
export const isBuffer = Sys.isBuffer = function (value) {
309+
return value !== null && !isUndefined(value) && value.constructor !== null && !isUndefined(value.constructor) &&
310+
typeof value.constructor.isBuffer === 'function' && value.constructor.isBuffer(value);
311+
}
312+
313+
/**
314+
* Determine if a value is an ArrayBuffer.
315+
*
316+
* @param {Object} value The value to test.
317+
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
318+
*/
319+
export const isArrayBuffer = Sys.isArrayBuffer = function (value) {
320+
return toString.call(value) === '[object ArrayBuffer]';
321+
}
322+
323+
/**
324+
* Determine if a value is a String.
325+
*
326+
* @param {Object} value The value to test.
327+
* @returns {boolean} True if value is a String, otherwise false.
328+
*/
329+
export const isString = Sys.isString = function (value) {
330+
return typeof value === 'string';
331+
}
332+
333+
/**
334+
* Determine if a value is a Number.
335+
*
336+
* @param {Object} value The value to test.
337+
* @returns {boolean} True if value is a Number, otherwise false.
338+
*/
339+
export const isNumber = Sys.isNumber = function (value) {
340+
return typeof value === 'number';
341+
}
342+
343+
/**
344+
* Determine if a value is an Object
345+
*
346+
* @param {Object} value The value to test.
347+
* @returns {boolean} True if value is an Object, otherwise false.
348+
*/
349+
export const isObject = Sys.isObject = function (value) {
350+
return value !== null && typeof value === 'object';
351+
}
352+
353+
/**
354+
* Determine if a value is only a Object, not an `Array` or `Function`.
355+
*
356+
* @param {Object} value The value to test.
357+
* @return {boolean} True if value is a `Object` only, otherwise false.
358+
*/
359+
export const isObjectOnly = Sys.isObjectOnly = function (value) {
360+
if (toString.call(value) !== '[object Object]') {
361+
return false;
362+
}
363+
364+
let prototype = Object.getPrototypeOf(value);
365+
return prototype === null || prototype === Object.prototype;
366+
}
367+
368+
/**
369+
* Determine if a value is a Blob
370+
*
371+
* @param {Object} value The value to test
372+
* @returns {boolean} True if value is a Blob, otherwise false
373+
*/
374+
export const isBlob = Sys.isBlob = function (value) {
375+
return toString.call(value) === '[object Blob]';
376+
}
377+
378+
/**
379+
* Determine if a value is a Function
380+
*
381+
* @param {Object} value The value to test
382+
* @returns {boolean} True if value is a Function, otherwise false
383+
*/
384+
export const isFunction = Sys.isFunction = function (value) {
385+
return toString.call(value) === '[object Function]';
386+
}
387+
388+
/**
389+
* Determine if a value is a Date
390+
*
391+
* @param {Object} value The value to test
392+
* @returns {boolean} True if value is a Date, otherwise false
393+
*/
394+
export const isDate = Sys.isDate = function (value) {
395+
return toString.call(value) === '[object Date]';
396+
}
397+
398+
/**
399+
* Determine if a value is a Stream
400+
*
401+
* @param {Object} value The value to test
402+
* @returns {boolean} True if value is a Stream, otherwise false
403+
*/
404+
export const isStream = Sys.isStream = function (value) {
405+
return isObject(value) && isFunction(value.pipe);
406+
}
407+
286408
function Sys() { }
287409

288410
export default Sys;
411+
412+
export const System = Sys;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-sys",
3-
"version": "1.0.6",
3+
"version": "1.1.0",
44
"description": "Universal package installer, get the command for managing packages, or auto install any package, using one command for all platforms. Automate the installation of macOS Brew, and Windows Chocolatey package managers. A promisify child process of spawn.",
55
"type": "module",
66
"main": "index.js",

0 commit comments

Comments
 (0)