Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XDG_CONFIG_HOME to store the config files [fix #189] #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/crypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class Crypt {
this.filename = '.kryptonite';
this.data = undefined;
this.password = 'password';
this.confDir = process.env.XDG_CONFIG_HOME || path.resolve(process.env.HOME, '.config')
this.confDir = path.resolve(this.confDir, 'fb-messenger-cli')
}

setPassword(pw) {
Expand All @@ -34,14 +36,17 @@ class Crypt {

save(data) {
const encrypted = this.encrypt(data);
const savePath = path.resolve(__dirname, '../', this.filename);
const savePath = path.resolve(this.confDir, this.filename);
if (!fs.existsSync(this.confDir)){
fs.mkdirSync(this.confDir);
}
fs.writeFileSync(savePath, encrypted);
}

load(callback) {
if (!this.data) {
fs.readFile(path.resolve(__dirname, '../', this.filename), (err, data) => {
if (err) {
fs.readFile(path.resolve(this.confDir, this.filename), (err, data) => {
if(err) {
callback('No saved profile, please login');
} else {
this.decrypt(data.toString(), (err, dec) => {
Expand All @@ -61,7 +66,7 @@ class Crypt {

flush() {
this.data = undefined;
fs.unlink(path.resolve(__dirname, '../', this.filename), (err) => {
fs.unlink(path.resolve(this.confDir, this.filename), (err) => {
err('Error while logging out')
});
}
Expand Down
9 changes: 7 additions & 2 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const path = require('path');
class Settings {
constructor () {
this.filename = '.settings';
this.path = path.resolve(__dirname, '../', this.filename);
this.confDir = process.env.XDG_CONFIG_HOME || path.resolve(process.env.HOME, '.config')
this.confDir = path.resolve(this.confDir, 'fb-messenger-cli')
this.path = path.resolve(this.confDir, this.filename);
this.properties = {
disableColors: false,
groupColors: true,
Expand All @@ -29,6 +31,9 @@ class Settings {

// Save the current properties dictionary on disk
save() {
if (!fs.existsSync(this.confDir)){
fs.mkdirSync(this.confDir);
}
fs.writeFile(this.path, JSON.stringify(this.properties, null, ' '), (err) => {
if (!err) {
console.log('Settings have been saved');
Expand All @@ -41,7 +46,7 @@ class Settings {
// Load previously saved properties from disk
// callback(error, properties), where properties is a dictionary
read(callback) {
fs.readFile(path.resolve(__dirname, '../', this.filename), (err, data) => {
fs.readFile(path.resolve(this.confDir, this.filename), (err, data) => {
if (!err) {
try {
const fileProperties = JSON.parse(data.toString());
Expand Down