forked from jorrit-stack/Raycast-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnix converter
More file actions
executable file
·65 lines (56 loc) · 1.74 KB
/
Unix converter
File metadata and controls
executable file
·65 lines (56 loc) · 1.74 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
59
60
61
62
63
64
65
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Unix Timestamp Converter
// @raycast.mode fullOutput
// @raycast.icon 🕒
// @raycast.argument1 { "type": "text", "placeholder": "Snippet JSON file path or Unix timestamp", "optional": true }
// Documentation:
// @raycast.description Pretty-print a JSON snippet file, converting Unix timestamps to readable UTC
// @raycast.author jorrit_harmamny
// @raycast.authorURL https://raycast.com/jorrit_harmamny
const fs = require("fs");
const path = require("path");
const input = process.argv[2];
function isUnixMsTimestamp(n) {
return typeof n === "number" && n > 1e12 && n < 2e13;
}
function convertTimestamps(obj) {
if (typeof obj === "number" && isUnixMsTimestamp(obj)) {
return {
original: obj,
utc: new Date(obj).toISOString()
};
} else if (Array.isArray(obj)) {
return obj.map(convertTimestamps);
} else if (typeof obj === "object" && obj !== null) {
const out = {};
for (const key in obj) {
out[key] = convertTimestamps(obj[key]);
}
return out;
}
return obj;
}
if (!input) {
console.error("Please provide a JSON file path or a Unix timestamp.");
process.exit(1);
}
// Try to parse as number first
const asNumber = Number(input);
if (!isNaN(asNumber) && isUnixMsTimestamp(asNumber)) {
// Single timestamp mode
console.log(JSON.stringify(convertTimestamps(asNumber), null, 2));
process.exit(0);
}
// Otherwise, treat as file path
let data;
try {
const raw = fs.readFileSync(input, "utf8");
data = JSON.parse(raw);
} catch (e) {
console.error("Failed to read or parse JSON file:", e.message);
process.exit(1);
}
const converted = convertTimestamps(data);
console.log(JSON.stringify(converted, null, 2));