Skip to content

Commit

Permalink
feat(#32): support ignore option
Browse files Browse the repository at this point in the history
  • Loading branch information
KID-joker committed Dec 22, 2024
1 parent 2241160 commit e0c3308
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -24,11 +24,12 @@ program
program
.command('current')
.description('check the packages of the current project')
.addOption(new Option('--ignore <value>', 'ignore specific packages'))
.addOption(registryOption)
.addOption(gptOption)
.addOption(gptModelOption)
.addOption(gptBaseURL)
.action((option: CommonOption) => {
.action((option: CurrentOption) => {
checkNode()
checkCurrent(option)
})
Expand Down
16 changes: 11 additions & 5 deletions src/io/current.ts
Original file line number Diff line number Diff line change
@@ -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<string, VersionOrRange> = {}

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)

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 14 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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()
})
})
Expand Down

0 comments on commit e0c3308

Please sign in to comment.