Created
November 2, 2021 03:44
-
-
Save andycarrell/0f2595904ae4aa559673ba3e2b07ae7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { rename, mkdir, rmdir } = require("fs"); | |
const copyfiles = require("copyfiles"); | |
const { promisify } = require("util"); | |
const temporaryDirectory = "./nzpqofqtzj"; | |
const mkdirAsync = promisify(mkdir); | |
const rmdirAsync = promisify(rmdir); | |
const renameAsync = promisify(rename); | |
const copyfilesAsync = promisify(copyfiles); | |
const copyAndRename = async (previousFileName, newFileName, copyTo) => { | |
// Make a temporary directory | |
await mkdirAsync(temporaryDirectory); | |
// Copy the old file to the temp directory | |
await copyfilesAsync([previousFileName, temporaryDirectory], true); | |
// Renamed the copied file | |
await renameAsync( | |
`${temporaryDirectory}/${previousFileName}`, | |
`${temporaryDirectory}/${newFileName}`, | |
); | |
// Copy the renamed file back to the root directory | |
await copyfilesAsync([`${temporaryDirectory}/${newFileName}`, copyTo], true); | |
// Remove the temporary directory | |
await rmdirAsync(temporaryDirectory, { recursive: true }); | |
}; | |
const [, , oldName, newName, copyToArg = "./"] = process.argv; | |
copyAndRename(oldName, newName, copyToArg); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment