Skip to content

Commit e592807

Browse files
committed
export CommonJs require, update version/usage, refactor installer
1 parent 09f0b99 commit e592807

File tree

6 files changed

+50
-34
lines changed

6 files changed

+50
-34
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ A `spawning` cross-platform version of Node's child_process.`spawn` that returns
1717
- `fork` another **script**, a _Node Js_ module instance, for additional sub processing base on `stderr` or `stdout` events.
1818
- pass additional callback for the `message` event.
1919

20+
A series of general use case `strick` type checkers.
21+
2022
## Install
2123

2224
```sh
@@ -132,6 +134,14 @@ import { where } from 'node-sys';
132134
let found = where('node');
133135
```
134136

137+
### Use CommonJS `require` like before in ESM
138+
139+
```js
140+
import { require } from 'node-sys';
141+
142+
const package = require('package');
143+
```
144+
135145
### CLI Usage
136146

137147
```s

bin/installer.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/usr/bin/env node
22

3-
import { packager, installer, where } from '../index.js';
3+
import {
4+
packager, installer, where,
5+
spawning, isNull, isMac, isWindows
6+
} from '../index.js';
47
import minimist from 'minimist';
58
import {
69
createRequire
710
} from 'module';
8-
import { spawn } from 'child_process';
11+
912
import { dirname, join } from 'path';
1013
import { fileURLToPath } from 'url';
1114

@@ -70,30 +73,16 @@ if (argv.help || argv.h) {
7073
} else if (argv.version || argv.v) {
7174
console.log(pack.version);
7275
} else if ((argv.get || argv.g)
73-
&& (
74-
(process.platform == 'darwin' && where('brew') === null)
75-
|| (process.platform == 'win32' && where('choco') === null)
76-
)) {
77-
let installOutput = '';
78-
const child = spawn(CMD[process.platform], [SYSTEM[process.platform]], {
79-
stdio: 'pipe',
80-
});
81-
82-
child.on('close', () => {
83-
return installOutput;
84-
});
85-
86-
child.on('exit', () => {
87-
return installOutput;
88-
});
89-
90-
child.stdout.on('data', (data) => {
91-
installOutput += data.toString();
92-
});
93-
94-
child.stderr.on('data', (data) => {
95-
throw (data.toString());
96-
});
76+
&& ((isMac() && isNull(where('brew')))
77+
|| (isWindows() && isNull(where('choco'))))
78+
) {
79+
spawning(CMD[process.platform], [SYSTEM[process.platform]])
80+
.then(function () {
81+
console.log('Finish!')
82+
})
83+
.catch(function (err) {
84+
console.error(err);
85+
})
9786
} else if (argv) {
9887
let args = argv._;
9988
let packages = args.shift();

index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import which from 'which';
44
import { spawn, fork } from 'child_process';
55
import { dirname, join } from 'path';
66
import { fileURLToPath } from 'url';
7+
import { createRequire } from 'module';
78

89
const __filename = fileURLToPath(
910
import.meta.url);
@@ -45,6 +46,8 @@ const SYS_MANAGERS = {
4546
shell: ['powershell'],
4647
};
4748

49+
function Sys() { }
50+
4851
function sysManager() {
4952
let managers = SYS_MANAGERS[process.platform];
5053
if (!managers || !managers.length) {
@@ -452,8 +455,12 @@ export const isMac = Sys.isMac = function () {
452455
return process.platform === 'darwin';
453456
}
454457

455-
function Sys() { }
458+
/**
459+
* Include an `CommonJS` module the old way.
460+
*
461+
* @param {string} module
462+
*/
463+
export const require = createRequire(import.meta.url);
456464

457465
export default Sys;
458-
459466
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "node-sys",
3-
"version": "1.1.2",
4-
"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, run as administrator.",
3+
"version": "1.1.3",
4+
"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, and run as administrator. A series of strick type checkers.",
55
"type": "module",
66
"main": "index.js",
77
"bin": {
@@ -48,7 +48,8 @@
4848
"child_process",
4949
"promisify",
5050
"administrator",
51-
"sudo"
51+
"sudo",
52+
"require"
5253
],
5354
"author": "l. stubbs <[email protected]>",
5455
"contributors": [

test/test.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import chai from 'chai';
2-
import Sys, { isLinux, isMac, isWindows } from '../index.js';
2+
import Sys from '../index.js';
33
import {
44
Readable
55
} from 'stream';
66
import {
77
packager, installer,
8-
where, spawning, System
8+
where, spawning, System,
9+
isLinux, isMac, isWindows, require
910
} from '../index.js';
1011

1112
const expect = chai.expect;
@@ -372,6 +373,14 @@ describe('Method: `where`', function () {
372373
});
373374
});
374375

376+
describe('Method: `require`', function () {
377+
it('should return included module like CommonJs', function (done) {
378+
const which = require('which');
379+
expect(Sys.isFunction(which)).to.equal(true);
380+
done();
381+
});
382+
});
383+
375384
describe('Function: `Sys`', function () {
376385
it('should instanced itself like a class', function () {
377386
const sys = new Sys();

0 commit comments

Comments
 (0)