Skip to content

Instantly share code, notes, and snippets.

@pratheeshrussell
Created March 15, 2020 17:26
Show Gist options
  • Save pratheeshrussell/ddcde72edc75bd5e8e23963088301a85 to your computer and use it in GitHub Desktop.
Save pratheeshrussell/ddcde72edc75bd5e8e23963088301a85 to your computer and use it in GitHub Desktop.
VS Code Extension- extension.js (version1)
const vscode = require('vscode');
/**
* @param {vscode.ExtensionContext} context
*/
//In case we need to create a terminal
let newTerminalId = 1;
//This function runs when the extension gets activated
function activate(context) {
let runBuildInParts = vscode.commands.registerCommand('extension.runBuildInParts', async function () {
runCommand("flutter build apk --split-per-abi");
});
let runBuildFat = vscode.commands.registerCommand('extension.runBuildFat', async function () {
runCommand("flutter build apk --release");
});
let runPackageRepair = vscode.commands.registerCommand('extension.runPackageRepair', async function () {
await vscode.workspace.findFiles("**/.packages").then((paths) =>{
if(paths.length > 0){
vscode.workspace.fs.delete(paths[0]);
}
});
runCommand("flutter pub get");
});
let runProjectRepair = vscode.commands.registerCommand('extension.runProjectRepair', async function () {
await runCommand("flutter clean");
runCommand("flutter create .");
});
context.subscriptions.push(runBuildInParts);
context.subscriptions.push(runBuildFat);
context.subscriptions.push(runPackageRepair);
context.subscriptions.push(runProjectRepair);
}
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
module.exports = {
activate,
deactivate
}
async function runCommand(cmd){
let isFlutter = await isFlutterProject();
if(isFlutter == false){
vscode.window.showInformationMessage('This doesn\'t seem to be a flutter project');
return;
}
let terminal;
if (ensureTerminalExists()) {
terminal =(vscode.window).activeTerminal;
} else {
terminal = vscode.window.createTerminal('Flutter Command Terminal' + newTerminalId++);
}
terminal.show;
terminal.sendText(cmd);
}
function ensureTerminalExists() {
if ((vscode.window).terminals.length === 0) {
return false;
}
return true;
}
async function isFlutterProject() {
let status = false;
await vscode.workspace.findFiles("**/pubspec.yaml").then((paths) =>{
if(paths.length > 0){
status = true;
}
});
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment