Files
jackwener__opencli/clis/boss/mark.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

76 lines
2.8 KiB
TypeScript

/**
* BOSS直聘 mark — label/mark a candidate.
*
* Available labels:
* 1=新招呼, 2=沟通中, 3=已约面, 4=已获取简历, 5=已交换电话,
* 6=已交换微信, 7=不合适, 8=牛人发起, 11=收藏
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
import { requirePage, navigateToChat, bossFetch, findFriendByUid, verbose } from './utils.js';
import { ArgumentError, EmptyResultError } from '@jackwener/opencli/errors';
const LABEL_MAP: Record<string, number> = {
'新招呼': 1, '沟通中': 2, '已约面': 3, '已获取简历': 4,
'已交换电话': 5, '已交换微信': 6, '不合适': 7, '牛人发起': 8, '收藏': 11,
};
cli({
site: 'boss',
name: 'mark',
description: 'BOSS直聘给候选人添加标签',
domain: 'www.zhipin.com',
strategy: Strategy.COOKIE,
navigateBefore: false,
browser: true,
args: [
{ name: 'uid', positional: true, required: true, help: 'Encrypted UID of the candidate' },
{ name: 'label', required: true, help: 'Label name (新招呼/沟通中/已约面/已获取简历/已交换电话/已交换微信/不合适/收藏) or label ID' },
{ name: 'remove', type: 'boolean', default: false, help: 'Remove the label instead of adding' },
],
columns: ['status', 'detail'],
func: async (page, kwargs) => {
requirePage(page);
const labelInput = kwargs.label;
const remove = kwargs.remove || false;
// Resolve label to ID
let labelId: number;
if (LABEL_MAP[labelInput]) {
labelId = LABEL_MAP[labelInput];
} else if (!isNaN(Number(labelInput))) {
labelId = Number(labelInput);
} else {
const entry = Object.entries(LABEL_MAP).find(([k]) => k.includes(labelInput));
if (entry) {
labelId = entry[1];
} else {
throw new ArgumentError(`未知标签: ${labelInput}。可用标签: ${Object.keys(LABEL_MAP).join(', ')}`);
}
}
verbose(`${remove ? 'Removing' : 'Adding'} label ${labelId} for ${kwargs.uid}...`);
await navigateToChat(page);
const friend = await findFriendByUid(page, kwargs.uid, { checkGreetList: true });
if (!friend) throw new EmptyResultError('boss candidate search');
const friendName = friend.name || '候选人';
const action = remove ? 'deleteMark' : 'addMark';
const params = new URLSearchParams({
friendId: String(friend.uid),
friendSource: String(friend.friendSource ?? 0),
labelId: String(labelId),
});
await bossFetch(page, `https://www.zhipin.com/wapi/zprelation/friend/label/${action}?${params.toString()}`);
const labelName = Object.entries(LABEL_MAP).find(([, v]) => v === labelId)?.[0] || String(labelId);
return [{
status: remove ? '✅ 标签已移除' : '✅ 标签已添加',
detail: `${friendName}: ${remove ? '移除' : '添加'}标签「${labelName}」`,
}];
},
});