mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
4fe9a73ebc
* 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.
110 lines
3.8 KiB
TypeScript
110 lines
3.8 KiB
TypeScript
import { AuthRequiredError, SelectorError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
|
|
cli({
|
|
site: 'twitter',
|
|
name: 'following',
|
|
description: 'Get accounts a Twitter/X user is following',
|
|
domain: 'x.com',
|
|
strategy: Strategy.INTERCEPT,
|
|
browser: true,
|
|
args: [
|
|
{ name: 'user', positional: true, type: 'string', required: false },
|
|
{ name: 'limit', type: 'int', default: 50 },
|
|
],
|
|
columns: ['screen_name', 'name', 'bio', 'followers'],
|
|
func: async (page, kwargs) => {
|
|
let targetUser = kwargs.user;
|
|
|
|
// If no user is specified, figure out the logged-in user's handle
|
|
if (!targetUser) {
|
|
await page.goto('https://x.com/home');
|
|
await page.wait({ selector: '[data-testid="primaryColumn"]' });
|
|
|
|
const href = await page.evaluate(`() => {
|
|
const link = document.querySelector('a[data-testid="AppTabBar_Profile_Link"]');
|
|
return link ? link.getAttribute('href') : null;
|
|
}`);
|
|
|
|
if (!href) {
|
|
throw new AuthRequiredError('x.com', 'Could not find logged-in user profile link. Are you logged in?');
|
|
}
|
|
targetUser = href.replace('/', '');
|
|
}
|
|
|
|
// 1. Navigate to profile page
|
|
await page.goto(`https://x.com/${targetUser}`);
|
|
await page.wait(3);
|
|
|
|
// 2. Install interceptor BEFORE SPA navigation.
|
|
// goto() resets JS context, but SPA click preserves it.
|
|
await page.installInterceptor('Following');
|
|
|
|
// 3. Click the following link via SPA navigation (preserves interceptor)
|
|
const safeUser = JSON.stringify(targetUser);
|
|
const clicked = await page.evaluate(`() => {
|
|
const target = ${safeUser};
|
|
const link = document.querySelector('a[href="/' + target + '/following"]');
|
|
if (link) { link.click(); return true; }
|
|
return false;
|
|
}`);
|
|
if (!clicked) {
|
|
throw new SelectorError('Twitter following link', 'Twitter may have changed the layout.');
|
|
}
|
|
await page.waitForCapture(5);
|
|
|
|
// 4. Scroll to trigger pagination API calls
|
|
await page.autoScroll({ times: Math.ceil(kwargs.limit / 20), delayMs: 2000 });
|
|
|
|
// 5. Retrieve intercepted data
|
|
const requests = await page.getInterceptedRequests();
|
|
const requestList = Array.isArray(requests) ? requests : [];
|
|
|
|
if (requestList.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
let results: any[] = [];
|
|
for (const req of requestList) {
|
|
try {
|
|
// GraphQL response: { data: { user: { result: { timeline: ... } } } }
|
|
let instructions = req.data?.user?.result?.timeline?.timeline?.instructions;
|
|
if (!instructions) continue;
|
|
|
|
let addEntries = instructions.find((i: any) => i.type === 'TimelineAddEntries');
|
|
if (!addEntries) {
|
|
addEntries = instructions.find((i: any) => i.entries && Array.isArray(i.entries));
|
|
}
|
|
|
|
if (!addEntries) continue;
|
|
|
|
for (const entry of addEntries.entries) {
|
|
if (!entry.entryId.startsWith('user-')) continue;
|
|
|
|
const item = entry.content?.itemContent?.user_results?.result;
|
|
if (!item || item.__typename !== 'User') continue;
|
|
|
|
const core = item.core || {};
|
|
const legacy = item.legacy || {};
|
|
|
|
results.push({
|
|
screen_name: core.screen_name || legacy.screen_name || 'unknown',
|
|
name: core.name || legacy.name || 'unknown',
|
|
bio: legacy.description || item.profile_bio?.description || '',
|
|
followers: legacy.followers_count || legacy.normal_followers_count || 0
|
|
});
|
|
}
|
|
} catch (e) {
|
|
// ignore parsing errors for individual payloads
|
|
}
|
|
}
|
|
|
|
// Deduplicate by screen_name
|
|
const unique = new Map();
|
|
results.forEach(r => unique.set(r.screen_name, r));
|
|
const deduplicatedResults = Array.from(unique.values());
|
|
|
|
return deduplicatedResults.slice(0, kwargs.limit);
|
|
}
|
|
});
|