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.
85 lines
3.1 KiB
TypeScript
85 lines
3.1 KiB
TypeScript
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import type { IPage } from '@jackwener/opencli/types';
|
|
|
|
export function buildMediumTagUrl(topic?: string): string {
|
|
return topic ? `https://medium.com/tag/${encodeURIComponent(topic)}` : 'https://medium.com/tag/technology';
|
|
}
|
|
|
|
export function buildMediumSearchUrl(keyword: string): string {
|
|
return `https://medium.com/search?q=${encodeURIComponent(keyword)}`;
|
|
}
|
|
|
|
export function buildMediumUserUrl(username: string): string {
|
|
return username.startsWith('@') ? `https://medium.com/${username}` : `https://medium.com/@${username}`;
|
|
}
|
|
|
|
export async function loadMediumPosts(page: IPage, url: string, limit: number): Promise<any[]> {
|
|
if (!page) throw new CommandExecutionError('Browser session required for medium posts');
|
|
await page.goto(url);
|
|
await page.wait({ selector: 'article', timeout: 5 });
|
|
const data = await page.evaluate(`
|
|
(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
|
|
const limit = ${Math.max(1, Math.min(limit, 50))};
|
|
const normalize = (value) => (value || '').replace(/\\s+/g, ' ').trim();
|
|
const posts = [];
|
|
const seen = new Set();
|
|
|
|
for (const article of Array.from(document.querySelectorAll('article'))) {
|
|
try {
|
|
const titleEl = article.querySelector('h2, h3, h1');
|
|
const title = normalize(titleEl?.textContent);
|
|
if (!title) continue;
|
|
|
|
const linkEl = titleEl?.closest('a') || article.querySelector('a[href*="/@"], a[href*="/p/"]');
|
|
let url = linkEl?.getAttribute('href') || '';
|
|
if (!url) continue;
|
|
if (!url.startsWith('http')) url = 'https://medium.com' + url;
|
|
if (seen.has(url)) continue;
|
|
|
|
const author = normalize(
|
|
Array.from(article.querySelectorAll('a[href^="/@"]'))
|
|
.map((node) => normalize(node.textContent))
|
|
.find((text) => text && text !== title),
|
|
);
|
|
|
|
const allText = normalize(article.textContent);
|
|
const dateEl = article.querySelector('time');
|
|
const date = normalize(dateEl?.textContent) ||
|
|
dateEl?.getAttribute('datetime') ||
|
|
allText.match(/\\b(?:[A-Z][a-z]{2}\\s+\\d{1,2}|\\d+[dhmw]\\s+ago)\\b/)?.[0] ||
|
|
'';
|
|
|
|
const readTime = allText.match(/(\\d+)\\s*min\\s*read/i)?.[0] || '';
|
|
const claps = allText.match(/\\b(\\d+(?:\\.\\d+)?[KkMm]?)\\s*claps?\\b/i)?.[1] || '';
|
|
|
|
const description = normalize(
|
|
Array.from(article.querySelectorAll('h3, p'))
|
|
.map((node) => normalize(node.textContent))
|
|
.find((text) => text && text !== title && text !== author && !/member-only story|response icon/i.test(text)),
|
|
);
|
|
|
|
seen.add(url);
|
|
posts.push({
|
|
rank: posts.length + 1,
|
|
title,
|
|
author,
|
|
date,
|
|
readTime,
|
|
claps,
|
|
description: description ? description.slice(0, 150) : '',
|
|
url,
|
|
});
|
|
|
|
if (posts.length >= limit) break;
|
|
} catch {}
|
|
}
|
|
|
|
return posts;
|
|
})()
|
|
`);
|
|
|
|
return Array.isArray(data) ? data : [];
|
|
}
|