@dreamdt/cli
Advanced tools
| # EditorConfig is awesome: https://EditorConfig.org | ||
| # top-most EditorConfig file | ||
| root = true | ||
| [*] | ||
| indent_style = space | ||
| indent_size = 2 | ||
| end_of_line = crlf | ||
| charset = utf-8 | ||
| trim_trailing_whitespace = false | ||
| insert_final_newline = false |
| import semver from 'semver'; | ||
| import chalk from 'chalk'; | ||
| import inquirer from 'inquirer'; | ||
| import axios from 'axios'; | ||
| import { compareVersions } from './compare-version.js'; | ||
| /** | ||
| * 检查 node 版本号 | ||
| * @param {*} wanted 要验证的 node 版本号 | ||
| */ | ||
| export async function checkNodeVersion(wanted) { | ||
| const processVersion = process.version; | ||
| if (!semver.satisfies(processVersion, wanted)) { | ||
| console.log(chalk.red(`您正在使用 Node ${processVersion}, 但这个版本的要求是 Node ${wanted}。\n建议您更换 Node 版本到 v14.x.x。`)); | ||
| const { next } = await inquirer.prompt([ | ||
| { | ||
| 'name': 'next', | ||
| 'type': 'list', | ||
| 'message': '你可以:', | ||
| 'pageSize': 15, | ||
| 'loop': false, | ||
| 'choices': [ | ||
| { 'name': '下载Node安装包', 'value': 'down' }, | ||
| { 'name': '忽略并继续', 'value': false } | ||
| ] | ||
| } | ||
| ]); | ||
| if (next === 'down') { | ||
| // 获取 node 18 版本的下载地址,并打开 | ||
| let res = await axios.get('https://nodejs.org/dist/'); | ||
| const html = res.data; | ||
| let versionList = html.match(/v14\.\d{1,2}\.\d{1,2}/g); | ||
| let latest; | ||
| if (Array.isArray(versionList)) { | ||
| versionList.sort(compareVersions); | ||
| latest = versionList[versionList.length - 1]; | ||
| } | ||
| console.log(`请访问 ${chalk.blue(`https://nodejs.org/dist/${latest}/`)} 下载Node安装包`); | ||
| exit(0); | ||
| } | ||
| } | ||
| } |
| // 定义一个比较版本号的函数 | ||
| export function compareVersions(version1, version2) { | ||
| // 去掉前缀 'v',然后按照 '.' 分割版本号字符串 | ||
| const nums1 = version1.substring(1).split('.').map(Number); | ||
| const nums2 = version2.substring(1).split('.').map(Number); | ||
| // 依次比较每一位数字 | ||
| for (let i = 0; i < Math.min(nums1.length, nums2.length); i++) { | ||
| if (nums1[i] !== nums2[i]) { | ||
| return nums1[i] - nums2[i]; | ||
| } | ||
| } | ||
| // 版本号长度不一样时,较长的版本号较大 | ||
| return nums1.length - nums2.length; | ||
| } |
| import { expect } from 'chai'; | ||
| import sinon from 'sinon'; | ||
| import { checkNodeVersion } from '../lib/check-node-version.js'; | ||
| describe('dreamcli', () => { | ||
| let consoleLogStub; | ||
| beforeEach(() => { | ||
| consoleLogStub = sinon.stub(console, 'log'); | ||
| }); | ||
| afterEach(() => { | ||
| consoleLogStub.restore(); | ||
| }); | ||
| // 测试版本命令 | ||
| it('should print the version', () => { | ||
| dreamcli(); | ||
| }); | ||
| it('should print the version', () => { | ||
| // checkNodeVersion('16'); | ||
| // 我的判断 node 版本函数里使用了交互式问题,让用户选择是否下载 node,这个应该如何写测试用例? | ||
| }) | ||
| }) |
+26
-5
| { | ||
| "name": "@dreamdt/cli", | ||
| "version": "0.0.3", | ||
| "version": "0.0.4", | ||
| "description": "", | ||
| "type": "module", | ||
| "main": "bin/index.js", | ||
@@ -10,3 +11,3 @@ "bin": { | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| "test": "mocha test/ -r chai/register-expect.js" | ||
| }, | ||
@@ -16,8 +17,28 @@ "keywords": [ | ||
| ], | ||
| "author": "", | ||
| "license": "MIT", | ||
| "author": "dreamdt", | ||
| "license": "ISC", | ||
| "publishConfig": { | ||
| "registry": "https://registry.npmjs.org/", | ||
| "access": "public" | ||
| }, | ||
| "engines": { | ||
| "node": "^12.10.0 || 13.x.x || 20.x.x", | ||
| "npm": "^10.5.0" | ||
| }, | ||
| "dependencies": { | ||
| "axios": "^1.6.8", | ||
| "boxen": "^7.1.1", | ||
| "chalk": "^5.3.0", | ||
| "commander": "^12.0.0", | ||
| "inquirer": "^9.2.19", | ||
| "semver": "^7.6.0" | ||
| }, | ||
| "devDependencies": { | ||
| "chai": "^5.1.0", | ||
| "mocha": "^10.4.0", | ||
| "sinon": "^17.0.1" | ||
| }, | ||
| "volta": { | ||
| "node": "20.12.2" | ||
| } | ||
| } | ||
| } |
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Empty package
Supply chain riskPackage does not contain any code. It may be removed, is name squatting, or the result of a faulty package publish.
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
4175
978.81%6
200%81
Infinity%1
-50%2
-33.33%1
-50%Yes
NaN6
Infinity%3
Infinity%2
100%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added