Skip to content

Commit

Permalink
finding and creating settings.ini in %appdata%
Browse files Browse the repository at this point in the history
  • Loading branch information
infeeeee committed Jul 9, 2019
1 parent bdd999b commit d435c6a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ UI:

Integration:
- [x] Portable executable for all three platforms
- [ ] Installer for windows
- [ ] Generate output for Rainmeter (Windows) (Just like [kimai-cmd](https://github.com/infeeeee/kimai-cmd))
- [ ] Generate output for Argos/Kargos/Bitbar (Gnome, Kde, Mac)

Expand Down Expand Up @@ -109,6 +110,8 @@ All settings stored in the settings.ini file. Place this file to the same direct

You can create your settings.ini file manually, by downloading, renaming and editing [settings.ini.example](https://github.com/infeeeee/kimai2-cmd/blob/master/settings.ini.example).

On the windows installer version settings.ini location: `C:\Users\Username\AppData\Roaming\kimai2-cmd\settings.ini`

## Developement version

### Installation
Expand Down
77 changes: 53 additions & 24 deletions kimai2-cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
const path = require('path');
const fs = require('fs');

const platform = process.platform
const appdata = process.env.appdata

//request
const request = require('request');

Expand Down Expand Up @@ -37,7 +40,7 @@ var pjson = require('./package.json');
* options.qs querystring
* options.reqbody request body
* options.verbose Verbose
* @returns {object} The redponse body as an object
* @returns {object} The response body as an object
*
*/
function callKimaiApi(httpMethod, kimaiMethod, serversettings, options = false) {
Expand Down Expand Up @@ -305,6 +308,13 @@ function kimaiStop(settings, id = false) {
})
}

/**
* Supplementary function for stopping multiple running measurements
*
* @param {*} settings All settings
* @param {*} jsonList As the output of kimaiList()
* @param {*} i Counter, do not use!
*/
function callKimaiStop(settings, jsonList, i = 0) {
return new Promise((resolve, reject) => {
const element = jsonList[i];
Expand All @@ -315,9 +325,7 @@ function callKimaiStop(settings, jsonList, i = 0) {
if (i < jsonList.length) {
callKimaiStop(settings, jsonList, i)
} else {
//return
resolve()
// uiMainMenu(settings)
}
})
})
Expand Down Expand Up @@ -388,7 +396,6 @@ function uiSelectMeasurement(thelist) {
* @param {string} message Prompt message
*/
function uiAutocompleteSelect(thelist, message) {

return new Promise((resolve, reject) => {
const choices = []
const names = []
Expand Down Expand Up @@ -429,8 +436,6 @@ function uiAutocompleteSelect(thelist, message) {
}




/**
* Prints list to terminal
*
Expand Down Expand Up @@ -478,7 +483,6 @@ function printList(arr, endpoint, options = false) {
} else {
console.log(element.project.name, '|', element.activity.name)
}

}
}
console.log()
Expand All @@ -493,25 +497,26 @@ function printList(arr, endpoint, options = false) {
*/
function iniPath(verbose) {
//different settings.ini path for developement and pkg version
const root = [
const iniRoot = [
path.dirname(process.execPath),
__dirname
__dirname,
path.join(appdata, '/kimai2-cmd')
]
const settingsPathPkg = path.join(root[0], '/settings.ini')
const settingsPathNode = path.join(root[1], '/settings.ini')

if (verbose) {
console.log('Looking for settings.ini in the following places:')
console.log(settingsPathPkg)
console.log(settingsPathNode)
console.log(iniRoot)
}
if (fs.existsSync(settingsPathPkg)) {
return settingsPathPkg
} else if (fs.existsSync(settingsPathNode)) {
return settingsPathNode
} else {

return false
for (let i = 0; i < iniRoot.length; i++) {
const currentIniPath = path.join(iniRoot[i], '/settings.ini')
if (fs.existsSync(currentIniPath)) {
return currentIniPath
}
}

// no ini found so:
return false
}

/**
Expand All @@ -523,13 +528,13 @@ function iniPath(verbose) {
function checkSettings(verbose = false) {
return new Promise((resolve, reject) => {
const settingsPath = iniPath(verbose)
if (verbose) console.log("found at: ", settingsPath)
if (settingsPath) {
if (verbose) console.log("settings.ini found at: ", settingsPath)
let settings = ini.parse(fs.readFileSync(settingsPath, 'utf-8'))
resolve(settings)
} else {
console.log('Settings.ini not found')
uiAskForSettings()
uiAskForSettings(verbose)
.then(settings => {
resolve(settings)
})
Expand All @@ -540,8 +545,10 @@ function checkSettings(verbose = false) {

/**
* Interactive ui: asks for settings than saves them
*
* @param {boolean} verbose
*/
function uiAskForSettings() {
function uiAskForSettings(verbose = false) {
return new Promise((resolve, reject) => {
let questions = [
{
Expand All @@ -566,14 +573,36 @@ function uiAskForSettings() {
.then(answers => {
let settings = {}
settings.serversettings = answers
fs.writeFileSync('./settings.ini', ini.stringify(settings))
console.log('settings saved to ' + iniPath())

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

fs.writeFileSync(thePath, ini.stringify(settings))
console.log('Settings saved to ' + iniPath())
resolve(settings)
});
})
}


/**
* Returns the ini save path based on os and installation type, creates folder if necessary
*/
function iniFullPath() {
let installDir = path.dirname(process.execPath).split("\\")

//I should replace this tererible if to some registry value readin, maybe for uninstaller
if (platform == 'win32' && installDir[installDir.length - 2] == "Program Files" && installDir[installDir.length - 1] == "kimai2-cmd") {
if (!fs.existsSync(path.join(appdata, 'kimai2-cmd'))) {
fs.mkdirSync(path.join(appdata, 'kimai2-cmd'))
}
return path.join(appdata, 'kimai2-cmd', 'settings.ini')
} else {
return './settings.ini'
}
}


/**
* Removes trailing slashes from url
*
Expand Down

0 comments on commit d435c6a

Please sign in to comment.