-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlink.js
62 lines (57 loc) · 1.41 KB
/
link.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
import config from "../config.js";
import fs from "node:fs/promises";
import { getDateDetails } from "./lib/get-date-details.js";
import path from "node:path";
import { runCommand } from "./lib/run-command.js";
import util from "node:util";
const {
values: { comment, title, url },
} = util.parseArgs({
options: {
comment: {
type: "string",
},
title: {
type: "string",
},
url: {
type: "string",
},
},
});
if (!comment) {
throw new Error("--comment is required.");
}
if (!title) {
throw new Error("--title is required.");
}
if (!url) {
throw new Error("--url is required.");
}
const file = path.join(config.dir.data, "links.json");
const links = await fs.readFile(file).then(JSON.parse);
const published = Date.now();
const dt = getDateDetails(published);
const formatted = JSON.stringify(
[
{
description: comment,
link: url,
published,
...dt,
shortDescription: `${comment.split("。")[0]}。`,
title,
type: "link",
},
...links,
],
null,
2,
);
await fs.mkdir(path.dirname(file), { recursive: true });
await fs.writeFile(file, `${formatted}\n`);
await runCommand("git", ["add", "--", file]);
await runCommand("git", ["commit", `--message=Bookmark ${url}`]);
const twitter = new URL("https://x.com/intent/tweet");
twitter.searchParams.append("text", `${comment} / ${title} ${url}`);
await runCommand("open", [twitter.href]);