|
| 1 | +const fs = require(`fs-extra`) |
| 2 | +const path = require(`path`) |
| 3 | +const yargs = require(`yargs`) |
| 4 | + |
| 5 | +const { getHistory, writeHistory } = require(`./history`) |
| 6 | + |
| 7 | +const args = yargs |
| 8 | + .option(`file`, { |
| 9 | + demand: true, |
| 10 | + type: `string`, |
| 11 | + }) |
| 12 | + .option(`replacements`, { |
| 13 | + default: [], |
| 14 | + type: `array`, |
| 15 | + }) |
| 16 | + .option(`exact`, { |
| 17 | + default: false, |
| 18 | + type: `boolean`, |
| 19 | + }) |
| 20 | + .option(`delete`, { |
| 21 | + default: false, |
| 22 | + type: `boolean`, |
| 23 | + }) |
| 24 | + .option(`fileContent`, { |
| 25 | + default: JSON.stringify( |
| 26 | + ` |
| 27 | + import * as React from 'react'; |
| 28 | + |
| 29 | + import Layout from '../components/layout'; |
| 30 | + |
| 31 | + export default function SomeComponent() { |
| 32 | + return ( |
| 33 | + <Layout> |
| 34 | + <h1 data-testid="message">Hello %REPLACEMENT%</h1> |
| 35 | + </Layout> |
| 36 | + ) |
| 37 | + } |
| 38 | + ` |
| 39 | + ).trim(), |
| 40 | + type: `string`, |
| 41 | + }) |
| 42 | + .option(`fileSource`, { |
| 43 | + type: `string`, |
| 44 | + }) |
| 45 | + .option(`restore`, { |
| 46 | + default: false, |
| 47 | + type: `boolean`, |
| 48 | + }).argv |
| 49 | + |
| 50 | +async function update() { |
| 51 | + const history = await getHistory() |
| 52 | + |
| 53 | + const { file: fileArg, replacements, restore } = args |
| 54 | + const filePath = path.resolve(fileArg) |
| 55 | + if (restore) { |
| 56 | + const original = history.get(filePath) |
| 57 | + if (original) { |
| 58 | + await fs.writeFile(filePath, original, `utf-8`) |
| 59 | + } else if (original === false) { |
| 60 | + await fs.remove(filePath) |
| 61 | + } else { |
| 62 | + console.log(`Didn't make changes to "${fileArg}". Nothing to restore.`) |
| 63 | + } |
| 64 | + history.delete(filePath) |
| 65 | + return |
| 66 | + } |
| 67 | + let exists = true |
| 68 | + if (!fs.existsSync(filePath)) { |
| 69 | + exists = false |
| 70 | + let fileContent |
| 71 | + if (args.fileSource) { |
| 72 | + fileContent = await fs.readFile(args.fileSource, `utf8`) |
| 73 | + } else if (args.fileContent) { |
| 74 | + fileContent = JSON.parse(args.fileContent).replace(/\+n/g, `\n`) |
| 75 | + } |
| 76 | + await fs.writeFile(filePath, fileContent, `utf8`) |
| 77 | + } |
| 78 | + const file = await fs.readFile(filePath, `utf8`) |
| 79 | + |
| 80 | + if (!history.has(filePath)) { |
| 81 | + history.set(filePath, exists ? file : false) |
| 82 | + } |
| 83 | + |
| 84 | + if (args.delete) { |
| 85 | + if (exists) { |
| 86 | + await fs.remove(filePath) |
| 87 | + } |
| 88 | + } else { |
| 89 | + const contents = replacements.reduce((replaced, pair) => { |
| 90 | + const [key, value] = pair.split(`:`) |
| 91 | + return replaced.replace( |
| 92 | + args.exact ? key : new RegExp(`%${key}%`, `g`), |
| 93 | + value |
| 94 | + ) |
| 95 | + }, file) |
| 96 | + |
| 97 | + await fs.writeFile(filePath, contents, `utf8`) |
| 98 | + } |
| 99 | + |
| 100 | + await writeHistory(history) |
| 101 | +} |
| 102 | + |
| 103 | +update() |
0 commit comments