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.
186 lines
6.6 KiB
TypeScript
186 lines
6.6 KiB
TypeScript
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import type { IPage } from '@jackwener/opencli/types';
|
|
import {
|
|
buildProductUrl,
|
|
buildProvenance,
|
|
cleanText,
|
|
extractAsin,
|
|
isAmazonEntity,
|
|
normalizeProductUrl,
|
|
PRIMARY_PRICE_SELECTORS,
|
|
parsePriceText,
|
|
assertUsableState,
|
|
gotoAndReadState,
|
|
} from './shared.js';
|
|
|
|
interface OfferPayload {
|
|
href?: string;
|
|
title?: string;
|
|
price_text?: string | null;
|
|
merchant_info?: string | null;
|
|
sold_by?: string | null;
|
|
ships_from_text?: string | null;
|
|
offer_link?: string | null;
|
|
review_url?: string | null;
|
|
qa_url?: string | null;
|
|
buybox_text?: string | null;
|
|
}
|
|
|
|
const OFFER_FACT_SELECTOR = [
|
|
'#sellerProfileTriggerId',
|
|
'#shipsFromSoldByInsideBuyBox_feature_div',
|
|
'#fulfillerInfoFeature_feature_div',
|
|
'#merchantInfoFeature_feature_div',
|
|
'#tabular-buybox-container',
|
|
'#merchant-info',
|
|
].join(', ');
|
|
|
|
function collapseAdjacentWords(text: string): string {
|
|
const parts = cleanText(text).split(' ').filter(Boolean);
|
|
const deduped: string[] = [];
|
|
for (const part of parts) {
|
|
if (deduped[deduped.length - 1] === part) continue;
|
|
deduped.push(part);
|
|
}
|
|
return deduped.join(' ');
|
|
}
|
|
|
|
function extractShipsFrom(text: string): string | null {
|
|
const normalized = cleanText(text);
|
|
const match = normalized.match(/Ships from\s+(.+?)(?=Sold by|and Fulfilled by|$)/i);
|
|
return match ? collapseAdjacentWords(match[1].replace(/Ships from/ig, '')) : null;
|
|
}
|
|
|
|
function extractSoldBy(text: string): string | null {
|
|
const normalized = cleanText(text);
|
|
const match = normalized.match(/Sold by\s+(.+?)(?=and Fulfilled by|Ships from|$)/i);
|
|
return match ? collapseAdjacentWords(match[1]) : null;
|
|
}
|
|
|
|
function isDeliveryLocationBlocked(text: string | null | undefined): boolean {
|
|
const normalized = cleanText(text).toLowerCase();
|
|
return normalized.includes('cannot be shipped to your selected delivery location')
|
|
|| normalized.includes('similar items shipping to')
|
|
|| normalized.includes('deliver to hong kong');
|
|
}
|
|
|
|
function normalizeOfferPayload(payload: OfferPayload): Record<string, unknown> {
|
|
const asin = extractAsin(payload.href ?? '') ?? null;
|
|
const sourceUrl = cleanText(payload.href) || buildProductUrl(payload.href ?? '');
|
|
const price = parsePriceText(payload.price_text);
|
|
const merchantInfo = cleanText(payload.merchant_info) || null;
|
|
const soldBy = cleanText(payload.sold_by)
|
|
|| extractSoldBy(payload.ships_from_text ?? '')
|
|
|| extractSoldBy(merchantInfo ?? '')
|
|
|| null;
|
|
const shipsFrom = extractShipsFrom(payload.ships_from_text ?? '')
|
|
|| extractShipsFrom(merchantInfo ?? '')
|
|
|| cleanText(payload.ships_from_text)
|
|
|| null;
|
|
const provenance = buildProvenance(sourceUrl);
|
|
|
|
return {
|
|
asin,
|
|
product_url: normalizeProductUrl(payload.href),
|
|
...provenance,
|
|
price_text: price.price_text,
|
|
price_value: price.price_value,
|
|
currency: price.currency,
|
|
merchant_info_text: merchantInfo,
|
|
sold_by: soldBy,
|
|
ships_from: shipsFrom,
|
|
offer_listing_url: cleanText(payload.offer_link) || null,
|
|
review_url: cleanText(payload.review_url) || null,
|
|
qa_url: cleanText(payload.qa_url) || null,
|
|
is_amazon_sold: isAmazonEntity(soldBy),
|
|
is_amazon_fulfilled: isAmazonEntity(shipsFrom) || /fulfilled by amazon/i.test(merchantInfo ?? ''),
|
|
};
|
|
}
|
|
|
|
async function readOfferPayload(page: IPage, input: string): Promise<OfferPayload> {
|
|
const url = buildProductUrl(input);
|
|
const state = await gotoAndReadState(page, url, 2500, 'offer');
|
|
assertUsableState(state, 'offer');
|
|
|
|
// Reconnecting to an existing Amazon target can surface the product page
|
|
// before the buy-box / merchant blocks are reattached to the DOM.
|
|
await page.wait({ selector: OFFER_FACT_SELECTOR, timeout: 6 }).catch(() => {});
|
|
|
|
return await page.evaluate(`
|
|
(() => ({
|
|
href: window.location.href,
|
|
title: document.title || '',
|
|
price_text: (() => {
|
|
const selectors = ${JSON.stringify(PRIMARY_PRICE_SELECTORS)};
|
|
for (const selector of selectors) {
|
|
const text = document.querySelector(selector)?.textContent || '';
|
|
if (text.trim()) return text;
|
|
}
|
|
return '';
|
|
})(),
|
|
merchant_info: document.querySelector('#merchant-info')?.textContent || '',
|
|
sold_by: document.querySelector('#sellerProfileTriggerId')?.textContent || '',
|
|
ships_from_text:
|
|
document.querySelector('#shipsFromSoldByInsideBuyBox_feature_div')?.textContent
|
|
|| document.querySelector('#fulfillerInfoFeature_feature_div')?.textContent
|
|
|| document.querySelector('#merchantInfoFeature_feature_div')?.textContent
|
|
|| document.querySelector('#tabular-buybox-container')?.textContent
|
|
|| '',
|
|
offer_link: document.querySelector('a[href*="/gp/offer-listing/"]')?.href || '',
|
|
review_url: document.querySelector('a[href*="#customerReviews"]')?.href || '',
|
|
qa_url: document.querySelector('a[href*="ask/questions"]')?.href || '',
|
|
buybox_text:
|
|
document.querySelector('#desktop_qualifiedBuyBox')?.textContent
|
|
|| document.querySelector('#buybox')?.textContent
|
|
|| '',
|
|
}))()
|
|
`) as OfferPayload;
|
|
}
|
|
|
|
cli({
|
|
site: 'amazon',
|
|
name: 'offer',
|
|
description: 'Amazon seller, buy box, and fulfillment facts from the product page',
|
|
domain: 'amazon.com',
|
|
strategy: Strategy.COOKIE,
|
|
navigateBefore: false,
|
|
args: [
|
|
{
|
|
name: 'input',
|
|
required: true,
|
|
positional: true,
|
|
help: 'ASIN or product URL, for example B0FJS72893',
|
|
},
|
|
],
|
|
columns: ['asin', 'price_text', 'sold_by', 'ships_from', 'is_amazon_sold', 'is_amazon_fulfilled'],
|
|
func: async (page, kwargs) => {
|
|
const input = String(kwargs.input ?? '');
|
|
const payload = await readOfferPayload(page, input);
|
|
const normalized = normalizeOfferPayload(payload);
|
|
|
|
if (!normalized.sold_by && !normalized.ships_from && !normalized.merchant_info_text) {
|
|
if (isDeliveryLocationBlocked(payload.buybox_text)) {
|
|
throw new CommandExecutionError(
|
|
'amazon offer buy box is blocked by the current delivery location',
|
|
'The shared Chrome profile is not set to the target US delivery address. Switch Amazon delivery location to the requested US destination, reopen the product page, and retry.',
|
|
);
|
|
}
|
|
|
|
throw new CommandExecutionError(
|
|
'amazon offer surface did not expose seller or fulfillment facts',
|
|
'The product page may have changed. Open the product page in Chrome, make sure the buy box is visible, and retry.',
|
|
);
|
|
}
|
|
|
|
return [normalized];
|
|
},
|
|
});
|
|
|
|
export const __test__ = {
|
|
extractShipsFrom,
|
|
extractSoldBy,
|
|
isDeliveryLocationBlocked,
|
|
normalizeOfferPayload,
|
|
};
|