Skip to content

Commit fbb219a

Browse files
committed
refactor!: convert to esm
1 parent b965ffa commit fbb219a

File tree

11 files changed

+134
-143
lines changed

11 files changed

+134
-143
lines changed

README.md

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,26 @@ Or
2424

2525
Like in the native NodeJS API, the callback is not required in case you wish to work with the returned child stream. The "sshOrNull" and "command" arguments are also facultative because they could be provided respectively as the "ssh" and "command" property of the options object.
2626

27-
Valid `options` properties are:
28-
29-
- `ssh`
30-
SSH connection if the command must run remotely
31-
- `command`
32-
Command to run unless provided as first argument
33-
- `cwd`
34-
Current working directory
35-
- `end`
36-
Close the SSH connection on exit, default to true if an ssh connection instance is provided.
37-
- `env`
38-
An environment to use for the execution of the command.
39-
- `pty`
40-
Set to true to allocate a pseudo-tty with defaults, or an object containing specific pseudo-tty settings. Apply only to SSH remote commands.
41-
- `cwd`
42-
Apply only to local commands.
43-
- `uid`
44-
Apply only to local commands.
45-
- `gid`
46-
Apply only to local commands.
27+
Valid `options` properties are:
28+
29+
- `ssh`
30+
SSH connection if the command must run remotely
31+
- `command`
32+
Command to run unless provided as first argument
33+
- `cwd`
34+
Current working directory
35+
- `end`
36+
Close the SSH connection on exit, default to true if an ssh connection instance is provided.
37+
- `env`
38+
An environment to use for the execution of the command.
39+
- `pty`
40+
Set to true to allocate a pseudo-tty with defaults, or an object containing specific pseudo-tty settings. Apply only to SSH remote commands.
41+
- `cwd`
42+
Apply only to local commands.
43+
- `uid`
44+
Apply only to local commands.
45+
- `gid`
46+
Apply only to local commands.
4747

4848
See the [ssh2] and [ssh2-connect] modules on how to create a new SSH connection.
4949

