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

output-tree

v1.0.5

Published

支持将树状结构输出为字符串目录,支持读取本地目录输出为树状结构

Downloads

30

Readme

安装

$ npm install output-tree

方法

readFileTree() // 读取文件的方法,输出一个树级结构对象
outputTree() // 传入树级结构的对象,输出一个tree的字符串目录结构

使用

const { outputTree } = require('output-tree')

const tree = [
    {
        name: 'home',
        children: [
            {
                name: 'index.vue',
            }
        ]
    },
    {
        name: 'about',
        children: [
            {
                name: 'index.vue',
            },
            {
                name: 'about-1',
                children: [
                    {
                        name: 'about-1.html',
                        children: []
                    },
                    {
                        name: 'about-1.css',
                        children: []
                    },
                ]
            }
        ]
    }
]

const result = outputTree({
    data: tree
})
console.log(result)

/**
 * // 输出
 * 
 * ├─home
 * │   └─index.vue
 * └─about        
 * │   ├─index.vue
 * │   └─about-1
 * │   │   ├─about-1.html
 * │   │   └─about-1.css
 *
 */

使用自定义key值


const { outputTree } = require('output-tree')

const tree = [
    {
        file: 'home',
        list: [
            {
                file: 'index.vue',
            },
            {
                file: 'test.txt'
            }
        ]
    }
]

const result = outputTree({
    data: tree,
    name: 'file',
    children: 'list'
})
console.log(result)

/**
 * // 输出
 * 
 * └─home
 * │   ├─index.vue
 * │   └─test.txt
 */

使用自定义模板

const tree = [
    {
        name: 'home',
        size: 10,
        children: [
            {
                name: 'index.vue',
                size: 9
            },
            {
                name: 'test.txt',
                size: 300
            }
        ]
    }
]

const result = outputTree({
    data: tree,
    template: '文件名:{name}, 文件大小:{size}kb'
})
console.log(result)

/**
 * // 输出
 * 
 * └─文件名:home, 文件大小:10kb
 * │   ├─文件名:index.vue, 文件大小:9kb
 * │   └─文件名:test.txt, 文件大小:300kb
 */

outputTree属性

| 属性 | 必填 | 类型 | 说明 | Default | | ------- | ------- | ------- | ------ | ------- | | data | 否 | object,array | name | 类型 | | name | 否 | string | name的key值 | 'name' | | children | 否 | string | children的key值 | 'children' | | template | 否 | string | 自定义模板 | '{name}' | | depth | 否 | number | 输出的层级有多深,默认无限深 | Infinity | | excludes | 否 | array | ['.js', '.css', 'node_modules'] 需要忽略的后缀名或者是文件夹名称 | [] | | indent | 否 | number | 缩进,符号之间的间隔 | 2 |

浏览器环境无法使用该方法:readFileTree

const { readFileTree } = require('output-tree')
// 读取本地文件目录
const tree = readFileTree({
    name: 'name', // 输出的name名
    children: 'children', // 输出的children名
    depth: 1, // 读取文件的层级
    path: __driname, // 读取文件的路径
})
/**
 * [
 *  {
 *    name: 'index.js', // 文件名
 *    path: 'C:\\Users\\Administrator\\Desktop\\output-tree\\index.js', // 文件路径
 *    extname: '.js', // 文件后缀名
 *    isFile: true, // 是否是文件类型
 *    isDirectory: false, // 是否是目录类型
 *    level: 1, // 层级
 *    children: [] // 子节点list
 *  },
 *  {
 *    name: 'package.json',
 *    path: 'C:\\Users\\Administrator\\Desktop\\output-tree\\package.json',
 *    extname: '.json',
 *    isFile: true,
 *    isDirectory: false,
 *    level: 1,
 *    children: []
 *  },
 *  {
 *    name: 'README.md',
 *    path: 'C:\\Users\\Administrator\\Desktop\\output-tree\\README.md',
 *    extname: '.md',
 *    isFile: true,
 *    isDirectory: false,
 *    level: 1,
 *    children: []
 *  }
 *]
 */

readFileTree属性

| 属性 | 必填 | 类型 | 说明 | Default | | ------- | ------- | ------- | ------ | ------- | | name | 否 | string | name的key值 | 'name' | | children | 否 | string | children的key值 | 'children' | | depth | 否 | number | 读取的文件层级有多深,默认无限深 | Infinity | | path | 否 | string | 读取文件的路径,需要绝对路径 | __dirname | | excludes | 否 | array | ['.js', '.css', 'node_modules'] 需要忽略的后缀名或者是文件夹名称 | [] |

结合使用

const tree = readFileTree({
    name: 'file',
    children: 'fileList',
    depth: 3,
    path: __dirname
})
const result = outputTree({
   data: tree,
   name: 'file',
   children: 'fileList',
   depth: Infinity,
   template: '{file}'
})