specdown-cli 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +36 -63
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,22 +5,8 @@ import { Command } from "commander";
|
|
|
5
5
|
|
|
6
6
|
// src/lib/update-check.ts
|
|
7
7
|
import chalk from "chalk";
|
|
8
|
-
import { createRequire } from "module";
|
|
9
|
-
import { fileURLToPath } from "url";
|
|
10
|
-
import { dirname, join } from "path";
|
|
11
8
|
var PKG_NAME = "specdown-cli";
|
|
12
9
|
var REGISTRY_URL = `https://registry.npmjs.org/${PKG_NAME}/latest`;
|
|
13
|
-
var CHECK_INTERVAL_MS = 24 * 60 * 60 * 1e3;
|
|
14
|
-
function getCurrentVersion() {
|
|
15
|
-
try {
|
|
16
|
-
const require2 = createRequire(import.meta.url);
|
|
17
|
-
const pkgPath = join(dirname(fileURLToPath(import.meta.url)), "../../package.json");
|
|
18
|
-
const pkg = require2(pkgPath);
|
|
19
|
-
return pkg.version;
|
|
20
|
-
} catch {
|
|
21
|
-
return "0.0.0";
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
10
|
function compareVersions(current, latest) {
|
|
25
11
|
const parse = (v) => v.split(".").map(Number);
|
|
26
12
|
const [cMaj, cMin, cPat] = parse(current);
|
|
@@ -29,9 +15,8 @@ function compareVersions(current, latest) {
|
|
|
29
15
|
if (lMin !== cMin) return lMin > cMin;
|
|
30
16
|
return lPat > cPat;
|
|
31
17
|
}
|
|
32
|
-
function checkForUpdate() {
|
|
18
|
+
function checkForUpdate(current) {
|
|
33
19
|
if (process.env.npm_lifecycle_event === "npx" || process.env.CI) return;
|
|
34
|
-
const current = getCurrentVersion();
|
|
35
20
|
setImmediate(async () => {
|
|
36
21
|
try {
|
|
37
22
|
const controller = new AbortController();
|
|
@@ -54,23 +39,24 @@ function checkForUpdate() {
|
|
|
54
39
|
}
|
|
55
40
|
|
|
56
41
|
// src/index.ts
|
|
57
|
-
import { createRequire
|
|
58
|
-
import { fileURLToPath
|
|
59
|
-
import { dirname as
|
|
42
|
+
import { createRequire } from "module";
|
|
43
|
+
import { fileURLToPath } from "url";
|
|
44
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
60
45
|
|
|
61
46
|
// src/commands/login.ts
|
|
62
47
|
import http from "http";
|
|
63
48
|
import { exec } from "child_process";
|
|
64
49
|
import crypto from "crypto";
|
|
50
|
+
import { URL } from "url";
|
|
65
51
|
import chalk2 from "chalk";
|
|
66
52
|
import ora from "ora";
|
|
67
53
|
|
|
68
54
|
// src/lib/config.ts
|
|
69
55
|
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
|
|
70
56
|
import { homedir } from "os";
|
|
71
|
-
import { join
|
|
72
|
-
var CONFIG_DIR =
|
|
73
|
-
var CONFIG_FILE =
|
|
57
|
+
import { join } from "path";
|
|
58
|
+
var CONFIG_DIR = join(homedir(), ".specdown");
|
|
59
|
+
var CONFIG_FILE = join(CONFIG_DIR, "config.json");
|
|
74
60
|
function readConfig() {
|
|
75
61
|
if (!existsSync(CONFIG_FILE)) return null;
|
|
76
62
|
try {
|
|
@@ -121,47 +107,34 @@ async function login() {
|
|
|
121
107
|
const authUrl = `${APP_URL}/cli/auth?port=${port}&state=${state}`;
|
|
122
108
|
return new Promise((resolve, reject) => {
|
|
123
109
|
const server = http.createServer((req, res) => {
|
|
124
|
-
|
|
125
|
-
res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
|
|
126
|
-
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
127
|
-
if (req.method === "OPTIONS") {
|
|
128
|
-
res.writeHead(204);
|
|
129
|
-
res.end();
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
if (req.method !== "POST") {
|
|
110
|
+
if (req.method !== "GET") {
|
|
133
111
|
res.writeHead(405);
|
|
134
112
|
res.end();
|
|
135
113
|
return;
|
|
136
114
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (payload.state !== state) {
|
|
145
|
-
res.writeHead(403);
|
|
146
|
-
res.end(JSON.stringify({ error: "Invalid state" }));
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
writeConfig({
|
|
150
|
-
access_token: payload.access_token,
|
|
151
|
-
refresh_token: payload.refresh_token,
|
|
152
|
-
user_email: payload.email,
|
|
153
|
-
user_id: payload.user_id
|
|
154
|
-
});
|
|
155
|
-
res.writeHead(200);
|
|
156
|
-
res.end(JSON.stringify({ ok: true }));
|
|
157
|
-
spinner.succeed(chalk2.green(`Logged in as ${chalk2.bold(payload.email)}`));
|
|
158
|
-
server.close();
|
|
159
|
-
resolve();
|
|
160
|
-
} catch {
|
|
161
|
-
res.writeHead(400);
|
|
162
|
-
res.end(JSON.stringify({ error: "Bad request" }));
|
|
115
|
+
try {
|
|
116
|
+
const parsed = new URL(req.url ?? "/", `http://127.0.0.1:${port}`);
|
|
117
|
+
const params = parsed.searchParams;
|
|
118
|
+
if (params.get("state") !== state) {
|
|
119
|
+
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
120
|
+
res.end("Invalid state");
|
|
121
|
+
return;
|
|
163
122
|
}
|
|
164
|
-
|
|
123
|
+
writeConfig({
|
|
124
|
+
access_token: params.get("access_token") ?? "",
|
|
125
|
+
refresh_token: params.get("refresh_token") ?? "",
|
|
126
|
+
user_email: params.get("email") ?? "",
|
|
127
|
+
user_id: params.get("user_id") ?? ""
|
|
128
|
+
});
|
|
129
|
+
res.writeHead(302, { Location: `${APP_URL}/cli/auth/success` });
|
|
130
|
+
res.end();
|
|
131
|
+
spinner.succeed(chalk2.green(`Logged in as ${chalk2.bold(params.get("email"))}`));
|
|
132
|
+
server.close();
|
|
133
|
+
resolve();
|
|
134
|
+
} catch {
|
|
135
|
+
res.writeHead(400);
|
|
136
|
+
res.end("Bad request");
|
|
137
|
+
}
|
|
165
138
|
});
|
|
166
139
|
const spinner = ora();
|
|
167
140
|
server.listen(port, "127.0.0.1", () => {
|
|
@@ -496,7 +469,7 @@ async function push(filePath, docPath) {
|
|
|
496
469
|
|
|
497
470
|
// src/commands/pull.ts
|
|
498
471
|
import { writeFileSync as writeFileSync2, mkdirSync as mkdirSync2 } from "fs";
|
|
499
|
-
import { dirname
|
|
472
|
+
import { dirname } from "path";
|
|
500
473
|
import chalk11 from "chalk";
|
|
501
474
|
import ora8 from "ora";
|
|
502
475
|
function normalizePath4(p) {
|
|
@@ -515,7 +488,7 @@ async function pull(docPath, outFile) {
|
|
|
515
488
|
}
|
|
516
489
|
const content = data.content ?? "";
|
|
517
490
|
if (outFile) {
|
|
518
|
-
const dir =
|
|
491
|
+
const dir = dirname(outFile);
|
|
519
492
|
if (dir !== ".") mkdirSync2(dir, { recursive: true });
|
|
520
493
|
writeFileSync2(outFile, content, "utf-8");
|
|
521
494
|
spinner.succeed(chalk11.green(`Saved to ${outFile}`));
|
|
@@ -663,9 +636,9 @@ async function search(query, opts) {
|
|
|
663
636
|
}
|
|
664
637
|
|
|
665
638
|
// src/index.ts
|
|
666
|
-
var _require =
|
|
667
|
-
var _pkg = _require(
|
|
668
|
-
checkForUpdate();
|
|
639
|
+
var _require = createRequire(import.meta.url);
|
|
640
|
+
var _pkg = _require(join2(dirname2(fileURLToPath(import.meta.url)), "../package.json"));
|
|
641
|
+
checkForUpdate(_pkg.version);
|
|
669
642
|
var program = new Command();
|
|
670
643
|
program.name("specdown").description("Manage SpecDown docs from your terminal").version(_pkg.version);
|
|
671
644
|
program.command("login").description("Log in to SpecDown").action(login);
|