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

1811a-utils-3

v1.0.0

Published

```javascript const fs = require("fs");

Downloads

5

Readme

复制文件夹

const fs = require("fs");


function copyDir(source, target) {
    if (!fs.existsSync(target)) {
        // 1.创建目标目录
        fs.mkdirSync(target);
    }

    // 2.读取源文件夹子目录
    let arr = fs.readdirSync(source)
    // 3.遍历每一个子目录
    arr.forEach(v => {
        // 拼接路径
        let midSource = source + "/" + v
        let midTarget = target + "/" + v
        if (fs.statSync(midSource).isFile()) {
            // 是文件
            fs.copyFileSync(midSource, midTarget)
        } else {
            // 是文件夹
            copyDir(midSource, midTarget)
        }
    })
}



copyDir("demo", "demo1")

删除文件夹

const fs = require("fs");
// 算法实现: 删除非空文件夹


function removeDir(dir) {
    // 1.获取文件夹的子目录
    let arr = fs.readdirSync(dir);
    // 2.循环子目录
    arr.forEach(v => {
        // 路径拼接下
        v = dir + "/" + v;
        // 2-1 读取文件信息    
        let isFile = fs.statSync(v).isFile()
        // 2-2 判断是文件还是文件夹
        if(isFile){
            // 是文件  直接删除
            fs.unlinkSync(v)
        }else{
            // 是文件夹  
            // 递归删除文件夹
            removeDir(v)
        }
    })
    // 3.删除文件夹
    fs.rmdirSync(dir);

}


removeDir("demo")

转化

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

function dirToJSON(dir) {
    let info = fs.statSync(dir)
    if (info.isFile()) {
        // 是文件
        return {
            // 获取路径最后一个名字
            basename: path.basename(dir),
            pathname: dir,
            extname: path.extname(dir),
            type: "file",
            size: info.size,
            content: fs.readFileSync(dir, "utf-8")
        }
    } else {
        // 是文件夹
        return {
            pathname: dir,
            type: "dir",
            children: fs.readdirSync(dir).map(v => {
                return dirToJSON(dir + "/" + v)
            })
        }

    }
}



// let o = dirToJSON("demo");
// fs.writeFileSync("data.json", JSON.stringify(o))


function JSONToDir(obj) {
    if (obj.type === "dir") {
        // 是文件夹
        fs.mkdirSync(obj.pathname);
        obj.children.forEach(item => {
            JSONToDir(item)
        })
    } else {
        // 是文件
        fs.writeFileSync(obj.pathname, obj.content)
    }
}

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