Skip to content

Commit 6d27965

Browse files
committed
chore(generateSearch): move code to be exported
To only run the function globally if the script is main. This also prevents other scripts from accidentally exiting early (config missing or search generation being complete earlier).
1 parent 31afe64 commit 6d27965

File tree

2 files changed

+46
-19
lines changed

2 files changed

+46
-19
lines changed

scripts/generateSearch.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
'use strict';
22

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+
125
const cheerio = require('cheerio');
136
const docsFilemap = require('../docs/source');
147
const fs = require('fs');
@@ -122,14 +115,7 @@ function generateContents() {
122115
return contents;
123116
}
124117

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) {
133119
await mongoose.connect(config.uri, { dbName: 'mongoose' });
134120

135121
// wait for the index to be created
@@ -181,5 +167,35 @@ async function run() {
181167

182168
console.log(`Added ${contents.length} Search Content`);
183169

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+
})();
185201
}

scripts/website.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,25 @@ if (isMain) {
606606
(async function main() {
607607
console.log(`Processing ~${files.length} files`);
608608

609-
require('./generateSearch');
609+
const generateSearch = require('./generateSearch');
610+
let generateSearchPromise;
611+
try {
612+
const config = generateSearch.getConfig();
613+
generateSearchPromise = generateSearch.generateSearch(config);
614+
} catch (err) {
615+
console.error("Generating Search failed:", err);
616+
}
610617
await deleteAllHtmlFiles();
611618
await pugifyAllFiles();
612619
await copyAllRequiredFiles();
613620
if (!!process.env.DOCS_DEPLOY && !!versionObj.versionedPath) {
614621
await moveDocsToTemp();
615622
}
616623

624+
if (!!generateSearchPromise) {
625+
await generateSearchPromise;
626+
}
627+
617628
console.log('Done Processing');
618629
})();
619630
}

0 commit comments

Comments
 (0)