(files, updateParent, opts)
| 486 | } |
| 487 | |
| 488 | function undoFiles(files, updateParent, opts) { |
| 489 | const { verbose, git: useGit } = opts |
| 490 | |
| 491 | // First undo any edits to the file |
| 492 | for (const [oldPath, newPath, oldHref, newHref] of files) { |
| 493 | const fileContent = fs.readFileSync(newPath, 'utf-8') |
| 494 | const { content, data } = readFrontmatter(fileContent) |
| 495 | |
| 496 | data[REDIRECT_FROM_KEY] = (data[REDIRECT_FROM_KEY] || []).filter((entry) => entry !== oldHref) |
| 497 | if (data[REDIRECT_FROM_KEY].length === 0) { |
| 498 | delete data[REDIRECT_FROM_KEY] |
| 499 | } |
| 500 | |
| 501 | fs.writeFileSync( |
| 502 | newPath, |
| 503 | readFrontmatter.stringify(content, data, { lineWidth: 10000 }), |
| 504 | 'utf-8' |
| 505 | ) |
| 506 | if (updateParent) { |
| 507 | addToChildren(newPath, removeFromChildren(oldPath, opts), opts) |
| 508 | } |
| 509 | |
| 510 | // Perhaps this was mentioned in a 'guide' in a learning track |
| 511 | for (const filePath of findInLearningTracks(newHref)) { |
| 512 | changeLearningTracks(filePath, newHref, oldHref) |
| 513 | if (verbose) { |
| 514 | console.log(`Updated learning tracks in ${filePath}`) |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | if (useGit) { |
| 519 | const cmd = `git commit -a -m "unset ${REDIRECT_FROM_KEY} on ${files.length} files"` |
| 520 | execSync(cmd) |
| 521 | if (verbose) { |
| 522 | console.log(`git commit command: ${chalk.grey(cmd)}`) |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | function findInLearningTracks(href) { |
| 528 | const allFiles = walk(path.join(DATA_ROOT, 'learning-tracks'), { |
no test coverage detected