-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(setup): add command for setting up CLI config
- Loading branch information
1 parent
0504232
commit e018853
Showing
13 changed files
with
421 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {Command} from '@oclif/core' | ||
import path from "node:path"; | ||
|
||
import {configFilename} from "../../core"; | ||
import {FileSystemDataSource} from "../../infrastructure/data-sources"; | ||
import {ConfigurationRepositoryImplementation} from "../../infrastructure/repositories"; | ||
import Setup from "../setup"; | ||
|
||
export default class SetDescription extends Command { | ||
static description = 'Set your default time entry description for track CLI.' | ||
|
||
static examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
|
||
public async run(): Promise<void> { | ||
const configurationRepository = new ConfigurationRepositoryImplementation(new FileSystemDataSource(path.join(this.config.configDir, configFilename))) | ||
try { | ||
const defaultTimeEntry = await Setup.setDefaultTimeEntryDescription(configurationRepository) | ||
this.log(`Your default time entry description has been set to: "${defaultTimeEntry}"`); | ||
} catch (error) { | ||
this.log(`There was an error trying to setting up your config. Detailed error:`) | ||
this.log(`${error}`) | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {Command} from '@oclif/core' | ||
import path from "node:path"; | ||
|
||
import {configFilename} from "../../core"; | ||
import {FileSystemDataSource, TogglApi, http} from "../../infrastructure/data-sources"; | ||
import {ConfigurationRepositoryImplementation} from "../../infrastructure/repositories"; | ||
import Setup from "../setup"; | ||
|
||
export default class SetProject extends Command { | ||
static description = 'Setup your default project for track CLI.' | ||
|
||
static examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
private static sortByName = (a: { name: string }, b: { name: string }) => | ||
a.name > b.name | ||
? 1 | ||
: a.name < b.name | ||
? -1 | ||
: 0 | ||
|
||
public async run(): Promise<void> { | ||
const configurationRepository = new ConfigurationRepositoryImplementation(new FileSystemDataSource(path.join(this.config.configDir, configFilename))) | ||
try { | ||
const config = await configurationRepository.getAll() | ||
const userInfo = await TogglApi.getUserInfo(config.apiToken, http) | ||
const result = await Setup.setDefaultProject(userInfo, configurationRepository) | ||
this.log(`Your default project has been set to: ${result}`); | ||
} catch (error) { | ||
this.log(`There was an error trying to setting up your config. Detailed error:`) | ||
this.log(`${error}`) | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {Command} from '@oclif/core' | ||
import path from "node:path"; | ||
|
||
import {configFilename} from "../../core"; | ||
import {FileSystemDataSource} from "../../infrastructure/data-sources"; | ||
import {ConfigurationRepositoryImplementation} from "../../infrastructure/repositories"; | ||
import Setup from "../setup"; | ||
|
||
export default class SetToken extends Command { | ||
static description = 'Setup your Toggl Track API token for track CLI.' | ||
|
||
static examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
public async run(): Promise<void> { | ||
const configurationRepository = new ConfigurationRepositoryImplementation(new FileSystemDataSource(path.join(this.config.configDir, configFilename))) | ||
try { | ||
await Setup.setApiKey(configurationRepository) | ||
this.log('Your token has been updated.') | ||
} catch (error) { | ||
this.log(`There was an error trying to setting up your config. Detailed error:`) | ||
this.log(`${error}`) | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {Command} from '@oclif/core' | ||
import path from "node:path"; | ||
|
||
import {configFilename} from "../../core"; | ||
import {FileSystemDataSource, TogglApi, http} from "../../infrastructure/data-sources"; | ||
import {ConfigurationRepositoryImplementation} from "../../infrastructure/repositories"; | ||
import Setup from "../setup"; | ||
|
||
export default class SetWorkspace extends Command { | ||
static description = 'Setup your default workspace for track CLI.' | ||
|
||
static examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
private static sortByName = (a: { name: string }, b: { name: string }) => | ||
a.name > b.name | ||
? 1 | ||
: a.name < b.name | ||
? -1 | ||
: 0 | ||
|
||
public async run(): Promise<void> { | ||
const configurationRepository = new ConfigurationRepositoryImplementation(new FileSystemDataSource(path.join(this.config.configDir, configFilename))) | ||
try { | ||
const config = await configurationRepository.getAll() | ||
const userInfo = await TogglApi.getUserInfo(config.apiToken, http) | ||
const result = await Setup.setDefaultWorkspace(userInfo, configurationRepository) | ||
this.log(`Your default workspace has been set to: ${result}`); | ||
} catch (error) { | ||
this.log(`There was an error trying to setting up your config. Detailed error:`) | ||
this.log(`${error}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* eslint-disable camelcase */ | ||
import {input, select} from '@inquirer/prompts'; | ||
import {Command} from '@oclif/core' | ||
import path from "node:path"; | ||
|
||
import {configFilename} from "../core"; | ||
import {FileSystemDataSource, TogglApi, UserInfo, http} from "../infrastructure/data-sources"; | ||
import {ConfigurationRepositoryImplementation} from "../infrastructure/repositories"; | ||
|
||
export default class Setup extends Command { | ||
static description = 'Setup your config for track CLI.' | ||
|
||
static examples = [ | ||
'<%= config.bin %> <%= command.id %>', | ||
] | ||
|
||
private static sortByName = (a: { name: string }, b: { name: string }) => | ||
a.name > b.name | ||
? 1 | ||
: a.name < b.name | ||
? -1 | ||
: 0 | ||
|
||
static async setApiKey(configurationRepository: ConfigurationRepositoryImplementation) { | ||
const apiKey = await input({message: 'Enter your name API Key:\n(For getting your Toggl API token you can go to https://track.toggl.com/profile and scroll to the bottom of your profile)\n'}); | ||
await configurationRepository.setApiToken(apiKey) | ||
return apiKey; | ||
} | ||
|
||
static async setDefaultProject({clients, projects}: UserInfo, configurationRepository: ConfigurationRepositoryImplementation) { | ||
const clientMap = new Map(clients.map(client => [client.id, client])) | ||
const choices = projects | ||
.filter(({active}) => active) | ||
.sort(Setup.sortByName) | ||
.map(project => ({ | ||
name: `${project.name} (${clientMap.get(project.client_id)?.name || 'No client'})`, | ||
value: project.id | ||
})) | ||
const defaultProjectId = await select({ | ||
choices, | ||
message: 'Select your default project', | ||
}); | ||
await configurationRepository.setDefaultProjectId(defaultProjectId) | ||
return choices.find(({value}) => value === defaultProjectId)?.name | ||
} | ||
|
||
static async setDefaultTimeEntryDescription(configurationRepository: ConfigurationRepositoryImplementation) { | ||
const defaultTimeEntry = await input({ | ||
default: 'Working', | ||
message: 'Enter your default time entry description.', | ||
validate: Boolean | ||
}); | ||
await configurationRepository.setDefaultTimeEntry(defaultTimeEntry) | ||
return defaultTimeEntry | ||
} | ||
|
||
static async setDefaultWorkspace({default_workspace_id, workspaces}: UserInfo, configurationRepository: ConfigurationRepositoryImplementation) { | ||
workspaces.sort(Setup.sortByName) | ||
const defaultWorkspace = workspaces.find(({id}) => id === default_workspace_id)! | ||
const choices = [defaultWorkspace, ...workspaces.filter(({id}) => id !== default_workspace_id)].map(workspace => ({ | ||
name: workspace.name, | ||
value: workspace.id | ||
})) | ||
const defaultWorkspaceId = await select({ | ||
choices, | ||
message: 'Select your default workspace', | ||
}); | ||
await configurationRepository.setDefaultWorkspaceId(defaultWorkspaceId) | ||
return choices.find(({value}) => value === defaultWorkspaceId)?.name | ||
} | ||
|
||
public async run(): Promise<void> { | ||
const configurationRepository = new ConfigurationRepositoryImplementation(new FileSystemDataSource(path.join(this.config.configDir, configFilename))) | ||
try { | ||
// Add your API Key | ||
const apiKey = await Setup.setApiKey(configurationRepository); | ||
// Get user info | ||
const userInfo = await TogglApi.getUserInfo(apiKey, http) | ||
// Select your default workspace | ||
await Setup.setDefaultWorkspace(userInfo, configurationRepository); | ||
// Select your default project | ||
await Setup.setDefaultProject(userInfo, configurationRepository); | ||
// Add your default time entry description | ||
await Setup.setDefaultTimeEntryDescription(configurationRepository); | ||
this.log('Your config for track CLI has been setup.') | ||
} catch (error) { | ||
this.log(`There was an error trying to setting up your config. Detailed error:`) | ||
this.log(`${error}`) | ||
} | ||
} | ||
} |
Oops, something went wrong.