Files
jackwener__opencli/clis/jd/item.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

105 lines
3.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 京东商品详情 — browser cookie, DOM scraping + evaluate.
*
* 依赖: 需要在 Chrome 已登录京东
* 用法: opencli jd item 100291143898
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
function extractAvifImages(imageUrls: string[], maxImages: number): string[] {
const unique = [...new Set(imageUrls.filter(Boolean))];
return unique
.filter((url) => url.includes('.avif') && url.includes('pcpubliccms'))
.slice(0, maxImages);
}
cli({
site: 'jd',
name: 'item',
description: '京东商品详情(价格、店铺、规格参数、AVIF 图片)',
domain: 'item.jd.com',
strategy: Strategy.COOKIE,
args: [
{
name: 'sku',
required: true,
positional: true,
help: '商品 SKU ID(如 100291143898)',
},
{
name: 'images',
type: 'int',
default: 10,
help: 'AVIF 图片数量上限(默认10)',
},
],
columns: ['title', 'price', 'shop', 'specs', 'avifImages'],
func: async (page, kwargs) => {
const sku = kwargs.sku;
const maxImages = kwargs.images as number;
const url = `https://item.jd.com/${sku}.html`;
await page.goto(url, { waitUntil: 'load' });
await page.wait(2);
// 滚动加载商品详情区域中的延迟图片
for (let i = 0; i < 6; i++) {
await page.evaluate(`window.scrollTo(0, ${i * 2500})`);
await page.wait(1);
}
await page.evaluate(`window.scrollTo(0, document.body.scrollHeight)`);
await page.wait(2);
const data = await page.evaluate(`
(() => {
const maxImg = ${maxImages};
// 尝试多种价格选择器
const skuMatch = location.pathname.match(/(\\d+)\\.html/);
const sku = skuMatch ? skuMatch[1] : '';
const priceEl = document.querySelector('.J-p-' + sku) ||
document.querySelector('[class*="price"] [class*="num"]') ||
document.querySelector('.p-price strong') ||
document.querySelector('.price.jd-price');
const price = priceEl?.textContent?.trim() || 'not found';
// 标题
const title = document.querySelector('.product-title')?.textContent?.trim() ||
document.title.split('-')[0].trim();
// 店铺
const shop = document.querySelector('.J-shop-name')?.textContent?.trim() || '京东自营';
// 所有图片
const allImgs = Array.from(document.querySelectorAll('img[src*="360buyimg.com"]'));
const srcs = allImgs.map(img => img.src).filter(Boolean);
// 所有 avif 图片(去重,只保留 pcpubliccms CDN)
const avifImages = ${extractAvifImages.toString()}(srcs, maxImg);
// 规格参数:从页面文本提取
const text = document.body.innerText;
const specMatch = text.match(/商品编号[\\s\\S]*?(?=包装清单|\\n\\n|$)/);
let specs = {};
if (specMatch) {
const lines = specMatch[0].split('\\n').filter(l => l.trim());
for (let i = 0; i < lines.length - 1; i += 2) {
const key = lines[i].trim();
const val = lines[i + 1]?.trim() || '';
if (key && val && key !== '商品编号') {
specs[key] = val;
}
}
}
return { title, price, shop, specs, avifImages, totalImages: new Set(srcs).size };
})()
`);
return [data];
},
});
export const __test__ = {
extractAvifImages,
};