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.
117 lines
4.0 KiB
TypeScript
117 lines
4.0 KiB
TypeScript
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { CommandExecutionError } from '@jackwener/opencli/errors';
|
|
import type { IPage } from '@jackwener/opencli/types';
|
|
|
|
function extractTweetId(url: string): string {
|
|
let pathname = '';
|
|
try {
|
|
pathname = new URL(url).pathname;
|
|
} catch {
|
|
throw new Error(`Invalid tweet URL: ${url}`);
|
|
}
|
|
|
|
const match = pathname.match(/\/status\/(\d+)/);
|
|
if (!match?.[1]) {
|
|
throw new Error(`Could not extract tweet ID from URL: ${url}`);
|
|
}
|
|
|
|
return match[1];
|
|
}
|
|
|
|
function buildDeleteScript(tweetId: string): string {
|
|
return `(async () => {
|
|
try {
|
|
const visible = (el) => !!el && (el.offsetParent !== null || el.getClientRects().length > 0);
|
|
const tweetId = ${JSON.stringify(tweetId)};
|
|
const targetArticle = Array.from(document.querySelectorAll('article')).find((article) =>
|
|
Array.from(article.querySelectorAll('a[href*="/status/"]')).some((link) => {
|
|
try {
|
|
return new URL(link.href, window.location.origin).pathname.includes('/status/' + tweetId);
|
|
} catch {
|
|
return false;
|
|
}
|
|
})
|
|
);
|
|
|
|
if (!targetArticle) {
|
|
return { ok: false, message: 'Could not find the tweet card matching the requested URL.' };
|
|
}
|
|
|
|
const buttons = Array.from(targetArticle.querySelectorAll('button,[role="button"]'));
|
|
const moreMenu = buttons.find((el) => visible(el) && (el.getAttribute('aria-label') || '').trim() === 'More');
|
|
if (!moreMenu) {
|
|
return { ok: false, message: 'Could not find the "More" context menu on the matched tweet. Are you sure you are logged in and looking at a valid tweet?' };
|
|
}
|
|
|
|
moreMenu.click();
|
|
await new Promise(r => setTimeout(r, 1000));
|
|
|
|
const items = Array.from(document.querySelectorAll('[role="menuitem"]'));
|
|
const deleteBtn = items.find((item) => {
|
|
const text = (item.textContent || '').trim();
|
|
return text.includes('Delete') && !text.includes('List');
|
|
});
|
|
|
|
if (!deleteBtn) {
|
|
return { ok: false, message: 'The matched tweet menu did not contain Delete. This tweet may not belong to you.' };
|
|
}
|
|
|
|
deleteBtn.click();
|
|
await new Promise(r => setTimeout(r, 1000));
|
|
|
|
const confirmBtn = document.querySelector('[data-testid="confirmationSheetConfirm"]');
|
|
if (confirmBtn) {
|
|
confirmBtn.click();
|
|
return { ok: true, message: 'Tweet successfully deleted.' };
|
|
} else {
|
|
return { ok: false, message: 'Delete confirmation dialog did not appear.' };
|
|
}
|
|
} catch (e) {
|
|
return { ok: false, message: e.toString() };
|
|
}
|
|
})()`;
|
|
}
|
|
|
|
cli({
|
|
site: 'twitter',
|
|
name: 'delete',
|
|
description: 'Delete a specific tweet by URL',
|
|
domain: 'x.com',
|
|
strategy: Strategy.UI, // Utilizes internal DOM flows for interaction
|
|
browser: true,
|
|
args: [
|
|
{ name: 'url', type: 'string', required: true, positional: true, help: 'The URL of the tweet to delete' },
|
|
],
|
|
columns: ['status', 'message'],
|
|
func: async (page: IPage | null, kwargs: any) => {
|
|
if (!page) throw new CommandExecutionError('Browser session required for twitter delete');
|
|
let tweetId = '';
|
|
try {
|
|
tweetId = extractTweetId(kwargs.url);
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : String(error);
|
|
throw new CommandExecutionError(message);
|
|
}
|
|
|
|
await page.goto(kwargs.url);
|
|
await page.wait({ selector: '[data-testid="primaryColumn"]' }); // Wait for tweet to load completely
|
|
|
|
const result = await page.evaluate(buildDeleteScript(tweetId));
|
|
|
|
if (result.ok) {
|
|
// Wait for the deletion request to be processed
|
|
await page.wait(2);
|
|
}
|
|
|
|
return [{
|
|
status: result.ok ? 'success' : 'failed',
|
|
message: result.message
|
|
}];
|
|
}
|
|
});
|
|
|
|
export const __test__ = {
|
|
buildDeleteScript,
|
|
extractTweetId,
|
|
};
|