forked from raycast/script-commands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-stringify-text.js
executable file
·71 lines (59 loc) · 1.52 KB
/
json-stringify-text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Json Stringify Text
// @raycast.mode fullOutput
// Optional parameters:
// @raycast.icon 💻
// @raycast.packageName Developer Utilities
// Documentation:
// @raycast.description Get JSON-formatted text
// @raycast.author Senthil Prabhu
const child_process = require("child_process");
const fs = require("fs");
// Function to read the output of pbpaste command
function pbpaste() {
return new Promise((resolve, reject) => {
const child = child_process.spawn('pbpaste');
// listen on child process stdout
let response = "";
child.stdout.on("data", (chunk) => {
response += chunk;
});
child.on("close", (code) => {
if (code != 0) {
reject();
} else {
resolve(response);
}
});
});
};
// Function to copy data to clipboard
function pbcopy(data) {
return new Promise(function(resolve, reject) {
const child = child_process.spawn('pbcopy');
child.on('error', function(err) {
reject(err);
});
child.on('close', function(err) {
resolve(data);
});
child.stdin.write(data);
child.stdin.end();
});
};
// Stringify the text from clipboard and copy back to it
pbpaste()
.then(function(result) {
return JSON.stringify(result);
})
.then(function(string) {
return pbcopy(string);
})
.then(function(string) {
console.log(string);
}).catch(function(e) {
console.error(new Error('Could not stringify text'));
console.error(e);
});