-
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.
- Loading branch information
Showing
3 changed files
with
92 additions
and
128 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//-------------------------------------- | ||
// Constant | ||
//-------------------------------------- | ||
const CONFIG_FILENAME = '.gpwd.json'; | ||
|
||
//-------------------------------------- | ||
// Module | ||
//-------------------------------------- | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
/** | ||
* Return HOME Directory path | ||
* | ||
* @return {string|boolean} | ||
*/ | ||
function getHomeDirectory(){ | ||
let path = process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"]; | ||
if( fs.existsSync(path) ){ | ||
return(path); | ||
} | ||
else{ | ||
return(false); | ||
} | ||
} | ||
|
||
/** | ||
* Return configuration | ||
* | ||
* @param {boolean} [is_human=false] | ||
* @return {Object} | ||
*/ | ||
function getConfig(is_human=false){ | ||
// Get Home directory path | ||
const home = getHomeDirectory(); | ||
if( home === false){ | ||
return({}); | ||
} | ||
|
||
// Get configuration | ||
const file = path.join(home, CONFIG_FILENAME); | ||
if( fs.existsSync(file) ){ | ||
try{ | ||
const config = require(file); | ||
if( is_human === false ){ | ||
return(config); | ||
} | ||
else if( "human" in config ){ | ||
return(config.human); | ||
} | ||
} | ||
catch(err){ | ||
error(err); | ||
} | ||
} | ||
|
||
return({}); | ||
} | ||
|
||
/** | ||
* Display Error and exit | ||
* | ||
* @param {string} str error message | ||
* @returns {void} | ||
*/ | ||
function error(str){ | ||
console.error("[Error] " + str); | ||
process.exit(1); | ||
} | ||
|
||
|
||
//-------------------------------------- | ||
// exports | ||
//-------------------------------------- | ||
module.exports.getConfig = getConfig; | ||
module.exports.getHomeDirectory = getHomeDirectory; | ||
module.exports.error = error; |