Files
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

57 lines
2.1 KiB
JavaScript

import { cli, Strategy } from '@jackwener/opencli/registry';
import { extractXhsUserNotes, normalizeXhsUserId } from './user-helpers.js';
async function readUserSnapshot(page) {
return await page.evaluate(`
(() => {
const safeClone = (value) => {
try {
return JSON.parse(JSON.stringify(value ?? null));
} catch {
return null;
}
};
const userStore = window.__INITIAL_STATE__?.user || {};
return {
noteGroups: safeClone(userStore.notes?._value || userStore.notes || []),
pageData: safeClone(userStore.userPageData?._value || userStore.userPageData || {}),
};
})()
`);
}
cli({
site: 'xiaohongshu',
name: 'user',
description: 'Get public notes from a Xiaohongshu user profile',
domain: 'www.xiaohongshu.com',
strategy: Strategy.COOKIE,
browser: true,
args: [
{ name: 'id', type: 'string', required: true, positional: true, help: 'User id or profile URL' },
{ name: 'limit', type: 'int', default: 15, help: 'Number of notes to return' },
],
columns: ['id', 'title', 'type', 'likes', 'url'],
func: async (page, kwargs) => {
const userId = normalizeXhsUserId(String(kwargs.id));
const limit = Math.max(1, Number(kwargs.limit ?? 15));
await page.goto(`https://www.xiaohongshu.com/user/profile/${userId}`);
let snapshot = await readUserSnapshot(page);
let results = extractXhsUserNotes(snapshot ?? {}, userId);
let previousCount = results.length;
for (let i = 0; results.length < limit && i < 4; i += 1) {
await page.autoScroll({ times: 1, delayMs: 1500 });
await page.wait(1);
snapshot = await readUserSnapshot(page);
const nextResults = extractXhsUserNotes(snapshot ?? {}, userId);
if (nextResults.length <= previousCount)
break;
results = nextResults;
previousCount = nextResults.length;
}
if (results.length === 0) {
throw new Error('No public notes found for this Xiaohongshu user.');
}
return results.slice(0, limit);
},
});