1. Package the Extension (Optional):
If you want to distribute your extension, you can package it into a .vsix
file.
Install vsce (VS Code Extension Manager):
Bash
npm install -g vsce
Run vsce package in your extension's directory. This will create a .vsix
file.
2. Important Considerations:
Error Handling: Enhance the error handling in the code to provide more
informative messages.
Configuration: Add settings to your package.json to allow users to
customize the shared module name and path.
Testing: Write unit tests to ensure your extension works correctly.
Workspace Edits: instead of using fs.WriteFileSync, use
vscode.workspace.applyEdit, and vscode.workspace.createWorkspaceEdit.
This will allow the user to undo the changes made by your extension.
Progress Reporting: For large projects, provide progress updates to the
user.
User Feedback: provide feedback to the user when the operation is
complete, or if there were any errors.
3. Auto Import Code
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
export function activate(context: vscode.ExtensionContext) {
let disposable =
vscode.commands.registerCommand('extension.autoImportSharedModule', async
() => {
const sharedModuleName = 'SharedModule'; // Replace with your shared
module name
const sharedModulePath = 'path/to/your/shared.module'; // Replace
with the relative path to your shared module
if (!vscode.workspace.workspaceFolders) {
vscode.window.showErrorMessage('No workspace folder open.');
return;
}
const workspacePath =
vscode.workspace.workspaceFolders[0].uri.fsPath;
async function processFile(filePath: string) {
try {
let content = fs.readFileSync(filePath, 'utf8');
// Simple check if the module is already imported.
if (content.includes(`import { ${sharedModuleName} } from
'${sharedModulePath}';`)) {
return; // Already imported
}
// Simple insertion of the import statement at the top.
const importStatement = `import { ${sharedModuleName} } from
'${sharedModulePath}';\n`;
content = importStatement + content;
fs.writeFileSync(filePath, content, 'utf8');
console.log(`Imported ${sharedModuleName} into ${filePath}`);
} catch (error) {
console.error(`Error processing ${filePath}: ${error}`);
}
}
async function processDirectory(dirPath: string) {
const files = fs.readdirSync(dirPath);
for (const file of files) {
const filePath = path.join(dirPath, file);
const stats = fs.statSync(filePath);
if (stats.isDirectory()) {
await processDirectory(filePath); // Recursive call for
subdirectories
} else if (file.endsWith('.ts') || file.endsWith('.tsx')) { //
Filter for TypeScript files
await processFile(filePath);
}
}
}
await processDirectory(workspacePath);
vscode.window.showInformationMessage(`Auto-imported
${sharedModuleName} into relevant files.`);
});
context.subscriptions.push(disposable);
}
export function deactivate() {}
vscode requirements
By medeb | personalblog | 20 Apr 2025
How do you rate this article?
0
personalblog
My daily experience in crypto world
Send a $0.01 microtip in crypto to the author, and earn yourself as you read!
% to author / 80% to me.We pay the tips from our rewards pool.