2017-02-16 03:27:16 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const config = require('../../core/config.js');
|
|
|
|
const db = require('../../core/database.js');
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
|
|
|
const inq = require('inquirer');
|
|
|
|
const fs = require('fs');
|
|
|
|
const hjson = require('hjson');
|
2023-03-18 20:30:37 +00:00
|
|
|
const log = require('../../core/logger');
|
2022-06-05 20:04:25 +00:00
|
|
|
|
|
|
|
const packageJson = require('../../package.json');
|
|
|
|
|
|
|
|
exports.printUsageAndSetExitCode = printUsageAndSetExitCode;
|
|
|
|
exports.getDefaultConfigPath = getDefaultConfigPath;
|
|
|
|
exports.getConfigPath = getConfigPath;
|
|
|
|
exports.initConfigAndDatabases = initConfigAndDatabases;
|
|
|
|
exports.getAreaAndStorage = getAreaAndStorage;
|
|
|
|
exports.looksLikePattern = looksLikePattern;
|
|
|
|
exports.getAnswers = getAnswers;
|
|
|
|
exports.writeConfig = writeConfig;
|
|
|
|
|
|
|
|
const HJSONStringifyCommonOpts = (exports.HJSONStringifyCommonOpts = {
|
|
|
|
emitRootBraces: true,
|
|
|
|
bracesSameLine: true,
|
|
|
|
space: 4,
|
|
|
|
keepWsc: true,
|
|
|
|
quotes: 'min',
|
|
|
|
eol: '\n',
|
|
|
|
});
|
|
|
|
|
|
|
|
const exitCodes = (exports.ExitCodes = {
|
|
|
|
SUCCESS: 0,
|
|
|
|
ERROR: -1,
|
|
|
|
BAD_COMMAND: -2,
|
|
|
|
BAD_ARGS: -3,
|
2017-02-25 06:39:31 +00:00
|
|
|
});
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isUndefined(exitCode)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
exitCode = exitCodes.ERROR;
|
|
|
|
}
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
process.exitCode = exitCode;
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (errMsg) {
|
2018-06-22 05:15:04 +00:00
|
|
|
console.error(errMsg);
|
|
|
|
}
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getDefaultConfigPath() {
|
2018-06-22 05:15:04 +00:00
|
|
|
return './config/';
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 18:31:24 +00:00
|
|
|
function getConfigPath() {
|
2020-06-30 04:27:31 +00:00
|
|
|
const baseConfigPath = argv.config ? argv.config : config.Config.getDefaultPath();
|
2018-06-22 05:15:04 +00:00
|
|
|
return baseConfigPath + 'config.hjson';
|
2017-02-20 18:31:24 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
function initConfig(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const configPath = getConfigPath();
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
config.Config.create(configPath, { keepWsc: true, hotReload: false }, cb);
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function initConfigAndDatabases(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
initConfig(callback);
|
|
|
|
},
|
|
|
|
function initDb(callback) {
|
2023-03-18 20:30:37 +00:00
|
|
|
log.init();
|
2018-06-22 05:15:04 +00:00
|
|
|
db.initializeDatabases(callback);
|
|
|
|
},
|
2018-06-30 05:04:31 +00:00
|
|
|
function initArchiveUtil(callback) {
|
|
|
|
// ensure we init ArchiveUtil without events
|
2022-06-05 20:04:25 +00:00
|
|
|
require('../../core/archive_util').getInstance(false); // false=hotReload
|
2018-06-30 05:04:31 +00:00
|
|
|
return callback(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getAreaAndStorage(tags) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return tags.map(tag => {
|
|
|
|
const parts = tag.toString().split('@');
|
|
|
|
const entry = {
|
2022-06-05 20:04:25 +00:00
|
|
|
areaTag: parts[0],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
2022-06-05 20:04:25 +00:00
|
|
|
entry.pattern = entry.areaTag; // handy
|
|
|
|
if (parts[1]) {
|
2018-06-22 05:15:04 +00:00
|
|
|
entry.storageTag = parts[1];
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
});
|
2018-03-10 18:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function looksLikePattern(tag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
// globs can start with @
|
2022-06-05 20:04:25 +00:00
|
|
|
if (tag.indexOf('@') > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-10 18:37:23 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return /[*?[\]!()+|^]/.test(tag);
|
2018-11-25 03:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getAnswers(questions, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
inq.prompt(questions).then(answers => {
|
2018-11-25 03:02:19 +00:00
|
|
|
return cb(answers);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeConfig(config, path) {
|
2022-06-05 20:04:25 +00:00
|
|
|
config = hjson
|
|
|
|
.stringify(config, HJSONStringifyCommonOpts)
|
|
|
|
.replace(/%ENIG_VERSION%/g, packageJson.version)
|
|
|
|
.replace(/%HJSON_VERSION%/g, hjson.version);
|
2018-11-25 03:02:19 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(path, config, 'utf8');
|
|
|
|
return true;
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-11-25 03:02:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|