forked from jorrit-stack/Raycast-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow-snippet.js
More file actions
executable file
·58 lines (50 loc) · 1.57 KB
/
show-snippet.js
File metadata and controls
executable file
·58 lines (50 loc) · 1.57 KB
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
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Show Snippet Pretty
// @raycast.mode fullOutput
// @raycast.icon 📝
// @raycast.argument1 { "type": "text", "placeholder": "Snippet JSON file path", "optional": true }
// Documentation:
// @raycast.description Pretty-print a JSON snippet file, replacing {clipboard} with clipboard contents
// @raycast.author jorrit_harmamny
// @raycast.authorURL https://raycast.com/jorrit_harmamny
const fs = require("fs");
const { execSync } = require("child_process");
const path = require("path");
// Get file path from argument or use default
const filePath = process.argv[2] || path.join(__dirname, "Snippets 2025-07-30 18.37.11.json");
// Get clipboard contents
let clipboard = "";
try {
clipboard = execSync("pbpaste").toString().trim();
} catch {
clipboard = "";
}
// Read and parse JSON
let data;
try {
const raw = fs.readFileSync(filePath, "utf8");
data = JSON.parse(raw);
} catch (e) {
console.error("Failed to read or parse JSON file:", e.message);
process.exit(1);
}
// Replace {clipboard} in all string values
function replaceClipboard(obj) {
if (typeof obj === "string") {
return obj.replace(/\{clipboard\}/g, clipboard);
} else if (Array.isArray(obj)) {
return obj.map(replaceClipboard);
} else if (typeof obj === "object" && obj !== null) {
const out = {};
for (const key in obj) {
out[key] = replaceClipboard(obj[key]);
}
return out;
}
return obj;
}
const replaced = replaceClipboard(data);
// Pretty print
console.log(JSON.stringify(replaced, null, 2));