mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-05-05 15:31:27 +00:00
feat add multi-platform support
This commit is contained in:
parent
56030f9291
commit
b488126830
6 changed files with 64 additions and 25 deletions
|
|
@ -16,6 +16,12 @@ const mockHasFormat = vi.fn();
|
|||
const mockGetImageData = vi.fn();
|
||||
|
||||
vi.mock('@teddyzhu/clipboard', () => ({
|
||||
default: {
|
||||
ClipboardManager: vi.fn().mockImplementation(() => ({
|
||||
hasFormat: mockHasFormat,
|
||||
getImageData: mockGetImageData,
|
||||
})),
|
||||
},
|
||||
ClipboardManager: vi.fn().mockImplementation(() => ({
|
||||
hasFormat: mockHasFormat,
|
||||
getImageData: mockGetImageData,
|
||||
|
|
@ -53,26 +59,19 @@ describe('clipboardUtils', () => {
|
|||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should log errors in DEBUG mode', async () => {
|
||||
it('should return false and not throw when error occurs in DEBUG mode', async () => {
|
||||
const originalEnv = process.env;
|
||||
vi.stubGlobal('process', {
|
||||
...process,
|
||||
env: { ...originalEnv, DEBUG: '1' },
|
||||
});
|
||||
|
||||
const consoleErrorSpy = vi
|
||||
.spyOn(console, 'error')
|
||||
.mockImplementation(() => {});
|
||||
mockHasFormat.mockImplementation(() => {
|
||||
throw new Error('Test error');
|
||||
});
|
||||
|
||||
await clipboardHasImage();
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
||||
'Error checking clipboard for image:',
|
||||
expect.any(Error),
|
||||
);
|
||||
consoleErrorSpy.mockRestore();
|
||||
const result = await clipboardHasImage();
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -101,26 +100,19 @@ describe('clipboardUtils', () => {
|
|||
expect(result).toBe(null);
|
||||
});
|
||||
|
||||
it('should log errors in DEBUG mode', async () => {
|
||||
it('should return null and not throw when error occurs in DEBUG mode', async () => {
|
||||
const originalEnv = process.env;
|
||||
vi.stubGlobal('process', {
|
||||
...process,
|
||||
env: { ...originalEnv, DEBUG: '1' },
|
||||
});
|
||||
|
||||
const consoleErrorSpy = vi
|
||||
.spyOn(console, 'error')
|
||||
.mockImplementation(() => {});
|
||||
mockHasFormat.mockImplementation(() => {
|
||||
throw new Error('Test error');
|
||||
});
|
||||
|
||||
await saveClipboardImage('/tmp/test');
|
||||
expect(consoleErrorSpy).toHaveBeenCalledWith(
|
||||
'Error saving clipboard image:',
|
||||
expect.any(Error),
|
||||
);
|
||||
consoleErrorSpy.mockRestore();
|
||||
const result = await saveClipboardImage('/tmp/test');
|
||||
expect(result).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -6,18 +6,41 @@
|
|||
|
||||
import * as fs from 'node:fs/promises';
|
||||
import * as path from 'node:path';
|
||||
import { ClipboardManager } from '@teddyzhu/clipboard';
|
||||
import { createDebugLogger } from '@qwen-code/qwen-code-core';
|
||||
|
||||
const debugLogger = createDebugLogger('CLIPBOARD_UTILS');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
type ClipboardModule = any;
|
||||
|
||||
let cachedClipboardModule: ClipboardModule | null = null;
|
||||
let clipboardLoadAttempted = false;
|
||||
|
||||
async function getClipboardModule(): Promise<ClipboardModule | null> {
|
||||
if (clipboardLoadAttempted) return cachedClipboardModule;
|
||||
clipboardLoadAttempted = true;
|
||||
|
||||
try {
|
||||
const modName = '@teddyzhu/clipboard';
|
||||
cachedClipboardModule = await import(modName);
|
||||
return cachedClipboardModule;
|
||||
} catch (_e) {
|
||||
debugLogger.error(
|
||||
'Failed to load @teddyzhu/clipboard native module. Clipboard image features will be unavailable.',
|
||||
);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the system clipboard contains an image
|
||||
* @returns true if clipboard contains an image
|
||||
*/
|
||||
export async function clipboardHasImage(): Promise<boolean> {
|
||||
try {
|
||||
const clipboard = new ClipboardManager();
|
||||
const mod = await getClipboardModule();
|
||||
if (!mod) return false;
|
||||
const clipboard = new mod.ClipboardManager();
|
||||
return clipboard.hasFormat('image');
|
||||
} catch (error) {
|
||||
debugLogger.error('Error checking clipboard for image:', error);
|
||||
|
|
@ -34,7 +57,9 @@ export async function saveClipboardImage(
|
|||
targetDir?: string,
|
||||
): Promise<string | null> {
|
||||
try {
|
||||
const clipboard = new ClipboardManager();
|
||||
const mod = await getClipboardModule();
|
||||
if (!mod) return null;
|
||||
const clipboard = new mod.ClipboardManager();
|
||||
|
||||
if (!clipboard.hasFormat('image')) {
|
||||
return null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue