Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
sumneko committed Jul 12, 2024
1 parent 803a8fd commit 98c8ed7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
26 changes: 10 additions & 16 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 !== '',
Expand Down
65 changes: 61 additions & 4 deletions src/tools/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
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<boolean>[] = [];
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;
}
}

Expand Down

0 comments on commit 98c8ed7

Please sign in to comment.