59 lines
1.7 KiB
TypeScript
Raw Normal View History

2023-05-05 21:27:40 +08:00
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import * as ncp from 'ncp';
2023-05-09 10:34:33 +08:00
import { logger } from '../util/logger';
function copyFileSync(source: string, target: string) {
const data = fs.readFileSync(source);
fs.writeFileSync(target, data);
}
function copyDirSync(source: string, target: string) {
// 创建目标目录
fs.mkdirSync(target, { recursive: true });
// 遍历目录中的所有文件和子目录
const files = fs.readdirSync(source);
for (const file of files) {
const sourcePath = path.join(source, file);
const targetPath = path.join(target, file);
const stats = fs.statSync(sourcePath);
if (stats.isDirectory()) {
// 递归拷贝子目录
copyDirSync(sourcePath, targetPath);
} else {
// 拷贝文件
copyFileSync(sourcePath, targetPath);
}
}
}
2023-05-05 21:27:40 +08:00
export function createChatDirectoryAndCopyInstructionsSync(extensionUri: vscode.Uri) {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders) {
return;
}
const workspaceRoot = workspaceFolders[0].uri.fsPath;
const chatDirPath = path.join(workspaceRoot, '.chat');
const instructionsSrcPath = path.join(extensionUri.fsPath, 'workflows');
2023-05-05 21:27:40 +08:00
try {
// 检查 .chat 目录是否存在,如果不存在,则创建它
if (!fs.existsSync(chatDirPath)) {
fs.mkdirSync(chatDirPath);
} else {
return;
}
// 将 workflows 目录复制到 .chat 目录中
copyDirSync(instructionsSrcPath, path.join(chatDirPath, 'workflows'));
2023-05-05 21:27:40 +08:00
} catch (error) {
logger.channel()?.error('Error creating .chat directory and copying workflows:', error);
2023-05-09 10:34:33 +08:00
logger.channel()?.show();
2023-05-05 21:27:40 +08:00
}
}