@jackwener/opencli 1.1.0 → 1.1.1
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/.agents/skills/cross-project-adapter-migration/SKILL.md +2 -2
- package/.github/pull_request_template.md +7 -0
- package/.github/workflows/doc-check.yml +36 -0
- package/.github/workflows/docs.yml +7 -42
- package/CHANGELOG.md +23 -0
- package/CLI-EXPLORER.md +9 -8
- package/README.md +25 -10
- package/README.zh-CN.md +26 -11
- package/SKILL.md +95 -31
- package/dist/browser/cdp.js +6 -1
- package/dist/browser/page.d.ts +4 -1
- package/dist/browser/page.js +7 -1
- package/dist/build-manifest.js +23 -16
- package/dist/cli-manifest.json +431 -276
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +189 -162
- package/dist/clis/apple-podcasts/commands.test.d.ts +2 -0
- package/dist/clis/apple-podcasts/commands.test.js +76 -0
- package/dist/clis/apple-podcasts/search.js +2 -2
- package/dist/clis/apple-podcasts/top.js +9 -2
- package/dist/clis/arxiv/search.js +1 -1
- package/dist/clis/bilibili/dynamic.js +1 -1
- package/dist/clis/bilibili/favorite.js +1 -1
- package/dist/clis/bilibili/feed.js +1 -1
- package/dist/clis/bilibili/following.js +1 -1
- package/dist/clis/bilibili/history.js +1 -1
- package/dist/clis/bilibili/me.js +1 -1
- package/dist/clis/bilibili/ranking.js +1 -1
- package/dist/clis/bilibili/search.js +3 -3
- package/dist/clis/bilibili/subtitle.js +1 -1
- package/dist/clis/bilibili/user-videos.js +1 -1
- package/dist/{bilibili.d.ts → clis/bilibili/utils.d.ts} +1 -1
- package/dist/clis/bloomberg/businessweek.js +17 -0
- package/dist/clis/bloomberg/economics.js +17 -0
- package/dist/clis/bloomberg/feeds.d.ts +1 -0
- package/dist/clis/bloomberg/feeds.js +15 -0
- package/dist/clis/bloomberg/industries.d.ts +1 -0
- package/dist/clis/bloomberg/industries.js +17 -0
- package/dist/clis/bloomberg/main.d.ts +1 -0
- package/dist/clis/bloomberg/main.js +17 -0
- package/dist/clis/bloomberg/markets.d.ts +1 -0
- package/dist/clis/bloomberg/markets.js +17 -0
- package/dist/clis/bloomberg/news.d.ts +1 -0
- package/dist/clis/bloomberg/news.js +105 -0
- package/dist/clis/bloomberg/opinions.d.ts +1 -0
- package/dist/clis/bloomberg/opinions.js +17 -0
- package/dist/clis/bloomberg/politics.d.ts +1 -0
- package/dist/clis/bloomberg/politics.js +17 -0
- package/dist/clis/bloomberg/tech.d.ts +1 -0
- package/dist/clis/bloomberg/tech.js +17 -0
- package/dist/clis/bloomberg/utils.d.ts +34 -0
- package/dist/clis/bloomberg/utils.js +364 -0
- package/dist/clis/bloomberg/utils.test.d.ts +1 -0
- package/dist/clis/bloomberg/utils.test.js +129 -0
- package/dist/clis/boss/batchgreet.js +2 -2
- package/dist/clis/boss/chatlist.js +2 -2
- package/dist/clis/boss/detail.js +2 -2
- package/dist/clis/boss/greet.js +4 -4
- package/dist/clis/boss/search.js +1 -1
- package/dist/clis/boss/send.js +1 -1
- package/dist/clis/boss/stats.js +2 -2
- package/dist/clis/chaoxing/assignments.js +1 -1
- package/dist/clis/chaoxing/exams.js +1 -1
- package/dist/{chaoxing.d.ts → clis/chaoxing/utils.d.ts} +1 -1
- package/dist/{chaoxing.js → clis/chaoxing/utils.js} +0 -2
- package/dist/clis/chaoxing/utils.test.d.ts +1 -0
- package/dist/{chaoxing.test.js → clis/chaoxing/utils.test.js} +1 -1
- package/dist/clis/chatgpt/read.js +1 -1
- package/dist/clis/chatwise/export.js +1 -1
- package/dist/clis/chatwise/model.js +2 -2
- package/dist/clis/chatwise/screenshot.js +1 -1
- package/dist/clis/codex/export.js +1 -1
- package/dist/clis/codex/model.js +2 -2
- package/dist/clis/codex/screenshot.js +1 -1
- package/dist/clis/coupang/add-to-cart.js +3 -4
- package/dist/clis/coupang/search.js +2 -4
- package/dist/clis/coupang/utils.test.d.ts +1 -0
- package/dist/{coupang.test.js → clis/coupang/utils.test.js} +1 -1
- package/dist/clis/ctrip/search.js +1 -1
- package/dist/clis/cursor/export.js +1 -1
- package/dist/clis/cursor/model.js +2 -2
- package/dist/clis/cursor/screenshot.js +1 -1
- package/dist/clis/jike/comment.js +2 -3
- package/dist/clis/jike/create.js +1 -2
- package/dist/clis/jike/feed.js +0 -1
- package/dist/clis/jike/like.js +1 -2
- package/dist/clis/jike/notifications.js +0 -1
- package/dist/clis/jike/post.yaml +1 -0
- package/dist/clis/jike/repost.js +1 -2
- package/dist/clis/jike/search.js +2 -3
- package/dist/clis/jike/topic.yaml +1 -0
- package/dist/clis/jike/user.yaml +1 -0
- package/dist/clis/jimeng/history.yaml +0 -1
- package/dist/clis/linkedin/search.js +7 -7
- package/dist/clis/linux-do/category.yaml +1 -0
- package/dist/clis/linux-do/search.yaml +4 -3
- package/dist/clis/linux-do/topic.yaml +1 -0
- package/dist/clis/notion/export.js +1 -1
- package/dist/clis/reddit/comment.js +3 -4
- package/dist/clis/reddit/read.js +4 -5
- package/dist/clis/reddit/save.js +2 -3
- package/dist/clis/reddit/saved.js +0 -1
- package/dist/clis/reddit/search.yaml +1 -0
- package/dist/clis/reddit/subscribe.js +0 -1
- package/dist/clis/reddit/upvote.js +2 -3
- package/dist/clis/reddit/upvoted.js +0 -1
- package/dist/clis/reddit/user-comments.yaml +1 -0
- package/dist/clis/reddit/user-posts.yaml +1 -0
- package/dist/clis/reddit/user.yaml +1 -0
- package/dist/clis/reuters/search.js +1 -1
- package/dist/clis/smzdm/search.js +2 -3
- package/dist/clis/stackoverflow/search.yaml +1 -0
- package/dist/clis/steam/top-sellers.yaml +29 -0
- package/dist/clis/twitter/accept.js +2 -2
- package/dist/clis/twitter/article.js +2 -2
- package/dist/clis/twitter/block.d.ts +1 -0
- package/dist/clis/twitter/block.js +88 -0
- package/dist/clis/twitter/delete.js +1 -1
- package/dist/clis/twitter/hide-reply.d.ts +1 -0
- package/dist/clis/twitter/hide-reply.js +66 -0
- package/dist/clis/twitter/like.js +1 -1
- package/dist/clis/twitter/post.js +1 -1
- package/dist/clis/twitter/reply-dm.js +1 -1
- package/dist/clis/twitter/reply.js +2 -2
- package/dist/clis/twitter/search.js +1 -1
- package/dist/clis/twitter/thread.js +2 -2
- package/dist/clis/twitter/trending.d.ts +1 -0
- package/dist/clis/twitter/trending.js +91 -0
- package/dist/clis/twitter/unblock.d.ts +1 -0
- package/dist/clis/twitter/unblock.js +71 -0
- package/dist/clis/v2ex/topic.yaml +1 -0
- package/dist/clis/weibo/hot.js +0 -1
- package/dist/clis/weread/book.js +1 -1
- package/dist/clis/weread/highlights.js +1 -1
- package/dist/clis/weread/notes.js +1 -1
- package/dist/clis/weread/search.js +1 -1
- package/dist/clis/wikipedia/search.js +1 -1
- package/dist/clis/xiaohongshu/creator-note-detail.d.ts +15 -0
- package/dist/clis/xiaohongshu/creator-note-detail.js +69 -5
- package/dist/clis/xiaohongshu/creator-note-detail.test.js +80 -33
- package/dist/clis/xiaohongshu/creator-notes.js +35 -5
- package/dist/clis/xiaohongshu/creator-notes.test.js +35 -6
- package/dist/clis/xiaohongshu/creator-profile.js +0 -1
- package/dist/clis/xiaohongshu/creator-stats.js +0 -1
- package/dist/clis/xiaohongshu/download.js +2 -3
- package/dist/clis/xiaohongshu/feed.yaml +0 -1
- package/dist/clis/xiaohongshu/notifications.yaml +0 -1
- package/dist/clis/xiaohongshu/search.js +2 -2
- package/dist/clis/xiaohongshu/user.js +1 -2
- package/dist/clis/yahoo-finance/quote.js +0 -1
- package/dist/clis/youtube/search.js +1 -1
- package/dist/clis/youtube/transcript.js +1 -1
- package/dist/clis/youtube/video.js +1 -1
- package/dist/clis/zhihu/download.js +1 -2
- package/dist/clis/zhihu/question.js +1 -1
- package/dist/clis/zhihu/search.yaml +4 -3
- package/dist/commanderAdapter.d.ts +21 -0
- package/dist/commanderAdapter.js +111 -0
- package/dist/{engine.d.ts → discovery.d.ts} +0 -6
- package/dist/{engine.js → discovery.js} +1 -98
- package/dist/download/index.d.ts +2 -6
- package/dist/download/index.js +19 -46
- package/dist/engine.test.d.ts +1 -1
- package/dist/engine.test.js +8 -7
- package/dist/execution.d.ts +22 -0
- package/dist/execution.js +129 -0
- package/dist/explore.js +121 -107
- package/dist/external-clis.yaml +48 -0
- package/dist/external.d.ts +7 -2
- package/dist/external.js +11 -14
- package/dist/main.js +1 -1
- package/dist/pipeline/steps/browser.js +8 -2
- package/dist/registry.d.ts +2 -0
- package/dist/registry.js +2 -0
- package/dist/runtime.d.ts +5 -0
- package/dist/runtime.js +8 -0
- package/dist/serialization.d.ts +34 -0
- package/dist/serialization.js +63 -0
- package/dist/types.d.ts +4 -1
- package/docs/.vitepress/config.mts +14 -3
- package/docs/adapters/browser/arxiv.md +27 -0
- package/docs/adapters/browser/barchart.md +32 -0
- package/docs/adapters/browser/bloomberg.md +70 -0
- package/docs/adapters/browser/chaoxing.md +39 -0
- package/docs/adapters/browser/grok.md +35 -0
- package/docs/adapters/browser/hf.md +42 -0
- package/docs/adapters/browser/jike.md +45 -0
- package/docs/adapters/browser/jimeng.md +39 -0
- package/docs/adapters/browser/linux-do.md +45 -0
- package/docs/adapters/browser/sinafinance.md +35 -0
- package/docs/adapters/browser/stackoverflow.md +35 -0
- package/docs/adapters/browser/steam.md +26 -0
- package/docs/adapters/browser/twitter.md +3 -0
- package/docs/adapters/browser/weread.md +48 -0
- package/docs/adapters/browser/wikipedia.md +30 -0
- package/docs/adapters/browser/xiaohongshu.md +5 -1
- package/docs/adapters/desktop/chatgpt.md +3 -3
- package/docs/adapters/index.md +13 -0
- package/docs/advanced/download.md +4 -4
- package/docs/developer/architecture.md +17 -4
- package/package.json +1 -1
- package/scripts/check-doc-coverage.sh +69 -0
- package/scripts/copy-yaml.cjs +7 -0
- package/src/browser/cdp.ts +6 -1
- package/src/browser/page.ts +7 -1
- package/src/build-manifest.ts +25 -19
- package/src/cli.ts +218 -139
- package/src/clis/apple-podcasts/commands.test.ts +95 -0
- package/src/clis/apple-podcasts/search.ts +2 -2
- package/src/clis/apple-podcasts/top.ts +12 -2
- package/src/clis/arxiv/search.ts +1 -1
- package/src/clis/bilibili/dynamic.ts +1 -1
- package/src/clis/bilibili/favorite.ts +1 -1
- package/src/clis/bilibili/feed.ts +1 -1
- package/src/clis/bilibili/following.ts +1 -1
- package/src/clis/bilibili/history.ts +1 -1
- package/src/clis/bilibili/me.ts +1 -1
- package/src/clis/bilibili/ranking.ts +1 -1
- package/src/clis/bilibili/search.ts +3 -3
- package/src/clis/bilibili/subtitle.ts +1 -1
- package/src/clis/bilibili/user-videos.ts +1 -1
- package/src/{bilibili.ts → clis/bilibili/utils.ts} +1 -1
- package/src/clis/bloomberg/businessweek.ts +18 -0
- package/src/clis/bloomberg/economics.ts +18 -0
- package/src/clis/bloomberg/feeds.ts +16 -0
- package/src/clis/bloomberg/industries.ts +18 -0
- package/src/clis/bloomberg/main.ts +18 -0
- package/src/clis/bloomberg/markets.ts +18 -0
- package/src/clis/bloomberg/news.ts +136 -0
- package/src/clis/bloomberg/opinions.ts +18 -0
- package/src/clis/bloomberg/politics.ts +18 -0
- package/src/clis/bloomberg/tech.ts +18 -0
- package/src/clis/bloomberg/utils.test.ts +135 -0
- package/src/clis/bloomberg/utils.ts +429 -0
- package/src/clis/boss/batchgreet.ts +2 -2
- package/src/clis/boss/chatlist.ts +2 -2
- package/src/clis/boss/detail.ts +2 -2
- package/src/clis/boss/greet.ts +4 -4
- package/src/clis/boss/search.ts +1 -1
- package/src/clis/boss/send.ts +1 -1
- package/src/clis/boss/stats.ts +2 -2
- package/src/clis/chaoxing/assignments.ts +1 -1
- package/src/clis/chaoxing/exams.ts +1 -1
- package/src/{chaoxing.test.ts → clis/chaoxing/utils.test.ts} +1 -1
- package/src/{chaoxing.ts → clis/chaoxing/utils.ts} +1 -3
- package/src/clis/chatgpt/README.zh-CN.md +3 -3
- package/src/clis/chatgpt/read.ts +1 -1
- package/src/clis/chatwise/export.ts +1 -1
- package/src/clis/chatwise/model.ts +2 -2
- package/src/clis/chatwise/screenshot.ts +1 -1
- package/src/clis/codex/export.ts +1 -1
- package/src/clis/codex/model.ts +2 -2
- package/src/clis/codex/screenshot.ts +1 -1
- package/src/clis/coupang/add-to-cart.ts +3 -4
- package/src/clis/coupang/search.ts +2 -4
- package/src/{coupang.test.ts → clis/coupang/utils.test.ts} +1 -1
- package/src/clis/ctrip/search.ts +1 -1
- package/src/clis/cursor/export.ts +1 -1
- package/src/clis/cursor/model.ts +2 -2
- package/src/clis/cursor/screenshot.ts +1 -1
- package/src/clis/jike/comment.ts +2 -3
- package/src/clis/jike/create.ts +1 -2
- package/src/clis/jike/feed.ts +0 -1
- package/src/clis/jike/like.ts +1 -2
- package/src/clis/jike/notifications.ts +0 -1
- package/src/clis/jike/post.yaml +1 -0
- package/src/clis/jike/repost.ts +1 -2
- package/src/clis/jike/search.ts +2 -3
- package/src/clis/jike/topic.yaml +1 -0
- package/src/clis/jike/user.yaml +1 -0
- package/src/clis/jimeng/history.yaml +0 -1
- package/src/clis/linkedin/search.ts +7 -7
- package/src/clis/linux-do/category.yaml +1 -0
- package/src/clis/linux-do/search.yaml +4 -3
- package/src/clis/linux-do/topic.yaml +1 -0
- package/src/clis/notion/export.ts +1 -1
- package/src/clis/reddit/comment.ts +3 -4
- package/src/clis/reddit/read.ts +4 -5
- package/src/clis/reddit/save.ts +2 -3
- package/src/clis/reddit/saved.ts +0 -1
- package/src/clis/reddit/search.yaml +1 -0
- package/src/clis/reddit/subscribe.ts +0 -1
- package/src/clis/reddit/upvote.ts +2 -3
- package/src/clis/reddit/upvoted.ts +0 -1
- package/src/clis/reddit/user-comments.yaml +1 -0
- package/src/clis/reddit/user-posts.yaml +1 -0
- package/src/clis/reddit/user.yaml +1 -0
- package/src/clis/reuters/search.ts +1 -1
- package/src/clis/smzdm/search.ts +2 -3
- package/src/clis/stackoverflow/search.yaml +1 -0
- package/src/clis/steam/top-sellers.yaml +29 -0
- package/src/clis/twitter/accept.ts +2 -2
- package/src/clis/twitter/article.ts +2 -2
- package/src/clis/twitter/block.ts +92 -0
- package/src/clis/twitter/delete.ts +1 -1
- package/src/clis/twitter/hide-reply.ts +70 -0
- package/src/clis/twitter/like.ts +1 -1
- package/src/clis/twitter/post.ts +1 -1
- package/src/clis/twitter/reply-dm.ts +1 -1
- package/src/clis/twitter/reply.ts +2 -2
- package/src/clis/twitter/search.ts +1 -1
- package/src/clis/twitter/thread.ts +2 -2
- package/src/clis/twitter/trending.ts +113 -0
- package/src/clis/twitter/unblock.ts +75 -0
- package/src/clis/v2ex/topic.yaml +1 -0
- package/src/clis/weibo/hot.ts +0 -1
- package/src/clis/weread/book.ts +1 -1
- package/src/clis/weread/highlights.ts +1 -1
- package/src/clis/weread/notes.ts +1 -1
- package/src/clis/weread/search.ts +1 -1
- package/src/clis/wikipedia/search.ts +1 -1
- package/src/clis/xiaohongshu/creator-note-detail.test.ts +82 -33
- package/src/clis/xiaohongshu/creator-note-detail.ts +89 -5
- package/src/clis/xiaohongshu/creator-notes.test.ts +39 -6
- package/src/clis/xiaohongshu/creator-notes.ts +44 -5
- package/src/clis/xiaohongshu/creator-profile.ts +0 -1
- package/src/clis/xiaohongshu/creator-stats.ts +0 -1
- package/src/clis/xiaohongshu/download.ts +2 -3
- package/src/clis/xiaohongshu/feed.yaml +0 -1
- package/src/clis/xiaohongshu/notifications.yaml +0 -1
- package/src/clis/xiaohongshu/search.ts +2 -2
- package/src/clis/xiaohongshu/user.ts +1 -2
- package/src/clis/yahoo-finance/quote.ts +0 -1
- package/src/clis/youtube/search.ts +1 -1
- package/src/clis/youtube/transcript.ts +1 -1
- package/src/clis/youtube/video.ts +1 -1
- package/src/clis/zhihu/download.ts +1 -2
- package/src/clis/zhihu/question.ts +1 -1
- package/src/clis/zhihu/search.yaml +4 -3
- package/src/commanderAdapter.ts +113 -0
- package/src/{engine.ts → discovery.ts} +1 -108
- package/src/download/index.ts +21 -54
- package/src/engine.test.ts +8 -7
- package/src/execution.ts +138 -0
- package/src/explore.ts +135 -109
- package/src/external-clis.yaml +9 -0
- package/src/external.ts +15 -12
- package/src/main.ts +1 -1
- package/src/pipeline/steps/browser.ts +7 -2
- package/src/registry.ts +5 -0
- package/src/runtime.ts +9 -0
- package/src/serialization.ts +79 -0
- package/src/types.ts +1 -1
- package/tests/e2e/browser-public.test.ts +25 -0
- package/tests/e2e/public-commands.test.ts +55 -1
- package/dist/clis/twitter/trending.yaml +0 -46
- package/docs/public/CNAME +0 -1
- package/src/clis/twitter/trending.yaml +0 -46
- /package/dist/{bilibili.js → clis/bilibili/utils.js} +0 -0
- /package/dist/{chaoxing.test.d.ts → clis/bloomberg/businessweek.d.ts} +0 -0
- /package/dist/{coupang.test.d.ts → clis/bloomberg/economics.d.ts} +0 -0
- /package/dist/{coupang.d.ts → clis/coupang/utils.d.ts} +0 -0
- /package/dist/{coupang.js → clis/coupang/utils.js} +0 -0
- /package/src/{coupang.ts → clis/coupang/utils.ts} +0 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commander adapter: bridges Registry commands to Commander subcommands.
|
|
3
|
+
*
|
|
4
|
+
* This is a THIN adapter — it only handles:
|
|
5
|
+
* 1. Commander arg/option registration
|
|
6
|
+
* 2. Collecting kwargs from Commander's action args
|
|
7
|
+
* 3. Calling executeCommand (which handles browser sessions, validation, etc.)
|
|
8
|
+
* 4. Rendering output and errors
|
|
9
|
+
*
|
|
10
|
+
* All execution logic lives in execution.ts.
|
|
11
|
+
*/
|
|
12
|
+
import chalk from 'chalk';
|
|
13
|
+
import { fullName, getRegistry } from './registry.js';
|
|
14
|
+
import { formatRegistryHelpText } from './serialization.js';
|
|
15
|
+
import { render as renderOutput } from './output.js';
|
|
16
|
+
import { executeCommand } from './execution.js';
|
|
17
|
+
import { CliError } from './errors.js';
|
|
18
|
+
/**
|
|
19
|
+
* Register a single CliCommand as a Commander subcommand.
|
|
20
|
+
*/
|
|
21
|
+
export function registerCommandToProgram(siteCmd, cmd) {
|
|
22
|
+
if (siteCmd.commands.some((c) => c.name() === cmd.name))
|
|
23
|
+
return;
|
|
24
|
+
const subCmd = siteCmd.command(cmd.name).description(cmd.description);
|
|
25
|
+
// Register positional args first, then named options
|
|
26
|
+
const positionalArgs = [];
|
|
27
|
+
for (const arg of cmd.args) {
|
|
28
|
+
if (arg.positional) {
|
|
29
|
+
const bracket = arg.required ? `<${arg.name}>` : `[${arg.name}]`;
|
|
30
|
+
subCmd.argument(bracket, arg.help ?? '');
|
|
31
|
+
positionalArgs.push(arg);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const flag = arg.required ? `--${arg.name} <value>` : `--${arg.name} [value]`;
|
|
35
|
+
if (arg.required)
|
|
36
|
+
subCmd.requiredOption(flag, arg.help ?? '');
|
|
37
|
+
else if (arg.default != null)
|
|
38
|
+
subCmd.option(flag, arg.help ?? '', String(arg.default));
|
|
39
|
+
else
|
|
40
|
+
subCmd.option(flag, arg.help ?? '');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
subCmd
|
|
44
|
+
.option('-f, --format <fmt>', 'Output format: table, json, yaml, md, csv', 'table')
|
|
45
|
+
.option('-v, --verbose', 'Debug output', false);
|
|
46
|
+
subCmd.addHelpText('after', formatRegistryHelpText(cmd));
|
|
47
|
+
subCmd.action(async (...actionArgs) => {
|
|
48
|
+
const actionOpts = actionArgs[positionalArgs.length] ?? {};
|
|
49
|
+
const startTime = Date.now();
|
|
50
|
+
// ── Collect kwargs ──────────────────────────────────────────────────
|
|
51
|
+
const kwargs = {};
|
|
52
|
+
for (let i = 0; i < positionalArgs.length; i++) {
|
|
53
|
+
const v = actionArgs[i];
|
|
54
|
+
if (v !== undefined)
|
|
55
|
+
kwargs[positionalArgs[i].name] = v;
|
|
56
|
+
}
|
|
57
|
+
for (const arg of cmd.args) {
|
|
58
|
+
if (arg.positional)
|
|
59
|
+
continue;
|
|
60
|
+
const camelName = arg.name.replace(/-([a-z])/g, (_m, ch) => ch.toUpperCase());
|
|
61
|
+
const v = actionOpts[arg.name] ?? actionOpts[camelName];
|
|
62
|
+
if (v !== undefined)
|
|
63
|
+
kwargs[arg.name] = v;
|
|
64
|
+
}
|
|
65
|
+
// ── Execute + render ────────────────────────────────────────────────
|
|
66
|
+
try {
|
|
67
|
+
if (actionOpts.verbose)
|
|
68
|
+
process.env.OPENCLI_VERBOSE = '1';
|
|
69
|
+
const result = await executeCommand(cmd, kwargs, actionOpts.verbose);
|
|
70
|
+
if (actionOpts.verbose && (!result || (Array.isArray(result) && result.length === 0))) {
|
|
71
|
+
console.error(chalk.yellow('[Verbose] Warning: Command returned an empty result.'));
|
|
72
|
+
}
|
|
73
|
+
const resolved = getRegistry().get(fullName(cmd)) ?? cmd;
|
|
74
|
+
renderOutput(result, {
|
|
75
|
+
fmt: actionOpts.format,
|
|
76
|
+
columns: resolved.columns,
|
|
77
|
+
title: `${resolved.site}/${resolved.name}`,
|
|
78
|
+
elapsed: (Date.now() - startTime) / 1000,
|
|
79
|
+
source: fullName(resolved),
|
|
80
|
+
footerExtra: resolved.footerExtra?.(kwargs),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
if (err instanceof CliError) {
|
|
85
|
+
console.error(chalk.red(`Error [${err.code}]: ${err.message}`));
|
|
86
|
+
if (err.hint)
|
|
87
|
+
console.error(chalk.yellow(`Hint: ${err.hint}`));
|
|
88
|
+
}
|
|
89
|
+
else if (actionOpts.verbose && err.stack) {
|
|
90
|
+
console.error(chalk.red(err.stack));
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
console.error(chalk.red(`Error: ${err.message ?? err}`));
|
|
94
|
+
}
|
|
95
|
+
process.exitCode = 1;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Register all commands from the registry onto a Commander program.
|
|
101
|
+
*/
|
|
102
|
+
export function registerAllCommands(program, siteGroups) {
|
|
103
|
+
for (const [, cmd] of getRegistry()) {
|
|
104
|
+
let siteCmd = siteGroups.get(cmd.site);
|
|
105
|
+
if (!siteCmd) {
|
|
106
|
+
siteCmd = program.command(cmd.site).description(`${cmd.site} commands`);
|
|
107
|
+
siteGroups.set(cmd.site, siteCmd);
|
|
108
|
+
}
|
|
109
|
+
registerCommandToProgram(siteCmd, cmd);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -7,14 +7,8 @@
|
|
|
7
7
|
* TS modules are loaded lazily only when their command is executed.
|
|
8
8
|
* 2. FALLBACK (filesystem scan): Traditional runtime discovery for development.
|
|
9
9
|
*/
|
|
10
|
-
import { type CliCommand } from './registry.js';
|
|
11
|
-
import type { IPage } from './types.js';
|
|
12
10
|
/**
|
|
13
11
|
* Discover and register CLI commands.
|
|
14
12
|
* Uses pre-compiled manifest when available for instant startup.
|
|
15
13
|
*/
|
|
16
14
|
export declare function discoverClis(...dirs: string[]): Promise<void>;
|
|
17
|
-
/**
|
|
18
|
-
* Execute a CLI command. Handles lazy-loading of TS modules.
|
|
19
|
-
*/
|
|
20
|
-
export declare function executeCommand(cmd: CliCommand, page: IPage | null, rawKwargs: Record<string, any>, debug?: boolean): Promise<any>;
|
|
@@ -11,11 +11,7 @@ import * as fs from 'node:fs';
|
|
|
11
11
|
import * as path from 'node:path';
|
|
12
12
|
import yaml from 'js-yaml';
|
|
13
13
|
import { Strategy, registerCommand } from './registry.js';
|
|
14
|
-
import { executePipeline } from './pipeline.js';
|
|
15
14
|
import { log } from './logger.js';
|
|
16
|
-
import { AdapterLoadError } from './errors.js';
|
|
17
|
-
/** Set of TS module paths that have been loaded */
|
|
18
|
-
const _loadedModules = new Set();
|
|
19
15
|
/**
|
|
20
16
|
* Discover and register CLI commands.
|
|
21
17
|
* Uses pre-compiled manifest when available for instant startup.
|
|
@@ -142,6 +138,7 @@ async function registerYamlCli(filePath, defaultSite) {
|
|
|
142
138
|
type: argDef?.type ?? 'str',
|
|
143
139
|
default: argDef?.default,
|
|
144
140
|
required: argDef?.required ?? false,
|
|
141
|
+
positional: argDef?.positional ?? false,
|
|
145
142
|
help: argDef?.description ?? argDef?.help ?? '',
|
|
146
143
|
choices: argDef?.choices,
|
|
147
144
|
});
|
|
@@ -166,97 +163,3 @@ async function registerYamlCli(filePath, defaultSite) {
|
|
|
166
163
|
log.warn(`Failed to load ${filePath}: ${err.message}`);
|
|
167
164
|
}
|
|
168
165
|
}
|
|
169
|
-
/**
|
|
170
|
-
* Validates and coerces arguments based on the command's Arg definitions.
|
|
171
|
-
*/
|
|
172
|
-
function coerceAndValidateArgs(cmdArgs, kwargs) {
|
|
173
|
-
const result = { ...kwargs };
|
|
174
|
-
for (const argDef of cmdArgs) {
|
|
175
|
-
const val = result[argDef.name];
|
|
176
|
-
// 1. Check required
|
|
177
|
-
if (argDef.required && (val === undefined || val === null || val === '')) {
|
|
178
|
-
throw new Error(`Argument "${argDef.name}" is required.\n${argDef.help ? `Hint: ${argDef.help}` : ''}`);
|
|
179
|
-
}
|
|
180
|
-
if (val !== undefined && val !== null) {
|
|
181
|
-
// 2. Type coercion
|
|
182
|
-
if (argDef.type === 'int' || argDef.type === 'number') {
|
|
183
|
-
const num = Number(val);
|
|
184
|
-
if (Number.isNaN(num)) {
|
|
185
|
-
throw new Error(`Argument "${argDef.name}" must be a valid number. Received: "${val}"`);
|
|
186
|
-
}
|
|
187
|
-
result[argDef.name] = num;
|
|
188
|
-
}
|
|
189
|
-
else if (argDef.type === 'boolean' || argDef.type === 'bool') {
|
|
190
|
-
if (typeof val === 'string') {
|
|
191
|
-
const lower = val.toLowerCase();
|
|
192
|
-
if (lower === 'true' || lower === '1')
|
|
193
|
-
result[argDef.name] = true;
|
|
194
|
-
else if (lower === 'false' || lower === '0')
|
|
195
|
-
result[argDef.name] = false;
|
|
196
|
-
else
|
|
197
|
-
throw new Error(`Argument "${argDef.name}" must be a boolean (true/false). Received: "${val}"`);
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
result[argDef.name] = Boolean(val);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
// 3. Choices validation
|
|
204
|
-
const coercedVal = result[argDef.name];
|
|
205
|
-
if (argDef.choices && argDef.choices.length > 0) {
|
|
206
|
-
// Only stringent check for string/number types against choices array
|
|
207
|
-
if (!argDef.choices.map(String).includes(String(coercedVal))) {
|
|
208
|
-
throw new Error(`Argument "${argDef.name}" must be one of: ${argDef.choices.join(', ')}. Received: "${coercedVal}"`);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
else if (argDef.default !== undefined) {
|
|
213
|
-
// Set default if value is missing
|
|
214
|
-
result[argDef.name] = argDef.default;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
return result;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Execute a CLI command. Handles lazy-loading of TS modules.
|
|
221
|
-
*/
|
|
222
|
-
export async function executeCommand(cmd, page, rawKwargs, debug = false) {
|
|
223
|
-
let kwargs;
|
|
224
|
-
try {
|
|
225
|
-
kwargs = coerceAndValidateArgs(cmd.args, rawKwargs);
|
|
226
|
-
}
|
|
227
|
-
catch (err) {
|
|
228
|
-
// Re-throw validation errors clearly
|
|
229
|
-
throw new Error(`[Argument Validation Error]\n${err.message}`);
|
|
230
|
-
}
|
|
231
|
-
// Lazy-load TS module on first execution
|
|
232
|
-
const internal = cmd;
|
|
233
|
-
if (internal._lazy && internal._modulePath) {
|
|
234
|
-
const modulePath = internal._modulePath;
|
|
235
|
-
if (!_loadedModules.has(modulePath)) {
|
|
236
|
-
try {
|
|
237
|
-
await import(`file://${modulePath}`);
|
|
238
|
-
_loadedModules.add(modulePath);
|
|
239
|
-
}
|
|
240
|
-
catch (err) {
|
|
241
|
-
throw new AdapterLoadError(`Failed to load adapter module ${modulePath}: ${err.message}`, 'Check that the adapter file exists and has no syntax errors.');
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
// After loading, the module's cli() call will have updated the registry
|
|
245
|
-
// with the real func/pipeline. Re-fetch the command.
|
|
246
|
-
const { getRegistry, fullName } = await import('./registry.js');
|
|
247
|
-
const updated = getRegistry().get(fullName(cmd));
|
|
248
|
-
if (updated && updated.func) {
|
|
249
|
-
return updated.func(page, kwargs, debug);
|
|
250
|
-
}
|
|
251
|
-
if (updated && updated.pipeline) {
|
|
252
|
-
return executePipeline(page, updated.pipeline, { args: kwargs, debug });
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (cmd.func) {
|
|
256
|
-
return cmd.func(page, kwargs, debug);
|
|
257
|
-
}
|
|
258
|
-
if (cmd.pipeline) {
|
|
259
|
-
return executePipeline(page, cmd.pipeline, { args: kwargs, debug });
|
|
260
|
-
}
|
|
261
|
-
throw new Error(`Command ${cmd.site}/${cmd.name} has no func or pipeline`);
|
|
262
|
-
}
|
package/dist/download/index.d.ts
CHANGED
|
@@ -23,13 +23,9 @@ export interface BrowserCookie {
|
|
|
23
23
|
httpOnly?: boolean;
|
|
24
24
|
expirationDate?: number;
|
|
25
25
|
}
|
|
26
|
-
/**
|
|
27
|
-
* Check if yt-dlp is available in PATH.
|
|
28
|
-
*/
|
|
26
|
+
/** Check if yt-dlp is available in PATH. */
|
|
29
27
|
export declare function checkYtdlp(): boolean;
|
|
30
|
-
/**
|
|
31
|
-
* Check if ffmpeg is available in PATH.
|
|
32
|
-
*/
|
|
28
|
+
/** Check if ffmpeg is available in PATH. */
|
|
33
29
|
export declare function checkFfmpeg(): boolean;
|
|
34
30
|
/**
|
|
35
31
|
* Detect content type from URL and optional headers.
|
package/dist/download/index.js
CHANGED
|
@@ -1,42 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Download utilities: HTTP downloads, yt-dlp wrapper, format conversion.
|
|
3
3
|
*/
|
|
4
|
-
import { spawn
|
|
4
|
+
import { spawn } from 'node:child_process';
|
|
5
5
|
import * as fs from 'node:fs';
|
|
6
6
|
import * as path from 'node:path';
|
|
7
7
|
import * as https from 'node:https';
|
|
8
8
|
import * as http from 'node:http';
|
|
9
9
|
import * as os from 'node:os';
|
|
10
10
|
import { URL } from 'node:url';
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*/
|
|
11
|
+
import { isBinaryInstalled } from '../external.js';
|
|
12
|
+
/** Check if yt-dlp is available in PATH. */
|
|
14
13
|
export function checkYtdlp() {
|
|
15
|
-
|
|
16
|
-
execSync('yt-dlp --version', { encoding: 'utf-8', stdio: 'pipe' });
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
14
|
+
return isBinaryInstalled('yt-dlp');
|
|
22
15
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Check if ffmpeg is available in PATH.
|
|
25
|
-
*/
|
|
16
|
+
/** Check if ffmpeg is available in PATH. */
|
|
26
17
|
export function checkFfmpeg() {
|
|
27
|
-
|
|
28
|
-
execSync('ffmpeg -version', { encoding: 'utf-8', stdio: 'pipe' });
|
|
29
|
-
return true;
|
|
30
|
-
}
|
|
31
|
-
catch {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
18
|
+
return isBinaryInstalled('ffmpeg');
|
|
34
19
|
}
|
|
20
|
+
/** Domains that host video content and can be downloaded via yt-dlp. */
|
|
21
|
+
const VIDEO_PLATFORM_DOMAINS = [
|
|
22
|
+
'youtube.com', 'youtu.be', 'bilibili.com', 'twitter.com',
|
|
23
|
+
'x.com', 'tiktok.com', 'vimeo.com', 'twitch.tv',
|
|
24
|
+
];
|
|
25
|
+
const IMAGE_EXTENSIONS = new Set(['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico', '.bmp', '.avif']);
|
|
26
|
+
const VIDEO_EXTENSIONS = new Set(['.mp4', '.webm', '.avi', '.mov', '.mkv', '.flv', '.m3u8', '.ts']);
|
|
27
|
+
const DOC_EXTENSIONS = new Set(['.html', '.htm', '.json', '.xml', '.txt', '.md', '.markdown']);
|
|
35
28
|
/**
|
|
36
29
|
* Detect content type from URL and optional headers.
|
|
37
30
|
*/
|
|
38
31
|
export function detectContentType(url, contentType) {
|
|
39
|
-
// Check content-type header first
|
|
40
32
|
if (contentType) {
|
|
41
33
|
if (contentType.startsWith('image/'))
|
|
42
34
|
return 'image';
|
|
@@ -45,28 +37,16 @@ export function detectContentType(url, contentType) {
|
|
|
45
37
|
if (contentType.startsWith('text/') || contentType.includes('json') || contentType.includes('xml'))
|
|
46
38
|
return 'document';
|
|
47
39
|
}
|
|
48
|
-
// Detect from URL
|
|
49
40
|
const urlLower = url.toLowerCase();
|
|
50
41
|
const ext = path.extname(new URL(url).pathname).toLowerCase();
|
|
51
|
-
|
|
52
|
-
if (['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.ico', '.bmp', '.avif'].includes(ext)) {
|
|
42
|
+
if (IMAGE_EXTENSIONS.has(ext))
|
|
53
43
|
return 'image';
|
|
54
|
-
|
|
55
|
-
// Video extensions
|
|
56
|
-
if (['.mp4', '.webm', '.avi', '.mov', '.mkv', '.flv', '.m3u8', '.ts'].includes(ext)) {
|
|
44
|
+
if (VIDEO_EXTENSIONS.has(ext))
|
|
57
45
|
return 'video';
|
|
58
|
-
|
|
59
|
-
// Video platforms (need yt-dlp)
|
|
60
|
-
if (urlLower.includes('youtube.com') || urlLower.includes('youtu.be') ||
|
|
61
|
-
urlLower.includes('bilibili.com') || urlLower.includes('twitter.com') ||
|
|
62
|
-
urlLower.includes('x.com') || urlLower.includes('tiktok.com') ||
|
|
63
|
-
urlLower.includes('vimeo.com') || urlLower.includes('twitch.tv')) {
|
|
46
|
+
if (VIDEO_PLATFORM_DOMAINS.some(d => urlLower.includes(d)))
|
|
64
47
|
return 'video';
|
|
65
|
-
|
|
66
|
-
// Document extensions
|
|
67
|
-
if (['.html', '.htm', '.json', '.xml', '.txt', '.md', '.markdown'].includes(ext)) {
|
|
48
|
+
if (DOC_EXTENSIONS.has(ext))
|
|
68
49
|
return 'document';
|
|
69
|
-
}
|
|
70
50
|
return 'binary';
|
|
71
51
|
}
|
|
72
52
|
/**
|
|
@@ -74,14 +54,7 @@ export function detectContentType(url, contentType) {
|
|
|
74
54
|
*/
|
|
75
55
|
export function requiresYtdlp(url) {
|
|
76
56
|
const urlLower = url.toLowerCase();
|
|
77
|
-
return (urlLower.includes(
|
|
78
|
-
urlLower.includes('youtu.be') ||
|
|
79
|
-
urlLower.includes('bilibili.com/video') ||
|
|
80
|
-
urlLower.includes('twitter.com') ||
|
|
81
|
-
urlLower.includes('x.com') ||
|
|
82
|
-
urlLower.includes('tiktok.com') ||
|
|
83
|
-
urlLower.includes('vimeo.com') ||
|
|
84
|
-
urlLower.includes('twitch.tv'));
|
|
57
|
+
return VIDEO_PLATFORM_DOMAINS.some(d => urlLower.includes(d));
|
|
85
58
|
}
|
|
86
59
|
/**
|
|
87
60
|
* HTTP download with progress callback.
|
package/dist/engine.test.d.ts
CHANGED
package/dist/engine.test.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Tests for
|
|
2
|
+
* Tests for discovery and execution modules.
|
|
3
3
|
*/
|
|
4
4
|
import { describe, it, expect } from 'vitest';
|
|
5
|
-
import { discoverClis
|
|
5
|
+
import { discoverClis } from './discovery.js';
|
|
6
|
+
import { executeCommand } from './execution.js';
|
|
6
7
|
import { cli, Strategy } from './registry.js';
|
|
7
8
|
describe('discoverClis', () => {
|
|
8
9
|
it('handles non-existent directories gracefully', async () => {
|
|
@@ -23,7 +24,7 @@ describe('executeCommand', () => {
|
|
|
23
24
|
],
|
|
24
25
|
func: async (_page, kwargs) => [{ noteId: kwargs['note-id'] }],
|
|
25
26
|
});
|
|
26
|
-
const result = await executeCommand(cmd,
|
|
27
|
+
const result = await executeCommand(cmd, { 'note-id': 'abc123' });
|
|
27
28
|
expect(result).toEqual([{ noteId: 'abc123' }]);
|
|
28
29
|
});
|
|
29
30
|
it('executes a command with func', async () => {
|
|
@@ -37,7 +38,7 @@ describe('executeCommand', () => {
|
|
|
37
38
|
return [{ title: kwargs.query ?? 'default' }];
|
|
38
39
|
},
|
|
39
40
|
});
|
|
40
|
-
const result = await executeCommand(cmd,
|
|
41
|
+
const result = await executeCommand(cmd, { query: 'hello' });
|
|
41
42
|
expect(result).toEqual([{ title: 'hello' }]);
|
|
42
43
|
});
|
|
43
44
|
it('executes a command with pipeline', async () => {
|
|
@@ -53,7 +54,7 @@ describe('executeCommand', () => {
|
|
|
53
54
|
],
|
|
54
55
|
});
|
|
55
56
|
// Pipeline commands require page for evaluate step, so we'll test the error path
|
|
56
|
-
await expect(executeCommand(cmd,
|
|
57
|
+
await expect(executeCommand(cmd, {})).rejects.toThrow();
|
|
57
58
|
});
|
|
58
59
|
it('throws for command with no func or pipeline', async () => {
|
|
59
60
|
const cmd = cli({
|
|
@@ -62,7 +63,7 @@ describe('executeCommand', () => {
|
|
|
62
63
|
description: 'empty command',
|
|
63
64
|
browser: false,
|
|
64
65
|
});
|
|
65
|
-
await expect(executeCommand(cmd,
|
|
66
|
+
await expect(executeCommand(cmd, {})).rejects.toThrow('has no func or pipeline');
|
|
66
67
|
});
|
|
67
68
|
it('passes debug flag to func', async () => {
|
|
68
69
|
let receivedDebug = false;
|
|
@@ -76,7 +77,7 @@ describe('executeCommand', () => {
|
|
|
76
77
|
return [];
|
|
77
78
|
},
|
|
78
79
|
});
|
|
79
|
-
await executeCommand(cmd,
|
|
80
|
+
await executeCommand(cmd, {}, true);
|
|
80
81
|
expect(receivedDebug).toBe(true);
|
|
81
82
|
});
|
|
82
83
|
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command execution: validates args, manages browser sessions, runs commands.
|
|
3
|
+
*
|
|
4
|
+
* This is the single entry point for executing any CLI command. It handles:
|
|
5
|
+
* 1. Argument validation and coercion
|
|
6
|
+
* 2. Browser session lifecycle (if needed)
|
|
7
|
+
* 3. Domain pre-navigation for cookie/header strategies
|
|
8
|
+
* 4. Timeout enforcement
|
|
9
|
+
* 5. Lazy-loading of TS modules from manifest
|
|
10
|
+
*/
|
|
11
|
+
import { type CliCommand, type Arg } from './registry.js';
|
|
12
|
+
/**
|
|
13
|
+
* Validates and coerces arguments based on the command's Arg definitions.
|
|
14
|
+
*/
|
|
15
|
+
export declare function coerceAndValidateArgs(cmdArgs: Arg[], kwargs: Record<string, any>): Record<string, any>;
|
|
16
|
+
/**
|
|
17
|
+
* Execute a CLI command. Automatically manages browser sessions when needed.
|
|
18
|
+
*
|
|
19
|
+
* This is the unified entry point — callers don't need to care about
|
|
20
|
+
* whether the command requires a browser or not.
|
|
21
|
+
*/
|
|
22
|
+
export declare function executeCommand(cmd: CliCommand, rawKwargs: Record<string, any>, debug?: boolean): Promise<any>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command execution: validates args, manages browser sessions, runs commands.
|
|
3
|
+
*
|
|
4
|
+
* This is the single entry point for executing any CLI command. It handles:
|
|
5
|
+
* 1. Argument validation and coercion
|
|
6
|
+
* 2. Browser session lifecycle (if needed)
|
|
7
|
+
* 3. Domain pre-navigation for cookie/header strategies
|
|
8
|
+
* 4. Timeout enforcement
|
|
9
|
+
* 5. Lazy-loading of TS modules from manifest
|
|
10
|
+
*/
|
|
11
|
+
import { Strategy, getRegistry, fullName } from './registry.js';
|
|
12
|
+
import { executePipeline } from './pipeline.js';
|
|
13
|
+
import { AdapterLoadError } from './errors.js';
|
|
14
|
+
import { shouldUseBrowserSession } from './capabilityRouting.js';
|
|
15
|
+
import { getBrowserFactory, browserSession, runWithTimeout, DEFAULT_BROWSER_COMMAND_TIMEOUT } from './runtime.js';
|
|
16
|
+
/** Set of TS module paths that have been loaded */
|
|
17
|
+
const _loadedModules = new Set();
|
|
18
|
+
/**
|
|
19
|
+
* Validates and coerces arguments based on the command's Arg definitions.
|
|
20
|
+
*/
|
|
21
|
+
export function coerceAndValidateArgs(cmdArgs, kwargs) {
|
|
22
|
+
const result = { ...kwargs };
|
|
23
|
+
for (const argDef of cmdArgs) {
|
|
24
|
+
const val = result[argDef.name];
|
|
25
|
+
// 1. Check required
|
|
26
|
+
if (argDef.required && (val === undefined || val === null || val === '')) {
|
|
27
|
+
throw new Error(`Argument "${argDef.name}" is required.\n${argDef.help ? `Hint: ${argDef.help}` : ''}`);
|
|
28
|
+
}
|
|
29
|
+
if (val !== undefined && val !== null) {
|
|
30
|
+
// 2. Type coercion
|
|
31
|
+
if (argDef.type === 'int' || argDef.type === 'number') {
|
|
32
|
+
const num = Number(val);
|
|
33
|
+
if (Number.isNaN(num)) {
|
|
34
|
+
throw new Error(`Argument "${argDef.name}" must be a valid number. Received: "${val}"`);
|
|
35
|
+
}
|
|
36
|
+
result[argDef.name] = num;
|
|
37
|
+
}
|
|
38
|
+
else if (argDef.type === 'boolean' || argDef.type === 'bool') {
|
|
39
|
+
if (typeof val === 'string') {
|
|
40
|
+
const lower = val.toLowerCase();
|
|
41
|
+
if (lower === 'true' || lower === '1')
|
|
42
|
+
result[argDef.name] = true;
|
|
43
|
+
else if (lower === 'false' || lower === '0')
|
|
44
|
+
result[argDef.name] = false;
|
|
45
|
+
else
|
|
46
|
+
throw new Error(`Argument "${argDef.name}" must be a boolean (true/false). Received: "${val}"`);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
result[argDef.name] = Boolean(val);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// 3. Choices validation
|
|
53
|
+
const coercedVal = result[argDef.name];
|
|
54
|
+
if (argDef.choices && argDef.choices.length > 0) {
|
|
55
|
+
if (!argDef.choices.map(String).includes(String(coercedVal))) {
|
|
56
|
+
throw new Error(`Argument "${argDef.name}" must be one of: ${argDef.choices.join(', ')}. Received: "${coercedVal}"`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else if (argDef.default !== undefined) {
|
|
61
|
+
result[argDef.name] = argDef.default;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Run a command's func or pipeline against a page.
|
|
68
|
+
*/
|
|
69
|
+
async function runCommand(cmd, page, kwargs, debug) {
|
|
70
|
+
// Lazy-load TS module on first execution (manifest fast-path)
|
|
71
|
+
const internal = cmd;
|
|
72
|
+
if (internal._lazy && internal._modulePath) {
|
|
73
|
+
const modulePath = internal._modulePath;
|
|
74
|
+
if (!_loadedModules.has(modulePath)) {
|
|
75
|
+
try {
|
|
76
|
+
await import(`file://${modulePath}`);
|
|
77
|
+
_loadedModules.add(modulePath);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
throw new AdapterLoadError(`Failed to load adapter module ${modulePath}: ${err.message}`, 'Check that the adapter file exists and has no syntax errors.');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// After loading, the module's cli() call will have updated the registry.
|
|
84
|
+
const updated = getRegistry().get(fullName(cmd));
|
|
85
|
+
if (updated?.func)
|
|
86
|
+
return updated.func(page, kwargs, debug);
|
|
87
|
+
if (updated?.pipeline)
|
|
88
|
+
return executePipeline(page, updated.pipeline, { args: kwargs, debug });
|
|
89
|
+
}
|
|
90
|
+
if (cmd.func)
|
|
91
|
+
return cmd.func(page, kwargs, debug);
|
|
92
|
+
if (cmd.pipeline)
|
|
93
|
+
return executePipeline(page, cmd.pipeline, { args: kwargs, debug });
|
|
94
|
+
throw new Error(`Command ${fullName(cmd)} has no func or pipeline`);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Execute a CLI command. Automatically manages browser sessions when needed.
|
|
98
|
+
*
|
|
99
|
+
* This is the unified entry point — callers don't need to care about
|
|
100
|
+
* whether the command requires a browser or not.
|
|
101
|
+
*/
|
|
102
|
+
export async function executeCommand(cmd, rawKwargs, debug = false) {
|
|
103
|
+
let kwargs;
|
|
104
|
+
try {
|
|
105
|
+
kwargs = coerceAndValidateArgs(cmd.args, rawKwargs);
|
|
106
|
+
}
|
|
107
|
+
catch (err) {
|
|
108
|
+
throw new Error(`[Argument Validation Error]\n${err.message}`);
|
|
109
|
+
}
|
|
110
|
+
if (shouldUseBrowserSession(cmd)) {
|
|
111
|
+
const BrowserFactory = getBrowserFactory();
|
|
112
|
+
return browserSession(BrowserFactory, async (page) => {
|
|
113
|
+
// Cookie/header strategies require same-origin context for credentialed fetch.
|
|
114
|
+
if ((cmd.strategy === Strategy.COOKIE || cmd.strategy === Strategy.HEADER) && cmd.domain) {
|
|
115
|
+
try {
|
|
116
|
+
await page.goto(`https://${cmd.domain}`);
|
|
117
|
+
await page.wait(2);
|
|
118
|
+
}
|
|
119
|
+
catch { }
|
|
120
|
+
}
|
|
121
|
+
return runWithTimeout(runCommand(cmd, page, kwargs, debug), {
|
|
122
|
+
timeout: cmd.timeoutSeconds ?? DEFAULT_BROWSER_COMMAND_TIMEOUT,
|
|
123
|
+
label: fullName(cmd),
|
|
124
|
+
});
|
|
125
|
+
}, { workspace: `site:${cmd.site}` });
|
|
126
|
+
}
|
|
127
|
+
// Non-browser commands run directly
|
|
128
|
+
return runCommand(cmd, null, kwargs, debug);
|
|
129
|
+
}
|