|
1 | 1 | import * as vscode from 'vscode'; |
2 | | -import * as fs from 'fs'; |
3 | | -import * as path from 'path'; |
4 | | - |
5 | | -export function activate(context: vscode.ExtensionContext) { |
6 | | - let disposable = vscode.commands.registerCommand('new-package-extension.createPackage', async (uri: vscode.Uri) => { |
7 | | - // Get the folder path where the context menu was triggered |
8 | | - const folderPath = uri.fsPath; |
9 | | - |
10 | | - // Prompt user for the new package folder name |
11 | | - const folderName = await vscode.window.showInputBox({ |
12 | | - prompt: 'Enter the name of the new package folder', |
13 | | - placeHolder: 'Package name' |
| 2 | +import { mkdir, writeFile } from 'node:fs/promises'; |
| 3 | +import { join } from 'node:path'; |
| 4 | + |
| 5 | +export async function activate(context: vscode.ExtensionContext) { |
| 6 | + const disposable = vscode.commands.registerCommand('extension.newPackage', async (uri: vscode.Uri) => { |
| 7 | + try { |
| 8 | + // Falls kein Ziel gewählt wurde → Arbeitsbereich |
| 9 | + let targetDir: string; |
| 10 | + if (uri && uri.fsPath) { |
| 11 | + targetDir = uri.fsPath; |
| 12 | + } else if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) { |
| 13 | + targetDir = vscode.workspace.workspaceFolders[0].uri.fsPath; |
| 14 | + } else { |
| 15 | + vscode.window.showErrorMessage('No workspace folder open.'); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + // Ordnername erfragen |
| 20 | + const folderName = await vscode.window.showInputBox({ |
| 21 | + prompt: 'Enter the new package name', |
| 22 | + placeHolder: 'my-package' |
| 23 | + }); |
| 24 | + |
| 25 | + if (!folderName) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const packagePath = join(targetDir, folderName); |
| 30 | + |
| 31 | + // Ordner anlegen |
| 32 | + await mkdir(packagePath, { recursive: true }); |
| 33 | + |
| 34 | + // index.ts anlegen |
| 35 | + const indexFilePath = join(packagePath, 'index.ts'); |
| 36 | + await writeFile(indexFilePath, '', { encoding: 'utf8' }); |
| 37 | + |
| 38 | + // Datei im Editor öffnen |
| 39 | + const doc = await vscode.workspace.openTextDocument(indexFilePath); |
| 40 | + await vscode.window.showTextDocument(doc); |
| 41 | + |
| 42 | + } catch (error) { |
| 43 | + vscode.window.showErrorMessage(`Failed to create package: ${String(error)}`); |
| 44 | + } |
14 | 45 | }); |
15 | 46 |
|
16 | | - if (!folderName) { |
17 | | - vscode.window.showErrorMessage('No folder name provided.'); |
18 | | - return; |
19 | | - } |
20 | | - |
21 | | - // Create the full path for the new folder |
22 | | - const newFolderPath = path.join(folderPath, folderName); |
23 | | - |
24 | | - try { |
25 | | - // Create the new folder |
26 | | - await fs.promises.mkdir(newFolderPath, { recursive: true }); |
27 | | - |
28 | | - // Create the index.ts file inside the new folder |
29 | | - const indexFilePath = path.join(newFolderPath, 'index.ts'); |
30 | | - await fs.promises.writeFile(indexFilePath, '// Index file for the package\n'); |
31 | | - |
32 | | - vscode.window.showInformationMessage(`Package '${folderName}' created successfully with index.ts`); |
33 | | - } catch (error: any) { |
34 | | - vscode.window.showErrorMessage(`Failed to create package: ${error.message}`); |
35 | | - } |
36 | | - }); |
37 | | - |
38 | | - context.subscriptions.push(disposable); |
| 47 | + context.subscriptions.push(disposable); |
39 | 48 | } |
40 | 49 |
|
41 | 50 | export function deactivate() {} |
0 commit comments