From e0c3308f2fee985a64ce897886de46428c05f414 Mon Sep 17 00:00:00 2001 From: KID-joker Date: Sun, 22 Dec 2024 10:45:42 +0800 Subject: [PATCH] feat(#32): support ignore option --- src/cli.ts | 5 +++-- src/io/current.ts | 16 +++++++++++----- src/types.ts | 4 ++++ test.js | 19 ++++++++++++++----- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index e0e6df6..af8d6f9 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,5 +1,5 @@ import type { Command } from 'commander' -import type { CommonOption, ConfigOption, GlobalOption, PackageOption } from './types' +import type { CommonOption, ConfigOption, CurrentOption, GlobalOption, PackageOption } from './types' import process from 'node:process' import { Option, program } from 'commander' import { version } from '../package.json' @@ -24,11 +24,12 @@ program program .command('current') .description('check the packages of the current project') + .addOption(new Option('--ignore ', 'ignore specific packages')) .addOption(registryOption) .addOption(gptOption) .addOption(gptModelOption) .addOption(gptBaseURL) - .action((option: CommonOption) => { + .action((option: CurrentOption) => { checkNode() checkCurrent(option) }) diff --git a/src/io/current.ts b/src/io/current.ts index df52b8e..dc5dced 100644 --- a/src/io/current.ts +++ b/src/io/current.ts @@ -1,21 +1,27 @@ -import type { CommonOption } from '../types' +import type { CurrentOption, VersionOrRange } from '../types' import { checkDependencies } from '../check' import { isGitPackage, isLocalPackage, isURLPackage } from '../filter' import { getDependenciesOfLockfile } from '../packages/lockfiles' import { getDependenciesOfPackageJson } from '../packages/package_json' import { error } from '../utils/console' -export default async function checkCurrent(options: CommonOption) { +export default async function checkCurrent(options: CurrentOption) { try { const dependenciesOfPackageJson = getDependenciesOfPackageJson() if (!dependenciesOfPackageJson) return - const entries = Object.entries(dependenciesOfPackageJson) - .filter(ele => !isLocalPackage(ele[1].range as string) && !isURLPackage(ele[1].range as string) && !isGitPackage(ele[1].range as string)) + const ignores = options.ignore?.split(',') || [] - const npmDependencies = Object.fromEntries(entries) + const npmDependencies: Record = {} + + for (const name in dependenciesOfPackageJson) { + const versionInfo = dependenciesOfPackageJson[name] + if (!ignores.includes(name) && !isLocalPackage(versionInfo.range as string) && !isURLPackage(versionInfo.range as string) && !isGitPackage(versionInfo.range as string)) { + npmDependencies[name] = versionInfo + } + } const dependenciesOfLockfile = await getDependenciesOfLockfile(npmDependencies) diff --git a/src/types.ts b/src/types.ts index 4049921..bf2d520 100644 --- a/src/types.ts +++ b/src/types.ts @@ -8,6 +8,10 @@ export interface CommonOption extends OpenaiOption { registry: string } +export interface CurrentOption extends CommonOption { + ignore: string +} + export interface GlobalOption extends CommonOption { manager: string } diff --git a/test.js b/test.js index 30f7ad8..d50218c 100644 --- a/test.js +++ b/test.js @@ -2,6 +2,15 @@ import assert from 'node:assert/strict' import { exec } from 'node:child_process' import { test } from 'node:test' +test('node tests', async (t) => { + await t.test('test node version deprecation check', (_t, done) => { + exec('node ./dist/cli.mjs node', (_error, stdout, stderr) => { + assert.ok(/node version/.test(stdout) || /node version/.test(stderr), 'Expected "node version" to be mentioned in output.') + done() + }) + }) +}) + test('current tests', async (t) => { await t.test('check if no deprecation warning is shown', (_t, done) => { exec('node ./dist/cli.mjs current', (_error, _stdout, stderr) => { @@ -18,12 +27,12 @@ test('current tests', async (t) => { done() }) }) -}) -test('node tests', async (t) => { - await t.test('test node version deprecation check', (_t, done) => { - exec('node ./dist/cli.mjs node', (_error, stdout, stderr) => { - assert.ok(/node version/.test(stdout) || /node version/.test(stderr), 'Expected "node version" to be mentioned in output.') + await t.test('check if no deprecation warning is shown if ignore deprecated package', (_t, done) => { + exec('pnpm i request --force && node ./dist/cli.mjs current --ignore request', { timeout: 160000 }, (_error, _stdout, stderr) => { + assert.ok(!/has been deprecated/.test(stderr), 'Not expected "has been deprecated" to be mentioned in deprecation warning.') + // Cleanup: Undo the installation + exec('pnpm remove request') done() }) })