Skip to content

Commit 783c9b5

Browse files
committed
feat: Promise-based API
The `rclone.promises` API provides an alternative set of asynchronous Rclone methods that return Promise objects rather than using subprocess The API is accessible via require("rclone").promises.
1 parent 6ac8659 commit 783c9b5

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ ls.stderr.on("data", (data) => {
3636
});
3737
```
3838

39+
There is also a Promise-based API:
40+
41+
```js
42+
const rclone = require("rclone.js").promises;
43+
44+
(async function() {
45+
const results = await rclone.ls("source:");
46+
console.log(results);
47+
})();
48+
```
49+
3950
### CLI
4051

4152
This simple CLI calls the JS API above and outputs `stdout` and `stderr`.

index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ api.cat = function() {
8484
});
8585
}
8686

87+
// Promise-based API.
88+
const promises = api.promises = {};
89+
8790
const COMMANDS = [
8891
"about", // Get quota information from the remote.
8992
"authorize", // Remote authorization.
@@ -154,9 +157,30 @@ const COMMANDS = [
154157
];
155158

156159
COMMANDS.forEach(commandName => {
160+
// Normal API command to return a subprocess.
157161
api[commandName] = function() {
158162
return api(commandName, ...arguments);
159163
}
164+
165+
// Promise API command to return a Promise.
166+
promises[commandName] = function() {
167+
const args = Array.from(arguments);
168+
169+
return new Promise((resolve, reject) => {
170+
const { stdout, stderr } = api(commandName, ...args);
171+
172+
let output = "";
173+
stdout.on("data", data => {
174+
output += data;
175+
});
176+
stdout.on("end", () => {
177+
resolve(output.trim());
178+
});
179+
stderr.on("data", (data) => {
180+
reject(data.toString());
181+
});
182+
});
183+
}
160184
});
161185

162186
module.exports = api;

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": "rclone.js",
3-
"version": "0.4.2",
3+
"version": "0.5.0",
44
"description": "JavaScript API for rclone",
55
"main": "index.js",
66
"bin": {

0 commit comments

Comments
 (0)