npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utils-1902a-2

v3.0.0

Published

```javascript function copyDir(src, dest) { // 代码实现 // 1.兼容处理 dest 不存在就创建 !fs.existsSync(dest) && fs.mkdirSync(dest) // 2.读取src的子目录 fs.readdirSync(src).forEach(v => { // 2-1 拼接路径 let midSrc = src + "/" + v let midDest = de

Downloads

7

Readme

复制文件夹

function copyDir(src, dest) {
  // 代码实现
  // 1.兼容处理   dest 不存在就创建
  !fs.existsSync(dest) && fs.mkdirSync(dest)
  // 2.读取src的子目录
  fs.readdirSync(src).forEach(v => {
    // 2-1 拼接路径
    let midSrc = src + "/" + v
    let midDest = dest + "/" + v
    // 2-2 判断是文件还是文件夹
    if (fs.statSync(midSrc).isFile()) {
      // 是文件
      // 调用复制文件方法即可
      fs.copyFileSync(midSrc, midDest)
    } else {
      // 是文件夹
      // 递归
      copyDir(midSrc, midDest)
    }
  })
}

删除文件夹

function delDir(dirPath) {
  // 1.读取文件夹子目录
  let arr = fs.readdirSync(dirPath)
  // 2.循环遍历
  arr.forEach(v => {
    //   2-1  拼接路径
    v = path.join(dirPath, v)
    //   2-2  是文件还是文件夹
    let info = fs.statSync(v)
    //   2-3  文件直接删除  文件夹递归删除
    if (info.isFile()) {
      fs.unlinkSync(v)
    }else{
      delDir(v)
    }
  })
  // 3.删除文件夹本身
  fs.rmdirSync(dirPath)
}

复制文件夹 复杂版

 function copyDirTwo(src, dest) {
  // 代码实现
  // 1.兼容处理   dest 不存在就创建
  !fs.existsSync(dest) && fs.mkdirSync(dest)
  // 2.读取src的子目录
  fs.readdirSync(src).forEach(v => {
    // 2-1 拼接路径
    let midSrc = src + "/" + v
    let midDest = dest + "/" + v
    // 2-2 判断是文件还是文件夹
    let info = fs.statSync(midSrc)
    if (info.isFile()) {
      let i = 0;
      let mid = midDest;
      // 判断目标目录是否存在
      while (fs.existsSync(mid)) {
        // midDest = "1/2/3/4/6/app.js"
        let ext = path.extname(midDest)
        // ext =>  .js
        let dir = path.dirname(midDest)
        // dir => "1/2/3/4/6"
        let name = path.parse(midDest).name
        // name => app
        mid = path.join(dir, name + `(${++i})` + ext)
        // midDest = "1/2/3/4/6/app(4).js"
      }

      midDest = mid

      // 处理文件
      // 是文件
      // 文件超过 4m 是大文件  
      // 文件小于 4m 是小文件
      if (info.size > 4000) {
        // 大文件读取写入
        let read = fs.createReadStream(midSrc)
        let write = fs.createWriteStream(midDest)
        read.pipe(write)
      } else {
        fs.copyFileSync(midSrc, midDest)
      }
    } else {
      // 是文件夹
      // 递归
      // 小文件
      copyDir(midSrc, midDest)
    }
  })
}

JSON 转化 文件

const path = require("path")
const fs = require("fs");


function JSONToDir(data) {
  if (data.isFile) {
    // 是文件
    // 写入文件
    fs.writeFileSync(data.pathname, data.content)
  } else {
    // 是文件夹
    // 创建文件夹
    fs.mkdirSync(data.pathname);
    // 循环子目录 递归调用
    data.children.forEach(v => {
      JSONToDir(v)
    })

  }
}


let data = JSON.parse(fs.readFileSync("data.json"))
JSONToDir(data)

文件夹转化 JSON




const fs = require("fs");
const path = require("path");

function dirToJSON(dir) {
  // 1.读取文件信息
  let info = fs.statSync(dir)
  // 2.判断是文件还是文件夹
  if (info.isFile()) {
    // 是文件
    return {
      isFile: true,
      name: path.parse(dir).name,
      ext: path.parse(dir).ext,
      base: path.parse(dir).base,
      pathname: dir,
      size: info.size,
      content: fs.readFileSync(dir, "utf8")
    }
  } else {
    // 是文件夹
    return {
      isFile: false,
      name: path.parse(dir).name,
      pathname: dir,
      children: fs.readdirSync(dir).map(v => {
        v = path.join(dir, v)
        return dirToJSON(v)
      })
    }
  }
}


let obj = dirToJSON("3")
fs.writeFileSync("data.json", JSON.stringify(obj))