@gobing-ai/knowledge-kit 0.1.16 → 0.1.17
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/.claude-plugin/marketplace.json +1 -1
- package/dist/index.js +162 -39
- package/package.json +1 -1
- package/plugins/kk/plugin.json +1 -1
- package/plugins/kk/publish-presets/daily-ai-voice.json +8 -8
- package/plugins/kk/publish-presets/itc-main.json +42 -0
- package/plugins/kk/scripts/cover-compositor.ts +54 -8
- package/plugins/kk/scripts/itc-stages.ts +3 -2
- package/plugins/kk/scripts/voice3-publish-window.ts +5 -1
- package/plugins/kk/workflows/kk-daily-publish3.yaml +10 -3
- package/plugins/publishings/bilibili-pub/dist/index.js +89 -16
- package/plugins/publishings/bilibili-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/cnblogs-pub/dist/index.js +89 -16
- package/plugins/publishings/cnblogs-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/csdn-pub/dist/index.js +89 -16
- package/plugins/publishings/csdn-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/infoq-pub/dist/index.js +119 -18
- package/plugins/publishings/infoq-pub/dist/profile-check.js +94 -17
- package/plugins/publishings/infoq-pub/src/index.ts +40 -1
- package/plugins/publishings/jike-pub/dist/index.js +89 -16
- package/plugins/publishings/jike-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/juejin-pub/dist/index.js +116 -19
- package/plugins/publishings/juejin-pub/dist/profile-check.js +93 -17
- package/plugins/publishings/juejin-pub/src/index.ts +41 -2
- package/plugins/publishings/linkedin-pub/dist/index.js +89 -16
- package/plugins/publishings/linkedin-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/shipinhao-pub/dist/index.js +89 -16
- package/plugins/publishings/shipinhao-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/sspai-pub/dist/index.js +89 -16
- package/plugins/publishings/sspai-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/substack-pub/dist/index.js +89 -16
- package/plugins/publishings/substack-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/surfdash-pub/dist/index.js +9 -1
- package/plugins/publishings/surfdash-pub/src/index.ts +17 -2
- package/plugins/publishings/wechatmp-pub/dist/index.js +176 -32
- package/plugins/publishings/wechatmp-pub/dist/profile-check.js +95 -18
- package/plugins/publishings/wechatmp-pub/src/cgi-draft.ts +14 -2
- package/plugins/publishings/wechatmp-pub/src/flow.ts +155 -21
- package/plugins/publishings/wechatmp-pub/src/profile-check.ts +8 -1
- package/plugins/publishings/weibo-pub/dist/index.js +89 -16
- package/plugins/publishings/weibo-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/x-pub/dist/index.js +89 -16
- package/plugins/publishings/x-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/xhs-pub/dist/index.js +89 -16
- package/plugins/publishings/xhs-pub/dist/profile-check.js +89 -16
- package/plugins/publishings/xiaoyuzhou-pub/dist/index.js +89 -16
- package/plugins/publishings/xiaoyuzhou-pub/dist/profile-check.js +90 -16
- package/plugins/publishings/zenn-pub/dist/index.js +93 -1
- package/plugins/publishings/zenn-pub/src/index.ts +134 -1
|
@@ -20470,8 +20470,11 @@ import {
|
|
|
20470
20470
|
mkdirSync as mkdirSync2,
|
|
20471
20471
|
openSync,
|
|
20472
20472
|
readdirSync as readdirSync2,
|
|
20473
|
+
readlinkSync,
|
|
20473
20474
|
realpathSync as realpathSync2,
|
|
20474
|
-
|
|
20475
|
+
renameSync as renameSync2,
|
|
20476
|
+
rmdirSync,
|
|
20477
|
+
unlinkSync
|
|
20475
20478
|
} from "fs";
|
|
20476
20479
|
function lstat(path) {
|
|
20477
20480
|
try {
|
|
@@ -20506,6 +20509,33 @@ function rmdir(path) {
|
|
|
20506
20509
|
throw error;
|
|
20507
20510
|
}
|
|
20508
20511
|
}
|
|
20512
|
+
function readlink(path) {
|
|
20513
|
+
try {
|
|
20514
|
+
return readlinkSync(path);
|
|
20515
|
+
} catch {
|
|
20516
|
+
return null;
|
|
20517
|
+
}
|
|
20518
|
+
}
|
|
20519
|
+
function unlink(path) {
|
|
20520
|
+
try {
|
|
20521
|
+
unlinkSync(path);
|
|
20522
|
+
return true;
|
|
20523
|
+
} catch (error) {
|
|
20524
|
+
if (error.code === "ENOENT")
|
|
20525
|
+
return false;
|
|
20526
|
+
throw error;
|
|
20527
|
+
}
|
|
20528
|
+
}
|
|
20529
|
+
function rename(from, to) {
|
|
20530
|
+
try {
|
|
20531
|
+
renameSync2(from, to);
|
|
20532
|
+
return true;
|
|
20533
|
+
} catch (error) {
|
|
20534
|
+
if (error.code === "ENOENT")
|
|
20535
|
+
return false;
|
|
20536
|
+
throw error;
|
|
20537
|
+
}
|
|
20538
|
+
}
|
|
20509
20539
|
function chmod(path, mode) {
|
|
20510
20540
|
chmodSync(path, mode);
|
|
20511
20541
|
}
|
|
@@ -20517,14 +20547,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join2(homedir2(), ".config", "kk", "browser-t
|
|
|
20517
20547
|
|
|
20518
20548
|
// ../../packages/publish-harness/src/profile-lease.ts
|
|
20519
20549
|
import { createHash, randomBytes } from "crypto";
|
|
20520
|
-
import { homedir as homedir3 } from "os";
|
|
20550
|
+
import { homedir as homedir3, hostname } from "os";
|
|
20521
20551
|
import { basename, dirname as dirname3, join as join3, resolve as resolve3 } from "path";
|
|
20522
20552
|
var DEFAULT_BOT_LEASE_ROOT = join3(homedir3(), ".config", "kk", "browser-leases");
|
|
20523
20553
|
var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
|
|
20524
20554
|
var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
|
|
20525
20555
|
var OWNER_FILE = "owner.json";
|
|
20526
20556
|
var HOLDER_PREFIX = "holder-";
|
|
20527
|
-
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
|
|
20557
|
+
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
|
|
20528
20558
|
async function acquireProfileLease(profileDir, options = {}) {
|
|
20529
20559
|
const fileSystem = options.fileSystem ?? createNodeFileSystem();
|
|
20530
20560
|
const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
|
|
@@ -20692,6 +20722,8 @@ function isProcessAlive(pid) {
|
|
|
20692
20722
|
}
|
|
20693
20723
|
}
|
|
20694
20724
|
function refuseNativeLock(profileDir) {
|
|
20725
|
+
if (reclaimStaleNativeLock(profileDir))
|
|
20726
|
+
return;
|
|
20695
20727
|
for (const name of NATIVE_LOCK_FILES) {
|
|
20696
20728
|
const path = join3(profileDir, name);
|
|
20697
20729
|
if (lstat(path) !== null) {
|
|
@@ -20699,6 +20731,24 @@ function refuseNativeLock(profileDir) {
|
|
|
20699
20731
|
}
|
|
20700
20732
|
}
|
|
20701
20733
|
}
|
|
20734
|
+
function reclaimStaleNativeLock(profileDir) {
|
|
20735
|
+
const lockPath = join3(profileDir, "SingletonLock");
|
|
20736
|
+
const target = readlink(lockPath);
|
|
20737
|
+
if (target === null)
|
|
20738
|
+
return false;
|
|
20739
|
+
const match = /^(.*)-(\d+)$/.exec(target);
|
|
20740
|
+
if (match === null)
|
|
20741
|
+
return false;
|
|
20742
|
+
const [, host, pidText] = match;
|
|
20743
|
+
if (host !== hostname())
|
|
20744
|
+
return false;
|
|
20745
|
+
const pid = Number(pidText);
|
|
20746
|
+
if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
|
|
20747
|
+
return false;
|
|
20748
|
+
for (const name of NATIVE_LOCK_FILES)
|
|
20749
|
+
unlink(join3(profileDir, name));
|
|
20750
|
+
return true;
|
|
20751
|
+
}
|
|
20702
20752
|
function sha256Hex(value) {
|
|
20703
20753
|
return createHash("sha256").update(value).digest("hex");
|
|
20704
20754
|
}
|
|
@@ -20735,6 +20785,27 @@ var DEFAULT_CHECK_TIMEOUT_MS = 30000;
|
|
|
20735
20785
|
var DEFAULT_STEP_TIMEOUT_MS = 30000;
|
|
20736
20786
|
var DEFAULT_POLL_MS = 500;
|
|
20737
20787
|
var ATTACH_POLL_MS = 100;
|
|
20788
|
+
function isInstantBrowserDeath(message) {
|
|
20789
|
+
return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
|
|
20790
|
+
}
|
|
20791
|
+
function classifyLaunchError(error, profileDir, channel) {
|
|
20792
|
+
const message = errorMessage2(error);
|
|
20793
|
+
if (/singleton|already in use|in use by another/i.test(message))
|
|
20794
|
+
return new BrowserProfileBusyError(profileDir, error);
|
|
20795
|
+
if (/playwright install|executable doesn't exist/i.test(message)) {
|
|
20796
|
+
return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
|
|
20797
|
+
}
|
|
20798
|
+
if (isInstantBrowserDeath(message)) {
|
|
20799
|
+
return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
|
|
20800
|
+
}
|
|
20801
|
+
return error instanceof Error ? error : new Error(message);
|
|
20802
|
+
}
|
|
20803
|
+
function quarantineProfileSyncData(profileDir) {
|
|
20804
|
+
const syncData = join5(profileDir, "Default", "Sync Data");
|
|
20805
|
+
if (lstat(syncData) === null)
|
|
20806
|
+
return false;
|
|
20807
|
+
return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
|
|
20808
|
+
}
|
|
20738
20809
|
function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
|
|
20739
20810
|
return {
|
|
20740
20811
|
async launch(request) {
|
|
@@ -20763,23 +20834,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
|
|
|
20763
20834
|
throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
|
|
20764
20835
|
}
|
|
20765
20836
|
const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
|
|
20837
|
+
const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
|
|
20838
|
+
headless,
|
|
20839
|
+
channel,
|
|
20840
|
+
args: ["--disable-blink-features=AutomationControlled"],
|
|
20841
|
+
ignoreDefaultArgs: ["--enable-automation"]
|
|
20842
|
+
});
|
|
20766
20843
|
let context;
|
|
20767
20844
|
try {
|
|
20768
|
-
context = await
|
|
20769
|
-
headless,
|
|
20770
|
-
channel,
|
|
20771
|
-
args: ["--disable-blink-features=AutomationControlled"],
|
|
20772
|
-
ignoreDefaultArgs: ["--enable-automation"]
|
|
20773
|
-
});
|
|
20845
|
+
context = await launch();
|
|
20774
20846
|
} catch (error) {
|
|
20775
|
-
|
|
20776
|
-
|
|
20777
|
-
|
|
20778
|
-
|
|
20779
|
-
|
|
20780
|
-
|
|
20847
|
+
if (!isInstantBrowserDeath(errorMessage2(error)))
|
|
20848
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20849
|
+
if (!quarantineProfileSyncData(profileDir))
|
|
20850
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20851
|
+
try {
|
|
20852
|
+
context = await launch();
|
|
20853
|
+
} catch (retryError) {
|
|
20854
|
+
throw classifyLaunchError(retryError, profileDir, channel);
|
|
20781
20855
|
}
|
|
20782
|
-
throw error;
|
|
20783
20856
|
}
|
|
20784
20857
|
try {
|
|
20785
20858
|
await context.addInitScript(() => {
|
|
@@ -19853,8 +19853,11 @@ import {
|
|
|
19853
19853
|
mkdirSync as mkdirSync2,
|
|
19854
19854
|
openSync,
|
|
19855
19855
|
readdirSync as readdirSync2,
|
|
19856
|
+
readlinkSync,
|
|
19856
19857
|
realpathSync as realpathSync2,
|
|
19857
|
-
|
|
19858
|
+
renameSync as renameSync2,
|
|
19859
|
+
rmdirSync,
|
|
19860
|
+
unlinkSync
|
|
19858
19861
|
} from "fs";
|
|
19859
19862
|
function lstat(path) {
|
|
19860
19863
|
try {
|
|
@@ -19889,6 +19892,33 @@ function rmdir(path) {
|
|
|
19889
19892
|
throw error;
|
|
19890
19893
|
}
|
|
19891
19894
|
}
|
|
19895
|
+
function readlink(path) {
|
|
19896
|
+
try {
|
|
19897
|
+
return readlinkSync(path);
|
|
19898
|
+
} catch {
|
|
19899
|
+
return null;
|
|
19900
|
+
}
|
|
19901
|
+
}
|
|
19902
|
+
function unlink(path) {
|
|
19903
|
+
try {
|
|
19904
|
+
unlinkSync(path);
|
|
19905
|
+
return true;
|
|
19906
|
+
} catch (error) {
|
|
19907
|
+
if (error.code === "ENOENT")
|
|
19908
|
+
return false;
|
|
19909
|
+
throw error;
|
|
19910
|
+
}
|
|
19911
|
+
}
|
|
19912
|
+
function rename(from, to) {
|
|
19913
|
+
try {
|
|
19914
|
+
renameSync2(from, to);
|
|
19915
|
+
return true;
|
|
19916
|
+
} catch (error) {
|
|
19917
|
+
if (error.code === "ENOENT")
|
|
19918
|
+
return false;
|
|
19919
|
+
throw error;
|
|
19920
|
+
}
|
|
19921
|
+
}
|
|
19892
19922
|
function chmod(path, mode) {
|
|
19893
19923
|
chmodSync(path, mode);
|
|
19894
19924
|
}
|
|
@@ -19900,14 +19930,14 @@ var DEFAULT_BOT_TRANSACTION_ROOT = join(homedir(), ".config", "kk", "browser-tra
|
|
|
19900
19930
|
|
|
19901
19931
|
// ../../packages/publish-harness/src/profile-lease.ts
|
|
19902
19932
|
import { createHash, randomBytes } from "crypto";
|
|
19903
|
-
import { homedir as homedir2 } from "os";
|
|
19933
|
+
import { homedir as homedir2, hostname } from "os";
|
|
19904
19934
|
import { basename, dirname as dirname3, join as join2, resolve as resolve2 } from "path";
|
|
19905
19935
|
var DEFAULT_BOT_LEASE_ROOT = join2(homedir2(), ".config", "kk", "browser-leases");
|
|
19906
19936
|
var KK_BOT_LEASE_PATH = "KK_BOT_LEASE_PATH";
|
|
19907
19937
|
var KK_BOT_LEASE_TOKEN = "KK_BOT_LEASE_TOKEN";
|
|
19908
19938
|
var OWNER_FILE = "owner.json";
|
|
19909
19939
|
var HOLDER_PREFIX = "holder-";
|
|
19910
|
-
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket"];
|
|
19940
|
+
var NATIVE_LOCK_FILES = ["SingletonLock", "SingletonSocket", "SingletonCookie"];
|
|
19911
19941
|
async function acquireProfileLease(profileDir, options = {}) {
|
|
19912
19942
|
const fileSystem = options.fileSystem ?? createNodeFileSystem();
|
|
19913
19943
|
const leaseRoot = options.leaseRoot ?? DEFAULT_BOT_LEASE_ROOT;
|
|
@@ -20075,6 +20105,8 @@ function isProcessAlive(pid) {
|
|
|
20075
20105
|
}
|
|
20076
20106
|
}
|
|
20077
20107
|
function refuseNativeLock(profileDir) {
|
|
20108
|
+
if (reclaimStaleNativeLock(profileDir))
|
|
20109
|
+
return;
|
|
20078
20110
|
for (const name of NATIVE_LOCK_FILES) {
|
|
20079
20111
|
const path = join2(profileDir, name);
|
|
20080
20112
|
if (lstat(path) !== null) {
|
|
@@ -20082,6 +20114,24 @@ function refuseNativeLock(profileDir) {
|
|
|
20082
20114
|
}
|
|
20083
20115
|
}
|
|
20084
20116
|
}
|
|
20117
|
+
function reclaimStaleNativeLock(profileDir) {
|
|
20118
|
+
const lockPath = join2(profileDir, "SingletonLock");
|
|
20119
|
+
const target = readlink(lockPath);
|
|
20120
|
+
if (target === null)
|
|
20121
|
+
return false;
|
|
20122
|
+
const match = /^(.*)-(\d+)$/.exec(target);
|
|
20123
|
+
if (match === null)
|
|
20124
|
+
return false;
|
|
20125
|
+
const [, host, pidText] = match;
|
|
20126
|
+
if (host !== hostname())
|
|
20127
|
+
return false;
|
|
20128
|
+
const pid = Number(pidText);
|
|
20129
|
+
if (!Number.isInteger(pid) || pid <= 0 || isProcessAlive(pid))
|
|
20130
|
+
return false;
|
|
20131
|
+
for (const name of NATIVE_LOCK_FILES)
|
|
20132
|
+
unlink(join2(profileDir, name));
|
|
20133
|
+
return true;
|
|
20134
|
+
}
|
|
20085
20135
|
function sha256Hex(value) {
|
|
20086
20136
|
return createHash("sha256").update(value).digest("hex");
|
|
20087
20137
|
}
|
|
@@ -20160,6 +20210,27 @@ var MAX_QUEUED_BYTES = 8 * 1024 * 1024;
|
|
|
20160
20210
|
var DEFAULT_LOGIN_TIMEOUT_MS = 180000;
|
|
20161
20211
|
var DEFAULT_OPEN_NAV_TIMEOUT_MS = 30000;
|
|
20162
20212
|
var DEFAULT_CHECK_TIMEOUT_MS = 30000;
|
|
20213
|
+
function isInstantBrowserDeath(message) {
|
|
20214
|
+
return /target page, context or browser has been closed|browser has been closed|target closed/i.test(message);
|
|
20215
|
+
}
|
|
20216
|
+
function classifyLaunchError(error, profileDir, channel) {
|
|
20217
|
+
const message = errorMessage2(error);
|
|
20218
|
+
if (/singleton|already in use|in use by another/i.test(message))
|
|
20219
|
+
return new BrowserProfileBusyError(profileDir, error);
|
|
20220
|
+
if (/playwright install|executable doesn't exist/i.test(message)) {
|
|
20221
|
+
return new PlaywrightUnavailableError(`Browser channel '${channel}' is not installed. Run \`bunx playwright install chromium\` (or install Google Chrome for KK_BROWSER_CHANNEL=chrome).`, { cause: error });
|
|
20222
|
+
}
|
|
20223
|
+
if (isInstantBrowserDeath(message)) {
|
|
20224
|
+
return new PlaywrightUnavailableError(`browser profile ${profileDir} makes Chromium exit during startup (no diagnostic reported). Move it aside (\`mv ${profileDir} ${profileDir}.broken-<date>\`) and log in again; \`storage-state.json\` is the only state worth keeping.`, { cause: error });
|
|
20225
|
+
}
|
|
20226
|
+
return error instanceof Error ? error : new Error(message);
|
|
20227
|
+
}
|
|
20228
|
+
function quarantineProfileSyncData(profileDir) {
|
|
20229
|
+
const syncData = join5(profileDir, "Default", "Sync Data");
|
|
20230
|
+
if (lstat(syncData) === null)
|
|
20231
|
+
return false;
|
|
20232
|
+
return rename(syncData, `${syncData}.broken-${new Date().toISOString().replace(/[:.]/g, "").slice(0, 15)}`);
|
|
20233
|
+
}
|
|
20163
20234
|
function createKkBrowserLauncher(loadPlaywright = () => import("playwright"), ownership, leaseOptions = {}) {
|
|
20164
20235
|
return {
|
|
20165
20236
|
async launch(request) {
|
|
@@ -20188,23 +20259,25 @@ async function launchKkContext(loadPlaywright, profileDir, headless) {
|
|
|
20188
20259
|
throw new PlaywrightUnavailableError('The "playwright" dependency is not installed. Run `bunx playwright install chromium`.', { cause: error });
|
|
20189
20260
|
}
|
|
20190
20261
|
const channel = process.env.KK_BROWSER_CHANNEL?.trim() || "chromium";
|
|
20262
|
+
const launch = () => playwright.chromium.launchPersistentContext(profileDir, {
|
|
20263
|
+
headless,
|
|
20264
|
+
channel,
|
|
20265
|
+
args: ["--disable-blink-features=AutomationControlled"],
|
|
20266
|
+
ignoreDefaultArgs: ["--enable-automation"]
|
|
20267
|
+
});
|
|
20191
20268
|
let context;
|
|
20192
20269
|
try {
|
|
20193
|
-
context = await
|
|
20194
|
-
headless,
|
|
20195
|
-
channel,
|
|
20196
|
-
args: ["--disable-blink-features=AutomationControlled"],
|
|
20197
|
-
ignoreDefaultArgs: ["--enable-automation"]
|
|
20198
|
-
});
|
|
20270
|
+
context = await launch();
|
|
20199
20271
|
} catch (error) {
|
|
20200
|
-
|
|
20201
|
-
|
|
20202
|
-
|
|
20203
|
-
|
|
20204
|
-
|
|
20205
|
-
|
|
20272
|
+
if (!isInstantBrowserDeath(errorMessage2(error)))
|
|
20273
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20274
|
+
if (!quarantineProfileSyncData(profileDir))
|
|
20275
|
+
throw classifyLaunchError(error, profileDir, channel);
|
|
20276
|
+
try {
|
|
20277
|
+
context = await launch();
|
|
20278
|
+
} catch (retryError) {
|
|
20279
|
+
throw classifyLaunchError(retryError, profileDir, channel);
|
|
20206
20280
|
}
|
|
20207
|
-
throw error;
|
|
20208
20281
|
}
|
|
20209
20282
|
try {
|
|
20210
20283
|
await context.addInitScript(() => {
|
|
@@ -20361,6 +20434,7 @@ async function runProfileCli(config, argv = process.argv.slice(2), deps = {}) {
|
|
|
20361
20434
|
if (command === "login") {
|
|
20362
20435
|
const loginTimeoutMs = timeoutMs ?? config.loginTimeoutMs ?? DEFAULT_LOGIN_TIMEOUT_MS;
|
|
20363
20436
|
const launcher = deps.launcher ?? kkBrowserLauncher;
|
|
20437
|
+
stdout(`${config.channel} profile login: a browser window is opening \u2014 sign in there; waiting up to ${Math.round(loginTimeoutMs / 1000)}s for the signed-in state`);
|
|
20364
20438
|
try {
|
|
20365
20439
|
if (url === undefined) {
|
|
20366
20440
|
await loginWithProfile({
|
|
@@ -12394,7 +12394,7 @@ class ZennCliFileCliTransport {
|
|
|
12394
12394
|
return ResultSchema.parse({
|
|
12395
12395
|
ok: false,
|
|
12396
12396
|
target: "zenn",
|
|
12397
|
-
error:
|
|
12397
|
+
error: missingZennRepositoryMessage()
|
|
12398
12398
|
});
|
|
12399
12399
|
}
|
|
12400
12400
|
const repoPath = repo.startsWith("~") ? join(homedir(), repo.slice(2)) : repo;
|
|
@@ -12480,10 +12480,99 @@ class ZennCliFileCliTransport {
|
|
|
12480
12480
|
return [exitCode, stdout, stderr];
|
|
12481
12481
|
}
|
|
12482
12482
|
}
|
|
12483
|
+
function resolveZennGithubConfig(env = process.env) {
|
|
12484
|
+
const repo = env.ZENN_GITHUB_REPO?.trim() ?? "";
|
|
12485
|
+
const token = env.GITHUB_TOKEN?.trim() ?? "";
|
|
12486
|
+
if (repo === "" || token === "")
|
|
12487
|
+
return;
|
|
12488
|
+
if (!/^[\w.-]+\/[\w.-]+$/.test(repo)) {
|
|
12489
|
+
throw new Error(`ZENN_GITHUB_REPO must be 'owner/repo', got '${repo}'`);
|
|
12490
|
+
}
|
|
12491
|
+
const branch = env.ZENN_BRANCH?.trim() || "main";
|
|
12492
|
+
const username = env.ZENN_USERNAME?.trim();
|
|
12493
|
+
return { repo, token, branch, ...username ? { username } : {} };
|
|
12494
|
+
}
|
|
12495
|
+
function missingZennRepositoryMessage(env = process.env) {
|
|
12496
|
+
const base = "no Zenn deploy target configured: set ZENN_GITHUB_REPO (owner/repo) + GITHUB_TOKEN for the GitHub Contents API, or ZENN_REPO (path to a local zenn-cli checkout)";
|
|
12497
|
+
const tokenOnly = (env.ZENN_TOKEN?.trim() ?? "") !== "";
|
|
12498
|
+
return tokenOnly ? `${base}. ZENN_TOKEN alone cannot publish an article: the Zenn public API (zenn.dev/openapi.yaml) exposes scraps, comments and images only \u2014 /public-api/v0/articles answers 404.` : base;
|
|
12499
|
+
}
|
|
12500
|
+
|
|
12501
|
+
class ZennGithubApiTransport {
|
|
12502
|
+
config;
|
|
12503
|
+
fetchImpl;
|
|
12504
|
+
kind = "file-cli";
|
|
12505
|
+
constructor(config, fetchImpl = fetch) {
|
|
12506
|
+
this.config = config;
|
|
12507
|
+
this.fetchImpl = fetchImpl;
|
|
12508
|
+
}
|
|
12509
|
+
async publish(payload) {
|
|
12510
|
+
const content = payload.content;
|
|
12511
|
+
const fields = resolveArticleFields(content);
|
|
12512
|
+
const markdown = renderZennMarkdown(content, fields);
|
|
12513
|
+
const path = `articles/${fields.slug}.md`;
|
|
12514
|
+
const endpoint = `https://api.github.com/repos/${this.config.repo}/contents/${path}`;
|
|
12515
|
+
const headers = {
|
|
12516
|
+
Authorization: `Bearer ${this.config.token}`,
|
|
12517
|
+
Accept: "application/vnd.github+json",
|
|
12518
|
+
"X-GitHub-Api-Version": "2022-11-28"
|
|
12519
|
+
};
|
|
12520
|
+
try {
|
|
12521
|
+
const existing = await this.fetchImpl(`${endpoint}?ref=${encodeURIComponent(this.config.branch)}`, {
|
|
12522
|
+
headers
|
|
12523
|
+
});
|
|
12524
|
+
let sha;
|
|
12525
|
+
if (existing.ok) {
|
|
12526
|
+
const body = await existing.json();
|
|
12527
|
+
sha = typeof body.sha === "string" ? body.sha : undefined;
|
|
12528
|
+
} else if (existing.status !== 404) {
|
|
12529
|
+
return ResultSchema.parse({
|
|
12530
|
+
ok: false,
|
|
12531
|
+
target: "zenn",
|
|
12532
|
+
error: `GitHub contents read failed (${existing.status}): ${(await existing.text()).slice(0, 200)}`
|
|
12533
|
+
});
|
|
12534
|
+
}
|
|
12535
|
+
const write = await this.fetchImpl(endpoint, {
|
|
12536
|
+
method: "PUT",
|
|
12537
|
+
headers: { ...headers, "Content-Type": "application/json" },
|
|
12538
|
+
body: JSON.stringify({
|
|
12539
|
+
message: `${sha ? "Update" : "Add"} article: ${fields.title}`,
|
|
12540
|
+
content: Buffer.from(markdown, "utf-8").toString("base64"),
|
|
12541
|
+
branch: this.config.branch,
|
|
12542
|
+
...sha ? { sha } : {}
|
|
12543
|
+
})
|
|
12544
|
+
});
|
|
12545
|
+
if (!write.ok) {
|
|
12546
|
+
return ResultSchema.parse({
|
|
12547
|
+
ok: false,
|
|
12548
|
+
target: "zenn",
|
|
12549
|
+
error: `GitHub contents write failed (${write.status}): ${(await write.text()).slice(0, 200)}`
|
|
12550
|
+
});
|
|
12551
|
+
}
|
|
12552
|
+
const written = await write.json();
|
|
12553
|
+
return ResultSchema.parse({
|
|
12554
|
+
ok: true,
|
|
12555
|
+
target: "zenn",
|
|
12556
|
+
id: fields.slug,
|
|
12557
|
+
...fields.published && this.config.username ? { url: `https://zenn.dev/${this.config.username}/articles/${fields.slug}` } : {},
|
|
12558
|
+
message: `${fields.published ? "Committed a published article" : "Committed a draft article (published: false)"} to ${this.config.repo}@${this.config.branch} (${path}${written.commit?.sha ? `, commit ${written.commit.sha.slice(0, 7)}` : ""})`
|
|
12559
|
+
});
|
|
12560
|
+
} catch (err) {
|
|
12561
|
+
return ResultSchema.parse({
|
|
12562
|
+
ok: false,
|
|
12563
|
+
target: "zenn",
|
|
12564
|
+
error: `Zenn GitHub transport error: ${err instanceof Error ? err.message : String(err)}`
|
|
12565
|
+
});
|
|
12566
|
+
}
|
|
12567
|
+
}
|
|
12568
|
+
}
|
|
12483
12569
|
function getPublishTransport() {
|
|
12484
12570
|
if (process.env.KNOWLEDGE_KIT_PUBLISH_TRANSPORT === "fake") {
|
|
12485
12571
|
return new FakeFileCliTransport;
|
|
12486
12572
|
}
|
|
12573
|
+
const github = resolveZennGithubConfig();
|
|
12574
|
+
if (github !== undefined)
|
|
12575
|
+
return new ZennGithubApiTransport(github);
|
|
12487
12576
|
return new ZennCliFileCliTransport;
|
|
12488
12577
|
}
|
|
12489
12578
|
async function writePublishResult(outputPath, result) {
|
|
@@ -12535,10 +12624,13 @@ if (import.meta.main) {
|
|
|
12535
12624
|
}
|
|
12536
12625
|
export {
|
|
12537
12626
|
ZennCliFileCliTransport,
|
|
12627
|
+
ZennGithubApiTransport,
|
|
12538
12628
|
generateZennSlug,
|
|
12539
12629
|
getPublishTransport,
|
|
12540
12630
|
main2 as main,
|
|
12541
12631
|
mapContentToZennMarkdown,
|
|
12632
|
+
missingZennRepositoryMessage,
|
|
12542
12633
|
processPublishIO,
|
|
12634
|
+
resolveZennGithubConfig,
|
|
12543
12635
|
slugify2 as slugify
|
|
12544
12636
|
};
|
|
@@ -165,7 +165,7 @@ export class ZennCliFileCliTransport implements PublishTransport {
|
|
|
165
165
|
return ResultSchema.parse({
|
|
166
166
|
ok: false,
|
|
167
167
|
target: 'zenn',
|
|
168
|
-
error:
|
|
168
|
+
error: missingZennRepositoryMessage(),
|
|
169
169
|
});
|
|
170
170
|
}
|
|
171
171
|
const repoPath = repo.startsWith('~') ? join(homedir(), repo.slice(2)) : repo;
|
|
@@ -264,13 +264,146 @@ export class ZennCliFileCliTransport implements PublishTransport {
|
|
|
264
264
|
}
|
|
265
265
|
}
|
|
266
266
|
|
|
267
|
+
/** GitHub Contents-API settings for the deploy repository Zenn reads. */
|
|
268
|
+
export interface ZennGithubConfig {
|
|
269
|
+
/** `owner/repo` of the repository connected to Zenn. */
|
|
270
|
+
repo: string;
|
|
271
|
+
token: string;
|
|
272
|
+
branch: string;
|
|
273
|
+
username?: string;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* `ZENN_TOKEN` cannot publish an article: the Zenn public API (`https://zenn.dev/openapi.yaml`,
|
|
278
|
+
* read 2026-11-12) exposes Scraps, comments and images only — articles are deployed from a GitHub
|
|
279
|
+
* repository, exactly as the CLI path assumes. What the token *can* replace is the CLI binary: this
|
|
280
|
+
* transport writes `articles/<slug>.md` straight into that repository through the GitHub Contents
|
|
281
|
+
* API, so no checkout, no `npx zenn-cli`, no `git push`.
|
|
282
|
+
*
|
|
283
|
+
* Required: `ZENN_GITHUB_REPO` (`owner/repo`) + `GITHUB_TOKEN` with `contents:write`.
|
|
284
|
+
* Optional: `ZENN_BRANCH` (default `main`), `ZENN_USERNAME` (only to report the article URL).
|
|
285
|
+
*/
|
|
286
|
+
export function resolveZennGithubConfig(env: NodeJS.ProcessEnv = process.env): ZennGithubConfig | undefined {
|
|
287
|
+
const repo = env.ZENN_GITHUB_REPO?.trim() ?? '';
|
|
288
|
+
const token = env.GITHUB_TOKEN?.trim() ?? '';
|
|
289
|
+
if (repo === '' || token === '') return undefined;
|
|
290
|
+
if (!/^[\w.-]+\/[\w.-]+$/.test(repo)) {
|
|
291
|
+
throw new Error(`ZENN_GITHUB_REPO must be 'owner/repo', got '${repo}'`);
|
|
292
|
+
}
|
|
293
|
+
const branch = env.ZENN_BRANCH?.trim() || 'main';
|
|
294
|
+
const username = env.ZENN_USERNAME?.trim();
|
|
295
|
+
return { repo, token, branch, ...(username ? { username } : {}) };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* The one message a run with no usable deploy target needs to see.
|
|
300
|
+
*
|
|
301
|
+
* `ZENN_TOKEN` alone is the common half-configuration (an operator reads "Zenn now has API keys"
|
|
302
|
+
* and sets one), and the old text — "ZENN_REPO environment variable is required" — sent them
|
|
303
|
+
* looking for a checkout instead of the GitHub repository Zenn actually deploys from.
|
|
304
|
+
*/
|
|
305
|
+
export function missingZennRepositoryMessage(env: NodeJS.ProcessEnv = process.env): string {
|
|
306
|
+
const base =
|
|
307
|
+
'no Zenn deploy target configured: set ZENN_GITHUB_REPO (owner/repo) + GITHUB_TOKEN for the GitHub Contents API, or ZENN_REPO (path to a local zenn-cli checkout)';
|
|
308
|
+
const tokenOnly = (env.ZENN_TOKEN?.trim() ?? '') !== '';
|
|
309
|
+
return tokenOnly
|
|
310
|
+
? `${base}. ZENN_TOKEN alone cannot publish an article: the Zenn public API (zenn.dev/openapi.yaml) exposes scraps, comments and images only — /public-api/v0/articles answers 404.`
|
|
311
|
+
: base;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Minimal `fetch` seam so the transport is testable without network access. */
|
|
315
|
+
export type FetchLike = (url: string, init?: RequestInit) => Promise<Response>;
|
|
316
|
+
|
|
317
|
+
/** Publishes a Zenn article by committing `articles/<slug>.md` through the GitHub Contents API. */
|
|
318
|
+
export class ZennGithubApiTransport implements PublishTransport {
|
|
319
|
+
public readonly kind = 'file-cli';
|
|
320
|
+
|
|
321
|
+
constructor(
|
|
322
|
+
private readonly config: ZennGithubConfig,
|
|
323
|
+
private readonly fetchImpl: FetchLike = fetch,
|
|
324
|
+
) {}
|
|
325
|
+
|
|
326
|
+
public async publish(payload: PublishTransportPayload): Promise<Result> {
|
|
327
|
+
const content = payload.content;
|
|
328
|
+
const fields = resolveArticleFields(content);
|
|
329
|
+
const markdown = renderZennMarkdown(content, fields);
|
|
330
|
+
const path = `articles/${fields.slug}.md`;
|
|
331
|
+
const endpoint = `https://api.github.com/repos/${this.config.repo}/contents/${path}`;
|
|
332
|
+
const headers: Record<string, string> = {
|
|
333
|
+
Authorization: `Bearer ${this.config.token}`,
|
|
334
|
+
Accept: 'application/vnd.github+json',
|
|
335
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
try {
|
|
339
|
+
// An existing file must be updated with its blob sha; a new one is created without it.
|
|
340
|
+
const existing = await this.fetchImpl(`${endpoint}?ref=${encodeURIComponent(this.config.branch)}`, {
|
|
341
|
+
headers,
|
|
342
|
+
});
|
|
343
|
+
let sha: string | undefined;
|
|
344
|
+
if (existing.ok) {
|
|
345
|
+
const body = (await existing.json()) as { sha?: string };
|
|
346
|
+
sha = typeof body.sha === 'string' ? body.sha : undefined;
|
|
347
|
+
} else if (existing.status !== 404) {
|
|
348
|
+
return ResultSchema.parse({
|
|
349
|
+
ok: false,
|
|
350
|
+
target: 'zenn',
|
|
351
|
+
error: `GitHub contents read failed (${existing.status}): ${(await existing.text()).slice(0, 200)}`,
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const write = await this.fetchImpl(endpoint, {
|
|
356
|
+
method: 'PUT',
|
|
357
|
+
headers: { ...headers, 'Content-Type': 'application/json' },
|
|
358
|
+
body: JSON.stringify({
|
|
359
|
+
message: `${sha ? 'Update' : 'Add'} article: ${fields.title}`,
|
|
360
|
+
content: Buffer.from(markdown, 'utf-8').toString('base64'),
|
|
361
|
+
branch: this.config.branch,
|
|
362
|
+
...(sha ? { sha } : {}),
|
|
363
|
+
}),
|
|
364
|
+
});
|
|
365
|
+
if (!write.ok) {
|
|
366
|
+
return ResultSchema.parse({
|
|
367
|
+
ok: false,
|
|
368
|
+
target: 'zenn',
|
|
369
|
+
error: `GitHub contents write failed (${write.status}): ${(await write.text()).slice(0, 200)}`,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
const written = (await write.json()) as { commit?: { sha?: string } };
|
|
373
|
+
|
|
374
|
+
return ResultSchema.parse({
|
|
375
|
+
ok: true,
|
|
376
|
+
target: 'zenn',
|
|
377
|
+
id: fields.slug,
|
|
378
|
+
// Only a published article has a public URL; a draft (`published: false`) stays in
|
|
379
|
+
// the repository until Zenn deploys it, so reporting one would be a broken link.
|
|
380
|
+
...(fields.published && this.config.username
|
|
381
|
+
? { url: `https://zenn.dev/${this.config.username}/articles/${fields.slug}` }
|
|
382
|
+
: {}),
|
|
383
|
+
message: `${fields.published ? 'Committed a published article' : 'Committed a draft article (published: false)'} to ${this.config.repo}@${this.config.branch} (${path}${written.commit?.sha ? `, commit ${written.commit.sha.slice(0, 7)}` : ''})`,
|
|
384
|
+
});
|
|
385
|
+
} catch (err: unknown) {
|
|
386
|
+
return ResultSchema.parse({
|
|
387
|
+
ok: false,
|
|
388
|
+
target: 'zenn',
|
|
389
|
+
error: `Zenn GitHub transport error: ${err instanceof Error ? err.message : String(err)}`,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
267
395
|
/**
|
|
268
396
|
* Resolves default PublishTransport based on environment configuration.
|
|
397
|
+
*
|
|
398
|
+
* GitHub Contents API when `ZENN_GITHUB_REPO` + `GITHUB_TOKEN` are configured; otherwise the local
|
|
399
|
+
* `zenn-cli` path (`ZENN_REPO`), which still needs a checkout and a push.
|
|
269
400
|
*/
|
|
270
401
|
export function getPublishTransport(): PublishTransport {
|
|
271
402
|
if (process.env.KNOWLEDGE_KIT_PUBLISH_TRANSPORT === 'fake') {
|
|
272
403
|
return new FakeFileCliTransport();
|
|
273
404
|
}
|
|
405
|
+
const github = resolveZennGithubConfig();
|
|
406
|
+
if (github !== undefined) return new ZennGithubApiTransport(github);
|
|
274
407
|
return new ZennCliFileCliTransport();
|
|
275
408
|
}
|
|
276
409
|
|