Files
jackwener__opencli/clis/bloomberg/news.ts
jakevin 4fe9a73ebc refactor: migrate adapter imports to package exports (#795)
* refactor: migrate adapter imports to package exports

Replace all relative imports (../../src/registry.js, ../../browser/cdp.js, etc.)
with package exports (@jackwener/opencli/registry, @jackwener/opencli/errors, etc.)
across all 484 adapter files.

This decouples adapter import resolution from directory structure:
- User CLIs in ~/.opencli/clis/ resolve via node_modules symlink
- Internal adapters resolve via Node.js self-referencing
- No more shim files needed for import resolution

Changes:
- package.json: add sub-path exports for all public modules
- clis/**: replace relative imports with @jackwener/opencli/...
- discovery.ts: simplify ensureUserCliCompatShims to symlink-only
- registry-api.ts: export CommandArgs type
- Remove root-level shim directories (browser/, download/, pipeline/)
- Remove shim entries from tsconfig.json include and package.json files

* test: add regression tests for package exports

Prevents regressions like #788/#791 by:
1. Scanning all adapter files for forbidden relative imports
   (../../src/, ../../browser/, etc.) — fails if any remain
2. Verifying every package.json export maps to an existing source file

18 new test cases.

* fix: use junction on Windows + broaden test patterns

- discovery.ts: use 'junction' symlink type on Windows (no admin required)
- package-exports.test.ts: generalize forbidden patterns to catch any
  depth of ../ traversal (not just ../../ and ../../../)

* fix: update stale vi.mock/importActual paths in adapter tests

Test files still used old relative paths for vi.mock() and
vi.importActual() calls. Updated 5 test files to use package exports.
Also broadened regression test patterns to catch mock/importActual paths.

* fix: use rm instead of unlink for symlink cleanup, add warn on failure

Addresses review feedback from Astro-Han:
- rm() handles both symlinks and stale directories (unlink fails on dirs)
- Log a warning when symlink creation fails instead of silent catch

* docs: update import examples to use package exports

Update all documentation, contributing guides, and skills to use
@jackwener/opencli/registry instead of ../../src/registry.js.

Without this, users following the docs would write adapters with
broken imports since the old shim files are no longer created.
2026-04-05 16:02:17 +08:00

137 lines
4.7 KiB
TypeScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { CliError } from '@jackwener/opencli/errors';
import {
extractStoryMediaLinks,
renderStoryBody,
validateBloombergLink,
type BloombergStory,
} from './utils.js';
cli({
site: 'bloomberg',
name: 'news',
description: 'Read a Bloomberg story/article page and return title, full content, and media links',
domain: 'www.bloomberg.com',
strategy: Strategy.COOKIE,
browser: true,
args: [
{ name: 'link', positional: true, required: true, help: 'Bloomberg story/article URL or relative Bloomberg path' },
],
columns: ['title', 'summary', 'link', 'mediaLinks', 'content'],
func: async (page, kwargs) => {
const url = validateBloombergLink(kwargs.link);
// Navigate and wait for the page to hydrate before extracting story data.
await page.goto(url);
await page.wait({ selector: 'article', timeout: 5 });
const loadStory = async () => page.evaluate(`(() => {
const isRobot = /Are you a robot/i.test(document.title)
|| /unusual activity/i.test(document.body.innerText)
|| /click the box below to let us know you're not a robot/i.test(document.body.innerText);
if (isRobot) {
return {
errorCode: 'ROBOT_PAGE',
title: document.title,
preview: document.body.innerText.slice(0, 400),
};
}
const raw = document.querySelector('#__NEXT_DATA__')?.textContent;
if (!raw) {
return {
errorCode: 'NO_NEXT_DATA',
title: document.title,
preview: document.body.innerText.slice(0, 400),
};
}
let parsed;
try {
parsed = JSON.parse(raw);
} catch (err) {
return {
errorCode: 'BAD_NEXT_DATA',
title: document.title,
preview: document.body.innerText.slice(0, 400),
message: String(err),
};
}
const story = parsed?.props?.pageProps?.story;
if (!story) {
return {
errorCode: 'NO_STORY',
title: document.title,
preview: document.body.innerText.slice(0, 400),
};
}
return {
story: {
headline: story.headline || story.seoHeadline || story.seoTitle || document.querySelector('h1')?.textContent?.trim() || document.title,
summary: story.summary || story.socialDescription || story.seoDescription || document.querySelector('meta[name="description"]')?.getAttribute('content') || '',
url: story.url || story.readingUrl || location.href,
body: story.body || null,
lede: story.lede || null,
ledeImageUrl: story.ledeImageUrl || null,
socialImageUrl: story.socialImageUrl || null,
imageAttachments: story.imageAttachments || {},
videoAttachments: story.videoAttachments || {},
}
};
})()`);
let result: any = await loadStory();
// Retry once — Bloomberg pages sometimes hydrate slowly.
if (result?.errorCode === 'NO_NEXT_DATA' || result?.errorCode === 'NO_STORY') {
await page.wait(4);
result = await loadStory();
}
if (result?.errorCode === 'ROBOT_PAGE') {
throw new CliError(
'FETCH_ERROR',
'Bloomberg served the bot-protection page instead of article content',
'Try again later or open the article in a regular Chrome session first, then rerun the command. This command uses your current Bloomberg access and does not bypass paywall or entitlement checks.',
);
}
if (result?.errorCode) {
throw new CliError(
'PARSE_ERROR',
`Bloomberg page did not expose article story data (${result.errorCode})`,
'This command currently works on standard Bloomberg story/article pages that expose __NEXT_DATA__. Audio, video, newsletter, or other non-standard/blocked pages may not work. Access still depends on your current Bloomberg session.',
);
}
const story = result?.story as BloombergStory | undefined;
if (!story) {
throw new CliError(
'PARSE_ERROR',
'Failed to extract Bloomberg story data',
'Bloomberg may have changed the page structure.',
);
}
const content = renderStoryBody(story.body);
if (!content) {
throw new CliError(
'PARSE_ERROR',
'Bloomberg article body was empty after parsing',
'Bloomberg may have changed the story-body format, the URL may not point to a standard article page, or the page may not be accessible in your current Bloomberg session.',
);
}
return [{
title: story.headline || '',
summary: story.summary || '',
link: story.url || url,
mediaLinks: extractStoryMediaLinks(story),
content,
}];
},
});