enigma-bbs/core/oputil/oputil_common.js

98 lines
2.2 KiB
JavaScript
Raw Normal View History

/* jslint node: true */
/* eslint-disable no-console */
'use strict';
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;
exports.getConfigPath = getConfigPath;
exports.initConfigAndDatabases = initConfigAndDatabases;
exports.getAreaAndStorage = getAreaAndStorage;
2018-03-10 18:37:23 +00:00
exports.looksLikePattern = looksLikePattern;
const exitCodes = exports.ExitCodes = {
SUCCESS : 0,
ERROR : -1,
BAD_COMMAND : -2,
BAD_ARGS : -3,
};
const argv = exports.argv = require('minimist')(process.argv.slice(2), {
alias : {
h : 'help',
v : 'version',
c : 'config',
n : 'no-prompt',
}
});
function printUsageAndSetExitCode(errMsg, exitCode) {
if(_.isUndefined(exitCode)) {
exitCode = exitCodes.ERROR;
}
process.exitCode = exitCode;
if(errMsg) {
console.error(errMsg);
}
}
function getDefaultConfigPath() {
return './config/';
}
function getConfigPath() {
const baseConfigPath = argv.config ? argv.config : config.getDefaultPath();
return baseConfigPath + 'config.hjson';
}
function initConfig(cb) {
const configPath = getConfigPath();
config.init(configPath, { keepWsc : true, noWatch : true }, cb);
}
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 => {
const parts = tag.toString().split('@');
const entry = {
areaTag : parts[0],
};
entry.pattern = entry.areaTag; // handy
if(parts[1]) {
entry.storageTag = parts[1];
}
return entry;
});
2018-03-10 18:37:23 +00:00
}
function looksLikePattern(tag) {
// globs can start with @
if(tag.indexOf('@') > 0) {
return false;
}
2018-03-10 18:37:23 +00:00
return /[*?[\]!()+|^]/.test(tag);
}