UNPKG

3.19 kBJavaScriptView Raw
1const path = require('path');
2const fs = require('fs-extra');
3const { CONST, l10n } = require('./utils');
4const { systemDownloadsFolder, defaultConfigPath } = CONST;
5
6const DEFAULTS = {
7 'downloader': 'system',
8 'aria2-jsonrpc': 'http://localhost:6800/jsonrpc/',
9 'qbittorrent-url': 'http://localhost:8080/',
10 'qbittorrent-auth': 'username:password',
11 'destination': systemDownloadsFolder,
12 'webhook-url': 'http://localhost/',
13 'webhook-token': 'DEFAULT_WEBHOOK_TOKEN',
14};
15const VALIDATORS = {
16 'downloader': (downloader) => {
17 const result = { ok: true, msg: '' };
18 const downloaders = fs.readdirSync(`${__dirname}/downloaders`).map((d) => path.basename(d, '.js'));
19 if (!(new Set(downloaders)).has(downloader)) {
20 result.ok = false;
21 result.msg = l10n('CMD_CFG_VALIDATORS_DOWNLOADER_ERR');
22 }
23 return result;
24 },
25 'destination': (destination) => {
26 const result = { ok: true, msg: '' };
27 if (!(fs.existsSync(destination))) {
28 result.ok = false;
29 result.msg = l10n('CMD_CFG_VALIDATORS_DESTINATION_ERR');
30 }
31 return result;
32 },
33};
34
35/**
36 * @class Config
37 * @member {string} configpath
38 * @member {Object} parameters
39 */
40class Config {
41/**
42 * Creates an instance of Config.
43 * @param {any} options [{ configpath = defaultConfigPath }={}]
44 * @memberof Config
45 */
46 constructor({ configpath = defaultConfigPath } = {}) {
47 this.configpath = configpath;
48
49 if (!fs.existsSync(this.configpath)) {
50 fs.ensureFileSync(this.configpath);
51 this.reset();
52 this.save();
53 }
54
55 this.parameters = Object.assign({}, DEFAULTS, JSON.parse(fs.readFileSync(this.configpath, 'utf-8')));
56 }
57
58 /**
59 * @memberof Config
60 */
61 save() {
62 fs.writeFileSync(this.configpath, JSON.stringify(this.parameters));
63 }
64
65 /**
66 * @param {string} key
67 * @return {{key:string, value:string}} key-value
68 * @memberof Config
69 */
70 get(key) {
71 if (Config.isValidKey(key)) {
72 return { key, value: this.parameters[key] };
73 }
74 return null;
75 }
76
77 /**
78 * Set a parameter and sync(save) immediately
79 * @param {string} key
80 * @param {string} value
81 * @return {{key:string, value:string}} key-value
82 * @memberof Config
83 */
84 set(key, value) {
85 if (Config.isValidKey(key)) {
86 this.parameters[key] = value;
87 this.save();
88 return { key, value };
89 }
90 return null;
91 }
92
93 /**
94 * Reset a key or all keys to DEFAULTS
95 *
96 * @param {?string} [key=null]
97 * @memberof Config
98 */
99 reset(key = null) {
100 if (Config.isValidKey(key)) {
101 this.parameters[key] = DEFAULTS[key];
102 } else {
103 this.parameters = Object.assign({}, DEFAULTS);
104 }
105 this.save();
106 }
107
108 /**
109 * @return {boolean} valid
110 * @memberof Config
111 */
112 isValid() {
113 return Object.keys(this.parameters).every((key) => {
114 if (VALIDATORS[key]) {
115 return VALIDATORS[key](key).ok;
116 }
117 return true;
118 });
119 }
120
121 /**
122 * @static
123 * @param {string} key
124 * @return {boolean} valid
125 * @memberof Config
126 */
127 static isValidKey(key) {
128 return key && Object.keys(DEFAULTS).includes(key);
129 }
130}
131
132Config.DEFAULTS = DEFAULTS;
133Config.VALIDATORS = VALIDATORS;
134
135exports.Config = Config;