🛠 refact: commands imported to extention.ts
This commit is contained in:
parent
b26840cba8
commit
74c11dc53c
18
package.json
18
package.json
@ -2,7 +2,9 @@
|
||||
"name": "harvest-vscode",
|
||||
"displayName": "harvest-vscode",
|
||||
"description": "Varvest Integration to VS Code",
|
||||
"version": "0.0.1",
|
||||
"version": "0.1.0",
|
||||
"publisher": "Tzedakah",
|
||||
"repository": "https://github.com/joshuashoemaker/harvest-vscode",
|
||||
"engines": {
|
||||
"vscode": "^1.49.0"
|
||||
},
|
||||
@ -10,14 +12,22 @@
|
||||
"Other"
|
||||
],
|
||||
"activationEvents": [
|
||||
"onCommand:harvest-vscode.helloWorld"
|
||||
"*"
|
||||
],
|
||||
"main": "./out/extension.js",
|
||||
"contributes": {
|
||||
"commands": [
|
||||
{
|
||||
"command": "harvest-vscode.helloWorld",
|
||||
"title": "Hello World"
|
||||
"command": "harvest-vscode.punchTime",
|
||||
"title": "Harvest: Punch Time"
|
||||
},
|
||||
{
|
||||
"command": "harvest-vscode.login",
|
||||
"title": "Harvest: Login"
|
||||
},
|
||||
{
|
||||
"command": "harvest-vscode.logout",
|
||||
"title": "Harvest: Logout"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
15
src/UseCases/Commands/Logout.ts
Normal file
15
src/UseCases/Commands/Logout.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import * as vscode from "vscode";
|
||||
import Harvest from "../../Entities/Harvest"
|
||||
import User from "../../Entities/User"
|
||||
|
||||
function Logout (context: vscode.ExtensionContext): vscode.Disposable {
|
||||
return vscode.commands.registerCommand('harvest-vscode.logout', async () => {
|
||||
new Harvest().destructor()
|
||||
new User().destructor()
|
||||
|
||||
await context.globalState.update('accountId', '')
|
||||
await context.globalState.update('accessToken', '')
|
||||
})
|
||||
}
|
||||
|
||||
export default Logout
|
45
src/UseCases/Commands/PunchTime.ts
Normal file
45
src/UseCases/Commands/PunchTime.ts
Normal file
@ -0,0 +1,45 @@
|
||||
import * as vscode from "vscode";
|
||||
import Harvest from "../../Entities/Harvest"
|
||||
import UserInterface from "../../Entities/Interfaces/UserInterface";
|
||||
import User from "../../Entities/User"
|
||||
import getUser from "../getUser";
|
||||
|
||||
function PunchTime (context: vscode.ExtensionContext): vscode.Disposable {
|
||||
return vscode.commands.registerCommand('harvest-vscode.punchTime', async () => {
|
||||
const harvest = new Harvest()
|
||||
let user = new User()
|
||||
|
||||
const accountId: string = context.globalState.get('accountId') || ''
|
||||
const accessToken: string = context.globalState.get('accessToken') || ''
|
||||
|
||||
if (!accountId || !accessToken) {
|
||||
vscode.window.showErrorMessage('Run "Harvest: Login" Command before trying to puch time')
|
||||
return
|
||||
}
|
||||
|
||||
harvest.accountId = accountId
|
||||
harvest.accessToken = accessToken
|
||||
|
||||
if (!user.id) {
|
||||
let userProps: UserInterface
|
||||
try {
|
||||
userProps = await getUser()
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
vscode.window.showErrorMessage('Could not retrieve user data from Harvest')
|
||||
return
|
||||
}
|
||||
if (!userProps.id) {
|
||||
vscode.window.showErrorMessage('Could not retrieve user data from Harvest')
|
||||
return
|
||||
}
|
||||
user.destructor()
|
||||
user = new User(userProps)
|
||||
vscode.window.showInformationMessage('Successfully authenticated with Harvest')
|
||||
}
|
||||
|
||||
console.log(user)
|
||||
})
|
||||
}
|
||||
|
||||
export default PunchTime
|
42
src/UseCases/Commands/SetUserAuthentication.ts
Normal file
42
src/UseCases/Commands/SetUserAuthentication.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import * as vscode from "vscode";
|
||||
import Harvest from "../../Entities/Harvest"
|
||||
import User from "../../Entities/User"
|
||||
|
||||
function SetUserAuthentication (context: vscode.ExtensionContext): vscode.Disposable {
|
||||
return vscode.commands.registerCommand('harvest-vscode.login', async () => {
|
||||
new Harvest().destructor()
|
||||
new User().destructor()
|
||||
|
||||
const accountId = await vscode.window.showInputBox({
|
||||
ignoreFocusOut: true,
|
||||
placeHolder: 'Harvest Acount Id'
|
||||
})
|
||||
|
||||
if (!accountId) {
|
||||
vscode.window.showErrorMessage('No Account Id Proveded')
|
||||
return
|
||||
}
|
||||
|
||||
const accessToken = await vscode.window.showInputBox({
|
||||
ignoreFocusOut: true,
|
||||
placeHolder: 'Harvest Access Token',
|
||||
password: true
|
||||
})
|
||||
|
||||
if (!accessToken) {
|
||||
vscode.window.showErrorMessage('No Access Token Proveded')
|
||||
return
|
||||
}
|
||||
|
||||
await context.globalState.update('accountId', accountId)
|
||||
await context.globalState.update('accessToken', accessToken)
|
||||
|
||||
new Harvest({
|
||||
accountId: accountId,
|
||||
accessToken: accessToken
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
export default SetUserAuthentication
|
@ -1,10 +1,8 @@
|
||||
import axios from 'axios'
|
||||
import Harvest from '../Entities/Harvest'
|
||||
import ErrorMessage from '../Constants/ErrorMessageInterface'
|
||||
import ErrorMessages from '../Constants/ErrorMessages'
|
||||
import UserInterface from '../Entities/Interfaces/UserInterface'
|
||||
|
||||
const getUser = async (): Promise<UserInterface | ErrorMessage> => {
|
||||
const getUser = async (): Promise<UserInterface> => {
|
||||
const harvest = new Harvest()
|
||||
let userResponse: any
|
||||
try {
|
||||
@ -14,15 +12,14 @@ const getUser = async (): Promise<UserInterface | ErrorMessage> => {
|
||||
)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
return ErrorMessages[0]
|
||||
}
|
||||
|
||||
const user = {
|
||||
id: userResponse.data.id || '',
|
||||
firstName: userResponse.data.first_name || '',
|
||||
lastName: userResponse.data.last_name || '',
|
||||
email: userResponse.data.email || '',
|
||||
avatar: userResponse.data.avatar_url || '',
|
||||
id: userResponse?.data?.id || '',
|
||||
firstName: userResponse?.data?.first_name || '',
|
||||
lastName: userResponse?.data?.last_name || '',
|
||||
email: userResponse?.data?.email || '',
|
||||
avatar: userResponse?.data?.avatar_url || '',
|
||||
}
|
||||
|
||||
return user
|
||||
|
@ -1,27 +1,22 @@
|
||||
// The module 'vscode' contains the VS Code extensibility API
|
||||
// Import the module and reference it with the alias vscode in your code below
|
||||
import * as vscode from 'vscode';
|
||||
import * as vscode from 'vscode'
|
||||
import Logout from './UseCases/Commands/Logout'
|
||||
import PunchTime from './UseCases/Commands/PunchTime'
|
||||
import SetUserAuthentication from './UseCases/Commands/SetUserAuthentication'
|
||||
|
||||
// this method is called when your extension is activated
|
||||
// your extension is activated the very first time the command is executed
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
const statusbar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100)
|
||||
statusbar.command = 'harvest-vscode.punchTime'
|
||||
|
||||
statusbar.text = 'Harvest'
|
||||
statusbar.show()
|
||||
|
||||
// Use the console to output diagnostic information (console.log) and errors (console.error)
|
||||
// This line of code will only be executed once when your extension is activated
|
||||
console.log('Congratulations, your extension "harvest-vscode" is now active!');
|
||||
const commands: vscode.Disposable[] = [
|
||||
SetUserAuthentication(context),
|
||||
Logout(context),
|
||||
PunchTime(context)
|
||||
]
|
||||
|
||||
// The command has been defined in the package.json file
|
||||
// Now provide the implementation of the command with registerCommand
|
||||
// The commandId parameter must match the command field in package.json
|
||||
let disposable = vscode.commands.registerCommand('harvest-vscode.helloWorld', () => {
|
||||
// The code you place here will be executed every time your command is executed
|
||||
|
||||
// Display a message box to the user
|
||||
vscode.window.showInformationMessage('Hello World from harvest-vscode!');
|
||||
});
|
||||
|
||||
context.subscriptions.push(disposable);
|
||||
context.subscriptions.push(...commands)
|
||||
}
|
||||
|
||||
// this method is called when your extension is deactivated
|
||||
export function deactivate() {}
|
||||
|
BIN
src/media/harvestLogo.png
Normal file
BIN
src/media/harvestLogo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 11 KiB |
Loading…
x
Reference in New Issue
Block a user