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 🙏

© 2025 – Pkg Stats / Ryan Hefner

magic-analyzer

v1.0.1

Published

A simple plugin for analyzing file type, code type, code volume, and percentage statistics based on the node environment.

Downloads

22

Readme

简介

A simple plugin for analyzing file type, code type, code volume, and percentage statistics based on the node environment。 一个简单的基于node环境的文件类型、代码类型、代码量、及占比统计分析插件

安装

#npm
npm install magic-analyzer

#yarn
yarn add magic-analyzer

使用配置说明

  • 第一步:在需要进行分析的项目根目录【或在你需要分析的文件目录】中新建一个xx.mjs 或者xx.js的文件

    xx.mjs【package.json中有"type": "module"配置】

    /* eslint-env node */
    import codeAnalyze from 'magic-analyzer'
    import {fileURLToPath} from 'url';
    import path from 'path';
      
    // 排除指定文件或目录不統計:自定义
    const exceptItem = [
        'node_modules',
        'public',
        'assets',
        'dist',
        'docker',
        'docs',
        'gitignore',
        'git',
        'development',
        'production',
        'md',
        'remark',
        'idea',
        'yarn',
        'eot',
        'ttf',
        'woff',
        'txt',
        'json'
    ]
    // 指定要统计的目录路径
    //获取当前文件的 URL
    const __filename = fileURLToPath(import.meta.url);
    // 将 URL 转换为文件路径
    const __dirname = path.dirname(__filename);
    const directoryPath = __dirname
    // 調用統計分析方法
    codeAnalyze(directoryPath,exceptItem)

    xx.js【package.json中无type配置】

    /* eslint-env node */
    const codeAnalyze = require('magic-analyzer')
    const path = require('path')
      
    // 排除指定文件或目录不統計:自定义
    const exceptItem = [
        'node_modules',
        'public',
        'assets',
        'dist',
        'docker',
        'docs',
        'gitignore',
        'git',
        'development',
        'production',
        'md',
        'remark',
        'idea',
        'yarn',
        'eot',
        'ttf',
        'woff',
        'txt',
        'json'
    ]
    // 指定要统计的目录路径
    const moduleURL = path.join(__dirname)
    // 使用 path 模块来操作文件路径
    const directoryPath = path.dirname(moduleURL)
    // 調用統計分析方法
    codeAnalyze(directoryPath,exceptItem)
  • 第二步:在package.json的script脚本中配置一个分析命令,codeTest自定义命名,如下只是举例

    // xxx.js 或 xxx.mjs 在根目录
    "scripts": {
        "codeTest": "node xxx.mjs"
    },
    ## 或者
    "scripts": {
        "codeTest": "node xxx.js"
    },
      
    // 若xxx.js 或 xxx.mjs不在根目录,只需要根据其所在位置和package.json文件位置而定
    // 若在src目录: /src/xxx.js 或者 /src/xxx.mjs
    "scripts": {
        "codeTest": "node src/xxx.js"
    },
    ## 或者
    "scripts": {
        "codeTest": "node src/xxx.mjs"
    },

    注意:node运行的xxx.js或xxx.mjs文件路径为所在项目的绝对路径,自行更换相关配置

  • 第三步:在控制台运行分析命令

    yarn run codeTest ## npm run codeTest

codeAnalyze函数用法

codeAnalyze(path,exceptItem,sortType)

参数

  • path:分析目录路径,相对路径或绝对路径。

  • exceptItem:排查不统计项字符串数组,例如:

    const exceptItem = ['dist','txt','json','md','LICENSE',……]
    // 即:不统计dist目录、txt文件、json文件、md文件、LICENSE使用许可文件
  • sortType:排序类型,根据 files(文件数量) 字段排序,不区分大小写

    • ASC:升序
    • DESC:降序(默认)
    'desc' 等价于 'DESC'

实例

案例01:xx.mjs 或者xx.js文件创建在根目录,分析整个项目

01:项目目录结构如下

