闲来无事,本着学习nodejs的心态写点小东西,应用nodejs的api实现rm、cp和mv简略的删除/复制/挪动文件/文件夹。

rm删除文件

其实也很简略,如果是文件就间接用fs.unlinkSync删除,如果是文件夹就递归一个个删除。

const fs = require("fs");const { join } = require("path");module.exports = async function deleteFiles(path) {  // 判断一下门路是否实在存在  if (!fs.existsSync(path)) {    console.warn(new Error("门路不存在。"));    return;  }  const file = fs.lstatSync(path);  // 是文件,间接删除  if (file.isFile()) {    fs.unlinkSync(path);    return;  }  // 是文件夹,遍历上面的所有文件  if (file.isDirectory()) {    const files = await fs.readdirSync(path);    if (files && files.length) {      for (const fileName of files) {        // 因为我之前我的项目应用的时候不想删除暗藏文件,所以在此过滤了.结尾的文件        if (fileName.startsWith(".")) {          continue;        }        const p = join(path, fileName);        const f = fs.lstatSync(p);        // 是文件,间接删除        if (f.isFile()) {          fs.unlinkSync(p);        }        // 是文件夹,递归调用 deleteFiles        if (f.isDirectory()) {          await deleteFiles(p);          // 文件夹外部文件删除实现之后,删除文件夹          fs.rmdirSync(p);        }      }    }    return;  }};Ï

cp复制文件

复制文件略微比删除复制一点,须要判断oldPath是文件还是文件夹,newPath是文件还是文件夹,再对不同的状况来生成可用的门路。

const fs = require("fs");const { join, dirname, basename } = require("path");module.exports = async function copyFiles(oldPath, newPath) {  // 判断门路是否存在,有一个不存在则抛出谬误  if (!fs.existsSync(oldPath) || !fs.existsSync(newPath)) {    console.warn(new Error("门路不存在。"));    return;  }  const oldFile = fs.lstatSync(oldPath);  const newFile = fs.lstatSync(newPath);  // 如果 oldPath 是文件,则间接复制 oldPath  if (oldFile.isFile()) {    // 须要思考 newPath 是文件还是目录    // 如果是文件门路,则能够间接应用进行复制    // 如果是目录门路,则须要拼接上 oldPath 的文件名    if (newFile.isDirectory()) {      newPath = join(newPath, basename(oldPath));    }    fs.copyFileSync(oldPath, newPath);    return;  }  // 如果 oldPath 是目录,则 newPath 应该也使目录  // 若 newPath 指标门路是文件,则默认复制到文件的目录下  if (newFile.isFile()) {    console.warn(new Error("参数2应为门路。"));    newPath = dirname(newPath);  }  if (oldFile.isDirectory()) {    const files = await fs.readdirSync(oldPath);    if (files && files.length) {      // 遍历目录下的所有文件,并将 fileName 拼接上目录门路      files.forEach(async (fileName) => {        const oPath = join(oldPath, fileName);        const oFile = fs.lstatSync(oPath);        // 如果拼接后的门路为文件,则间接复制        if (oFile.isFile()) {          // 当然,新文件也须要拼接上 fileName          const newFile = join(newPath, fileName);          fs.copyFileSync(oPath, newFile);        }        // 如果是目录,则递归调用 moveFiles        if (oFile.isDirectory()) {          const oldDir = join(oldPath, fileName);          const newDir = join(newPath, fileName);          // 须要判断拼接后的 newDir 是否存在此目录,如果不存在则创立          if (!fs.existsSync(newDir)) {            await fs.mkdirSync(newDir);          }          moveFiles(oldDir, newDir);        }      });    }    return;  }};

mv挪动文件

挪动文件能够偷个懒,先调用 copyFiles 函数复制文件,再调用 deleteFiles 删除文件就是挪动了哈哈哈哈。

const copyFiles = require("./copy");const deleteFiles  = require("./delete");module.exports = async function moveFiles(oldPath, newPath) {  copyFiles(oldPath, newPath).then((res) => {    deleteFiles(oldPath);  });};

应用yarn命令调用

当然,为了更真切一些,能够再package.json外面配置一下rm/mv/cp,这样就更像了。

"scripts": {  "rm": "node ./rm.js",  "mv": "node ./mv.js",  "cp": "node ./cp.js"}

间接这样配置必定是不行的,咱们还须要读取一下命令时的输出,应用node自带的process读取命令传的参数

// cp.jsconst copyFiles = require("./copy");copyFiles(process.argv[2], process.argv[3]);// mv.jsconst moveFiles = require("./move");moveFiles(process.argv[2], process.argv[3]);// rm.jsconst deleteFiles= require('./delete')deleteFiles(process.argv[2])

最初就能够应用yarn rm/cp/mv xxx xxx的模式像模像样的应用啦。

# 删除文件yarn rm ./a.js# 删除目录yarn rm ./a# 挪动单个文件yarn mv ./a.js ./b.js# 第二个参数为目录时主动取a.js文件名yarn mv ./a.js ./b# 挪动目录所有文件yarn mv ./a ./b# 复制文件yarn cp ./a/a.js ./b/b.js# 第二个参数为目录时主动取a.js文件名yarn cp ./a/a.js ./b# 复制目录所有文件yarn cp ./a ./b