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 86% rename from docs/search.js rename to scripts/generateSearch.js index 53b3cc3eef4..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('../'); @@ -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'; @@ -29,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) { @@ -40,11 +40,11 @@ for (const filename of files) { 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) { - console.log(content); + console.error(content); throw err; } contents.push(content); @@ -107,24 +107,37 @@ for (const filename of files) { body: html, url: `${filename.replace('.pug', '.html').replace(/^docs/, '')}#${el.prop('id')}` }); - + content.validateSync(); contents.push(content); }); } } -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() { + 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 + await Content.init(); + await Content.deleteMany({ version }); for (const content of contents) { 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 { @@ -142,4 +155,4 @@ async function run() { console.log(results.map(res => res.url)); process.exit(0); -} \ No newline at end of file +}