vue_demo/				   # 项目名称
        ├── node_modules/        # 项目依赖的 node 模块
        ├── public/              # 公共资源目录
        │   ├── favicon.ico      # 网页图标
        │   └── index.html       # html模板(单页面应用)
        ├── src/                 # 源代码目录
        │   ├── assets/          # 静态资源文件夹,如图片、字体等
        │   ├── components/      # 公共组件
        │   ├── router/          # 路由文件夹
        │   ├── store/           # Vuex状态管理文件夹
        │   ├── views/           # 视图层组件
        │   ├── App.vue          # Vue 根组件,也是整个应用的入口
        │   └── main.js          # Vue 实例化入口文件,也是整个应用的入口
        ├── .gitignore           # Git管理忽略文件
        ├── package.json         # 项目配置文件
        ├── README.md            # 项目说明文件
        ├── xxx.mjs              # magic-analyzer分析配置文件
        └── vite.config.mjs      # vite配置文件

02:配置 xxx.js 或者 xxx.mjs文件:见文档 使用配置说明

//语法:codeAnalyze(path,exceptItem,sortType)

// 分析vue_demo整个项目: 分析目录相对于 xxx.js 或者 xxx.mjs 文件的相对路径
const exceptItem = ['node_modules','gitignore','assets','md']
codeAnalyze('../vue_demo',exceptItem) // sortType不传默认DESC(降序)排序

// 若只分析src目录
const exceptItem = ['assets']
codeAnalyze('./src',exceptItem,'asc') // 升序排序

// 也可以通过如下+ path获取分析目录或者文件路径:分类目录或文件绝对路径,见 “使用配置说明”
import {fileURLToPath} from 'url';
import path from 'path';

// 指定要统计的目录路径
const moduleURL = path.join(__dirname)
// 使用 path 模块来操作文件路径
const directoryPath = path.dirname(moduleURL)
// 調用統計分析方法
codeAnalyze(directoryPath,exceptItem)
…………

03:运行命令 见 使用配置说明

04:分析结果即(analyzer.json)

  • 降序(默认)排序:DESC

    {
        "js": {
            "files": 8,
            "lines": 302,
            "fileNumPercentage": "42.11%",
            "codeNumPercentage": "28.44%"
        },
        "mjs": {
            "files": 7,
            "lines": 299,
            "fileNumPercentage": "36.84%",
            "codeNumPercentage": "28.15%"
        },
        "json": {
            "files": 3,
            "lines": 96,
            "fileNumPercentage": "15.79%",
            "codeNumPercentage": "9.04%"
        },
        "lock": {
            "files": 1,
            "lines": 365,
            "fileNumPercentage": "5.26%",
            "codeNumPercentage": "34.37%"
        }
    }
  • 升序排序:ASC

    {
        "lock": {
            "files": 1,
            "lines": 365,
            "fileNumPercentage": "5.26%",
            "codeNumPercentage": "34.37%"
        },
        "json": {
            "files": 3,
            "lines": 96,
            "fileNumPercentage": "15.79%",
            "codeNumPercentage": "9.04%"
        },
        "mjs": {
            "files": 7,
            "lines": 299,
            "fileNumPercentage": "36.84%",
            "codeNumPercentage": "28.15%"
        },
        "js": {
            "files": 8,
            "lines": 302,
            "fileNumPercentage": "42.11%",
            "codeNumPercentage": "28.44%"
        }
    }

生成文件说明

根目录之下会生成一个analyzer.json文件,该文件是分析当前项目生成的统计文件,生成json案例如下:

{
  "mjs": {
    "files": 1, // .mjs文件总数
    "lines": 43, // .mjs代码行数
    "fileNumPercentage": "50.00%", // 文件数量百分比占比(基于本次分析总文件量)
    "codeNumPercentage": "75.44%" // 代码量百分比占比(基于本次分析总代码量)
  },
  "js": {
    "files": 1,
    "lines": 14,
    "fileNumPercentage": "50.00%",
    "codeNumPercentage": "24.56%"
  }
}

使用环境

  • node >= 12.0.0
  • package.json"type": "module"只能使用使用说明中的xx.mjs文件
  • package.json中没有type配置,可以使用xx.mjs 或者xx.js的文件进行分析配置

注意事项

  • 分析文件不能过多,否则会分享失败并且报错,比如排除不必要的distnode_moduleassets等等。
  • 默认分析当前项目

更新日志

  • 2024-06-04
    • 新增sortType排序类型,通过files排序
    • 更新package.json文件,添加仓库地址
    • 更新README.md文档