From 411da0d982871e925d742b9422d11257f74385b8 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 17 Feb 2023 15:23:18 +0100 Subject: [PATCH 1/7] chore(docs/search): disconnect on error --- docs/search.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/docs/search.js b/docs/search.js index 53b3cc3eef4..25a5158da39 100644 --- a/docs/search.js +++ b/docs/search.js @@ -114,7 +114,12 @@ for (const filename of files) { } } -run().catch(error => console.error(error.stack)); +run().catch(async error => { + console.error(error.stack); + + // ensure the script exists in case of error + await mongoose.disconnect(); +}); async function run() { await mongoose.connect(config.uri, { dbName: 'mongoose' }); @@ -142,4 +147,4 @@ async function run() { console.log(results.map(res => res.url)); process.exit(0); -} \ No newline at end of file +} From 0f05198c030df5b761bf99b76ae8483eb07cb2c7 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 17 Feb 2023 15:23:56 +0100 Subject: [PATCH 2/7] chore(docs/search): wait for indexes to be created --- docs/search.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/search.js b/docs/search.js index 25a5158da39..608a9cd0d43 100644 --- a/docs/search.js +++ b/docs/search.js @@ -124,6 +124,9 @@ run().catch(async error => { async function run() { await mongoose.connect(config.uri, { dbName: 'mongoose' }); + // wait for the index to be created + await Content.init(); + await Content.deleteMany({ version }); for (const content of contents) { if (version === '6.x') { From 299d74e7605352a6c94e41799a98e1a99780bfa8 Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 17 Feb 2023 15:27:37 +0100 Subject: [PATCH 3/7] chore(docs/search): ignore strictQuery revert warning --- docs/search.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/search.js b/docs/search.js index 608a9cd0d43..854ca5a36c4 100644 --- a/docs/search.js +++ b/docs/search.js @@ -16,6 +16,8 @@ markdown.setOptions({ } }); +mongoose.set('strictQuery', false); + // 5.13.5 -> 5.x, 6.8.2 -> 6.x, etc. version = version.slice(0, version.indexOf('.')) + '.x'; From 2daeddec9c1a1899cdec772c38c129d3ac80cf2f Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 17 Feb 2023 15:32:00 +0100 Subject: [PATCH 4/7] chore(docs/search): update loop to use "entries" --- docs/search.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/search.js b/docs/search.js index 854ca5a36c4..5affb7128aa 100644 --- a/docs/search.js +++ b/docs/search.js @@ -31,10 +31,8 @@ contentSchema.index({ title: 'text', body: 'text' }); const Content = mongoose.model('Content', contentSchema, 'Content'); const contents = []; -const files = Object.keys(filemap); -for (const filename of files) { - const file = filemap[filename]; +for (const [filename, file] of Object.entries(filemap)) { if (file.api) { // API docs are special, raw content is in the `docs` property for (const _class of file.docs) { From 14973b08b58af1a333fc8978aaa0bf06e5a629fa Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 17 Feb 2023 15:39:40 +0100 Subject: [PATCH 5/7] chore(docs/search): use "api/file.html" over "api.html" --- docs/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/search.js b/docs/search.js index 5affb7128aa..883623b562d 100644 --- a/docs/search.js +++ b/docs/search.js @@ -40,7 +40,7 @@ for (const [filename, file] of Object.entries(filemap)) { const content = new Content({ title: `API: ${prop.string}`, body: prop.description, - url: `api.html#${prop.anchorId}` + url: `api/${_class.fileName}.html#${prop.anchorId}` }); const err = content.validateSync(); if (err != null) { From 0b7bbbbc23a2b79fb05defaef27f91efc54398bb Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 17 Feb 2023 15:41:11 +0100 Subject: [PATCH 6/7] chore(docs/search): add error when config is missing --- docs/search.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/search.js b/docs/search.js index 883623b562d..cc8bea24e3c 100644 --- a/docs/search.js +++ b/docs/search.js @@ -44,7 +44,7 @@ for (const [filename, file] of Object.entries(filemap)) { }); const err = content.validateSync(); if (err != null) { - console.log(content); + console.error(content); throw err; } contents.push(content); @@ -122,6 +122,11 @@ run().catch(async error => { }); async function run() { + if (!config || !config.uri) { + console.error('No Config or config.URI given, please create a .config.js file with those values'); + process.exit(-1); + } + await mongoose.connect(config.uri, { dbName: 'mongoose' }); // wait for the index to be created From de316d0a1e64eb41ccc00a74c2aed871f990674e Mon Sep 17 00:00:00 2001 From: hasezoey Date: Fri, 17 Feb 2023 15:42:52 +0100 Subject: [PATCH 7/7] chore: move search generation script to "scripts" --- package.json | 2 +- docs/search.js => scripts/generateSearch.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename docs/search.js => scripts/generateSearch.js (98%) diff --git a/package.json b/package.json index e4f6d1b03a1..47930c2e8dc 100644 --- a/package.json +++ b/package.json @@ -79,7 +79,7 @@ "docs:checkout:gh-pages": "git checkout gh-pages", "docs:checkout:legacy": "git checkout 5.x", "docs:generate": "node ./scripts/website.js", - "docs:generate:search": "node docs/search.js", + "docs:generate:search": "node ./scripts/generateSearch.js", "docs:merge:stable": "git merge master", "docs:merge:legacy": "git merge 5.x", "docs:test": "npm run docs:generate && npm run docs:generate:search", diff --git a/docs/search.js b/scripts/generateSearch.js similarity index 98% rename from docs/search.js rename to scripts/generateSearch.js index cc8bea24e3c..16aee3387bd 100644 --- a/docs/search.js +++ b/scripts/generateSearch.js @@ -2,7 +2,7 @@ const config = require('../.config'); const cheerio = require('cheerio'); -const filemap = require('./source'); +const filemap = require('../docs/source'); const fs = require('fs'); const pug = require('pug'); const mongoose = require('../'); @@ -107,7 +107,7 @@ for (const [filename, file] of Object.entries(filemap)) { body: html, url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}` }); - + content.validateSync(); contents.push(content); }); @@ -137,7 +137,7 @@ async function run() { if (version === '6.x') { let url = content.url.startsWith('/') ? content.url : `/${content.url}`; if (!url.startsWith('/docs')) { - url = '/docs' + url; + url = '/docs' + url; } content.url = url; } else {