首页 > 编程语言 >译:使用现代的 Node.js 构建简单的CLI工具

译:使用现代的 Node.js 构建简单的CLI工具

时间:2024-02-22 23:00:16浏览次数:25  
标签:Node node CLI changelog js assert mjs test

原文地址: https://evertpot.com/node-changelog-cli-tool/

作者: Evert Pot

发布时间:2023-02-13

只使用 Node.js 的标准库,不安装任何外部依赖,写一个命令行工具。

前言

作者是多个开源项目的维护者,长久以来都是手动维护项目的变更日志(changelog)。

下面是项目 a12n-server 的变更日志示例:

0.22.0 (2022-09-27)
-------------------

Warning note for upgraders. This release has a database migration on the
`oauth2_tokens` table. For most users this is the largest table, some
downtime may be expected while the server runs its migrations.

* #425: Using a `client_secret` is now supported with `authorization_code`,
  and it's read from either the request body or HTTP Basic Authorization
  header.
* The service now keeps track when issuing access tokens, whether those tokens
  have used a `client_secret` or not, which `grant_type` was used to issue them
  and what scopes were requested. This work is done to better support OAuth2
  scopes in the future, and eventually OpenID Connect.
* Fixed broken 'principal uri' in introspection endpoint response.
* OAuth2 service is almost entirely rewritten.
* The number of tokens issued is now displayed on the home page.
* Large numbers are now abbreviated with `K` and `M`.
* #426: Updated to Curveball 0.20.
* #427: Typescript types for the database schema are now auto-generated with
  `mysql-types-generator`.

内容使用markdown编写的。你可能会想:Git不是有 commit 日志吗?为什么要费力手写这个呢?

原因是它们的受众不一样。我希望让用户关注到相对重要的变更事项,并同时注意到变化对用户带来的影响。

我觉得写一个命令行工具来做这些事会更方便,维护多个项目(如此多的变更)也更容易一些。所以,我就做了 changelog-tool!如果你想知道这背后涉及了哪些技术选择,请接着往下阅读。

目标&特色

工具已支持的功能:

  • 重新格式化日志(有点像美化)( changelog format );
  • 通过命令行添加一行日志( changelog add --minor -m "New feature" );
  • 自动设置发布日期( changelog release );
  • 将特定版本的日志导入标准输出,以便其他工具可以使用(例如与GitHub发布集成)。

我还有一些非功能性的需求:

  • 使用最新的 Node.js 特性;
  • 使用最新的 JavaScript 标准和特性(ESM);
  • 避免非必要的外部依赖;
  • 低维护成本。

想立即找到这个工具吗?它是开源的,你只需访问 Github

原理解析

ESM & Typescript ESM

ESM 模块现在使用起来已经非常丝滑了。这是习惯上的一个小改变,但我一般建议是将文件保存为 .mjs 来使用 ESM

下面是 parse.mjs 的前几行代码:

import { readFile } from 'node:fs/promises'
import { Changelog, VersionLog } from './changelog.mjs'

/**
 * @param {string} filename
 * @returns {Promise<Changelog>}
 */
export async function parseFile(filename) {
  return parse(
    await readFile(filename, 'utf-8')
  )
}

CommonJS -> ESM 的过渡并非没有痛苦,但对于像这样的新项目来说,它是非常理想的选择。(顶层 await

标签:Node,node,CLI,changelog,js,assert,mjs,test
From: https://www.cnblogs.com/roseAT/p/18028402

相关文章

  • js-模块化AMD vs CMD
    1.AMD和require.jsdefine(function(){varbasicNum=0;varadd=function(x,y){returnx+y;};return{add:add,basicNum:basicNum};});//定义一个依赖underscore.js的模块define(['underscore'],func......
  • C# 采用HttpWebRequest 、WebClient和HttpClient下载https的文件异常问题
    今天有个客户反应,程序下载文件失败,无法正常使用。远程客户电脑后,查看错误日志,都是提示https:****************************.dll等一系列文件的下载错误提示提示基础连接已经关闭:发送时发生错误。在网上找了很多方案都没有能解决,大多都是https链接时增加指定协议,很遗憾未能解......
  • arm架构 麒麟系统,linux环境安装Oracle Instant Client
    $wgethttps://download.oracle.com/otn_software/linux/instantclient/instantclient-basic-linux-arm64.zip$unzipinstantclient-basic-linux-arm64.zip$wgethttps://download.oracle.com/otn_software/linux/instantclient/instantclient-sqlplus-linux-arm64.zip......
  • Jenkins CLI 任意文件读取漏洞(CVE-2024-23897)复现
    0x00漏洞简介Jenkins是一款基于JAVA开发的开源自动化服务器。Jenkins使用args4j来解析命令行输入,并支持通过HTTP、WebSocket等协议远程传入命令行参数。在args4j中,用户可以通过@字符来加载任意文件。这一特性存在安全风险,攻击者可以利用它来读取服务器上的任意文件。0x01影响......
  • NFS Server 和 Client 的配置
    NFSServer和Client的配置NFSServer的安装‍Linux上NFSServer的安装及配置在Linux上安装NFS共享服务:yuminstall-ynfs-server‍启动NFS服务:systemctlstartrpcbindsystemctlstartnfs‍加入开机自启动:systemctlenablerpcbindsystemctlenable......
  • 认识three.js和本地环境搭建
    要学习three.js首先我们要了解几个概念什么是3d,什么是webgl,那么three.js又是什么。下面我们来一一介绍。首先什么是3d?3D,全称为Three-Dimensional,即三维,是指在空间中具有三个维度(长度、宽度和高度)的概念。在现实世界中,我们所处的就是一个三维空间,物体都有立体形态,可以从不同的......
  • C# http请求 ( post方式 JSON格式 )
    直接上代码usingSystem.Net;usingSystem.IO;privatevoidbtnPost_Click(objectsender,EventArgse){this.txtResult.AppendText(DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss")+"准备请求"+"\r\n");//js......
  • .net core 读取appsetting.json 封装
    /*需要引入的包Microsoft.Extensions.Configuration-提供配置的核心功能。Microsoft.Extensions.Configuration.Json-支持从JSON文件加载配置。Microsoft.Extensions.Configuration.FileExtensions-支持文件相关的配置,如设置基路径。Microsoft.Extensions.Configura......
  • js闭包和循环
    for(vari=1;i<=5;i++){setTimeout(functiontimer(){console.log(i);},i*1000);}以上代码输出都是6。因为整个过程中只有i一个变量,所有循环共享i变量,循环执行完毕(定时器执行之前)后i为6for(vari=1;i<=5;i++){letj=i;setTimeout(function......
  • js 基础知识
     01-数据类型值类型(基本类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol。引用数据类型(对象类型):对象(Object)、数组(Array)、函数(Function),还有两个特殊的对象:正则(RegExp)和日期(Date)。02-检测数据类型2.1-typeof<!DOCTYPEhtml><ht......