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

@w3cdoc/cli-base

v1.0.0

Published

Base Module for Command Line Tools

Downloads

4

Readme

CLI BASE TOOLS

Node Cli 基础方法库

Getting Started

  • $ npm install @w3cdoc/cli-base --save
import { consts, utils, files } from '@w3cdoc/cli-base';
const { print } = utils;

print('debug', 'hello', 'world');

API

1. consts 静态常量

| 属性名 | 描述 | 类型 | | -------- | ------------------- | --------- | | HOMEPATH | 用户 home 目录 | string | | IS_WIN | 是否是 windows 系统 | boolean |

2. utils 一些常用的方法

| 方法名 | 描述 | 参数定义 | 返回值 | | --------------- | ------------------------------------ | ------------------------------------------------------------------------------------- | ------------------------------ | | progressBar | 返回一个进度条 string | recent: number, total?: number, label?: string | string | | print | 带颜色级别的 console.log | type: 'debug'|'info'|'success'|'warn'|'error', ...msgs: BaseType[] | (filepath: string) => any | | printJSON | 打印单层的 JSON | json: Record<string, BaseType> | void | | parseProperties | 解析 .properties 文件,返回一个 JSON | file: string | Promise<Record<string, any>> | | templateRender | 最简单的模板渲染 | tpl: string, data: Record<string, any> = {} | string | | confirm | node 控制台二次确认 | message: string, defaultValue = false | Promise<boolean> | | select | node 控制台用户选择 | message: string, options: SelectOptions[] | string[], defaultValue: string | number | Promise<string> | | input | node 控制台用户输入 | message: string, defaultValue?: string, validator = (v: string) => any | Promise<string> | | password | node 控制台密码输入 | message: string, defaultValue?: string, validator = (v: string) => any | Promise<string> | | holding | node 控制台进入等待状态,按回车继续 | tips = '按回车继续...' | Promise<boolean> | | exec | 执行 shell 命令,返回执行结果 | command: string, cwd = process.cwd() | Promise<any> | | execSync | 同步执行 shell 命令 | command: string, cwd = process.cwd() | SpawnSyncReturns |

其中 inputpassword 方法的第 3 个参数是校验方法,入参是输入的值,如果返回 false 或 Error(支持 Promise),则表示校验失败,例如:

utils.input('请输入用户名', null, async (value) => {
  // 直接校验
  return value === 'zhangsan'; 

  // 返回自定义错误文案
  return value === 'zhangsan' ? true : new Error('校验失败!');

  // 远程校验
  return await request(`api/validate?name=${value}`);
});

扩展的类型定义

/**
 * 基本类型,不算 object、null、undefined
 */
export type BaseType = string | number | boolean | symbol;

3. files 文件操作相关的常用方法

| 方法名 | 描述 | 参数定义 | 返回值 | | --------------- | --------------------------------------------------------- | ----------------------------------------------------------------------------------------------- | ------------------------ | | fileIterator | 遍历目录文件,执行 callback | 见下文 | Promise<any> | | dirSyncIterator | 遍历目录文件,同步到目标目录,并对每一个文件执行 callback | 见下文 | Promise<any> | | getFileCount | 统计目录中的文件数量 | src: string, exclude?: RegExp | Promise<number> | | copyDir | 逐个复制目录中的文件 | options: OptionShape | Promise<any> | | clearDir | 清空目录(有二次确认) | options: ClearOptions | Promise<boolean> | | findInFolder | 从目录中查找内容符合条件的文件 | src: string, callback: (filePath: string, content: string) => boolean, options: SearchOptions | Promise<SearchResult[] |

扩展的类型定义

// 遍历文件目录,执行 callback
export async function fileIterator(
  /** 目录路径 */
  src: string,
  /** 执行到文件时的回调,如果返回 false 或 async false,则遍历中止 */
  fileCallback: (filePath: string, fileRelativePath: string) => any,
  /** 执行到目录时的回调 */
  dirCallback?: (dirRelativePath: string) => any,
  /** 忽略的文件 */
): Promise<any>;

// 遍历文件目录,同步到目标目录,并对每一个文件执行 callback
export async function dirSyncIterator(
  /** 源文件目录 */
  source: string,
  /** 目标目录 */
  target: string,
  /** 执行到文件时的回调 */
  callback: (sourceFile: string, targetFile: string) => any,
  /** 忽略的文件 */
  exclude?: RegExp
): Promise<any>

// copyDir 方法的参数
export interface OptionShape {
  /** 文件来源目录 */
  src: string;
  /** 目标目录 */
  dist: string;
  /** 文件内容替换规则  */
  replacer?: { holder: RegExp; value: string }[];
  /** 忽略的文件 */
  exclude?: RegExp;
  /** 只读文件(不读取&替换的文件) */
  readonlyFile?: RegExp;
  /** 文件名替换方法 */
  fileNameTransfer?: (name: string) => string;
  /** 文件内容格式化方法 */
  contentFormatter?: (content: string, src: string) => Promise<string>;
}

// clearDir 方法的参数 
export interface ClearOptions {
  /** 要清除的目录 */
  src: string;
  /** 是否有二次确认 */
  confirm?: boolean;
  /** 二次确认文案 */
  confirmText?: string;
  /** 忽略的文件 */
  exclude?: RegExp;
}

// findInFolder 方法的参数
export interface SearchOptions {
  /** 是否找到所有文件,默认 false,即找到第 1 个就返回 */
  all?: boolean;
  /** 如果指定了 include,则只处理匹配的文件 */
  include?: RegExp;
  /** 匹配规则的文件跳过 */
  exclude?: RegExp;
  /** 文件大小限制(byte),超过大小的文件不遍历 */
  limit?: number;
  /** 读取文件的编码,默认是 utf-8 */
  encoding?: BufferEncoding;
}

export interface SearchResult {
  filePath: string;
  fileRelativePath: string;
  fileSize: number;
}

CHANGELOG

  • 1.0.0 发布 1.0 版本,整合并优化 已有能力
  • 1.0.4 优化文档,迁移代码仓库
  • 1.1.0 去掉 BaseAction ,简化架构
  • 1.1.7 增强 print 方法,可以将日志输出到本地

LICENSE

BSD-3-Clause License

Contact Us

[email protected]