Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#32): support ignore option #34

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading