From 932f73d87fe8b33b23d28ac7fb651cc811770507 Mon Sep 17 00:00:00 2001 From: Andy Duong Date: Thu, 26 Oct 2023 09:21:29 +0700 Subject: [PATCH] Refactor and add the git status --- .tool-versions | 2 +- Makefile | 3 + package-lock.json | 6 +- package.json | 3 +- src/extension.ts | 309 ++++++++++++++++++---------------------------- src/helper.js | 23 ++++ src/item.js | 70 +++++++++++ src/utils.js | 40 ++++++ 8 files changed, 260 insertions(+), 196 deletions(-) create mode 100644 src/helper.js create mode 100644 src/item.js create mode 100644 src/utils.js diff --git a/.tool-versions b/.tool-versions index 826ccf4..11848fa 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -nodejs 16.15.0 +nodejs 16.20.1 diff --git a/Makefile b/Makefile index 2ae246b..023ea8b 100755 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ compile: package: printf "Y\n" | vsce package +install_vsce: + npm install --global @vscode/vsce + install: code --install-extension bar-helper-0.4.0.vsix rm bar-helper-0.4.0.vsix diff --git a/package-lock.json b/package-lock.json index a664b69..4738f8c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "bar-helper", - "version": "0.1.0", + "version": "0.4.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "bar-helper", - "version": "0.1.0", + "version": "0.4.1", "devDependencies": { "@types/glob": "^8.1.0", "@types/node": "^18.15.11", @@ -15,7 +15,7 @@ "typescript": "^5.0.3" }, "engines": { - "vscode": "^1.34.0" + "vscode": "^1.77.0" } }, "node_modules/@types/glob": { diff --git a/package.json b/package.json index 870abf8..6aaf2da 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "bar-helper", "displayName": "Bar Helper", "description": "Run any command from the status bar.", - "version": "0.4.1", + "version": "0.4.2", "publisher": "andyduong1920", "repository": { "type": "git", @@ -116,7 +116,6 @@ "glob": "^9.3.4", "typescript": "^5.0.3" }, - "dependencies": {}, "keywords": [ "bar", "helper", diff --git a/src/extension.ts b/src/extension.ts index a8a5ac4..d43f1b0 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,204 +1,102 @@ "use strict"; -import { - commands, - window, - ExtensionContext, - StatusBarAlignment, - workspace, -} from "vscode"; +import { commands, window, ExtensionContext, workspace } from "vscode"; -const runTestFileItem = window.createStatusBarItem(StatusBarAlignment.Left, -1); -const runTestLineItem = window.createStatusBarItem(StatusBarAlignment.Left, -2); -const formatCodeFileItem = window.createStatusBarItem( - StatusBarAlignment.Left, - -3 -); -const runDBMigrateItem = window.createStatusBarItem( - StatusBarAlignment.Left, - -4 -); -const startInteractiveConsoleItem = window.createStatusBarItem( - StatusBarAlignment.Left, - -5 -); -const startWebServerItem = window.createStatusBarItem( - StatusBarAlignment.Left, - -6 -); -const runDBRemigrateItem = window.createStatusBarItem( - StatusBarAlignment.Left, - -7 -); -const gitFetchItem = window.createStatusBarItem(StatusBarAlignment.Left, -9); -const gitPushItem = window.createStatusBarItem(StatusBarAlignment.Left, -10); - -// Right side -const gitRebaseSkipItem = window.createStatusBarItem( - StatusBarAlignment.Right, - 1 -); -const gitRebaseContinueItem = window.createStatusBarItem( - StatusBarAlignment.Right, - 2 -); - -// Adjust here to add more items -const TEST_ITEMS = [runTestFileItem, runTestLineItem]; -const BAR_ITEMS = [ - runDBRemigrateItem, - runDBMigrateItem, - startInteractiveConsoleItem, - startWebServerItem, - formatCodeFileItem, - gitPushItem, - gitFetchItem, - gitRebaseContinueItem, - gitRebaseSkipItem, - ...TEST_ITEMS, -]; - -const sendToTerminal = (thisText: string) => { - let terminal = undefined; - - if (window.activeTerminal) { - terminal = window.activeTerminal; - } else { - terminal = window.createTerminal("Bar Helper"); - } - - terminal.show(); - - terminal.sendText(thisText); -}; - -// Adjust here to add more items -const isTestFile = (filePath: any) => { - return isRubyTestFile(filePath) || isElixirTestFile(filePath); -}; - -const isRubyTestFile = (filePath: any) => { - return filePath.includes("_spec.rb"); -}; - -const isElixirTestFile = (filePath: any) => { - return filePath.includes("_test.exs"); -}; - -const isRubyFile = (filePath: any) => { - return filePath.includes(".rb"); -}; - -const isElixirFile = (filePath: any) => { - return filePath.includes(".exs") || filePath.includes(".ex"); -}; - -const isJSFile = (filePath: any) => { - return filePath.includes(".js"); -}; - -const onUpdatePath = () => { - const editor = window.activeTextEditor; - - if (editor === undefined) { - hideTestItems(); - formatCodeFileItem.hide(); - } else { - const filePath = editor.document.fileName; - - // Always show the format item on any file - formatCodeFileItem.show(); - - if (isTestFile(filePath)) { - showTestItems(); - } else { - hideTestItems(); - } - } -}; - -const hideTestItems = () => { - TEST_ITEMS.forEach((item) => { - item.hide(); - }); -}; - -const showTestItems = () => { - showItems(TEST_ITEMS); -}; - -const showItems = (items: any) => { - items.forEach((item: any) => { - item.show(); - }); -}; - -const setupItem = ( - item: any, - thisText: any, - thisTooltip: any, - thisCommand: any -) => { - item.text = thisText; - item.tooltip = thisTooltip; - item.command = thisCommand; -}; +// @ts-ignore +import * as Utils from "./utils"; +// @ts-ignore +import * as Helper from "./helper"; +// @ts-ignore +import * as Item from "./item"; +// @ts-ignore +import { TEST_ITEMS, BAR_ITEMS } from "./item"; export function activate(context: ExtensionContext) { + // TODO: Move to the ItemSetup file // Adjust here to add more items - setupItem(runTestFileItem, "đŸ”Ĩ⌃a", "Test file", "barHelper.runTestFile"); - setupItem(runTestLineItem, "1ī¸âƒŖ ⌃z", "Test line", "barHelper.runTestLine"); - setupItem( - runDBRemigrateItem, + Utils.setupItem( + Item.runTestFileItem, + "đŸ”Ĩ⌃a", + "Test file", + "barHelper.runTestFile" + ); + Utils.setupItem( + Item.runTestLineItem, + "1ī¸âƒŖ ⌃z", + "Test line", + "barHelper.runTestLine" + ); + Utils.setupItem( + Item.runDBRemigrateItem, "⭕ reMIGRATION", "db:drop db:create db:migrate && db:seed", "barHelper.runDBRemigrate" ); - setupItem( - startInteractiveConsoleItem, + Utils.setupItem( + Item.startInteractiveConsoleItem, "⛑ī¸âŒƒi", "Start the interactive console", "barHelper.startInteractiveConsole" ); - setupItem( - startWebServerItem, + Utils.setupItem( + Item.startWebServerItem, "🚁⌃s", "Start the web server", "barHelper.startWebServer" ); - setupItem( - formatCodeFileItem, + Utils.setupItem( + Item.formatCodeFileItem, "🎨 ⌃f", "format the file", "barHelper.formatCodeFile" ); - setupItem(runDBMigrateItem, "âŦ†ī¸ ⌃m", "db:migrate", "barHelper.runDBMigrate"); - setupItem(gitPushItem, "🚀⌃u", "git push --force", "barHelper.runGitPush"); - setupItem(gitFetchItem, "âŦ FETCH", "git fetch.", "git.fetch"); - setupItem( - gitRebaseContinueItem, + Utils.setupItem( + Item.runDBMigrateItem, + "âŦ†ī¸ ⌃m", + "db:migrate", + "barHelper.runDBMigrate" + ); + Utils.setupItem( + Item.gitPushItem, + "🚀⌃u", + "git push --force", + "barHelper.runGitPush" + ); + Utils.setupItem(Item.gitFetchItem, "âŦ fetch", "git fetch", "git.fetch"); + Utils.setupItem( + Item.gitStatusItem, + "ℹī¸ status", + "git status", + "barHelper.runGitStatus" + ); + + // Right side + Utils.setupItem( + Item.gitRebaseContinueItem, "🏃 rebase:CONTINUE", "git add . && git rebase --continue.", "barHelper.runGitRebaseContinue" ); - setupItem( - gitRebaseSkipItem, + Utils.setupItem( + Item.gitRebaseSkipItem, "👋 rebase:SKIP", "git rebase --skip.", "barHelper.runGitRebaseSkip" ); - showItems([ - runDBRemigrateItem, - runDBMigrateItem, - startInteractiveConsoleItem, - startWebServerItem, - gitPushItem, - gitFetchItem, - gitRebaseContinueItem, - gitRebaseSkipItem, + Utils.showItems([ + Item.runDBRemigrateItem, + Item.runDBMigrateItem, + Item.startInteractiveConsoleItem, + Item.startWebServerItem, + Item.gitPushItem, + Item.gitStatusItem, + Item.gitFetchItem, + Item.gitRebaseContinueItem, + Item.gitRebaseSkipItem, ]); + // TODO: Move to the CommandSetup file const runTestFileCommand = commands.registerCommand( "barHelper.runTestFile", () => { @@ -210,10 +108,10 @@ export function activate(context: ExtensionContext) { const filePath = editor.document.fileName; const relativePath = workspace.asRelativePath(filePath, false); - if (isRubyTestFile(filePath)) { - sendToTerminal(`bundle exec rspec ${relativePath}`); - } else if (isElixirTestFile(filePath)) { - sendToTerminal(`mix test ${relativePath}`); + if (Helper.isRubyTestFile(filePath)) { + Utils.sendToTerminal(`bundle exec rspec ${relativePath}`); + } else if (Helper.isElixirTestFile(filePath)) { + Utils.sendToTerminal(`mix test ${relativePath}`); } } } @@ -231,10 +129,12 @@ export function activate(context: ExtensionContext) { const relativePath = workspace.asRelativePath(filePath, false); const lineNumber = editor.selection.active.line + 1; - if (isRubyTestFile(filePath)) { - sendToTerminal(`bundle exec rspec ${relativePath}:${lineNumber}`); - } else if (isElixirTestFile(filePath)) { - sendToTerminal(`mix test ${relativePath}:${lineNumber}`); + if (Helper.isRubyTestFile(filePath)) { + Utils.sendToTerminal( + `bundle exec rspec ${relativePath}:${lineNumber}` + ); + } else if (Helper.isElixirTestFile(filePath)) { + Utils.sendToTerminal(`mix test ${relativePath}:${lineNumber}`); } } } @@ -244,7 +144,7 @@ export function activate(context: ExtensionContext) { "barHelper.runDBRemigrate", () => { // TODO: Support Elixir - sendToTerminal( + Utils.sendToTerminal( "bin/rails db:environment:set RAILS_ENV=development && bin/rails db:drop db:setup" ); } @@ -254,7 +154,7 @@ export function activate(context: ExtensionContext) { "barHelper.runDBMigrate", () => { // TODO: Support Elixir - sendToTerminal("bundle exec rails db:migrate"); + Utils.sendToTerminal("bundle exec rails db:migrate"); } ); @@ -262,7 +162,7 @@ export function activate(context: ExtensionContext) { "barHelper.startInteractiveConsole", () => { // TODO: Support Elixir - sendToTerminal("bundle exec rails console"); + Utils.sendToTerminal("bundle exec rails console"); } ); @@ -270,7 +170,7 @@ export function activate(context: ExtensionContext) { "barHelper.startWebServer", () => { // TODO: Support Elixir - sendToTerminal("foreman start -f Procfile.dev"); + Utils.sendToTerminal("foreman start -f Procfile.dev"); } ); @@ -285,13 +185,13 @@ export function activate(context: ExtensionContext) { const filePath = editor.document.fileName; const relativePath = workspace.asRelativePath(filePath, false); - if (isRubyFile(filePath)) { - sendToTerminal(`bundle exec rubocop -a ${relativePath}`); - } else if (isElixirFile(filePath)) { - sendToTerminal(`mix format ${relativePath}`); - } else if (isJSFile(filePath)) { + if (Helper.isRubyFile(filePath)) { + Utils.sendToTerminal(`bundle exec rubocop -a ${relativePath}`); + } else if (Helper.isElixirFile(filePath)) { + Utils.sendToTerminal(`mix format ${relativePath}`); + } else if (Helper.isJSFile(filePath)) { // TODO: Support npm - sendToTerminal(`yarn eslint --color --fix ${relativePath}`); + Utils.sendToTerminal(`yarn eslint --color --fix ${relativePath}`); } } } @@ -300,21 +200,28 @@ export function activate(context: ExtensionContext) { const runGitPushCommand = commands.registerCommand( "barHelper.runGitPush", () => { - sendToTerminal("ggpush -f"); + Utils.sendToTerminal("ggpush -f"); + } + ); + + const runGitStatusCommand = commands.registerCommand( + "barHelper.runGitStatus", + () => { + Utils.sendToTerminal("gst"); } ); const runGitRebaseContinueCommand = commands.registerCommand( "barHelper.runGitRebaseContinue", () => { - sendToTerminal("ga . && g rebase --continue"); + Utils.sendToTerminal("ga . && g rebase --continue"); } ); const runGitRebaseSkipCommand = commands.registerCommand( "barHelper.runGitRebaseSkip", () => { - sendToTerminal("g rebase --skip"); + Utils.sendToTerminal("g rebase --skip"); } ); @@ -323,6 +230,7 @@ export function activate(context: ExtensionContext) { const textEditorDisposable = window.onDidChangeActiveTextEditor(onUpdatePath); // Adjust here to add more items + // TODO: Move to the CommandSetup file context.subscriptions.concat([ textEditorDisposable, runTestFileCommand, @@ -333,13 +241,34 @@ export function activate(context: ExtensionContext) { startWebServerCommand, formatCodeFileCommand, runGitPushCommand, + runGitStatusCommand, runGitRebaseContinueCommand, runGitRebaseSkipCommand, ]); } export function deactivate() { - BAR_ITEMS.forEach((item) => { + BAR_ITEMS.forEach((item: any) => { item.dispose(); }); } + +const onUpdatePath = () => { + const editor = window.activeTextEditor; + + if (editor === undefined) { + Utils.hideTestItems(); + Item.formatCodeFileItem.hide(); + } else { + const filePath = editor.document.fileName; + + // Always show the format item on any file + Item.formatCodeFileItem.show(); + + if (Helper.isTestFile(filePath)) { + Utils.showTestItems(); + } else { + Utils.hideTestItems(); + } + } +}; diff --git a/src/helper.js b/src/helper.js new file mode 100644 index 0000000..7fad955 --- /dev/null +++ b/src/helper.js @@ -0,0 +1,23 @@ +export const isTestFile = (filePath: any) => { + return isRubyTestFile(filePath) || isElixirTestFile(filePath); +}; + +export const isRubyTestFile = (filePath: any) => { + return filePath.includes("_spec.rb"); +}; + +export const isElixirTestFile = (filePath: any) => { + return filePath.includes("_test.exs"); +}; + +export const isRubyFile = (filePath: any) => { + return filePath.includes(".rb"); +}; + +export const isElixirFile = (filePath: any) => { + return filePath.includes(".exs") || filePath.includes(".ex"); +}; + +export const isJSFile = (filePath: any) => { + return filePath.includes(".js"); +}; diff --git a/src/item.js b/src/item.js new file mode 100644 index 0000000..32c1723 --- /dev/null +++ b/src/item.js @@ -0,0 +1,70 @@ +import { window, StatusBarAlignment } from "vscode"; + +// Item definitions + +export const runTestFileItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -1 +); +export const runTestLineItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -2 +); +export const formatCodeFileItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -3 +); +export const runDBMigrateItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -4 +); +export const startInteractiveConsoleItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -5 +); +export const startWebServerItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -6 +); +export const runDBRemigrateItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -7 +); +export const gitStatusItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -9 +); +export const gitFetchItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -10 +); +export const gitPushItem = window.createStatusBarItem( + StatusBarAlignment.Left, + -11 +); + +// Right side +export const gitRebaseSkipItem = window.createStatusBarItem( + StatusBarAlignment.Right, + 1 +); +export const gitRebaseContinueItem = window.createStatusBarItem( + StatusBarAlignment.Right, + 2 +); + +// Adjust here to add more items +export const TEST_ITEMS = [runTestFileItem, runTestLineItem]; +export const BAR_ITEMS = [ + runDBRemigrateItem, + runDBMigrateItem, + startInteractiveConsoleItem, + startWebServerItem, + formatCodeFileItem, + gitPushItem, + gitStatusItem, + gitFetchItem, + gitRebaseContinueItem, + gitRebaseSkipItem, + ...TEST_ITEMS, +]; diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..b33c01d --- /dev/null +++ b/src/utils.js @@ -0,0 +1,40 @@ +export const sendToTerminal = (thisText: string) => { + let terminal = undefined; + + if (window.activeTerminal) { + terminal = window.activeTerminal; + } else { + terminal = window.createTerminal("Bar Helper"); + } + + terminal.show(); + + terminal.sendText(thisText); +}; + +export const showItems = (items: any) => { + items.forEach((item: any) => { + item.show(); + }); +}; + +export const setupItem = ( + item: any, + thisText: any, + thisTooltip: any, + thisCommand: any +) => { + item.text = thisText; + item.tooltip = thisTooltip; + item.command = thisCommand; +}; + +export const hideTestItems = () => { + TEST_ITEMS.forEach((item) => { + item.hide(); + }); +}; + +export const showTestItems = () => { + showItems(TEST_ITEMS); +};