| 42 | main(program.opts()) |
| 43 | |
| 44 | async function main(opts) { |
| 45 | let errors = 0 |
| 46 | for (const file of MANDATORY_FILES) { |
| 47 | const source = fs.readFileSync(file, 'utf-8') |
| 48 | const destination = path.join(DESTINATION, file) |
| 49 | |
| 50 | if (opts.check) { |
| 51 | // The destination has to exist and be identical |
| 52 | try { |
| 53 | const copied = fs.readFileSync(destination, 'utf-8') |
| 54 | if (copied !== source) { |
| 55 | // console.warn(chalk.red(`The file ${destination} is different from ${file}`)) |
| 56 | console.warn(`The file ${chalk.red(destination)} is different from ${chalk.red(file)}`) |
| 57 | errors++ |
| 58 | } else if (opts.verbose) { |
| 59 | console.log(`The file ${chalk.green(destination)} is up-to-date 🥰`) |
| 60 | } |
| 61 | } catch (error) { |
| 62 | if (error.code === 'ENOENT') { |
| 63 | console.warn(`The file ${chalk.red(destination)} does not exist`) |
| 64 | errors++ |
| 65 | } else { |
| 66 | throw error |
| 67 | } |
| 68 | } |
| 69 | } else { |
| 70 | try { |
| 71 | const copied = fs.readFileSync(destination, 'utf-8') |
| 72 | if (copied === source) { |
| 73 | if (opts.verbose) { |
| 74 | console.log(`The file ${chalk.green(destination)} was perfect already 👌`) |
| 75 | } |
| 76 | continue |
| 77 | } |
| 78 | } catch (error) { |
| 79 | if (error.code !== 'ENOENT') throw error |
| 80 | } |
| 81 | if (!opts.dryRun) { |
| 82 | await mkdirp(path.dirname(destination)) |
| 83 | fs.writeFileSync(destination, source, 'utf-8') |
| 84 | if (opts.verbose) { |
| 85 | console.log(`Copied latest ${chalk.green(file)} to ${chalk.bold(destination)} 👍🏼`) |
| 86 | } |
| 87 | } else if (opts.verbose) { |
| 88 | console.log(`Would copy latest ${chalk.bold(file)} to ${chalk.bold(destination)}`) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (errors > 0) { |
| 94 | console.warn( |
| 95 | '\n', |
| 96 | chalk.yellow( |
| 97 | 'Run this script again without --check to make all fixture data files up-to-date. ' + |
| 98 | 'Then commit and check in.' |
| 99 | ) |
| 100 | ) |
| 101 | } |