Skip to content

Commit 6ac60e7

Browse files
authored
Merge pull request #599 from devchat-ai/optimize
Refactor and improve file path handling
2 parents 8ffad3b + 825e8c7 commit 6ac60e7

File tree

6 files changed

+48
-37
lines changed

6 files changed

+48
-37
lines changed

gui

prebuild.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
require('dotenv').config()
1+
require('dotenv').config();
22

3-
const fs = require('fs')
4-
const path = require('path')
3+
const fs = require('fs');
4+
const path = require('path');
55

66
function copyIcon(src, dst) {
77
if (!src) {
8-
console.warn(`Icon path for ${dst} is not defined in your environment variables`)
9-
return
8+
console.warn(`Icon path for ${dst} is not defined in your environment variables`);
9+
return;
1010
}
11-
console.log(`Replacing icon ${dst} by ${src}`)
11+
console.log(`Replacing icon ${dst} by ${src}`);
1212
if (!fs.existsSync(src)) {
13-
console.warn(`Icon file ${src} does not exist.`)
14-
return
13+
console.warn(`Icon file ${src} does not exist.`);
14+
return;
1515
}
1616

17-
const destPath = path.join(__dirname, 'assets', dst)
17+
const destPath = path.join(__dirname, 'assets', dst);
1818

1919
try {
20-
fs.copyFileSync(src, destPath)
21-
fs.chmodSync(destPath, 0o644)
20+
fs.copyFileSync(src, destPath);
21+
fs.chmodSync(destPath, 0o644);
2222
} catch(e) {
23-
console.warn(`Failed to copy logo ${e}`)
23+
console.warn(`Failed to copy logo ${e}`);
2424
}
2525
}
2626

@@ -31,7 +31,7 @@ function updatePackageJson() {
3131
ASSISTANT_NAME_EN: process.env.ASSISTANT_NAME_EN || "DevChat",
3232
ASSISTANT_NAME_ZH: process.env.ASSISTANT_NAME_ZH || "DevChat"
3333
}
34-
console.log(`Updating package.json, env: ${JSON.stringify(placeholders)}`)
34+
console.log(`Updating package.json, env: ${JSON.stringify(placeholders)}`);
3535

3636
let packageJson = fs.readFileSync('package.json', 'utf8');
3737

@@ -44,8 +44,8 @@ function updatePackageJson() {
4444
fs.writeFileSync('package.json', packageJson);
4545
}
4646

47-
copyIcon(process.env.EXTENSION_ICON, 'devchat.png')
48-
copyIcon(process.env.SIDEBAR_ICON, 'devchat_icon.svg')
49-
copyIcon(process.env.DIFF_APPLY_ICON, 'devchat_apply.svg')
47+
copyIcon(process.env.EXTENSION_ICON, 'devchat.png');
48+
copyIcon(process.env.SIDEBAR_ICON, 'devchat_icon.svg');
49+
copyIcon(process.env.DIFF_APPLY_ICON, 'devchat_apply.svg');
5050

51-
updatePackageJson()
51+
updatePackageJson();

src/context/contextCodeSelected.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export async function handleCodeSelected(fileSelected: string, codeSelected: str
1616

1717
// get relative path of workspace
1818
const workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath();
19-
const relativePath = path.relative(workspaceDir!, fileSelected);
19+
const relativePath = workspaceDir
20+
? path.relative(workspaceDir, fileSelected)
21+
: fileSelected;
2022

2123
// convert fileContent to markdown code block with languageId and file path
2224
const data = {

src/context/contextFileSelected.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export async function handleFileSelected(fileSelected: string) {
1919

2020
// get relative path of workspace
2121
const workspaceDir = UiUtilWrapper.workspaceFoldersFirstPath();
22-
const relativePath = path.relative(workspaceDir!, fileSelected);
22+
const relativePath = workspaceDir
23+
? path.relative(workspaceDir, fileSelected)
24+
: fileSelected;
2325

2426
// convert fileContent to markdown code block with languageId and file path
2527
const data = {

src/contributes/commands.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,31 +216,38 @@ export function registerInstallCommandsCommand(
216216
const currentVersion = UiUtilWrapper.extensionPath();
217217
const previousVersion = devchatConfig.get("last_devchat_version", "");
218218

219+
let copiedDirectory = false;
219220
if (!fs.existsSync(sysMericoDirPath) || (updatePublicWorkflow === false && currentVersion !== previousVersion)) {
220221
logger.channel()?.debug("Creating directory: " + sysMericoDirPath);
221222
await copyDirectory(pluginDirPath, sysDirPath);
223+
copiedDirectory = true;
222224
}
223225
devchatConfig.set("last_devchat_version", currentVersion);
224226

225-
// Check if ~/.chat/scripts directory exists
226-
if (!fs.existsSync(sysMericoDirPath)) {
227-
// Directory does not exist, wait for updateWorkflows to finish
228-
logger.channel()?.debug("Update workflows...");
229-
await dcClient.updateWorkflows();
230-
await dcClient.updateCustomWorkflows();
227+
if (copiedDirectory) {
228+
logger.channel()?.debug("Directory copied successfully.");
231229
sendCommandListByDevChatRun();
232230
} else {
233-
// Directory exists, execute sendCommandListByDevChatRun immediately
234-
logger.channel()?.debug("Sending and updating workflows...");
235-
await sendCommandListByDevChatRun();
236-
237-
// Then asynchronously execute updateWorkflows
238-
await dcClient.updateWorkflows();
239-
await dcClient.updateCustomWorkflows();
240-
241-
await sendCommandListByDevChatRun();
231+
// Check if ~/.chat/scripts directory exists
232+
if (!fs.existsSync(sysMericoDirPath)) {
233+
// Directory does not exist, wait for updateWorkflows to finish
234+
logger.channel()?.debug("Update workflows...");
235+
await dcClient.updateWorkflows();
236+
await dcClient.updateCustomWorkflows();
237+
sendCommandListByDevChatRun();
238+
} else {
239+
// Directory exists, execute sendCommandListByDevChatRun immediately
240+
logger.channel()?.debug("Sending and updating workflows...");
241+
await sendCommandListByDevChatRun();
242+
243+
// Then asynchronously execute updateWorkflows
244+
await dcClient.updateWorkflows();
245+
await dcClient.updateCustomWorkflows();
246+
247+
await sendCommandListByDevChatRun();
248+
}
242249
}
243-
250+
244251
// Ensure the panel is activated
245252
await ensureChatPanel(context);
246253
}

workflowsCommands

0 commit comments

Comments
 (0)