Skip to content

Commit 3b7b558

Browse files
committed
refactor: improve error handling for task file creation
1 parent 2be4649 commit 3b7b558

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

src/task.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { promises as fs } from 'node:fs';
22
import { basename, resolve } from 'node:path';
33
import { window, workspace } from 'vscode';
44
import { getConfig } from 'vscode-get-config';
5-
import { getOutName } from './util.ts';
5+
import { fileExists, getOutName } from './util.ts';
66

77
async function createBuildTask(isJXA = false): Promise<void> {
88
if (typeof workspace.workspaceFolders === 'undefined' || workspace.workspaceFolders.length < 1) {
@@ -88,7 +88,18 @@ async function createBuildTask(isJXA = false): Promise<void> {
8888
const dotFolder = resolve(workspace.workspaceFolders[0].uri.fsPath, '.vscode');
8989
const buildFile = resolve(dotFolder, 'tasks.json');
9090

91-
await fs.mkdir(dotFolder);
91+
try {
92+
await fs.mkdir(dotFolder);
93+
} catch {
94+
console.warn('[idleberg.applescript] This workspace already contains a .vscode folder.');
95+
}
96+
97+
if (await fileExists(buildFile)) {
98+
window.showErrorMessage(
99+
'This workspace already has a task file. If you want to override it, delete it manually and try again.',
100+
);
101+
return;
102+
}
92103

93104
try {
94105
await fs.writeFile(buildFile, jsonString);

src/util.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { spawn } from 'node:child_process';
2+
import { constants } from 'node:fs';
3+
import { access } from 'node:fs/promises';
24
// Dependencies
35
import { basename, dirname, extname, join } from 'node:path';
46
import lineColumn from 'line-column';
@@ -108,3 +110,13 @@ export async function spawnPromise(
108110
});
109111
});
110112
}
113+
114+
export async function fileExists(filePath: string): Promise<boolean> {
115+
try {
116+
await access(filePath, constants.F_OK);
117+
} catch {
118+
return false;
119+
}
120+
121+
return true;
122+
}

0 commit comments

Comments
 (0)