From 35b26c6a14a8f0dec88c96fbaad2100d847e3093 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Fri, 8 Dec 2023 10:47:24 -0500 Subject: [PATCH] Port test-moved-content.js to TypeScript (#47443) --- .github/workflows/move-content.yml | 1 + package.json | 2 +- ...moved-content.js => test-moved-content.ts} | 26 ++++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) rename src/content-render/scripts/{test-moved-content.js => test-moved-content.ts} (66%) diff --git a/.github/workflows/move-content.yml b/.github/workflows/move-content.yml index 12ddbaef4dfa..8ed914b0dd4c 100644 --- a/.github/workflows/move-content.yml +++ b/.github/workflows/move-content.yml @@ -8,6 +8,7 @@ on: pull_request: paths: - src/content-render/scripts/move-content.js + - src/content-render/scripts/test-move-content.ts - 'src/frame/lib/**/*.js' - .github/workflows/move-content.yml diff --git a/package.json b/package.json index 9bb8eecbc85d..0c8d82a4bfb9 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "sync-search-server": "cross-env NODE_ENV=production PORT=4002 MINIMAL_RENDER=true CHANGELOG_DISABLED=true node src/frame/server.js", "sync-webhooks": "src/rest/scripts/update-files.js -o webhooks", "test": "cross-env NODE_OPTIONS='--max_old_space_size=4096 --experimental-vm-modules' jest --logHeapUsage", - "test-moved-content": "node src/content-render/scripts/test-moved-content.js", + "test-moved-content": "tsx src/content-render/scripts/test-moved-content.ts", "test-watch": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --watch --notify --notifyMode=change --coverage", "toggle-ghae-feature-flags": "node src/versions/scripts/toggle-ghae-feature-flags.js", "tsc": "tsc --noEmit", diff --git a/src/content-render/scripts/test-moved-content.js b/src/content-render/scripts/test-moved-content.ts similarity index 66% rename from src/content-render/scripts/test-moved-content.js rename to src/content-render/scripts/test-moved-content.ts index de69712de16f..e823001439ca 100644 --- a/src/content-render/scripts/test-moved-content.js +++ b/src/content-render/scripts/test-moved-content.ts @@ -4,56 +4,58 @@ import path from 'path' import { program } from 'commander' -import readFrontmatter from '#src/frame/lib/read-frontmatter.js' +import readFrontmatter from 'src/frame/lib/read-frontmatter.js' const ROOT = process.env.ROOT || '.' const CONTENT_ROOT = path.resolve(path.join(ROOT, 'content')) program .description('Test that a file correctly got moved') - .arguments('old', 'old file or folder name') - .arguments('new', 'new file or folder name') + .arguments('old') + .arguments('new') .parse(process.argv) -main(program.args) +main([program.args[0], program.args[1]]) -async function main(nameTuple) { +async function main(nameTuple: [string, string]) { const [before, after] = nameTuple assert(!fs.existsSync(before), `File or folder ${before} exists`) assert(fs.existsSync(after), `File or folder ${after} exists`) if (after.endsWith('.md')) { const fileContent = fs.readFileSync(after, 'utf-8') - const { data } = readFrontmatter(fileContent) + const fm = readFrontmatter(fileContent) + const { data } = fm const oldHref = makeHref(CONTENT_ROOT, before) - assert(data.redirect_from.includes(oldHref), `Redirect from ${oldHref} not found`) + if (data) assert(data.redirect_from.includes(oldHref), `Redirect from ${oldHref} not found`) { const parentIndexMd = path.join(path.dirname(after), 'index.md') const fileContent = fs.readFileSync(parentIndexMd, 'utf-8') const { data } = readFrontmatter(fileContent) const afterShortname = '/' + after.split('/').slice(-1)[0].replace(/\.md$/, '') - assert(data.children.includes(afterShortname), `Child ${afterShortname} not found`) + if (data) assert(data.children.includes(afterShortname), `Child ${afterShortname} not found`) } } else { const fileContent = fs.readFileSync(path.join(after, 'index.md'), 'utf-8') const { data } = readFrontmatter(fileContent) const oldHref = makeHref(CONTENT_ROOT, before) - assert(data.redirect_from.includes(oldHref), `Redirect from ${oldHref} not found`) + if (data) assert(data.redirect_from.includes(oldHref), `Redirect from ${oldHref} not found`) { const parentIndexMd = path.join(path.dirname(after), 'index.md') const fileContent = fs.readFileSync(parentIndexMd, 'utf-8') const { data } = readFrontmatter(fileContent) const afterShortname = '/' + after.split('/').slice(-1) - assert(data.children.includes(afterShortname), `Child ${afterShortname} not found`) + if (data) assert(data.children.includes(afterShortname), `Child ${afterShortname} not found`) } } } -function makeHref(root, filePath) { +function makeHref(root: string, filePath: string) { const nameSplit = path.relative(root, filePath).split(path.sep) if (nameSplit.slice(-1)[0] === 'index.md') { nameSplit.pop() } else { - nameSplit.push(nameSplit.pop().replace(/\.md$/, '')) + const last = nameSplit.pop() + if (last) nameSplit.push(last.replace(/\.md$/, '')) } return '/' + nameSplit.join('/') }