Repository navigation
Expand file tree
/
Copy pathuntrack.mjs
More file actions
139 lines (122 loc) ยท 4.35 KB
/
Copy pathuntrack.mjs
File metadata and controls
139 lines (122 loc) ยท 4.35 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env node
/**
* m1kkit untrack [slug] [--token=<claimToken>] [--bearer=<๊ฐ์ธํ ํฐ>] [--host=m1k.app] [--yes]
*
* m1k ๋ฐฉ๋ฌธ์ ํธ๋์ปค์์ ์ฌ์ดํธ ๋ฑ๋ก์ ์ทจ์ํ๋ค(์ญ์ ).
*
* `track`์ผ๋ก ์๋ชป ๋ฑ๋กํ์ ๋ ์ง์ธ ๋ฐฉ๋ฒ์ด ์์๋ค โ ๋์๋ณด๋ ์ญ์ ๋ ๋ธ๋ผ์ฐ์
* ๋ก๊ทธ์ธ์ด ํ์ํ๊ณ , CLI๋ก ์ต๋ช
๋ฑ๋กํ ์ฌ์ดํธ๋ ์์ง ์ด๋ ๊ณ์ ์๋ ์ ๋ถ์ด
* ์์ด์ ๋์๋ณด๋์ ๋จ์ง๋ ์๋๋ค. ๊ทธ๋์ claim ํ ํฐ์ผ๋ก ์ง์ด๋ค.
*
* slug/ํ ํฐ์ ์ ์ฃผ๋ฉด ํ์ฌ ํด๋์ ./.m1k.json ์์ ์ฝ๋๋ค.
*/
import { existsSync, readFileSync, writeFileSync, unlinkSync } from "fs";
import { resolve } from "path";
import { createInterface } from "readline";
const args = process.argv.slice(2);
const flags = Object.fromEntries(
args
.filter((a) => a.startsWith("--"))
.map((a) => {
const [k, v] = a.replace(/^--/, "").split("=");
return [k, v ?? true];
}),
);
const positional = args.filter((a) => !a.startsWith("--"));
const store = readStore();
const slug = positional[0] || store.slug;
const claimToken = flags.token || store.claimToken;
const bearer = flags.bearer || process.env.M1K_TOKEN;
const host = flags.host || store.host || process.env.M1K_HOST || "m1k.app";
if (!slug || flags.help) {
console.log(`
m1kkit untrack โ ๋ฐฉ๋ฌธ์ ํธ๋์ปค์์ ์ฌ์ดํธ ์ญ์
์ฌ์ฉ๋ฒ:
npx m1kkit untrack [slug] [์ต์
]
slug๋ฅผ ์ ์ฃผ๋ฉด ํ์ฌ ํด๋์ ./.m1k.json ์์ ์ฝ์ด์.
์ต์
:
--token=<claimToken> ๋ฏธ๊ท์ ์ฌ์ดํธ์ฉ. ๊ธฐ๋ณธ๊ฐ์ ./.m1k.json ์ claimToken
--bearer=<๊ฐ์ธํ ํฐ> ์ด๋ฏธ ๋ด ๊ณ์ ์ ๊ท์ํ ์ฌ์ดํธ์ฉ (๋๋ M1K_TOKEN ํ๊ฒฝ๋ณ์)
--host=m1k.app
--yes ํ์ธ ์์ด ์ญ์
๋๋๋ฆด ์ ์์ด์ โ ๋์ ๋ฐฉ๋ฌธ ๊ธฐ๋ก๋ ๊ฐ์ด ์ง์์ง๋๋ค.
`);
process.exit(slug ? 0 : 1);
}
if (!claimToken && !bearer) {
console.error(`โ ์ธ์ฆ ์๋จ์ด ์์ด์.
๋ฏธ๊ท์ ์ฌ์ดํธ๋ฉด --token=<claimToken> (./.m1k.json ์ ์์ด์)
๊ท์ํ ์ฌ์ดํธ๋ฉด --bearer=<๊ฐ์ธํ ํฐ>`);
process.exit(1);
}
const scheme = /^(localhost|127\.|0\.0\.0\.0)/.test(host) ? "http" : "https";
const base = `${scheme}://${host}`;
if (!flags.yes && process.stdin.isTTY) {
const rl = createInterface({ input: process.stdin, output: process.stdout });
const answer = await new Promise((res) =>
rl.question(`'${slug}' ๋ฅผ ์ญ์ ํ ๊น์? ๋์ ๋ฐฉ๋ฌธ ๊ธฐ๋ก๋ ๊ฐ์ด ์ง์์ง๊ณ ๋๋๋ฆด ์ ์์ด์. (y/N): `, res),
);
rl.close();
if (!/^y(es)?$/i.test(answer.trim())) {
console.log("์ทจ์ํ์ด์.");
process.exit(0);
}
}
let res, data;
try {
res = await fetch(`${base}/api/sites/cli`, {
method: "DELETE",
headers: {
"content-type": "application/json",
...(bearer ? { authorization: `Bearer ${bearer}` } : {}),
},
body: JSON.stringify({ slug, claimToken }),
});
data = await res.json().catch(() => ({}));
} catch (e) {
console.error(`โ ์ญ์ ์์ฒญ ์คํจ: ${e.message}`);
process.exit(1);
}
if (!res.ok) {
console.error(`โ ${data?.error || data?.message || res.statusText}`);
process.exit(1);
}
console.log(`\nโ ์ญ์ ํ์ด์. ${data.url || slug}\n`);
clearStore(slug);
/** ./.m1k.json ์ฝ๊ธฐ (์๊ฑฐ๋ ๊นจ์ก์ผ๋ฉด ๋น ๊ฐ์ฒด) */
function readStore() {
const file = resolve(process.cwd(), ".m1k.json");
if (!existsSync(file)) return {};
try {
return JSON.parse(readFileSync(file, "utf8"));
} catch {
return {};
}
}
/**
* ์ง์ด ์ฌ์ดํธ๋ฅผ ๊ฐ๋ฆฌํค๋ ./.m1k.json ์ ์ ๋ฆฌํ๋ค โ ๋จ๊ฒจ๋๋ฉด ๋ค์ `claim`์ด
* ์ฃฝ์ ํ ํฐ์ผ๋ก ๋ธ๋ผ์ฐ์ ๋ฅผ ์ฐ๋ค. ๋ค๋ฅธ ์ฌ์ดํธ๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์์ผ๋ฉด ๊ฑด๋๋ฆฌ์ง ์๋๋ค.
*/
function clearStore(deletedSlug) {
const file = resolve(process.cwd(), ".m1k.json");
if (!existsSync(file)) return;
let json;
try {
json = JSON.parse(readFileSync(file, "utf8"));
} catch {
return;
}
if (json.slug !== deletedSlug) return;
const rest = { ...json };
delete rest.slug;
delete rest.url;
delete rest.claimToken;
if (Object.keys(rest).length === 0) {
unlinkSync(file);
console.log(" ./.m1k.json ๋ ์ง์ ์ด์.");
} else {
writeFileSync(file, JSON.stringify(rest, null, 2) + "\n");
console.log(" ./.m1k.json ์์ ํด๋น ํญ๋ชฉ์ ์ง์ ์ด์.");
}
console.log(` App.tsx ์ trackSlug / .env ์ *_M1K_SLUG ๋ ์ง์ ์ง์์ฃผ์ธ์.\n`);
}