-
Notifications
You must be signed in to change notification settings - Fork 2
/
startupHelpers.js
171 lines (149 loc) · 5.01 KB
/
startupHelpers.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
const Promise = require('bluebird');
const fs = Promise.promisifyAll(require('fs-extra'));
const log4js = require('log4js');
const logger = log4js.getLogger('startupHelpers.js');
const path = require('path');
const sway = require('sway');
const yaml = require('js-yaml');
const _ = require('lodash');
const nconf = require('nconf');
const Datastore = require('nedb');
const SonosDiscovery = require('sonos-discovery');
const discovery = new SonosDiscovery();
function filePathExists(filePath) {
return new Promise((resolve, reject) => {
fs.stat(filePath, (err, stats) => {
if (err && err.code === 'ENOENT') {
return resolve(false);
} else if (err) {
return reject(err);
}
if (stats.isFile() || stats.isDirectory()) {
return resolve(true);
}
return resolve(true);
});
});
}
function createTtsDirectory(ttsDir) {
return Promise.resolve()
.then(() => {
return filePathExists(ttsDir);
})
.then((ttsExists) => {
if (!ttsExists) {
return fs.mkdirAsync(ttsDir);
}
return null;
})
.catch((err) => {
logger.warn(`Could not create tts directory ${ttsDir}, please create it manually for all features to work.`);
logger.warn(`Error : ${err}`);
return null;
});
}
function createDatabaseDirectory(databaseDir) {
return Promise.resolve()
.then(() => {
return filePathExists(databaseDir);
})
.then((ttsExists) => {
if (!ttsExists) {
return fs.mkdirAsync(databaseDir);
}
return null;
})
.catch((err) => {
logger.warn(`Could not create database directory ${databaseDir}, please create it manually for all features to work.`);
logger.warn(`Error : ${err}`);
return null;
});
}
function createRunningSwaggerFile(webRoot) {
// This copies swagger.yaml to production.swagger.yaml and replaces host and schemes if necessary
// It also validates the new swagger file to make sure it is all ok
const orignalSwaggerFile = path.resolve(__dirname, './api/swagger/swagger.yaml');
const runtimeSwaggerFile = path.resolve(__dirname, './api/swagger/production.swagger.yaml');
let swaggerJsonObject;
return Promise.resolve()
.then(() => {
return fs.readFileAsync(orignalSwaggerFile, 'utf8');
})
.then((fileContents) => {
return yaml.safeLoad(fileContents);
})
.then((swaggerObject) => {
swaggerJsonObject = swaggerObject;
swaggerJsonObject.host = webRoot.replace(/http(s)*:\/\//, '');
if (webRoot.startsWith('https')) {
swaggerJsonObject.schemes = ['https'];
}
return sway.create({
definition: swaggerJsonObject
});
})
.then((swaggerObj) => {
const result = swaggerObj.validate();
let errors = result.errors;
let warnings = result.warnings;
if (_.isEmpty(errors)) {
errors = null;
}
if (_.isEmpty(warnings)) {
warnings = null;
}
logger.warn(errors, warnings);
if (errors) {
throw errors;
}
return fs.writeFileAsync(runtimeSwaggerFile, yaml.safeDump(swaggerJsonObject));
})
.catch((error) => {
logger.error('Error occurred modifying swagger file:', error);
throw error;
});
}
function loadConfig() {
// Load settings and set defaults
nconf.file(path.resolve(__dirname, './settings.json'));
nconf.defaults({
settings: {
port: 10010,
staticWebRootPath: path.resolve(__dirname, 'static'),
ttsProvider: 'google',
webRoot: 'http://localhost:10010',
databasePath: path.resolve(__dirname, 'localDatabase')
}
});
// Store settings so they can be easily accessed
return nconf.get('settings');
}
function loadDatabases(settings) {
const iplayerPodcastDB = Promise.promisifyAll(new Datastore({
filename: path.resolve(settings.databasePath, 'iplayerPodcastDB.json'),
autoload: true
}));
const refreshSettingsDB = Promise.promisifyAll(new Datastore({
filename: path.resolve(settings.databasePath, 'refreshSettingsDB.json'),
autoload: true
}));
const iplayerProgramDB = Promise.promisifyAll(new Datastore({
filename: path.resolve(settings.databasePath, 'iplayerProgramDB.json'),
autoload: true
}));
const dbSettings = {
iplayerPodcastDB,
refreshSettingsDB,
iplayerProgramDB
};
return dbSettings;
}
module.exports = {
createRunningSwaggerFile,
createTtsDirectory,
createDatabaseDirectory,
loadConfig,
loadDatabases,
discovery
};