'use strict';
const pkg = require('../package.json');
const fs = require('fs/promises');
const glob = require('glob');
const path = require('path');
const scanStarts = [
'docs/**/*.md',
'docs/**/*.pug',
'lib/**/*.js',
'types/**/*.ts',
'README.md'
];
const startDir = path.resolve(__dirname, '..');
const mongodbRegex = /https:\/\/mongodb\.github\.io\/node-mongodb-native\/(\d+\.\d+\/)/g;
const mongodbDepRegex = /\d+\.\d+/;
* The mongodb version, as extracted via {@link mongodbDepRegex} from the package.json
* @type {string|undefined}
*/
let currentMongodbVersion = undefined;
* Map <https://mongodb.github.io/node-mongodb-native/MAJOR.MINOR/> urls to their current mongodb's version.
* @param {string} block The String block to look for urls
* @returns {string} The modified Block
*/
function mapURLsMongoDb(block) {
if (currentMongodbVersion === undefined) {
throw new Error('Expected `currentMongodbVersion` to be defined at this point');
}
let match;
let out = '';
let lastIndex = 0;
while ((match = mongodbRegex.exec(block)) !== null) {
const startIndex = match.index + match[0].length - match[1].length;
out += block.slice(lastIndex, startIndex);
lastIndex = startIndex + match[1].length;
out += currentMongodbVersion;
out += '/';
}
out += block.slice(lastIndex);
return out;
}
async function main() {
if (currentMongodbVersion === undefined) {
currentMongodbVersion = pkg?.dependencies?.['mongodb']?.match(mongodbDepRegex)?.[0];
if (currentMongodbVersion === undefined) {
throw new Error('Cannot replace mongodb version links due to not being able to extract version from dependencies!');
}
}
const promises = [];
for await (const dirent of glob.iterate(scanStarts, { withFileTypes: true, cwd: startDir, nodir: true })) {
if (!dirent.isFile()) {
continue;
}
const fullPath = dirent.fullpath();
const promise = fs.readFile(fullPath, { encoding: 'utf8' }).then(text => fs.writeFile(fullPath, mapURLsMongoDb(text)));
promises.push(promise);
}
await Promise.allSettled(promises);
}
main();