From 80033f44ae8126de3919375763c216bd80ff4ce9 Mon Sep 17 00:00:00 2001 From: Naomi Carrigan Date: Tue, 19 Dec 2023 22:36:32 -0800 Subject: [PATCH] feat: convert rename tool into proper utility (#52626) --- package.json | 1 + .../rename-challenge-files.js | 39 --------- .../rename-challenge-files.ts | 81 +++++++++++++++++++ 3 files changed, 82 insertions(+), 39 deletions(-) delete mode 100644 tools/challenge-helper-scripts/rename-challenge-files.js create mode 100644 tools/challenge-helper-scripts/rename-challenge-files.ts diff --git a/package.json b/package.json index ec2c7e1300a..115b8f6383a 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "preseed": "npm-run-all create:shared", "playwright:install-build-tools": "cd ./e2e && npm i && npx playwright install && npx playwright install-deps", "playwright:install-build-tools-linux": "sh ./playwright-install.sh", + "rename-challenges": "ts-node tools/challenge-helper-scripts/rename-challenge-files.ts", "seed": "pnpm seed:surveys && pnpm seed:exams && cross-env DEBUG=fcc:* node ./tools/scripts/seed/seed-demo-user", "seed:certified-user": "pnpm seed:surveys && pnpm seed:exams && cross-env DEBUG=fcc:* node ./tools/scripts/seed/seed-demo-user certified-user", "seed:exams": "cross-env DEBUG=fcc:* node tools/scripts/seed-exams/create-exams", diff --git a/tools/challenge-helper-scripts/rename-challenge-files.js b/tools/challenge-helper-scripts/rename-challenge-files.js deleted file mode 100644 index 38d9e64e212..00000000000 --- a/tools/challenge-helper-scripts/rename-challenge-files.js +++ /dev/null @@ -1,39 +0,0 @@ -const { readdir, readFile } = require('fs').promises; -const { join } = require('path'); -const { exec } = require('child_process'); -const { promisify } = require('util'); -const gray = require('gray-matter'); - -/** - * From the root directory, run the following CLI command to rename all RWD-22 challenge files: - * node tools/challenge-helper-scripts/rename-challenge-files.js - */ -(async () => { - const asyncExec = promisify(exec); - const blocks = await readdir( - join( - process.cwd(), - 'curriculum/challenges/english/14-responsive-web-design-22' - ) - ); - for (const block of blocks) { - const files = await readdir( - join( - process.cwd(), - `curriculum/challenges/english/14-responsive-web-design-22/${block}` - ) - ); - for (const file of files) { - const fileData = await readFile( - join( - process.cwd(), - `curriculum/challenges/english/14-responsive-web-design-22/${block}/${file}` - ) - ); - const challengeId = await gray(fileData).data.id; - await asyncExec( - `git mv curriculum/challenges/english/14-responsive-web-design-22/${block}/${file} curriculum/challenges/english/14-responsive-web-design-22/${block}/${challengeId}.md` - ); - } - } -})(); diff --git a/tools/challenge-helper-scripts/rename-challenge-files.ts b/tools/challenge-helper-scripts/rename-challenge-files.ts new file mode 100644 index 00000000000..8c8c30083be --- /dev/null +++ b/tools/challenge-helper-scripts/rename-challenge-files.ts @@ -0,0 +1,81 @@ +import { exec } from 'child_process'; +import { readFile, readdir } from 'fs/promises'; +import { join } from 'path'; +import { promisify } from 'util'; + +import gray from 'gray-matter'; +import { prompt } from 'inquirer'; + +const asyncExec = promisify(exec); + +void (async () => { + const superblocks = await readdir( + join(process.cwd(), 'curriculum', 'challenges', 'english') + ); + + const { superblock } = (await prompt({ + name: 'superblock', + message: 'Select target superblock:', + type: 'list', + choices: superblocks.map(e => ({ name: e, value: e })) + })) as { superblock: string }; + + const blocks = await readdir( + join(process.cwd(), 'curriculum', 'challenges', 'english', superblock) + ); + + const { block } = (await prompt({ + name: 'block', + message: 'Select target block:', + type: 'list', + choices: blocks.map(e => ({ name: e, value: e })) + })) as { block: string }; + + const files = await readdir( + join( + process.cwd(), + 'curriculum', + 'challenges', + 'english', + superblock, + block + ) + ); + console.log(`Processing ${files.length} files.`); + for (const file of files) { + const fileData = await readFile( + join( + process.cwd(), + 'curriculum', + 'challenges', + 'english', + superblock, + block, + file + ) + ); + const challengeId = (await gray(fileData).data.id) as string; + if (`${challengeId}.md` === file) { + console.warn(`${file} already has the correct name. Skipping.`); + continue; + } + + await asyncExec( + `git mv ${join( + 'curriculum', + 'challenges', + 'english', + superblock, + block, + file + )} ${join( + 'curriculum', + 'challenges', + 'english', + superblock, + block, + `${challengeId}.md` + )}` + ); + } +})();