2017-02-16 03:27:16 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const resolvePath = require('../misc_util.js').resolvePath;
|
|
|
|
|
|
|
|
const config = require('../../core/config.js');
|
|
|
|
const db = require('../../core/database.js');
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
|
|
|
|
|
|
|
exports.printUsageAndSetExitCode = printUsageAndSetExitCode;
|
|
|
|
exports.getDefaultConfigPath = getDefaultConfigPath;
|
2017-02-20 18:31:24 +00:00
|
|
|
exports.getConfigPath = getConfigPath;
|
2017-02-16 03:27:16 +00:00
|
|
|
exports.initConfigAndDatabases = initConfigAndDatabases;
|
|
|
|
exports.getAreaAndStorage = getAreaAndStorage;
|
|
|
|
|
|
|
|
const exitCodes = exports.ExitCodes = {
|
|
|
|
SUCCESS : 0,
|
|
|
|
ERROR : -1,
|
|
|
|
BAD_COMMAND : -2,
|
|
|
|
BAD_ARGS : -3,
|
|
|
|
};
|
|
|
|
|
2017-02-25 06:39:31 +00:00
|
|
|
const argv = exports.argv = require('minimist')(process.argv.slice(2), {
|
|
|
|
alias : {
|
|
|
|
h : 'help',
|
|
|
|
v : 'version',
|
|
|
|
c : 'config',
|
|
|
|
n : 'no-prompt',
|
|
|
|
}
|
|
|
|
});
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
function printUsageAndSetExitCode(errMsg, exitCode) {
|
|
|
|
if(_.isUndefined(exitCode)) {
|
|
|
|
exitCode = exitCodes.ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
process.exitCode = exitCode;
|
|
|
|
|
|
|
|
if(errMsg) {
|
|
|
|
console.error(errMsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultConfigPath() {
|
2017-11-25 22:45:19 +00:00
|
|
|
return './config/';
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 18:31:24 +00:00
|
|
|
function getConfigPath() {
|
2017-11-25 22:45:19 +00:00
|
|
|
const baseConfigPath = argv.config ? argv.config : config.getDefaultPath();
|
|
|
|
return baseConfigPath + 'config.hjson';
|
2017-02-20 18:31:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
function initConfig(cb) {
|
2017-02-20 18:31:24 +00:00
|
|
|
const configPath = getConfigPath();
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2017-02-20 18:31:24 +00:00
|
|
|
config.init(configPath, { keepWsc : true }, cb);
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initConfigAndDatabases(cb) {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
initConfig(callback);
|
|
|
|
},
|
|
|
|
function initDb(callback) {
|
|
|
|
db.initializeDatabases(callback);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAreaAndStorage(tags) {
|
|
|
|
return tags.map(tag => {
|
2017-02-19 02:00:09 +00:00
|
|
|
const parts = tag.toString().split('@');
|
2017-02-16 03:27:16 +00:00
|
|
|
const entry = {
|
|
|
|
areaTag : parts[0],
|
|
|
|
};
|
2017-02-21 05:31:01 +00:00
|
|
|
entry.pattern = entry.areaTag; // handy
|
2017-02-16 03:27:16 +00:00
|
|
|
if(parts[1]) {
|
|
|
|
entry.storageTag = parts[1];
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
});
|
|
|
|
}
|