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

56 lines
2.2 KiB
JavaScript

/**
* Shared Xiaoyuzhou utilities — page data extraction and formatting.
*
* Xiaoyuzhou (小宇宙) is a Next.js app that embeds full page data in
* <script id="__NEXT_DATA__">. We fetch the HTML and extract that JSON
* instead of using their authenticated API.
*/
import { CliError } from '@jackwener/opencli/errors';
/**
* Fetch a Xiaoyuzhou page and extract __NEXT_DATA__.props.pageProps.
* @param path - URL path, e.g. '/podcast/xxx' or '/episode/xxx'
*/
export async function fetchPageProps(path) {
const url = `https://www.xiaoyuzhoufm.com${path}`;
// Node.js fetch sends UA "node" which gets blocked; use a browser-like UA
const resp = await fetch(url, {
headers: { 'User-Agent': 'Mozilla/5.0 (compatible; opencli)' },
});
if (!resp.ok) {
throw new CliError('FETCH_ERROR', `HTTP ${resp.status} for ${path}`, 'Please check the ID — you can find it in xiaoyuzhoufm.com URLs');
}
const html = await resp.text();
// [\s\S]*? for multiline safety (JSON may span lines)
const match = html.match(/<script id="__NEXT_DATA__"[^>]*>([\s\S]*?)<\/script>/);
if (!match) {
throw new CliError('PARSE_ERROR', 'Failed to extract __NEXT_DATA__', 'Page structure may have changed');
}
let parsed;
try {
parsed = JSON.parse(match[1]);
}
catch {
throw new CliError('PARSE_ERROR', 'Malformed __NEXT_DATA__ JSON', 'Page structure may have changed');
}
const pageProps = parsed.props?.pageProps;
if (!pageProps || Object.keys(pageProps).length === 0) {
throw new CliError('NOT_FOUND', 'Resource not found', 'Please check the ID — you can find it in xiaoyuzhoufm.com URLs');
}
return pageProps;
}
/** Format seconds to mm:ss (e.g. 3890 → "64:50"). Returns '-' for invalid input. */
export function formatDuration(seconds) {
if (!Number.isFinite(seconds) || seconds < 0)
return '-';
seconds = Math.round(seconds);
const m = Math.floor(seconds / 60);
const s = seconds % 60;
return `${m}:${String(s).padStart(2, '0')}`;
}
/** Format ISO date string to YYYY-MM-DD. Returns '-' for missing input. */
export function formatDate(iso) {
if (!iso)
return '-';
return iso.slice(0, 10);
}