Skip to content

Commit

Permalink
Merge pull request #13 from infeeeee/dev
Browse files Browse the repository at this point in the history
1.0.2
  • Loading branch information
infeeeee authored Mar 25, 2020
2 parents 17e6b86 + 49d9752 commit 272dc2d
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ Prerequisites:
```
git clone https://github.com/infeeeee/kimai2-cmd
cd kimai2-cmd
npm install
npm i
```

### Build

Prerequisite: globally installed [pkg](https://github.com/zeit/pkg):

```
npm install pkg -g
npm i pkg -g
```

Build for current platform and architecture
Expand Down
196 changes: 123 additions & 73 deletions kimai2-cmd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node

/* -------------------------------------------------------------------------- */
/* Modules */
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -106,28 +107,51 @@ function callKimaiApi(httpMethod, kimaiMethod, serversettings, options = false)
function uiMainMenu(settings) {
console.log()
inquirer
.prompt([
{
type: 'list',
name: 'mainmenu',
message: 'Select command',
pageSize: process.stdout.rows - 1,
choices:
[
{ name: 'Restart recent measurement', value: 'restart' },
{ name: 'Start new measurement', value: 'start' },
{ name: 'Stop all active measurements', value: 'stop-all' },
{ name: 'Stop an active measurement', value: 'stop' },
new inquirer.Separator(),
{ name: 'List active measurements', value: 'list-active' },
{ name: 'List recent measurements', value: 'list-recent' },
{ name: 'List projects', value: 'list-projects' },
{ name: 'List activities', value: 'list-activities' },
new inquirer.Separator(),
{ name: 'Exit', value: 'exit' }
]
}
])
.prompt([{
type: 'list',
name: 'mainmenu',
message: 'Select command',
pageSize: process.stdout.rows - 1,
choices: [{
name: 'Restart recent measurement',
value: 'restart'
},
{
name: 'Start new measurement',
value: 'start'
},
{
name: 'Stop all active measurements',
value: 'stop-all'
},
{
name: 'Stop an active measurement',
value: 'stop'
},
new inquirer.Separator(),
{
name: 'List active measurements',
value: 'list-active'
},
{
name: 'List recent measurements',
value: 'list-recent'
},
{
name: 'List projects',
value: 'list-projects'
},
{
name: 'List activities',
value: 'list-activities'
},
new inquirer.Separator(),
{
name: 'Exit',
value: 'exit'
}
]
}])
.then(answers => {
if (program.verbose) {
console.log('selected answer: ' + answers.mainmenu)
Expand All @@ -153,8 +177,11 @@ function uiMainMenu(settings) {
case 'stop':
kimaiList(settings, 'timesheets/active', false)
.then(res => {
return uiSelectMeasurement(res[1])
}).then(stopId => {
if (res[1].length > 0) {
return uiSelectMeasurement(res[1])
}
})
.then(stopId => {
return kimaiStop(settings, stopId)
})
.then(res => uiMainMenu(res[0]))
Expand Down Expand Up @@ -208,13 +235,15 @@ function uiKimaiStart(settings) {
const selected = {}
kimaiList(settings, 'projects', false)
.then(res => {
// console.log(res[1])
return uiAutocompleteSelect(res[1], 'Select project')
})
.then(res => {
// console.log(res)
selected.projectId = res.id
return kimaiList(settings, 'activities', false, { filter: { project: res.id } })
return kimaiList(settings, 'activities', false, {
filter: {
project: res.id
}
})
})
.then(res => {
return uiAutocompleteSelect(res[1], 'Select activity')
Expand Down Expand Up @@ -248,7 +277,9 @@ function kimaiStart(settings, project, activity) {
console.log("kimaistart calling api:", body)
}

callKimaiApi('POST', 'timesheets', settings.serversettings, { reqbody: body })
callKimaiApi('POST', 'timesheets', settings.serversettings, {
reqbody: body
})
.then(res => {
console.log('Started: ' + res.id)
resolve()
Expand Down Expand Up @@ -296,9 +327,13 @@ function kimaiStop(settings, id = false) {
} else {
kimaiList(settings, 'timesheets/active', false)
.then(res => {
const jsonList = res[1]
return callKimaiStop(settings, jsonList)
//callKimaiStop(settings, jsonList)
if (res[1].length > 0) {
const jsonList = res[1]
return callKimaiStop(settings, jsonList)
} else {
console.log('No active measurements')
resolve([settings])
}
})
.then(_ => {
resolve()
Expand Down Expand Up @@ -343,7 +378,9 @@ function callKimaiStop(settings, jsonList, i = 0) {
function kimaiList(settings, endpoint, print = false, options = false) {
const filter = options.filter || false
return new Promise((resolve, reject) => {
callKimaiApi('GET', endpoint, settings.serversettings, { qs: filter })
callKimaiApi('GET', endpoint, settings.serversettings, {
qs: filter
})
.then(jsonList => {
if (print) {
printList(settings, jsonList, endpoint)
Expand Down Expand Up @@ -477,22 +514,24 @@ function formattedDuration(begin, end, returnArray = false) {
function uiSelectMeasurement(thelist) {
return new Promise((resolve, reject) => {
const choices = []
if (thelist.length == 0) {
reject()
}
for (let i = 0; i < thelist.length; i++) {
const element = thelist[i];
choices.push({
name: element.project.name + " | " + element.activity.name, value: element.id
name: element.project.name + " | " + element.activity.name,
value: element.id
})
}
inquirer
.prompt([
{
type: 'list',
name: 'selectMeasurement',
message: 'Select measurement',
pageSize: process.stdout.rows - 1,
choices: choices
}
]).then(answers => {
.prompt([{
type: 'list',
name: 'selectMeasurement',
message: 'Select measurement',
pageSize: process.stdout.rows - 1,
choices: choices
}]).then(answers => {
resolve(answers.selectMeasurement)
})
})
Expand All @@ -511,31 +550,30 @@ function uiAutocompleteSelect(thelist, message) {
for (let i = 0; i < thelist.length; i++) {
const element = thelist[i];
choices.push({
name: element.name, id: element.id
name: element.name,
id: element.id
})
names.push(element.name)
}
inquirer.registerPrompt('autocomplete', require('inquirer-autocomplete-prompt'));
inquirer
.prompt([
{
type: 'autocomplete',
name: 'autoSelect',
message: message,
pageSize: process.stdout.rows - 2,
source: function (answers, input) {
input = input || '';
return new Promise((resolve, reject) => {
var fuzzyResult = fuzzy.filter(input, names);
resolve(
fuzzyResult.map(function (el) {
return el.original;
})
)
})
}
.prompt([{
type: 'autocomplete',
name: 'autoSelect',
message: message,
pageSize: process.stdout.rows - 2,
source: function (answers, input) {
input = input || '';
return new Promise((resolve, reject) => {
var fuzzyResult = fuzzy.filter(input, names);
resolve(
fuzzyResult.map(function (el) {
return el.original;
})
)
})
}
]).then(answers => {
}]).then(answers => {
let ind = names.indexOf(answers.autoSelect)
let selectedChoice = choices[ind]
// console.log(selectedChoice)
Expand Down Expand Up @@ -599,8 +637,7 @@ function checkSettings() {
*/
function uiAskForSettings() {
return new Promise((resolve, reject) => {
let questions = [
{
let questions = [{
type: 'input',
name: 'kimaiurl',
message: "Kimai2 url:"
Expand Down Expand Up @@ -644,7 +681,9 @@ function uiAskForSettings() {
settings.rainmeter.meterstyle = "styleProjects"

const thePath = iniFullPath()
if (program.verbose) { console.log('Trying to save settings to: ' + thePath) }
if (program.verbose) {
console.log('Trying to save settings to: ' + thePath)
}

fs.writeFileSync(thePath, ini.stringify(settings))
console.log('Settings saved to ' + iniPath())
Expand All @@ -663,17 +702,23 @@ function iniFullPath() {

//Maybe I should replace this terrible 'if' with some registry value reading
if (platform == 'win32' && installDir[installDir.length - 2] == "Program Files" && installDir[installDir.length - 1] == "kimai2-cmd") {
if (program.verbose) { console.log('This is an installer based windows installation') }
if (program.verbose) {
console.log('This is an installer based windows installation')
}
if (!fs.existsSync(path.join(appdata, 'kimai2-cmd'))) {
fs.mkdirSync(path.join(appdata, 'kimai2-cmd'))
}
return path.join(iniRoot.wininstaller, 'settings.ini')
} else if (dirArr[0] == 'snapshot' || dirArr[1] == 'snapshot') {
if (program.verbose) { console.log('This is a pkg version') }
if (program.verbose) {
console.log('This is a pkg version')
}
//for pkg version:
return path.join(iniRoot.pkg, 'settings.ini')
} else {
if (program.verbose) { console.log('This is an npm version') }
if (program.verbose) {
console.log('This is an npm version')
}
//For npm version:
return path.join(iniRoot.npm, 'settings.ini')
}
Expand Down Expand Up @@ -722,9 +767,10 @@ function updateRainmeter(settings) {
.then(res => {
// active measurement. Rainmeter only supports one active measurement.
rainmeterVars.Variables.serverUrl = settings.serversettings.kimaiurl
rainmeterVars.Variables.activeRecording = (res[1].length) ? res[1][0].project.name + ' | ' + res[1][0].activity.name : "No active recording"
rainmeterVars.Variables.activeRecording = (res[1].length) ? res[1][0].project.name + ' - ' + res[1][0].activity.name : "No active recording"
rainmeterVars.Variables.activeHrs = (res[1].length) ? formattedDuration(res[1][0].begin, undefined, true)[0] : ""
rainmeterVars.Variables.activeMins = (res[1].length) ? formattedDuration(res[1][0].begin, undefined, true)[1] : ""
rainmeterVars.Variables.activeRunning = (res[1].length) ? "1" : "0"

//Add first id as default
rainmeterVars.Variables.measurementid = rainmeterRaw.recent[0].id
Expand Down Expand Up @@ -757,8 +803,12 @@ function updateRainmeter(settings) {
let rainmeterDataIni = ini.stringify(rainmeterData).replaceAll('\\\\#', '#').replaceAll('"\\[', '[').replaceAll('\]"', ']').replaceAll('\\\\"', '"')

// write rainmeter files
fs.writeFileSync(rainmeterVarPath, ini.stringify(rainmeterVars), { encoding: 'utf16le' })
fs.writeFileSync(rainmeterDataPath, rainmeterDataIni, { encoding: 'utf16le' })
fs.writeFileSync(rainmeterVarPath, ini.stringify(rainmeterVars), {
encoding: 'utf16le'
})
fs.writeFileSync(rainmeterDataPath, rainmeterDataIni, {
encoding: 'utf16le'
})
if (program.verbose) {
console.log("Rainmeter files:")
console.log(rainmeterVarPath, rainmeterDataPath)
Expand All @@ -775,8 +825,8 @@ function updateRainmeter(settings) {

//different settings.ini path for developement and pkg and windows installer version
const iniRoot = {
pkg: path.dirname(process.execPath),//This is for pkg version
npm: __dirname//This is for npm version
pkg: path.dirname(process.execPath), //This is for pkg version
npm: __dirname //This is for npm version
}

if (appdata) {
Expand Down Expand Up @@ -900,4 +950,4 @@ if (!program.args.length) {
.then(settings => {
uiMainMenu(settings)
})
}
}
2 changes: 1 addition & 1 deletion kimai2-innosetup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{A10BF7B2-6641-4B06-9C68-268B649FCE57}
AppName=kimai2-cmd
AppVersion=1.0.0
AppVersion=1.0.1
AppPublisher=infeeeee
AppPublisherURL=https://github.com/infeeeee/kimai2-cmd
AppSupportURL=https://github.com/infeeeee/kimai2-cmd
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kimai2-cmd",
"version": "1.0.0",
"version": "1.0.2",
"description": "Command line client for Kimai2",
"main": "kimai2-cmd.js",
"bin": {
Expand All @@ -9,7 +9,7 @@
"scripts": {
"start": "node kimai2-cmd.js",
"build-nix": "pkg --out-path builds package.json",
"build-current": "pkg --targets node10 --out-path builds kimai2-cmd.js",
"build-current": "pkg --targets node10 --out-path builds kimai2-cmd.js",
"copy-exe-to-rainmeter": "copy .\\builds\\kimai2-cmd.exe %userprofile%\\Documents\\Rainmeter\\Skins\\kimai2-cmd-rainmeter\\@Resources\\kimai2-cmd\\"
},
"keywords": [
Expand Down Expand Up @@ -44,4 +44,4 @@
"node10-macos"
]
}
}
}

0 comments on commit 272dc2d

Please sign in to comment.