| 393 | } |
| 394 | |
| 395 | function moveFiles(files, opts) { |
| 396 | const { verbose, git: useGit } = opts |
| 397 | // Before we do anything, assert that the files are valid |
| 398 | for (const [oldPath] of files) { |
| 399 | const fileContent = fs.readFileSync(oldPath, 'utf-8') |
| 400 | const { errors } = fm(fileContent, { filepath: oldPath }) |
| 401 | errors.forEach((error, i) => { |
| 402 | if (!i) console.warn(chalk.yellow(`Error parsing file (${oldPath}) frontmatter:`)) |
| 403 | console.error(`${chalk.red(error.message)}: ${chalk.yellow(error.reason)}`) |
| 404 | }) |
| 405 | if (errors.length > 0) throw new Error('There were more than 0 parse errors') |
| 406 | } |
| 407 | |
| 408 | // In the first loop, we exclusively perform the rename. No file edits! |
| 409 | // The reason is that we don't want lump renaming and edits in the same |
| 410 | // git commit. |
| 411 | // By having a dedicated git commit that purely renames (without changing |
| 412 | // any content) is best practice to avoid complex 3-way diffs that |
| 413 | // `git merge` does when you later have to merge in the latest `main` |
| 414 | // into your ongoing renaming branch. |
| 415 | for (const [oldPath, newPath] of files) { |
| 416 | if (verbose) { |
| 417 | console.log(`Moving ${chalk.bold(oldPath)} to ${chalk.bold(newPath)}`) |
| 418 | } |
| 419 | |
| 420 | if (useGit) { |
| 421 | const cmd = `git mv ${oldPath} ${newPath}` |
| 422 | execSync(cmd) |
| 423 | if (verbose) { |
| 424 | console.log(`git mv command: ${chalk.grey(cmd)}`) |
| 425 | } |
| 426 | } else { |
| 427 | fs.renameSync(oldPath, newPath) |
| 428 | if (verbose) { |
| 429 | console.log(`Renamed ${chalk.bold(oldPath)} to ${chalk.bold(newPath)}`) |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | if (useGit) { |
| 435 | const cmd = `git commit -a -m "renamed ${files.length} files"` |
| 436 | execSync(cmd) |
| 437 | if (verbose) { |
| 438 | console.log(`git commit command: ${chalk.grey(cmd)}`) |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | function editFiles(files, updateParent, opts) { |
| 444 | const { verbose, git: useGit } = opts |