Skip to content
/ Packages/CLI
1/19/2025
9.3m
AI 摘要

本文介绍了多个 CLI 工具及其用途,包括 ufo 处理 URL、del 删除文件、conventional-changelog-cli 生成日志、minimist 解析参数、trash 移动文件到回收站、date-fns 操作日期、tiny-glob 匹配文件等,cli-progress 显示进度、concurrently 并行执行命令、commitizen 规范 commit 信息等,涵盖开发、部署、调试等多个方面。

CLI

CLI 相关库

ufo ---> 功能丰富的URL处理

用法多样,解决大多数URL处理问题。包括提取内容、替换内容、判断内容等。

import { parseURL, withQuery, ... } from 'ufo'

// Result: { protocol: 'http:', auth: '', host: 'foo.com', pathname: '/foo', search: '?test=123', hash: '#token' }
parseURL('http://foo.com/foo?test=123#token')

// Result: /foo?page=a&token=secret
withQuery('/foo?page=a', { token: 'secret' })

// Result: { test: '123', unicode: '好' }
getQuery('http://foo.com/foo?test=123&unicode=%E5%A5%BD')

// Result: true
isSamePath('/foo', '/foo/')

// Result: http://example.com
withHttp('https://example.com')

del ---> glob匹配删除文件

import del from 'del'

(async () => {
	const deletedFilePaths = await del(['temp/*.js', '!temp/unicorn.js']);
	const deletedDirectoryPaths = await del(['temp', 'public']);

	console.log('Deleted files:\n', deletedFilePaths.join('\n'));
	console.log('\n\n');
	console.log('Deleted directories:\n', deletedDirectoryPaths.join('\n'));
})();

conventional-changelog-cli ---> 自动生成日志

根据git记录,生成CHANGELOG.md日志

$ npm i conventional-changelog-cli -g
$ conventional-changelog -p angular -i CHANGELOG.md -s

minimist ---> 解析命令行参数

var argv = require('minimist')(process.argv.slice(2));
console.log(argv);
$ node example/parse.js -x 3 -y 4 -n5 -abc --beep=boop foo bar baz
{ _: [ 'foo', 'bar', 'baz' ],
  x: 3,
  y: 4,
  n: 5,
  a: true,
  b: true,
  c: true,
  beep: 'boop' }

trash ---> 移动文件到回收站

不同于rimraf/deltrash只是将文件移动到回收站,而不是删除。

import trash from 'trash';

await trash(['*.png', '!rainbow.png']);

data-fns ---> 日期操作库

操作包括,给日期做加减法、找出最靠近某个日期的日期等等,还有最重要的支持i18n的格式化:

const { formatDistance, subDays } = require('date-fns')
const { zhCN }  = require('date-fns/locale')

console.log(formatDistance(new Date('2022-05-26'), new Date(), { addSuffix: true, locale: zhCN }))
// 大约 16 小时前

console.log(formatDistance(new Date('2022-05-20'), new Date(), { addSuffix: true, locale: zhCN }))
// 7 天前

tiny-glob ---> 高效的文件glob匹配

import glob from 'tiny-glob'

(async() => {
  let files = await glob('test/*/*.{js,md}');
  console.log(files)
  //=> [ 'test/README.md', 'test/webpack.config.js' ]
})()

相似项目:mrmlnc/fast-globsindresorhus/globbyisaacs/node-glob

文件无关:isaacs/minimatchisaacs/pico

mkdist ---> file-to-file transpiler

$ npx mkdist

resolve-from ---> 像 require.resolve() 但是指定 from

另一个类似的包,但是查询全局 sindresorhus/resolve-global

local—pkg ---> 获取本地包的信息

const resolveFrom = require('resolve-from');
 
// There is a file at `./foo/bar.js`
 
resolveFrom('foo', './bar');
//=> '/Users/sindresorhus/dev/test/foo/bar.js'

is-installed-globally ---> 判断当前包是否是全局安装

如果您的 CLI 在全局和本地安装时需要不同的行为,这会很有用。

cli-progress ---> 在终端中显示进度

import isInstalledGlobally from 'is-installed-globally';

// With `npm install your-package`
console.log(isInstalledGlobally);
//=> false

// With `npm install --global your-package`
console.log(isInstalledGlobally);
//=> true

concurrently ---> 同时执行多个scripts命令

支持命令行使用:

支持命令行使用:

$ npm i concurrently -g
$ concurrently "command1 arg" "command2 arg"

commitizen ---> 规范 commit 信息

安装和配置:

安装和配置:

 npm install -g commitizen
 commitizen init cz-conventional-changelog --pnpm --save-dev --save-exact

npm-run-all ---> 并行执行多个脚本

$ git add .
$ git cz
[email protected], [email protected]

? Select the type of change that you're committing: (Use arrow keys)
❯ feat:     A new feature 
  fix:      A bug fix 
  docs:     Documentation only changes 
  style:    Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc) 
  refactor: A code change that neither fixes a bug nor adds a feature 
  perf:     A code change that improves performance 
  test:     Adding missing tests or correcting existing tests

npm-run-all ---> 并行执行多个脚本

is-ci ---> 判断代码运行环境是否为命令行

可以在 package.json 中这样使用

lint-staged ---> 针对暂存文件运行 linters

配合 toplenboren/simple-git-hooks,实现在 commit 之前执行代码检查

changelogen ---> CHANGELOG 生成工具

还支持 bump 命令发版

$ npx changelogen@latest

$ npx changelogen@latest --bump

just ---> command runner

just 有大量有用的功能,并且比 make 进行了许多改进,使用 rust 编写。

open ---> 打开一切

brew install just

debug ---> 埋点调试

通过设置环境变量,灵活选择调试的模块

$ just test-all
cc *.c -o main
./test --all
Yay, all your tests passed!

cac ---> 命令行参数解析工具

如果你想开发一款命令行工具,试试它

ink ---> 用 React 写命令行工具

挺有意思

listr ---> 终端列表任务 UI

用于展示执行任务的过程

boxen ---> 在终端中创建框

import React, {useState, useEffect} from 'react';
import {render, Text} from 'ink';

const Counter = () => {
	const [counter, setCounter] = useState(0);

	useEffect(() => {
		const timer = setInterval(() => {
			setCounter(previousCounter => previousCounter + 1);
		}, 100);

		return () => {
			clearInterval(timer);
		};
	}, []);

	return <Text color="green">{counter} tests passed</Text>;
};

render(<Counter />);

yargs ---> parse args for CLI

clack ---> build beautiful prompt in cli

Two options:

import boxen from 'boxen';

console.log(boxen('unicorn', {padding: 1}));
/*
┌─────────────┐
│             │
│   unicorn   │
│             │
└─────────────┘
*/

difftastic ---> Diff 命令行工具

#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}

Released under the MIT License.