Skip to content

Commit

Permalink
Merge pull request github#30437 from github/repo-sync
Browse files Browse the repository at this point in the history
Repo sync
  • Loading branch information
docs-bot authored Dec 8, 2023
2 parents 9403f50 + 429b885 commit a970d35
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/move-content.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
}

0 comments on commit a970d35

Please sign in to comment.