aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/scripts/configLib/SetupWizard.js
blob: ac2f3a08c5901bbaf3942725154aa11dc6faf338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
  start as _start,
  get,
} from 'prompt';

/**
  * Server setup wizard, quick server setup and all that jazz. . .
  * @author Marzavec ( https://github.com/marzavec )
  * @version v2.0.0
  * @license WTFPL ( http://www.wtfpl.net/txt/copying/ )
  */
class SetupWizard {
  /**
    * Create a `SetupWizard` instance for initializing the server's config.json
    * @param {Object} serverConfig reference to the server config class
    */
  constructor(serverConfig) {
    this.serverConfig = serverConfig;
  }

  /**
    * Roll a d20 and begin the wizarding process
    */
  async start() {
    // load the current config to use as defaults, if available
    const currentConfig = await this.serverConfig.load() || {};

    // auto generate the salt if not currrently created
    currentConfig.tripSalt = currentConfig.tripSalt
      || [...Array(Math.floor(Math.random() * 1024) + 1024)].map(() => (~~(Math.random() * 36)).toString(36)).join('');

    // load the setup questions & set their defaults
    const questions = require('../setupSchema/Questions.js');
    questions.properties = this.setQuestionDefaults(questions.properties, currentConfig);

    // force password re-entry
    questions.properties.adminTrip.default = '';
    questions.properties.adminTrip.required = true;

    // output the packages setup banner
    require('../setupSchema/Banner.js');

    // let's start playing 20 questions
    _start();
    get(questions, (err, result) => this.finalize(err, result));
  }

  /**
    * Compares the currently loaded config with the stock questions, adds a default
    * and required option to the question
    * @param {Object} questions the set of questions from /setupSchema
    * @param {Object} currentConfig the current server options
    */
  setQuestionDefaults(questions, currentConfig) {
    Object.keys(questions).forEach((qName) => {
      if (typeof currentConfig[qName] !== 'undefined') {
        questions[qName].default = currentConfig[qName];
        questions[qName].required = false;
      } else {
        questions[qName].required = true;
      }
    });

    return questions;
  }

  /**
    * Looks like all the questions have been answered, check for errors or save
    * the new config file
    *
    * @param {Object} err any errors generated by Prompt
    * @param {Object} result the answers / new config setup
    */
  async finalize(err, result) {
    // output errors and die if needed
    if (err) {
      console.error(err);
      process.exit(0);
    }

    // initialize default mods config
    if (typeof result.mods === 'undefined') {
      result.mods = [];
    }

    // If we should log errors with the err stack when they occur.
    // See: CommandManager.js
    if (typeof result.logErrDetailed === 'undefined') {
      result.logErrDetailed = false;
    }

    // finally create the actual JSON file
    try {
      this.serverConfig.config = result;
      await this.serverConfig.save();
    } catch (e) {
      console.error(`Couldn't write config to ${this.serverConfig.configPath}
        ${e.stack}`);
    }

    // output the packages final notice before quitting
    require('../setupSchema/Footer.js');

    process.exit(0);
  }
}

export default SetupWizard;