2017-02-16 03:27:16 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2022-06-05 20:04:25 +00:00
|
|
|
const resolvePath = require('../../core/misc_util.js').resolvePath;
|
2018-11-11 06:59:26 +00:00
|
|
|
const {
|
|
|
|
printUsageAndSetExitCode,
|
|
|
|
getConfigPath,
|
|
|
|
argv,
|
|
|
|
ExitCodes,
|
2018-11-25 03:02:19 +00:00
|
|
|
getAnswers,
|
|
|
|
writeConfig,
|
|
|
|
HJSONStringifyCommonOpts,
|
2022-06-05 20:04:25 +00:00
|
|
|
} = require('./oputil_common.js');
|
|
|
|
const getHelpFor = require('./oputil_help.js').getHelpFor;
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const inq = require('inquirer');
|
|
|
|
const mkdirsSync = require('fs-extra').mkdirsSync;
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const hjson = require('hjson');
|
|
|
|
const paths = require('path');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const sanatizeFilename = require('sanitize-filename');
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.handleConfigCommand = handleConfigCommand;
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2018-11-08 03:24:05 +00:00
|
|
|
const ConfigIncludeKeys = [
|
|
|
|
'theme',
|
2022-06-05 20:04:25 +00:00
|
|
|
'users.preAuthIdleLogoutSeconds',
|
|
|
|
'users.idleLogoutSeconds',
|
|
|
|
'users.newUserNames',
|
|
|
|
'users.failedLogin',
|
|
|
|
'users.unlockAtEmailPwReset',
|
2018-11-11 08:00:42 +00:00
|
|
|
'paths.logs',
|
2018-11-11 03:00:25 +00:00
|
|
|
'loginServers',
|
|
|
|
'contentServers',
|
2018-11-11 03:26:23 +00:00
|
|
|
'fileBase.areaStoragePrefix',
|
2018-11-11 08:00:42 +00:00
|
|
|
'logging.rotatingFile',
|
2018-11-08 03:24:05 +00:00
|
|
|
];
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
const QUESTIONS = {
|
2022-06-05 20:04:25 +00:00
|
|
|
Intro: [
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'createNewConfig',
|
|
|
|
message: 'Create a new configuration?',
|
|
|
|
type: 'confirm',
|
|
|
|
default: false,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'configPath',
|
|
|
|
message: 'Configuration path:',
|
|
|
|
default: getConfigPath(),
|
|
|
|
when: answers => answers.createNewConfig,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
OverwriteConfig: [
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'overwriteConfig',
|
|
|
|
message: 'Config file exists. Overwrite?',
|
|
|
|
type: 'confirm',
|
|
|
|
default: false,
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Basic: [
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'boardName',
|
|
|
|
message: 'BBS name:',
|
|
|
|
default: 'New ENiGMA½ BBS',
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Misc: [
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'loggingLevel',
|
|
|
|
message: 'Logging level:',
|
|
|
|
type: 'list',
|
|
|
|
choices: ['Error', 'Warn', 'Info', 'Debug', 'Trace'],
|
|
|
|
default: 2,
|
|
|
|
filter: s => s.toLowerCase(),
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
MessageConfAndArea: [
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'msgConfName',
|
|
|
|
message: 'First message conference:',
|
|
|
|
default: 'Local',
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'msgConfDesc',
|
|
|
|
message: 'Conference description:',
|
|
|
|
default: 'Local Areas',
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'msgAreaName',
|
|
|
|
message: 'First area in message conference:',
|
|
|
|
default: 'General',
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'msgAreaDesc',
|
|
|
|
message: 'Area description:',
|
|
|
|
default: 'General chit-chat',
|
|
|
|
},
|
|
|
|
],
|
2017-02-16 03:27:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function makeMsgConfAreaName(s) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return s.toLowerCase().replace(/\s+/g, '_');
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function askNewConfigQuestions(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const ui = new inq.ui.BottomBar();
|
|
|
|
|
|
|
|
let configPath;
|
|
|
|
let config;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function intro(callback) {
|
|
|
|
getAnswers(QUESTIONS.Intro, answers => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!answers.createNewConfig) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback('exit');
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust for ~ and the like
|
|
|
|
configPath = resolvePath(answers.configPath);
|
|
|
|
|
|
|
|
const configDir = paths.dirname(configPath);
|
|
|
|
mkdirsSync(configDir);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check if the file exists and can be written to
|
|
|
|
//
|
|
|
|
fs.access(configPath, fs.F_OK | fs.W_OK, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
if ('EACCES' === err.code) {
|
2018-06-22 05:15:04 +00:00
|
|
|
ui.log.write(`${configPath} cannot be written to`);
|
|
|
|
callback('exit');
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if ('ENOENT' === err.code) {
|
2018-06-22 05:15:04 +00:00
|
|
|
callback(null, false);
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
callback(null, true); // exists + writable
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function promptOverwrite(needPrompt, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (needPrompt) {
|
2018-06-22 05:15:04 +00:00
|
|
|
getAnswers(QUESTIONS.OverwriteConfig, answers => {
|
2018-11-08 03:24:05 +00:00
|
|
|
return callback(answers.overwriteConfig ? null : 'exit');
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
2018-11-08 03:24:05 +00:00
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
function basic(callback) {
|
|
|
|
getAnswers(QUESTIONS.Basic, answers => {
|
2020-06-07 21:26:11 +00:00
|
|
|
const defaultConfig = require('../../core/config_default')();
|
2018-11-08 03:24:05 +00:00
|
|
|
|
|
|
|
// start by plopping in values we want directly from config.js
|
2022-06-05 20:04:25 +00:00
|
|
|
const template = hjson.rt.parse(
|
|
|
|
fs.readFileSync(
|
|
|
|
paths.join(__dirname, '../../misc/config_template.in.hjson'),
|
|
|
|
'utf8'
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-11-08 03:24:05 +00:00
|
|
|
const direct = {};
|
|
|
|
_.each(ConfigIncludeKeys, keyPath => {
|
|
|
|
_.set(direct, keyPath, _.get(defaultConfig, keyPath));
|
|
|
|
});
|
|
|
|
|
|
|
|
config = _.mergeWith(template, direct);
|
|
|
|
|
|
|
|
// we can override/add to it based on user input from this point on...
|
|
|
|
config.general.boardName = answers.boardName;
|
2018-11-10 02:02:07 +00:00
|
|
|
|
2018-11-08 03:24:05 +00:00
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function msgConfAndArea(callback) {
|
|
|
|
getAnswers(QUESTIONS.MessageConfAndArea, answers => {
|
2022-06-05 20:04:25 +00:00
|
|
|
const confName = makeMsgConfAreaName(answers.msgConfName);
|
|
|
|
const areaName = makeMsgConfAreaName(answers.msgAreaName);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
config.messageConferences[confName] = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: answers.msgConfName,
|
|
|
|
desc: answers.msgConfDesc,
|
|
|
|
sort: 1,
|
|
|
|
default: true,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config.messageConferences[confName].areas = {};
|
|
|
|
config.messageConferences[confName].areas[areaName] = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: answers.msgAreaName,
|
|
|
|
desc: answers.msgAreaDesc,
|
|
|
|
sort: 1,
|
|
|
|
default: true,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-11-08 03:24:05 +00:00
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function misc(callback) {
|
|
|
|
getAnswers(QUESTIONS.Misc, answers => {
|
2018-11-08 03:24:05 +00:00
|
|
|
config.logging.rotatingFile.level = answers.loggingLevel;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-11-08 03:24:05 +00:00
|
|
|
return callback(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2018-11-08 03:24:05 +00:00
|
|
|
return cb(err, configPath, config);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 07:19:01 +00:00
|
|
|
const copyFileSyncSilent = (to, from, flags) => {
|
|
|
|
try {
|
|
|
|
fs.copyFileSync(to, from, flags);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-11-25 03:02:19 +00:00
|
|
|
/* absorb! */
|
|
|
|
}
|
2018-11-11 07:19:01 +00:00
|
|
|
};
|
|
|
|
|
2017-02-20 18:31:24 +00:00
|
|
|
function buildNewConfig() {
|
2022-06-05 20:04:25 +00:00
|
|
|
askNewConfigQuestions((err, configPath, config) => {
|
|
|
|
if (err) {
|
2020-07-29 02:34:30 +00:00
|
|
|
return err;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 02:34:30 +00:00
|
|
|
// ensure 'menus' exists
|
|
|
|
mkdirsSync(paths.join(__dirname, '../../config/menus'));
|
|
|
|
|
2020-07-05 20:06:34 +00:00
|
|
|
const boardName = sanatizeFilename(config.general.boardName)
|
2022-06-05 20:04:25 +00:00
|
|
|
.replace(/[^a-z0-9_-]/gi, '_')
|
2018-11-14 02:44:27 +00:00
|
|
|
.replace(/_+/g, '_')
|
|
|
|
.toLowerCase();
|
2020-07-05 20:06:34 +00:00
|
|
|
|
2020-07-07 03:02:18 +00:00
|
|
|
const includeFilesIn = [
|
2020-07-05 20:06:34 +00:00
|
|
|
'message_base.in.hjson',
|
|
|
|
'private_mail.in.hjson',
|
|
|
|
'login.in.hjson',
|
|
|
|
'new_user.in.hjson',
|
|
|
|
'doors.in.hjson',
|
|
|
|
'file_base.in.hjson',
|
2023-02-20 22:21:42 +00:00
|
|
|
'activitypub.in.hjson',
|
2020-07-05 20:06:34 +00:00
|
|
|
];
|
|
|
|
|
2020-07-07 03:02:18 +00:00
|
|
|
let includeFiles = [];
|
|
|
|
includeFilesIn.forEach(incFile => {
|
2020-07-05 20:06:34 +00:00
|
|
|
const outName = `${boardName}-${incFile.replace('.in', '')}`;
|
2020-07-07 03:02:18 +00:00
|
|
|
includeFiles.push(outName);
|
|
|
|
|
2020-07-05 20:06:34 +00:00
|
|
|
copyFileSyncSilent(
|
|
|
|
paths.join(__dirname, '../../misc/menu_templates', incFile),
|
|
|
|
paths.join(__dirname, '../../config/menus', outName),
|
|
|
|
fs.constants.COPYFILE_EXCL
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-07 03:02:18 +00:00
|
|
|
// We really only need includes to be replaced
|
2022-06-05 20:04:25 +00:00
|
|
|
const mainTemplate = fs
|
|
|
|
.readFileSync(
|
|
|
|
paths.join(__dirname, '../../misc/menu_templates/main.in.hjson'),
|
|
|
|
'utf8'
|
|
|
|
)
|
|
|
|
.replace(/%INCLUDE_FILES%/g, includeFiles.join('\n\t\t')); // cheesy, but works!
|
2020-07-05 20:06:34 +00:00
|
|
|
|
2020-07-07 03:02:18 +00:00
|
|
|
const menuFile = `${boardName}-main.hjson`;
|
|
|
|
fs.writeFileSync(
|
|
|
|
paths.join(__dirname, '../../config/menus', menuFile),
|
2020-07-05 20:06:34 +00:00
|
|
|
mainTemplate,
|
2020-07-07 03:02:18 +00:00
|
|
|
'utf8'
|
|
|
|
);
|
2020-07-05 20:06:34 +00:00
|
|
|
|
2020-07-07 03:02:18 +00:00
|
|
|
config.general.menuFile = paths.join(__dirname, '../../config/menus/', menuFile);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (writeConfig(config, configPath)) {
|
2020-07-07 03:02:18 +00:00
|
|
|
console.info('Configuration generated');
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
console.error('Failed writing configuration');
|
|
|
|
}
|
|
|
|
});
|
2017-02-20 18:31:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-11 06:59:26 +00:00
|
|
|
function catCurrentConfig() {
|
|
|
|
try {
|
2022-06-05 20:04:25 +00:00
|
|
|
const config = hjson.rt.parse(fs.readFileSync(getConfigPath(), 'utf8'));
|
2018-11-25 03:02:19 +00:00
|
|
|
const hjsonOpts = Object.assign({}, HJSONStringifyCommonOpts, {
|
2022-06-05 20:04:25 +00:00
|
|
|
colors: false === argv.colors ? false : true,
|
|
|
|
keepWsc: false === argv.comments ? false : true,
|
2018-11-11 06:59:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
console.log(hjson.stringify(config, hjsonOpts));
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
|
|
|
if ('ENOENT' == e.code) {
|
2018-11-11 06:59:26 +00:00
|
|
|
console.error(`File not found: ${getConfigPath()}`);
|
|
|
|
} else {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 18:31:24 +00:00
|
|
|
function handleConfigCommand() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === argv.help) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('Config'), ExitCodes.ERROR);
|
|
|
|
}
|
2017-02-20 18:31:24 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const action = argv._[1];
|
2017-02-20 18:31:24 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (action) {
|
|
|
|
case 'new':
|
|
|
|
return buildNewConfig();
|
|
|
|
case 'cat':
|
|
|
|
return catCurrentConfig();
|
2017-02-20 18:31:24 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
|
|
|
return printUsageAndSetExitCode(getHelpFor('Config'), ExitCodes.ERROR);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|