diff --git a/src/plugin/index.ts b/src/plugin/index.ts index eb89206..a32a7ab 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -55,24 +55,18 @@ async function initPlugin() { if (!y3.env.scriptUri) { return; } - const targetDir = y3.uri(y3.env.scriptUri, scriptDir); const templateDir = y3.extensionPath('template/plugin'); - const listfile = await y3.fs.readFile(y3.uri(templateDir, 'listfile.json')); - const nameMap: { [key: string]: string } = listfile ? JSON.parse(listfile.string) : {}; - for (const [name, fileType] of await y3.fs.dir(templateDir)) { - if (fileType === vscode.FileType.Directory) { - continue; - } - if (name === 'listfile.json') { - continue; - } - const newName = nameMap[name] ?? name; - let overwrite = name.endsWith('.d.ts'); - await y3.fs.copy(y3.uri(templateDir, name), y3.uri(targetDir, newName), { overwrite: overwrite }); - } - if (listfile) { - await vscode.commands.executeCommand('vscode.open', y3.uri(targetDir, nameMap['1.js'])); + const targetDir = y3.uri(y3.env.scriptUri, scriptDir); + await y3.fs.copy(templateDir, targetDir, { + overwrite: true, + recursive: true, + nameMap: 'listfile.json', + }); + const needOpen = y3.uri(targetDir, '1-使用代码修改物编.js'); + if (!await y3.fs.isFile(needOpen)) { + return; } + await vscode.commands.executeCommand('vscode.open', needOpen); } function updatePluginManager() { diff --git a/src/plugin/plugin.ts b/src/plugin/plugin.ts index 293bebc..f7c8649 100644 --- a/src/plugin/plugin.ts +++ b/src/plugin/plugin.ts @@ -29,7 +29,7 @@ export class Plugin { for (let i = 0; i < lines.length; i++) { let line = lines[i]; if (line.startsWith('export ')) { - lines[i] = line.replace(/export\s+(async\s+)function\s+([\w_]+)/, (_, async, name) => { + lines[i] = line.replace(/export\s+(async\s+)function\s+([\w_\u10000-\uFFFFFFFF]+)/, (_, async, name) => { this.exports[name] = { name, async: async !== '', diff --git a/src/tools/fs.ts b/src/tools/fs.ts index b35eaa0..f7813e4 100644 --- a/src/tools/fs.ts +++ b/src/tools/fs.ts @@ -139,18 +139,75 @@ export async function stat(uri: vscode.Uri | string, relativePath?: string) { } catch {} } -export async function copy(source: vscode.Uri | string, target: vscode.Uri | string, options?: { overwrite?: boolean }) { +export async function isFile(uri: vscode.Uri | string, relativePath?: string) { + let statInfo = await stat(uri, relativePath); + return statInfo?.type === vscode.FileType.File; +} + +export async function isDirectory(uri: vscode.Uri | string, relativePath?: string) { + let statInfo = await stat(uri, relativePath); + return statInfo?.type === vscode.FileType.Directory; +} + +export async function isExists(uri: vscode.Uri | string, relativePath?: string) { + return (await stat(uri, relativePath)) !== undefined; +} + +interface CopyOptions { + overwrite?: boolean; + recursive?: boolean; + nameMap?: string; + pattern?: RegExp; +} + +async function loadNameMap(uri: vscode.Uri, nameMapPath: string): Promise<{[key: string]: string} | undefined>{ + let nameMapFile = await readFile(uri, nameMapPath); + if (!nameMapFile) { + return undefined; + } + try { + let map = JSON.parse(nameMapFile.string); + if (typeof map !== 'object' || map === null) { + return undefined; + } + return map; + } catch { + return undefined; + } +} + +export async function copy(source: vscode.Uri | string, target: vscode.Uri | string, options?: CopyOptions): Promise { if (typeof source === 'string') { source = vscode.Uri.file(source); } if (typeof target === 'string') { target = vscode.Uri.file(target); } - try { + const fileStat = await stat(source); + if (!fileStat) { + return false; + } + if (fileStat.type === vscode.FileType.Directory) { + let promises: Promise[] = []; + let nameMap = options?.nameMap ? await loadNameMap(source, options?.nameMap) : undefined; + for (const [name, fileType] of await dir(source)) { + if (options?.pattern && !options.pattern.test(name)) { + continue; + } + if (options?.nameMap && options.nameMap === name) { + continue; + } + let childSource = vscode.Uri.joinPath(source, name); + let childTarget = vscode.Uri.joinPath(target, nameMap?.[name] ?? name); + if (fileType !== vscode.FileType.Directory || options?.recursive) { + promises.push(copy(childSource, childTarget, options)); + } + } + let results = await Promise.all(promises); + return results.every(value => value); + } else { await vscode.workspace.fs.copy(source, target, options); return true; - } catch { - return false; } }