move config and local db to ~/.adafruit_io
This commit is contained in:
parent
5db3b4819d
commit
fc4438ad00
6 changed files with 60 additions and 16 deletions
14
cli.js
14
cli.js
|
|
@ -1,10 +1,14 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
process.title = 'adafruit-io';
|
||||
const path = require('path');
|
||||
require('dotenv').config({silent: true, path: path.join(__dirname, '.env')});
|
||||
|
||||
const CLI = require('./cli/index'),
|
||||
cli = new CLI();
|
||||
const CLI = require('./cli/index');
|
||||
|
||||
cli.init();
|
||||
CLI.getConfigPath().then(path => {
|
||||
|
||||
require('dotenv').config({silent: true, path: path});
|
||||
|
||||
const cli = new CLI();
|
||||
cli.init();
|
||||
|
||||
}).catch(console.error);
|
||||
|
|
|
|||
|
|
@ -272,8 +272,6 @@ class ClientCLI extends CLI {
|
|||
|
||||
this.saveEnv();
|
||||
|
||||
this.info('Client settings saved');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
43
cli/index.js
43
cli/index.js
|
|
@ -6,7 +6,8 @@ const yargs = require('yargs'),
|
|||
version = require('../package.json').version,
|
||||
spawn = require('child_process').spawn,
|
||||
fs = require('fs'),
|
||||
path = require('path');
|
||||
path = require('path'),
|
||||
mkdir = require('mkdirp');
|
||||
|
||||
class CLI {
|
||||
|
||||
|
|
@ -134,8 +135,14 @@ class CLI {
|
|||
if(/^AIO/.test(key)) out += `${key}=${process.env[key]}\n`;
|
||||
});
|
||||
|
||||
if(out)
|
||||
fs.writeFileSync(path.join(__dirname, '..', '.env'), out);
|
||||
if(out) {
|
||||
this.constructor.getConfigPath()
|
||||
.then(f => {
|
||||
fs.writeFileSync(f, out);
|
||||
this.info('Configuration saved');
|
||||
})
|
||||
.catch(this.error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -210,6 +217,36 @@ class CLI {
|
|||
|
||||
}
|
||||
|
||||
static getUserHome() {
|
||||
return process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
|
||||
}
|
||||
|
||||
static getAdafruitIoFolder() {
|
||||
|
||||
const folder = path.join(this.getUserHome(), '.adafruit_io');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
mkdir(path.join(this.getUserHome(), '.adafruit_io'), err => {
|
||||
|
||||
if(err)
|
||||
return reject(err);
|
||||
|
||||
resolve(folder);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
static getConfigPath() {
|
||||
return this.getAdafruitIoFolder().then(folder => path.join(folder, 'config'));
|
||||
}
|
||||
|
||||
static getDbPath() {
|
||||
return this.getAdafruitIoFolder().then(folder => path.join(folder, 'adafruit_io.db'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -60,8 +60,10 @@ class ServerCLI extends CLI {
|
|||
if(command === 'config')
|
||||
return this.requireAuth(Yargs(process.argv.slice(4)));
|
||||
|
||||
process.env.AIO_SERVER_PORT = argv.port;
|
||||
this.saveEnv();
|
||||
if(parseInt(process.env.AIO_SERVER_PORT) !== parseInt(argv.port)) {
|
||||
process.env.AIO_SERVER_PORT = argv.port;
|
||||
this.saveEnv();
|
||||
}
|
||||
|
||||
this[command]();
|
||||
|
||||
|
|
@ -120,9 +122,11 @@ class ServerCLI extends CLI {
|
|||
process.env.AIO_SERVER_USER = argv.username;
|
||||
process.env.AIO_SERVER_KEY = argv.key;
|
||||
|
||||
this.saveEnv();
|
||||
|
||||
this.info('Server settings saved');
|
||||
this.constructor.getDbPath()
|
||||
.then(db => {
|
||||
process.env.AIO_SERVER_DB = db;
|
||||
this.saveEnv();
|
||||
}).catch(this.error);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -46,6 +46,7 @@
|
|||
"express": "^4.13.3",
|
||||
"express-csv": "^0.6.0",
|
||||
"inquirer": "^0.10.0",
|
||||
"mkdirp": "^0.5.1",
|
||||
"mqtt": "^1.4.1",
|
||||
"nedb": "^1.1.3",
|
||||
"restler": "^3.2.2",
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ const nedb = require('nedb'),
|
|||
path = require('path');
|
||||
|
||||
const db = new nedb({
|
||||
filename: path.join(__dirname, '..', 'adafruit-io.db')
|
||||
filename: process.env.AIO_SERVER_DB || path.join(__dirname, '..', 'adafruit-io.db')
|
||||
});
|
||||
|
||||
db.loadDatabase();
|
||||
|
|
|
|||
Loading…
Reference in a new issue