Files
jackwener__opencli/clis/barchart/flow.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

121 lines
4.1 KiB
TypeScript

/**
* Barchart unusual options activity (options flow).
* Shows high volume/OI ratio trades that may indicate institutional activity.
* Auth: CSRF token from <meta name="csrf-token"> + session cookies.
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
cli({
site: 'barchart',
name: 'flow',
description: 'Barchart unusual options activity / options flow',
domain: 'www.barchart.com',
strategy: Strategy.COOKIE,
args: [
{ name: 'type', type: 'str', default: 'all', help: 'Filter: all, call, or put', choices: ['all', 'call', 'put'] },
{ name: 'limit', type: 'int', default: 20, help: 'Number of results' },
],
columns: [
'symbol', 'type', 'strike', 'expiration', 'last',
'volume', 'openInterest', 'volOiRatio', 'iv',
],
func: async (page, kwargs) => {
const optionType = kwargs.type || 'all';
const limit = kwargs.limit ?? 20;
await page.goto('https://www.barchart.com/options/unusual-activity/stocks');
await page.wait(5);
const data = await page.evaluate(`
(async () => {
const limit = ${limit};
const typeFilter = '${optionType}'.toLowerCase();
// Wait for CSRF token to appear (Angular may inject it after initial render)
let csrf = '';
for (let i = 0; i < 10; i++) {
csrf = document.querySelector('meta[name="csrf-token"]')?.content || '';
if (csrf) break;
await new Promise(r => setTimeout(r, 500));
}
if (!csrf) return { error: 'no-csrf' };
const headers = { 'X-CSRF-TOKEN': csrf };
const fields = [
'baseSymbol','strikePrice','expirationDate','optionType',
'lastPrice','volume','openInterest','volumeOpenInterestRatio','volatility',
].join(',');
// Fetch extra rows when filtering by type since server-side filter doesn't work
const fetchLimit = typeFilter !== 'all' ? limit * 3 : limit;
// Try unusual_activity first, fall back to mostActive (unusual_activity is
// empty outside market hours)
const lists = [
'options.unusual_activity.stocks.us',
'options.mostActive.us',
];
for (const list of lists) {
try {
const url = '/proxies/core-api/v1/options/get?list=' + list
+ '&fields=' + fields
+ '&orderBy=volumeOpenInterestRatio&orderDir=desc'
+ '&raw=1&limit=' + fetchLimit;
const resp = await fetch(url, { credentials: 'include', headers });
if (!resp.ok) continue;
const d = await resp.json();
let items = d?.data || [];
if (items.length === 0) continue;
// Apply client-side type filter
if (typeFilter !== 'all') {
items = items.filter(i => {
const t = ((i.raw || i).optionType || '').toLowerCase();
return t === typeFilter;
});
}
return items.slice(0, limit).map(i => {
const r = i.raw || i;
return {
symbol: r.baseSymbol || r.symbol,
type: r.optionType,
strike: r.strikePrice,
expiration: r.expirationDate,
last: r.lastPrice,
volume: r.volume,
openInterest: r.openInterest,
volOiRatio: r.volumeOpenInterestRatio,
iv: r.volatility,
};
});
} catch(e) {}
}
return [];
})()
`);
if (!data) return [];
if (data.error === 'no-csrf') {
throw new Error('Could not extract CSRF token from barchart.com. Make sure you are logged in.');
}
if (!Array.isArray(data)) return [];
return data.slice(0, limit).map(r => ({
symbol: r.symbol || '',
type: r.type || '',
strike: r.strike,
expiration: r.expiration ?? null,
last: r.last != null ? Number(Number(r.last).toFixed(2)) : null,
volume: r.volume,
openInterest: r.openInterest,
volOiRatio: r.volOiRatio != null ? Number(Number(r.volOiRatio).toFixed(2)) : null,
iv: r.iv != null ? Number(Number(r.iv).toFixed(2)) + '%' : null,
}));
},
});