Skip to content

Commit 8dcb9e8

Browse files
committed
Add configurable file name and content for new package
Introduces extension settings to customize the file name and default content when creating a new package. The command now uses these settings, supporting template variables for file content, and updates the keybinding condition for better context sensitivity.
1 parent 35fb6a0 commit 8dcb9e8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

package.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@
1717
"package": "npm run build && vsce package"
1818
},
1919
"contributes": {
20+
"configuration": {
21+
"title": "New Package Extension Settings",
22+
"properties": {
23+
"newPackage.fileContent": {
24+
"type": "string",
25+
"default": "export {};",
26+
"markdownDescription": "Default content for the created file.\nSupported variables:\n- `${fileName}` - The file name\n- `${packageName}` - The package name",
27+
"scope": "resource",
28+
"editPresentation": "multilineText"
29+
},
30+
"newPackage.fileName": {
31+
"type": "string",
32+
"default": "index.ts",
33+
"description": "File name for the main file created in the new package folder."
34+
}
35+
}
36+
},
2037
"commands": [
2138
{
2239
"command": "extension.newPackage",
@@ -48,7 +65,7 @@
4865
{
4966
"command": "extension.newPackage",
5067
"key": "shift+p",
51-
"when": "explorerViewletVisible"
68+
"when": "explorerViewletVisible && explorerViewletFocus"
5269
}
5370
]
5471
},

src/extension.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { join } from 'node:path';
55
export async function activate(context: vscode.ExtensionContext) {
66
const disposable = vscode.commands.registerCommand('extension.newPackage', async (uri: vscode.Uri) => {
77
try {
8-
// Falls kein Ziel gewählt wurde → Arbeitsbereich
8+
// Zielverzeichnis ermitteln
99
let targetDir: string;
1010
if (uri && uri.fsPath) {
1111
targetDir = uri.fsPath;
@@ -16,7 +16,7 @@ export async function activate(context: vscode.ExtensionContext) {
1616
return;
1717
}
1818

19-
// Ordnername erfragen
19+
// Ordnername abfragen
2020
const folderName = await vscode.window.showInputBox({
2121
prompt: 'Enter the new package name',
2222
placeHolder: 'my_package'
@@ -26,17 +26,26 @@ export async function activate(context: vscode.ExtensionContext) {
2626
return;
2727
}
2828

29+
const config = vscode.workspace.getConfiguration('newPackage');
30+
const fileNameSetting = config.get<string>('fileName', 'index.ts');
31+
const fileContentTemplate = config.get<string>('fileContent', '');
32+
2933
const packagePath = join(targetDir, folderName);
3034

3135
// Ordner anlegen
3236
await mkdir(packagePath, { recursive: true });
3337

34-
// index.ts anlegen
35-
const indexFilePath = join(packagePath, 'index.ts');
36-
await writeFile(indexFilePath, 'export {}', { encoding: 'utf8' });
38+
// Template-Variablen ersetzen
39+
const fileContent = fileContentTemplate
40+
.replace(/\$\{fileName\}/g, fileNameSetting)
41+
.replace(/\$\{packageName\}/g, folderName);
42+
43+
// Datei schreiben
44+
const filePath = join(packagePath, fileNameSetting);
45+
await writeFile(filePath, fileContent, { encoding: 'utf8' });
3746

3847
// Datei im Editor öffnen
39-
const doc = await vscode.workspace.openTextDocument(indexFilePath);
48+
const doc = await vscode.workspace.openTextDocument(filePath);
4049
await vscode.window.showTextDocument(doc);
4150

4251
} catch (error) {

0 commit comments

Comments
 (0)