(opts, nameTuple)
| 41 | main(program.opts(), program.args) |
| 42 | |
| 43 | async function main(opts, nameTuple) { |
| 44 | const { verbose, undo } = opts |
| 45 | if (nameTuple.length !== 2) { |
| 46 | console.error( |
| 47 | chalk.red(`Must be exactly 2 file paths as arguments. Not ${nameTuple.length} arguments.`) |
| 48 | ) |
| 49 | process.exit(1) |
| 50 | } |
| 51 | const [old, new_] = nameTuple |
| 52 | if (old === new_) { |
| 53 | throw new Error('old == new') |
| 54 | } |
| 55 | |
| 56 | const uppercases = new_.match(/[A-Z]+/g) || [] |
| 57 | if (uppercases.length > 0) { |
| 58 | throw new Error(`Uppercase in file name not allowed ('${uppercases}')`) |
| 59 | } |
| 60 | |
| 61 | let oldPath = old |
| 62 | let newPath = new_ |
| 63 | if (undo) { |
| 64 | oldPath = new_ |
| 65 | newPath = old |
| 66 | } else { |
| 67 | oldPath = old |
| 68 | newPath = new_ |
| 69 | } |
| 70 | |
| 71 | // The file you're about to move needs to exist |
| 72 | if (!fs.existsSync(oldPath)) { |
| 73 | console.error(chalk.red(`${oldPath} does not exist.`)) |
| 74 | process.exit(1) |
| 75 | } |
| 76 | |
| 77 | let isFolder = fs.lstatSync(oldPath).isDirectory() |
| 78 | |
| 79 | // Before validating, see if we need to fake that the newPath should be. |
| 80 | // This is to mimic how bash `mv` works where you can do: |
| 81 | // |
| 82 | // mv some/place/a/file.txt destin/ation/ |
| 83 | // |
| 84 | // which is implied to mean the same as; |
| 85 | // |
| 86 | // mv some/place/a/file.txt destin/ation/file.txt |
| 87 | // |
| 88 | if (undo) { |
| 89 | if (isFolder) { |
| 90 | const wouldBe = path.join(oldPath, path.basename(newPath)) |
| 91 | // We can't know if the `newPath` is a directory or file because |
| 92 | // whichever it is, it doesn't exist. |
| 93 | if (fs.existsSync(wouldBe) && !fs.lstatSync(wouldBe).isDirectory()) { |
| 94 | isFolder = false |
| 95 | oldPath = wouldBe |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | if (!isFolder) { |
| 100 | if (fs.existsSync(newPath) && fs.lstatSync(newPath).isDirectory()) { |
no test coverage detected