jd-skills 0.1.0
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/README.md +559 -0
- package/ThirdPartyNoticeText.txt +117 -0
- package/bin/cli.mjs +14 -0
- package/dist/_chunks/config.mjs +2 -0
- package/dist/_chunks/config2.mjs +42 -0
- package/dist/_chunks/jd-cookie.mjs +3 -0
- package/dist/_chunks/jd-cookie2.mjs +109 -0
- package/dist/_chunks/libs/@clack/core.mjs +767 -0
- package/dist/_chunks/libs/@clack/prompts.mjs +334 -0
- package/dist/_chunks/libs/@kwsites/file-exists.mjs +562 -0
- package/dist/_chunks/libs/@kwsites/promise-deferred.mjs +37 -0
- package/dist/_chunks/libs/@vercel/detect-agent.mjs +138 -0
- package/dist/_chunks/libs/simple-git.mjs +3584 -0
- package/dist/_chunks/libs/xdg-basedir.mjs +14 -0
- package/dist/_chunks/rolldown-runtime.mjs +24 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +6646 -0
- package/package.json +139 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { dirname, join } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
const CONFIG_PATH = join(join(homedir(), ".jd-skills"), "config.json");
|
|
5
|
+
function readConfig() {
|
|
6
|
+
try {
|
|
7
|
+
if (!existsSync(CONFIG_PATH)) return {};
|
|
8
|
+
const raw = readFileSync(CONFIG_PATH, "utf-8");
|
|
9
|
+
const parsed = JSON.parse(raw);
|
|
10
|
+
if (typeof parsed !== "object" || parsed === null) return {};
|
|
11
|
+
return parsed;
|
|
12
|
+
} catch {
|
|
13
|
+
return {};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function writeConfig(config) {
|
|
17
|
+
mkdirSync(dirname(CONFIG_PATH), { recursive: true });
|
|
18
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2), { mode: 384 });
|
|
19
|
+
}
|
|
20
|
+
function getBrokerUrl() {
|
|
21
|
+
const envUrl = process.env.JD_BROKER_URL;
|
|
22
|
+
if (envUrl && envUrl.trim().length > 0) return envUrl.trim().replace(/\/+$/, "");
|
|
23
|
+
const fileConfig = readConfig();
|
|
24
|
+
if (fileConfig.brokerUrl && fileConfig.brokerUrl.trim().length > 0) return fileConfig.brokerUrl.trim().replace(/\/+$/, "");
|
|
25
|
+
return "";
|
|
26
|
+
}
|
|
27
|
+
function setBrokerUrl(url) {
|
|
28
|
+
const config = readConfig();
|
|
29
|
+
if (url && url.trim().length > 0) config.brokerUrl = url.trim().replace(/\/+$/, "");
|
|
30
|
+
else delete config.brokerUrl;
|
|
31
|
+
config.version = 1;
|
|
32
|
+
writeConfig(config);
|
|
33
|
+
}
|
|
34
|
+
function resetConfig() {
|
|
35
|
+
try {
|
|
36
|
+
if (existsSync(CONFIG_PATH)) writeFileSync(CONFIG_PATH, "{}", { mode: 384 });
|
|
37
|
+
} catch {}
|
|
38
|
+
}
|
|
39
|
+
function getConfig() {
|
|
40
|
+
return readConfig();
|
|
41
|
+
}
|
|
42
|
+
export { setBrokerUrl as i, getConfig as n, resetConfig as r, getBrokerUrl as t };
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { t as getBrokerUrl } from "./config2.mjs";
|
|
2
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
3
|
+
import { dirname, join } from "path";
|
|
4
|
+
import { homedir } from "os";
|
|
5
|
+
const CACHE_PATH = join(join(homedir(), ".jd-skills"), "cookies.json");
|
|
6
|
+
const COOKIE_TTL_MS = 1440 * 60 * 1e3;
|
|
7
|
+
function getSdkPackageName() {
|
|
8
|
+
return (process.env.JD_BROKER_SDK_PACKAGE ?? "").trim();
|
|
9
|
+
}
|
|
10
|
+
function installSdkHint() {
|
|
11
|
+
const pkg = getSdkPackageName() || "<your-org>/<your-cookie-broker-sdk>";
|
|
12
|
+
return `install ${pkg} from your private registry and set JD_BROKER_SDK_PACKAGE=${pkg.split("/")[0] + "/<sdk>"} in your env`;
|
|
13
|
+
}
|
|
14
|
+
function readCache() {
|
|
15
|
+
try {
|
|
16
|
+
if (!existsSync(CACHE_PATH)) return null;
|
|
17
|
+
const raw = readFileSync(CACHE_PATH, "utf-8");
|
|
18
|
+
const parsed = JSON.parse(raw);
|
|
19
|
+
if (typeof parsed.cookie !== "string" || typeof parsed.expiresAt !== "number") return null;
|
|
20
|
+
return parsed;
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function writeCache(entry) {
|
|
26
|
+
try {
|
|
27
|
+
mkdirSync(dirname(CACHE_PATH), { recursive: true });
|
|
28
|
+
writeFileSync(CACHE_PATH, JSON.stringify(entry, null, 2), { mode: 384 });
|
|
29
|
+
} catch {}
|
|
30
|
+
}
|
|
31
|
+
async function getJdCookie(opts) {
|
|
32
|
+
if (!opts?.forceRefresh) {
|
|
33
|
+
const cached = readCache();
|
|
34
|
+
if (cached && cached.expiresAt > Date.now()) return cached.cookie;
|
|
35
|
+
}
|
|
36
|
+
const sdkPackage = getSdkPackageName();
|
|
37
|
+
if (!sdkPackage) return null;
|
|
38
|
+
let loginFn = null;
|
|
39
|
+
try {
|
|
40
|
+
loginFn = (await import(
|
|
41
|
+
/* @vite-ignore */
|
|
42
|
+
`${sdkPackage}/client`
|
|
43
|
+
)).login ?? null;
|
|
44
|
+
} catch {
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
if (!loginFn) return null;
|
|
48
|
+
try {
|
|
49
|
+
const cookie = await loginFn({
|
|
50
|
+
brokerUrl: getBrokerUrl(),
|
|
51
|
+
open: true
|
|
52
|
+
});
|
|
53
|
+
if (typeof cookie === "string" && cookie.length > 0) {
|
|
54
|
+
writeCache({
|
|
55
|
+
cookie,
|
|
56
|
+
expiresAt: Date.now() + COOKIE_TTL_MS,
|
|
57
|
+
cachedAt: Date.now()
|
|
58
|
+
});
|
|
59
|
+
return cookie;
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function getCookieStatus() {
|
|
67
|
+
const cached = readCache();
|
|
68
|
+
if (!cached) return { loggedIn: false };
|
|
69
|
+
return {
|
|
70
|
+
loggedIn: true,
|
|
71
|
+
expiresAt: cached.expiresAt,
|
|
72
|
+
remainingMs: cached.expiresAt - Date.now()
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
async function isBrokerSdkAvailable() {
|
|
76
|
+
const sdkPackage = getSdkPackageName();
|
|
77
|
+
if (!sdkPackage) return false;
|
|
78
|
+
try {
|
|
79
|
+
return typeof (await import(
|
|
80
|
+
/* @vite-ignore */
|
|
81
|
+
`${sdkPackage}/client`
|
|
82
|
+
)).login === "function";
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async function isBrokerReachable(brokerUrl) {
|
|
88
|
+
try {
|
|
89
|
+
return (await fetch(brokerUrl, {
|
|
90
|
+
method: "HEAD",
|
|
91
|
+
signal: AbortSignal.timeout(3e3)
|
|
92
|
+
})).status < 500;
|
|
93
|
+
} catch {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function getJdSourcePromptState() {
|
|
98
|
+
if (!await isBrokerSdkAvailable()) return "sdk_missing";
|
|
99
|
+
const cookieStatus = getCookieStatus();
|
|
100
|
+
if (cookieStatus.loggedIn && cookieStatus.remainingMs && cookieStatus.remainingMs > 0) return "logged_in";
|
|
101
|
+
const { getBrokerUrl } = await import("./config.mjs");
|
|
102
|
+
return await isBrokerReachable(getBrokerUrl()) ? "auth_required" : "unreachable";
|
|
103
|
+
}
|
|
104
|
+
function clearJdCookie() {
|
|
105
|
+
try {
|
|
106
|
+
if (existsSync(CACHE_PATH)) writeFileSync(CACHE_PATH, "", { mode: 384 });
|
|
107
|
+
} catch {}
|
|
108
|
+
}
|
|
109
|
+
export { getSdkPackageName as a, isBrokerSdkAvailable as c, getJdSourcePromptState as i, getCookieStatus as n, installSdkHint as o, getJdCookie as r, isBrokerReachable as s, clearJdCookie as t };
|