(files, updateParent, opts)
| 441 | } |
| 442 | |
| 443 | function editFiles(files, updateParent, opts) { |
| 444 | const { verbose, git: useGit } = opts |
| 445 | |
| 446 | // Second loop. This time our only job is to edit the `redirects_from` |
| 447 | // frontmatter key. |
| 448 | // See comment in the first loop above for why we're looping over the files |
| 449 | // two times. |
| 450 | for (const [oldPath, newPath, oldHref, newHref] of files) { |
| 451 | const fileContent = fs.readFileSync(newPath, 'utf-8') |
| 452 | const { content, data } = readFrontmatter(fileContent) |
| 453 | if (!(REDIRECT_FROM_KEY in data)) { |
| 454 | data[REDIRECT_FROM_KEY] = [] |
| 455 | } |
| 456 | data[REDIRECT_FROM_KEY].push(oldHref) |
| 457 | fs.writeFileSync( |
| 458 | newPath, |
| 459 | readFrontmatter.stringify(content, data, { lineWidth: 10000 }), |
| 460 | 'utf-8' |
| 461 | ) |
| 462 | if (verbose) { |
| 463 | console.log(`Added ${oldHref} to 'redirects_from' in ${newPath}`) |
| 464 | } |
| 465 | |
| 466 | if (updateParent) { |
| 467 | addToChildren(newPath, removeFromChildren(oldPath, opts), opts) |
| 468 | } |
| 469 | |
| 470 | // Perhaps this was mentioned in a 'guide' in a learning track |
| 471 | for (const filePath of findInLearningTracks(oldHref)) { |
| 472 | changeLearningTracks(filePath, oldHref, newHref) |
| 473 | if (verbose) { |
| 474 | console.log(`Updated learning tracks in ${filePath}`) |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | if (useGit) { |
| 480 | const cmd = `git commit -a -m "set ${REDIRECT_FROM_KEY} on ${files.length} files"` |
| 481 | execSync(cmd) |
| 482 | if (verbose) { |
| 483 | console.log(`git commit command: ${chalk.grey(cmd)}`) |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | function undoFiles(files, updateParent, opts) { |
| 489 | const { verbose, git: useGit } = opts |
no test coverage detected