|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -let config; |
4 |
| -try { |
5 |
| - config = require('../.config.js'); |
6 |
| -} finally { |
7 |
| - if (!config || !config.uri) { |
8 |
| - console.error('No Config or config.URI given, please create a .config.js file with those values in the root of the repository'); |
9 |
| - process.exit(-1); |
10 |
| - } |
11 |
| -} |
| 3 | +const isMain = require.main === module; |
| 4 | + |
12 | 5 | const cheerio = require('cheerio');
|
13 | 6 | const docsFilemap = require('../docs/source');
|
14 | 7 | const fs = require('fs');
|
@@ -122,14 +115,7 @@ function generateContents() {
|
122 | 115 | return contents;
|
123 | 116 | }
|
124 | 117 |
|
125 |
| -run().catch(async error => { |
126 |
| - console.error(error.stack); |
127 |
| - |
128 |
| - // ensure the script exists in case of error |
129 |
| - await mongoose.disconnect(); |
130 |
| -}); |
131 |
| - |
132 |
| -async function run() { |
| 118 | +async function generateSearch(config) { |
133 | 119 | await mongoose.connect(config.uri, { dbName: 'mongoose' });
|
134 | 120 |
|
135 | 121 | // wait for the index to be created
|
@@ -181,5 +167,35 @@ async function run() {
|
181 | 167 |
|
182 | 168 | console.log(`Added ${contents.length} Search Content`);
|
183 | 169 |
|
184 |
| - process.exit(0); |
| 170 | + // this likely should not be done as part of this script, but by the caller, |
| 171 | + // but this script is currently the only one that connects in the website generation. |
| 172 | + await mongoose.disconnect(); |
| 173 | +} |
| 174 | + |
| 175 | +function getConfig() { |
| 176 | + const config = require('../.config.js'); |
| 177 | + |
| 178 | + if (!config || !config.uri) { |
| 179 | + throw new Error('No Config or config.URI given, please create a .config.js file with those values in the root of the repository'); |
| 180 | + } |
| 181 | + |
| 182 | + return config; |
| 183 | +} |
| 184 | + |
| 185 | +module.exports.generateSearch = generateSearch; |
| 186 | +module.exports.getConfig = getConfig; |
| 187 | + |
| 188 | +// only run the following code if this file is the main module / entry file |
| 189 | +if (isMain) { |
| 190 | + (async function main() { |
| 191 | + const config = getConfig(); |
| 192 | + try { |
| 193 | + await generateSearch(config); |
| 194 | + } catch (error) { |
| 195 | + console.error(error); |
| 196 | + process.exit(-1); |
| 197 | + } finally { |
| 198 | + await mongoose.disconnect(); |
| 199 | + } |
| 200 | + })(); |
185 | 201 | }
|
0 commit comments