From 864df5401168e4a0512d290a7e433792b8d748e8 Mon Sep 17 00:00:00 2001 From: Sem Bauke Date: Thu, 23 Mar 2023 16:19:21 +0100 Subject: [PATCH] feat(tools): redirect generation script (#49798) * feat: redirect generation script * feat: add comment * Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams --------- Co-authored-by: Oliver Eyton-Williams --- tools/scripts/redirect-gen.ts | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tools/scripts/redirect-gen.ts diff --git a/tools/scripts/redirect-gen.ts b/tools/scripts/redirect-gen.ts new file mode 100644 index 00000000000..cb0d45684be --- /dev/null +++ b/tools/scripts/redirect-gen.ts @@ -0,0 +1,36 @@ +import fs from 'fs'; +import path from 'path'; + +/* This can be used to create NGINX maps for redirects. After running this + script with `npx ts-node redirect-gen`, the map should appear in the current + directory. +*/ + +function createRedirectMap(): void { + const basePath = '../../../curriculum/challenges/english/18-project-euler'; + const directories = fs.readdirSync(path.resolve(__dirname, basePath)); + + let mapObject = ''; + + for (let i = 0; i < directories.length; i++) { + const files = fs.readdirSync( + path.resolve(__dirname, `${basePath}/${directories[i]}`) + ); + + for (let j = 0; j < files.length; j++) { + const fileName = path.parse(files[j]).name; + mapObject += `~^/learn/coding-interview-prep/project-euler/${fileName}/?$ /learn/project-euler/${directories[i]}/${fileName}; \n`; + } + } + + fs.writeFile('redirectMap.map', mapObject, 'utf8', function (err) { + if (err) { + console.log('An error occurred while writing MAP redirect file', err); + return console.log(err); + } + + console.log('Map file has been saved.'); + }); +} + +createRedirectMap();