Skip to content

Instantly share code, notes, and snippets.

@andycarrell
Created November 2, 2021 03:44
Show Gist options
  • Save andycarrell/0f2595904ae4aa559673ba3e2b07ae7e to your computer and use it in GitHub Desktop.
Save andycarrell/0f2595904ae4aa559673ba3e2b07ae7e to your computer and use it in GitHub Desktop.
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