Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"Cancel": "Cancel",
"Exit the program": "Exit the program",
"Running": "Running",
"Clipboard unavailable. Copy the script manually:": "Clipboard unavailable. Copy the script manually:",
"Clipboard copy failed; script printed for manual copy.": "Clipboard copy failed; script printed for manual copy.",
"Copied to clipboard!": "Copied to clipboard!",
"Your new script": "Your new script",
"Invalid config property": "Invalid config property",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-Hans.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"Cancel": "取消",
"Exit the program": "退出程序",
"Running": "正在运行",
"Clipboard unavailable. Copy the script manually:": "剪贴板不可用,请手动复制脚本:",
"Clipboard copy failed; script printed for manual copy.": "复制到剪贴板失败,已打印脚本供手动复制。",
"Copied to clipboard!": "已复制到剪贴板!",
"Your new script": "您的新脚本",
"Invalid config property": "无效的配置属性",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/zh-Hant.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
"Cancel": "取消",
"Exit the program": "退出程序",
"Running": "正在運行",
"Clipboard unavailable. Copy the script manually:": "剪貼簿不可用,請手動複製腳本:",
"Clipboard copy failed; script printed for manual copy.": "複製到剪貼簿失敗,已列印腳本供手動複製。",
"Copied to clipboard!": "已複製到剪貼簿!",
"Your new script": "您的新腳本",
"Invalid config property": "無效的配置屬性",
Expand Down
17 changes: 15 additions & 2 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ const sample = <T>(arr: T[]): T | undefined => {
return len ? arr[Math.floor(Math.random() * len)] : undefined;
};

export async function copyScriptWithFallback(
script: string,
writeClipboard: (value: string) => Promise<void> = clipboardy.write,
ui: { note: typeof p.note; outro: typeof p.outro } = { note: p.note, outro: p.outro }
) {
try {
await writeClipboard(script);
ui.outro(i18n.t('Copied to clipboard!'));
} catch (_error) {
ui.note(script, i18n.t('Clipboard unavailable. Copy the script manually:'));
ui.outro(i18n.t('Clipboard copy failed; script printed for manual copy.'));
}
}

async function runScript(script: string) {
p.outro(`${i18n.t('Running')}: ${script}`);
console.log('');
Expand Down Expand Up @@ -198,8 +212,7 @@ async function runOrReviseFlow(
label: '📋 ' + i18n.t('Copy'),
hint: i18n.t('Copy the generated script to your clipboard'),
value: async () => {
await clipboardy.write(script);
p.outro(i18n.t('Copied to clipboard!'));
await copyScriptWithFallback(script);
},
},
{
Expand Down
35 changes: 35 additions & 0 deletions test/copy-action-fallback.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import jitiFactory from 'jiti';

const jiti = jitiFactory(import.meta.url, { interopDefault: true });
const { copyScriptWithFallback } = await jiti('../src/prompt.ts');

test('copy fallback prints the script when clipboard write fails', async () => {
const notes = [];
const outros = [];
await copyScriptWithFallback(
'echo hello',
async () => {
throw new Error('clipboard backend unavailable');
},
{
note: (...args) => notes.push(args),
outro: (...args) => outros.push(args),
}
);
assert.equal(notes.length, 1);
assert.deepEqual(notes[0], ['echo hello', 'Clipboard unavailable. Copy the script manually:']);
assert.equal(outros.at(-1)?.[0], 'Clipboard copy failed; script printed for manual copy.');
});

test('copy success still reports clipboard success', async () => {
const notes = [];
const outros = [];
await copyScriptWithFallback('echo hello', async () => {}, {
note: (...args) => notes.push(args),
outro: (...args) => outros.push(args),
});
assert.equal(notes.length, 0);
assert.equal(outros.at(-1)?.[0], 'Copied to clipboard!');
});