Skip to content

Commit

Permalink
feat: add mode option ('verbose','quite')
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohansebas committed Jul 23, 2024
1 parent 6693544 commit 74f8288
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const configCommand = async (name: 'scanner' | 'all', options: { reset?:
}

console.dir({
general: conf.get('general'),
scanner: conf.get('scanner'),
})

Expand Down
19 changes: 13 additions & 6 deletions packages/cli/src/commands/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export const scannerCommand = async (
spinner.color = 'green'
spinner.start()

const preferences = (conf.get('scanner') || {}) as { checkContent?: boolean; checkDependencies?: boolean }
const preferencesScanner = (conf.get('scanner') || {}) as { checkContent?: boolean; checkDependencies?: boolean }
const preferencesGeneral = (conf.get('general') || {}) as { mode?: 'verbose' | 'quite' }

const projectPath = name || process.cwd()

Expand All @@ -30,12 +31,16 @@ export const scannerCommand = async (
process.exit(1)
}

if (program.opts().mode) {
preferencesGeneral.mode = program.opts().mode
}

if (process.argv.includes('--check-content') || process.argv.includes('--no-check-content')) {
preferences.checkContent = options.checkContent
preferencesScanner.checkContent = options.checkContent
}

if (process.argv.includes('--check-dependencies') || process.argv.includes('--no-check-dependencies')) {
preferences.checkDependencies = options.checkDependencies
preferencesScanner.checkDependencies = options.checkDependencies
}

try {
Expand All @@ -45,8 +50,9 @@ export const scannerCommand = async (

const report = await generateReport(files, {
root: resolvedProjectPath,
checkContent: preferences.checkContent ?? true,
checkDependencies: preferences.checkDependencies ?? true,
checkContent: preferencesScanner.checkContent ?? true,
checkDependencies: preferencesScanner.checkDependencies ?? true,
mode: preferencesGeneral.mode,
})

spinner.stop()
Expand All @@ -58,6 +64,7 @@ export const scannerCommand = async (
} catch {
spinner.fail()
} finally {
conf.set('scanner', preferences)
conf.set('scanner', preferencesScanner)
conf.set('general', preferencesGeneral)
}
}
19 changes: 14 additions & 5 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node

import { Argument, Command } from 'commander'
import { Argument, Command, Option } from 'commander'
import Conf from 'conf'
import { green } from 'picocolors'

Expand All @@ -16,20 +16,31 @@ export const conf = new Conf({
type: 'object',
default: { checkDependencies: true, checkContent: true },
},
general: {
type: 'object',
default: {
mode: 'quite',
},
},
},
})

export const program = new Command(packageJson.name)
.version(packageJson.version)
.usage(`${green('[command]')} ${green('[options]')}`)
.addOption(
new Option('-m, --mode <arg>', 'select the mode in which the information will be printed.').choices([
'quite',
'verbose',
]),
)
.hook('postAction', async () => {
await checkUpdates()
})

program
.command('config')
// TODO: support all option
.addArgument(new Argument('[command]', 'get global options for commands').choices(['scanner', 'all']))
.addArgument(new Argument('[command]', 'get global options for commands').choices(['general', 'scanner', 'all']))
.option('--reset', 'explicitly tell the CLI to reset any stored preferences')
.action(configCommand)

Expand All @@ -44,11 +55,9 @@ program
// .allowUnknownOption()

//TODO: add option --output -o support csv, json yaml html table
//TODO: add option --verbose --quite
program
.command('scan')
.argument('[project-directory]')
.option('-m, --mode <arg>', 'select the mode in which the information will be printed.')
.option('--no-check-content')
.option('--check-content')
.option('--check-dependencies')
Expand Down
6 changes: 5 additions & 1 deletion packages/scanner/src/helpers/get-formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export function getFormatters({
biome,
prettier,
config,
}: { biome: BiomeConfig | null; prettier: PrettierConfig | null; config: ConfigReport }): Formatters[] | null {
}: { biome: BiomeConfig | null; prettier: PrettierConfig | null; config: ConfigReport }) {
if (config.mode === 'verbose') {
return { prettier, biome }
}

const result: Formatters[] = []

if (biome !== null) {
Expand Down
6 changes: 5 additions & 1 deletion packages/scanner/src/helpers/get-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export function getLinters({
biome,
eslint,
config,
}: { biome: BiomeConfig | null; eslint: ESLintConfig | null; config: ConfigReport }): Linters[] | null {
}: { biome: BiomeConfig | null; eslint: ESLintConfig | null; config: ConfigReport }) {
if (config.mode === 'verbose') {
return { eslint, biome }
}

const result: Linters[] = []

if (biome !== null) {
Expand Down
6 changes: 5 additions & 1 deletion packages/scanner/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ export async function generateReport(files: string[], config?: ConfigReport): Pr
let parseConfig: ConfigReport

if (!config) {
parseConfig = { root: process.cwd(), checkDependencies: !TEST_MODE }
parseConfig = { root: process.cwd(), checkDependencies: !TEST_MODE, mode: 'quite' }
} else {
parseConfig = config

if (config.mode === undefined) {
parseConfig.mode = 'quite'
}

if (config.checkDependencies === undefined) {
parseConfig.checkDependencies = !TEST_MODE
}
Expand Down
7 changes: 5 additions & 2 deletions packages/scanner/src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { BiomeConfig, ESLintConfig, PrettierConfig } from './configs'

export type PackageManager = 'pnpm' | 'npm' | 'yarn' | 'bun' | 'deno'

export type Languages = 'javascript' | 'typescript'
Expand Down Expand Up @@ -27,8 +29,8 @@ export interface Project extends Package {
export interface Package {
name?: string
languages: Languages[] | null
linters: Linters[] | null
formatter: Formatters[] | null
linters: Linters[] | null | { biome: BiomeConfig | null; eslint: ESLintConfig | null }
formatter: Formatters[] | null | { biome: BiomeConfig | null; prettier: PrettierConfig | null }
// css: string[]
// frameworks: string[]
// database: string
Expand All @@ -46,6 +48,7 @@ export interface PackageJson {

export interface ConfigReport {
root: string
mode?: 'verbose' | 'quite'
checkContent?: boolean
checkDependencies?: boolean
}
25 changes: 24 additions & 1 deletion packages/scanner/tests/formatters/getFormatter.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getFormatters } from '@/helpers'
import type { ConfigReport } from '@/types'
import { describe, expect, it, vi } from 'vitest'

vi.stubEnv('NODE_ENV', 'production')

describe('getFormatters()', () => {
describe('mode: quite', () => {
describe('biome', () => {
it('should return ["biome"] when biome is installed and formatter is true and checkDependencies(true)', () => {
const report = getFormatters({
Expand Down Expand Up @@ -164,3 +165,25 @@ describe('getFormatters()', () => {
expect(result).toBeNull()
})
})

describe('mode: verbose', () => {
it('should return both prettier and biome when config.mode is "verbose"', () => {
const biome = { path: '/path/to/biome', installed: true, linter: true }
const prettier = { config: false, installed: true }
const config: ConfigReport = { mode: 'verbose', checkDependencies: true, checkContent: true, root: './project' }

const result = getFormatters({ biome, prettier, config })

expect(result).toEqual({ prettier, biome })
})

it('should return null when no linters meet criteria and config.mode is not "verbose"', () => {
const biome = { installed: true, linter: false }
const prettier = { config: false, installed: false }
const config: ConfigReport = { mode: 'quite', checkDependencies: false, checkContent: false, root: 'project' }

const result = getFormatters({ biome, prettier, config })

expect(result).toBeNull()
})
})
24 changes: 23 additions & 1 deletion packages/scanner/tests/linters/getLinters.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { getLinters } from '@/helpers'
import type { ConfigReport } from '@/types'
import { describe, expect, it, vi } from 'vitest'

vi.stubEnv('NODE_ENV', 'production')

describe('getLinters()', () => {
describe('mode: quite', () => {
describe('biome', () => {
it('should return ["biome"] when biome is installed and linter is enabled and checkDependencies(true)', () => {
const report = getLinters({
Expand Down Expand Up @@ -164,3 +165,24 @@ describe('getLinters()', () => {
expect(result).toBeNull()
})
})
describe('mode: verbose', () => {
it('should return both eslint and biome when config.mode is "verbose"', () => {
const biome = { path: '/path/to/biome', installed: true, linter: true }
const eslint = { config: false, installed: true }
const config: ConfigReport = { mode: 'verbose', checkDependencies: true, checkContent: true, root: './project' }

const result = getLinters({ biome, eslint, config })

expect(result).toEqual({ eslint, biome })
})

it('should return null when no linters meet criteria and config.mode is not "verbose"', () => {
const biome = { installed: true, linter: false }
const eslint = { config: false, installed: false }
const config: ConfigReport = { mode: 'quite', checkDependencies: false, checkContent: false, root: 'project' }

const result = getLinters({ biome, eslint, config })

expect(result).toBeNull()
})
})

0 comments on commit 74f8288

Please sign in to comment.