Files
jackwener__opencli/clis/weibo/me.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

78 lines
2.8 KiB
TypeScript

/**
* Weibo me — current logged-in user profile.
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
import { CommandExecutionError } from '@jackwener/opencli/errors';
import { getSelfUid } from './utils.js';
cli({
site: 'weibo',
name: 'me',
description: 'My Weibo profile info',
domain: 'weibo.com',
strategy: Strategy.COOKIE,
args: [],
columns: ['screen_name', 'uid', 'followers', 'following', 'statuses', 'verified', 'location'],
func: async (page) => {
await page.goto('https://weibo.com');
await page.wait(2);
const uid = await getSelfUid(page);
const data = await page.evaluate(`
(async () => {
const uid = ${JSON.stringify(uid)};
// Try Vue store first
const app = document.querySelector('#app')?.__vue_app__;
const store = app?.config?.globalProperties?.$store;
const cfg = store?.state?.config?.config;
const u = cfg?.user;
// Fetch detail info
const detailResp = await fetch('/ajax/profile/detail?uid=' + uid, {credentials: 'include'});
const detail = detailResp.ok ? await detailResp.json() : null;
const d = detail?.data || {};
if (u && u.id) {
return {
screen_name: u.screen_name,
uid: u.id,
followers: u.followers_count,
following: u.friends_count,
statuses: u.statuses_count,
verified: u.verified || false,
location: u.location || '',
description: u.description || d.description || '',
avatar: u.avatar_hd || u.avatar_large || '',
profile_url: 'https://weibo.com' + (u.profile_url || '/u/' + u.id),
};
}
// Fallback: fetch profile API
const resp = await fetch('/ajax/profile/info?uid=' + uid, {credentials: 'include'});
if (!resp.ok) return {error: 'HTTP ' + resp.status};
const info = await resp.json();
if (!info.ok) return {error: 'API error'};
const p = info.data?.user;
if (!p) return {error: 'User data not found'};
return {
screen_name: p.screen_name,
uid: p.id,
followers: p.followers_count,
following: p.friends_count,
statuses: p.statuses_count,
verified: p.verified || false,
location: p.location || '',
description: p.description || d.description || '',
avatar: p.avatar_hd || p.avatar_large || '',
profile_url: 'https://weibo.com' + (p.profile_url || '/u/' + p.id),
};
})()
`);
if (!data || typeof data !== 'object') throw new CommandExecutionError('Failed to fetch profile');
if ((data as Record<string, unknown>).error) throw new CommandExecutionError(String((data as Record<string, unknown>).error));
return data as Record<string, unknown>;
},
});