@@ -66,10 +66,10 @@ If the exit code is not `0`, the thrown error object contains the `stdout`, `std
6666
A command, a configuration object and a callback:
6767

6868
```js
69-
connect = require('ssh2-connect');
70-
exec = require('ssh2-exec');
71-
connect({host: localhost}, function(err, ssh){
72-
exec('ls -la', {ssh: ssh}, (err, stdout, stderr, code){
69+
import { connect } from "ssh2-connect";
70+
import { exec } from "ssh2-exec";
71+
connect({ host: localhost }, (err, ssh) => {
72+
exec("ls -la", { ssh: ssh }, (err, stdout, stderr, code) => {
7373
console.info(stdout, stderr, code);
7474
});
7575
});
@@ -78,25 +78,28 @@ connect({host: localhost}, function(err, ssh){
7878
A configuration object with a ssh2 connection and working a the return child object:
7979

8080
```js
81-
connect = require('ssh2-connect');
82-
exec = require('ssh2-exec');
83-
connect({host: localhost}, function(err, ssh){
84-
child = exec({
85-
command: 'ls -la',
86-
ssh: ssh
87-
}, function(err, stdout, stderr, code){
88-
console.info(stdout);
89-
});
90-
child.stdout.on('data', function(data){
81+
import { connect } from "ssh2-connect";
82+
import { exec } from "ssh2-exec";
83+
connect({ host: localhost }, function (err, ssh) {
84+
child = exec(
85+
{
86+
command: "ls -la",
87+
ssh: ssh,
88+
},
89+
function (err, stdout, stderr, code) {
90+
console.info(stdout);
91+
},
92+
);
93+
child.stdout.on("data", function (data) {
9194
console.info(data);
9295
});
93-
child.stderr.on('data', function(data){
96+
child.stderr.on("data", function (data) {
9497
console.error(data);
9598
});
96-
child.on('exit', function(code){
97-
console.info('Exit', code);
99+
child.on("exit", function (code) {
100+
console.info("Exit", code);
98101
});
99-
})
102+
});
100103
```
101104

102105
## Development

commitlint.config.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,3 @@
1-
module.exports = {
1+
export default {
22
extends: ["@commitlint/config-conventional"],
3-
rules: {
4-
"type-enum": [
5-
2,
6-
"always",
7-
[
8-
"build",
9-
"chore",
10-
"ci",
11-
"docs",
12-
"feat",
13-
"fix",
14-
"refactor",
15-
"revert",
16-
"test",
17-
],
18-
],
19-
},
203
};

lib/index.js

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { exec, spawn } = require("node:child_process");
2-
const { EventEmitter } = require("node:events");
3-
const stream = require("node:stream");
1+
import * as process from "node:child_process";
2+
import { EventEmitter } from "node:events";
3+
import stream from "node:stream";
44

55
// Utilities
66
const is_ssh_connection = function (obj) {
@@ -10,7 +10,7 @@ const is_object = function (obj) {
1010
return obj && typeof obj === "object" && !Array.isArray(obj);
1111
};
1212

13-
const local = (module.exports.local = function (options, callback) {
13+
export const local = function (options, callback) {
1414
const commandOptions = {};
1515
commandOptions.env = options.env || process.env;
1616
commandOptions.cwd = options.cwd || null;
@@ -24,7 +24,7 @@ const local = (module.exports.local = function (options, callback) {
2424
commandOptions.stdio = options.stdio;
2525
}
2626
if (callback) {
27-
return exec(
27+
return process.exec(
2828
options.command,
2929
commandOptions,
3030
function (err, stdout, stderr, ...args) {
@@ -55,71 +55,17 @@ const local = (module.exports.local = function (options, callback) {
5555
},
5656
);
5757
} else {
58-
return spawn(
58+
return process.spawn(
5959
options.command,
6060
[],
6161
Object.assign(commandOptions, {
6262
shell: options.shell || true,
6363
}),
6464
);
6565
}
66-
});
67-
68-
module.exports = function (...args) {
69-
let callback;
70-
const options = {};
71-
for (const i in args) {
72-
const arg = args[i];
73-
if (arg == null) {
74-
if (options.ssh != null) {
75-
throw Error(
76-
`Invalid Argument: argument ${i} cannot be null, the connection is already set`,
77-
);
78-
}
79-
options.ssh = arg;
80-
} else if (is_ssh_connection(arg)) {
81-
if (options.ssh != null) {
82-
throw Error(
83-
`Invalid Argument: argument ${i} cannot be an SSH connection, the connection is already set`,
84-
);
85-
}
86-
options.ssh = arg;
87-
} else if (is_object(arg)) {
88-
if (arg.cmd) {
89-
arg.command = arg.cmd;
90-
console.warn("Option `cmd` is deprecated in favor of `command`");
91-
}
92-
for (const k in arg) {
93-
options[k] = arg[k];
94-
}
95-
} else if (typeof arg === "string") {
96-
if (options.command != null) {
97-
throw Error(
98-
`Invalid Argument: argument ${i} cannot be a string, a command already exists`,
99-
);
100-
}
101-
options.command = arg;
102-
} else if (typeof arg === "function") {
103-
if (callback) {
104-
throw Error(
105-
`Invalid Argument: argument ${i} cannot be a function, a callback already exists`,
106-
);
107-
}
108-
callback = arg;
109-
} else {
110-
throw Error(
111-
`Invalid arguments: argument ${i} is invalid, got ${JSON.stringify(arg)}`,
112-
);
113-
}
114-
}
115-
if (options.ssh) {
116-
return remote(options, callback);
117-
} else {
118-
return local(options, callback);
119-
}
12066
};
12167

122-
const remote = (module.exports.remote = function (options, callback) {
68+
export const remote = function (options, callback) {
12369
const child = new EventEmitter();
12470
child.stdout = new stream.Readable();
12571
child.stdout._read = function (_size) {};
@@ -133,7 +79,8 @@ const remote = (module.exports.remote = function (options, callback) {
13379
child.stream.signal(signal);
13480
}
13581
};
136-
let stdout = (stderr = "");
82+
let stdout = "";
83+
let stderr = "";
13784
if (options.cwd) {
13885
options.command = `cd ${options.cwd}; ${options.command}`;
13986
}
@@ -167,8 +114,11 @@ const remote = (module.exports.remote = function (options, callback) {
167114
stdout += data;
168115
}
169116
});
170-
let code = (signal = null);
171-
let exitCalled = (stdoutCalled = stderrCalled = false);
117+
let code = null;
118+
let signal = null;
119+
let exitCalled = false;
120+
let stdoutCalled = false;
121+
let stderrCalled = false;
172122
const exit = function () {
173123
if (!(exitCalled && stdoutCalled && stderrCalled)) {
174124
return;
@@ -227,4 +177,58 @@ const remote = (module.exports.remote = function (options, callback) {
227177
});
228178
});
229179
return child;
230-
});
180+
};
181+
182+
export const exec = function (...args) {
183+
let callback;
184+
const options = {};
185+
for (const i in args) {
186+
const arg = args[i];
187+
if (arg == null) {
188+
if (options.ssh != null) {
189+
throw Error(
190+
`Invalid Argument: argument ${i} cannot be null, the connection is already set`,
191+
);
192+
}
193+
options.ssh = arg;
194+
} else if (is_ssh_connection(arg)) {
195+
if (options.ssh != null) {
196+
throw Error(
197+
`Invalid Argument: argument ${i} cannot be an SSH connection, the connection is already set`,
198+
);
199+
}
200+
options.ssh = arg;
201+
} else if (is_object(arg)) {
202+
if (arg.cmd) {
203+
arg.command = arg.cmd;
204+
console.warn("Option `cmd` is deprecated in favor of `command`");
205+
}
206+
for (const k in arg) {
207+
options[k] = arg[k];
208+
}
209+
} else if (typeof arg === "string") {
210+
if (options.command != null) {
211+
throw Error(
212+
`Invalid Argument: argument ${i} cannot be a string, a command already exists`,
213+
);
214+
}
215+
options.command = arg;
216+
} else if (typeof arg === "function") {
217+
if (callback) {
218+
throw Error(
219+
`Invalid Argument: argument ${i} cannot be a function, a callback already exists`,
220+
);
221+
}
222+
callback = arg;
223+
} else {
224+
throw Error(
225+
`Invalid arguments: argument ${i} is invalid, got ${JSON.stringify(arg)}`,
226+
);
227+
}
228+
}
229+
if (options.ssh) {
230+
return remote(options, callback);
231+
} else {
232+
return local(options, callback);
233+
}
234+
};

lib/promises.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const exec = require(".");
1+
import { exec as execCallback } from "./index.js";
22

3-
module.exports = function (...args) {
3+
export const exec = function (...args) {
44
if (typeof args[args.length - 1] === "function") {
55
throw Error("Last argument cannot be a callback function");
66
}
77
return new Promise(function (resolve, reject) {
8-
exec(...args, function (err, stdout, stderr, code) {
8+
execCallback(...args, function (err, stdout, stderr, code) {
99
if (err) {
1010
err.stdout = stdout;
1111
err.stderr = stderr;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"mocha": "^10.7.3",
4444
"mocha-they": "^0.1.3",
4545
"should": "^13.2.3",
46-
"ssh2-connect": "^3.4.3",
46+
"ssh2-connect": "^4.0.2",
4747
"standard-version": "^9.5.0"
4848
},
4949
"exports": {
@@ -60,5 +60,6 @@
6060
"release:major": "standard-version --release-as major",
6161
"postrelease": "git push --follow-tags origin master",
6262
"test": "mocha 'test/**/*.js'"
63-
}
63+
},
64+
"type": "module"
6465
}

test/api.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const exec = require("../lib/index");
2-
const { connect, they } = require("./test");
1+
import { exec } from "../lib/index.js";
2+
import { connect, they } from "./test.js";
3+
4+
const __filename = new URL("", import.meta.url).pathname;
35

46
describe("exec.api", () => {
57
they(

test/callback.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const exec = require('../lib/index');
2-
const { connect, they } = require('./test');
1+
import { exec } from "../lib/index.js";
2+
import { connect, they } from "./test.js";
33

44
describe("exec.callback", () => {
55
they(

test/child.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const exec = require("../lib/index");
2-
const { connect, they } = require("./test");
1+
import { exec } from "../lib/index.js";
2+
import { connect, they } from "./test.js";
33

44
describe("exec.child", () => {
55
they(

test/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const exec = require('../lib/index');
2-
const { connect, they } = require('./test');
1+
import { exec } from "../lib/index.js";
2+
import { connect, they } from "./test.js";
33

44
describe("exec.options", () => {
55
they(

test/promise.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const exec = require('../lib/promises');
2-
const { connect, they } = require('./test');
1+
import { exec } from "../lib/promises.js";
2+
import { connect, they } from "./test.js";
33

44
describe("exec.promise", () => {
5-
they(
5+
they.only(
66
"handle a successful command",
77
connect(({ ssh }) => {
88
return exec({

0 commit comments

Comments
 (0)