-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
235 lines (200 loc) · 7.13 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const vscode = require('vscode');
const toggl = require('./toggl');
const {open} = require('fs');
async function activate(context) {
const configureToggl = vscode.commands.registerCommand(
'autoggl.configureToggl',
async () => {
let apiToken = await vscode.window.showInputBox({
password: true,
ignoreFocusOut: true,
placeHolder: 'API Token',
prompt: 'Get your API Token at https://toggl.com/app/profile',
});
if (!apiToken || apiToken === '' || apiToken === undefined) {
vscode.window.showErrorMessage(
'No token provided. If a token is configured already, it will not be replaced.',
);
return;
}
let tokenValidation = await toggl.validateApiToken(apiToken);
if (tokenValidation.success) {
// To prevent potential permissions issues when modifying configuration
// and for sake of supporting alternative .vscode/ locations, we must
// update the global configuration rather than the autoggl configuration
// alone. Third argument in update() method should be `true` to setGlobal
// and getConfiguration() should be called without a section.
await vscode.workspace
.getConfiguration()
.update('autoggl.togglApiToken', apiToken, true);
// Verify successful update of configuration value
if (
vscode.workspace.getConfiguration('autoggl').get('togglApiToken') ===
apiToken
) {
vscode.window.showInformationMessage(
'Toggl API token updated successfully!',
);
} else {
vscode.window.showErrorMessage(
'Unable to update Toggl API token. Please try updating in settings manually.',
);
}
} else {
vscode.window.showErrorMessage(
`Toggl Token Error: ${tokenValidation.error}`,
);
}
let workspaces = await toggl.getUserWorkspaces(apiToken);
// Vscode QuickPick takes in an array of strings for selection
// so we'll map over the workspaces to get the names array
// then get the matching object to configure id.
let workspaceNames = workspaces.map((workspace) => {
return workspace.name;
});
let selectedWorkspaceName = await vscode.window.showQuickPick(
workspaceNames,
{
canPickMany: false,
ignoreFocusOut: true,
placeHolder: 'Select a workspace..',
},
);
let selectedWorkspaces = await workspaces.filter(
(workspace) => workspace.name === selectedWorkspaceName,
);
await vscode.workspace
.getConfiguration()
.update('autoggl.workspaceId', selectedWorkspaces[0].id, true);
vscode.window.showInformationMessage(
`Autoggl: Set ${selectedWorkspaceName} as active Workspace.`,
);
},
);
const enable = vscode.commands.registerCommand('autoggl.enable', async () => {
await vscode.workspace
.getConfiguration()
.update('autoggl.enabled', true, true);
if (vscode.workspace.getConfiguration().get('autoggl.enabled') === true) {
vscode.window.showInformationMessage('Autoggl: Tracking Enabled');
} else {
vscode.window.showErrorMessage(
'Autoggl: Unable to enable Autoggl. Try enabling manually in settings.',
);
}
});
const disable = vscode.commands.registerCommand(
'autoggl.disable',
async () => {
await vscode.workspace
.getConfiguration()
.update('autoggl.enabled', false, true);
if (
vscode.workspace.getConfiguration().get('autoggl.enabled') === false
) {
vscode.window.showInformationMessage('Autoggl: Tracking Disabled');
} else {
vscode.window.showErrorMessage(
'Autoggl: Unable to disable Autoggl. Try disabling manually in settings.',
);
}
},
);
const pauseTimer = vscode.commands.registerCommand(
'autoggl.pauseTimer',
async () => {
let apiToken = vscode.workspace
.getConfiguration()
.get('autoggl.togglApiToken');
let activeTimeEntryId = vscode.workspace
.getConfiguration()
.get('autoggl.activeTimeEntryId');
await toggl.stopTimer(apiToken, activeTimeEntryId).then(() => {
vscode.window.showInformationMessage('Autoggl: Timer Paused');
});
},
);
const startTimer = vscode.commands.registerCommand(
'autoggl.startTimer',
async () => {
let apiToken = vscode.workspace
.getConfiguration('autoggl')
.get('togglApiToken');
let workspaceId = vscode.workspace
.getConfiguration('autoggl')
.get('workspaceId');
let openedProjectName = vscode.workspace.workspaceFolders[0].name;
let togglProjects = await toggl.getWorkspaceProjects(
apiToken,
workspaceId,
);
let currentProjectId;
// Create a project if it doesn't exist, or else get the existing
if (!togglProjects) {
let createdProject = await toggl.createProject(
apiToken,
openedProjectName,
workspaceId,
);
currentProjectId = createdProject.id;
} else {
let togglProjectNames = togglProjects.map((project) => {
return project.name;
});
if (togglProjectNames.includes(openedProjectName)) {
currentProjectId = togglProjects.filter(
(project) => project.name === openedProjectName,
)[0].id;
} else {
let createdProject = await toggl.createProject(
apiToken,
openedProjectName,
workspaceId,
);
currentProjectId = createdProject.id;
}
vscode.window.showInformationMessage(
`Autoggl: Starting timer for '${openedProjectName}'.`,
);
}
// Start a time entry
let timeEntry = await toggl.startTimer(apiToken, currentProjectId);
let timeEntryId = timeEntry.id;
await vscode.workspace
.getConfiguration()
.update('autoggl.activeTimeEntryId', timeEntryId, true);
await vscode.window.showInformationMessage(
`Autoggl: Timer Started for '${openedProjectName}'`,
);
},
);
// Register our commands to context
context.subscriptions.push(configureToggl);
context.subscriptions.push(enable);
context.subscriptions.push(disable);
context.subscriptions.push(pauseTimer);
context.subscriptions.push(startTimer);
// Start project tracking if currently enabled
if (vscode.workspace.getConfiguration('autoggl').get('enabled')) {
vscode.commands.executeCommand('autoggl.startTimer');
}
}
exports.activate = activate;
// this method is called when your extension is deactivated
async function deactivate() {
const apiToken = vscode.workspace
.getConfiguration('autoggl')
.get('togglApiToken');
const activeTimeEntryId = vscode.workspace
.getConfiguration()
.get('autoggl.activeTimeEntryId');
await toggl.stopTimer(apiToken, activeTimeEntryId);
vscode.workspace
.getConfiguration()
.update('autoggl.activeTimeEntryId', undefined, true);
}
exports.deactivate = deactivate;
module.exports = {
activate,
deactivate,
};