Skip to content

Commit 27f3c2b

Browse files
authoredNov 27, 2023
fix: respect config file when no explicit cli flags (#88)
1 parent 0402966 commit 27f3c2b

File tree

7 files changed

+84
-43
lines changed

7 files changed

+84
-43
lines changed
 

‎src/cli.ts

+15-26
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,30 @@ import c from 'picocolors'
66
import { version } from '../package.json'
77
import { check } from './commands/check'
88
import { usage } from './commands/usage'
9+
import { resolveConfig } from './config'
10+
import { LOG_LEVELS, MODE_CHOICES } from './constants'
911
import type { CommonOptions } from './types'
10-
import { LOGLEVELS, resolveConfig } from './config'
11-
import type { SortOption } from './utils/sort'
12+
import { SORT_CHOICES } from './utils/sort'
1213
import { checkGlobal } from './commands/check/checkGlobal'
1314

1415
function commonOptions(args: Argv<object>): Argv<CommonOptions> {
1516
return args
1617
.option('cwd', {
1718
alias: 'C',
18-
default: '',
1919
type: 'string',
2020
describe: 'specify the current working directory',
2121
})
2222
.option('loglevel', {
23-
default: 'info',
2423
type: 'string',
2524
describe: 'log level',
26-
choices: LOGLEVELS,
25+
choices: LOG_LEVELS,
2726
})
2827
.option('failOnOutdated', {
2928
type: 'boolean',
3029
describe: 'exit with code 1 if outdated dependencies are found',
3130
})
3231
.option('silent', {
3332
alias: 's',
34-
default: false,
3533
type: 'boolean',
3634
describe: 'complete silent',
3735
})
@@ -45,12 +43,6 @@ function commonOptions(args: Argv<object>): Argv<CommonOptions> {
4543
type: 'boolean',
4644
describe: 'force fetching from server, bypass cache',
4745
})
48-
.option('sort', {
49-
type: 'string',
50-
default: 'diff-asc' as SortOption,
51-
choices: ['time-asc', 'time-desc', 'diff-asc', 'diff-desc', 'name-asc', 'name-desc', 'time', 'diff', 'name'],
52-
describe: 'sort by most outdated absolute or relative to dependency',
53-
})
5446
.option('ignore-paths', {
5547
type: 'string',
5648
describe: 'ignore paths for search package.json',
@@ -77,11 +69,6 @@ function commonOptions(args: Argv<object>): Argv<CommonOptions> {
7769
describe: 'update only for dependencies',
7870
conflicts: ['dev'],
7971
})
80-
.option('include-locked', {
81-
alias: 'l',
82-
type: 'boolean',
83-
describe: 'include locked dependencies & devDependencies',
84-
})
8572
}
8673

8774
// eslint-disable-next-line no-unused-expressions
@@ -96,7 +83,6 @@ yargs(hideBin(process.argv))
9683
.option('detail', {
9784
alias: 'a',
9885
type: 'boolean',
99-
default: false,
10086
describe: 'show more info',
10187
})
10288
.help()
@@ -110,47 +96,50 @@ yargs(hideBin(process.argv))
11096
(args) => {
11197
return commonOptions(args)
11298
.positional('mode', {
113-
default: 'default',
11499
type: 'string',
115100
describe: 'the mode how version range resolves, can be "default", "major", "minor", "latest" or "newest"',
116-
choices: ['default', 'major', 'minor', 'patch', 'latest', 'newest'],
101+
choices: MODE_CHOICES,
117102
})
118103
.option('write', {
119104
alias: 'w',
120-
default: false,
121105
type: 'boolean',
122106
describe: 'write to package.json',
123107
})
124108
.option('global', {
125109
alias: 'g',
126-
default: false,
127110
type: 'boolean',
128111
describe: 'update global packages',
129112
})
130113
.option('interactive', {
131114
alias: 'I',
132-
default: false, // TODO: enable by default: !process.env.CI && process.stdout.isTTY,
133115
type: 'boolean',
134116
describe: 'interactive mode',
135117
})
136118
.option('install', {
137119
alias: 'i',
138-
default: false,
139120
type: 'boolean',
140121
describe: 'install directly after bumping',
141122
})
142123
.option('update', {
143124
alias: 'u',
144-
default: false,
145125
type: 'boolean',
146126
describe: 'update directly after bumping',
147127
})
148128
.option('all', {
149129
alias: 'a',
150-
default: false,
151130
type: 'boolean',
152131
describe: 'show all packages up to date info',
153132
})
133+
.option('sort', {
134+
type: 'string',
135+
choices: SORT_CHOICES,
136+
describe: 'sort by most outdated absolute or relative to dependency',
137+
})
138+
.option('includeLocked', {
139+
alias: 'l',
140+
type: 'boolean',
141+
describe: 'include locked dependencies & devDependencies',
142+
})
154143
.help()
155144
},
156145
async (args) => {

‎src/config.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import _debug from 'debug'
44
import { createConfigLoader } from 'unconfig'
55
import type { CommonOptions } from './types'
66
import { toArray } from './utils/toArray'
7+
import { DEFAULT_CHECK_OPTIONS, DEFAULT_USAGE_OPTIONS } from './constants'
78

89
const debug = _debug('taze:config')
910

10-
export const LOGLEVELS = ['debug', 'info', 'warn', 'error', 'silent']
11-
1211
function normalizeConfig<T extends CommonOptions>(options: T) {
1312
options.ignorePaths = toArray(options.ignorePaths)
1413
options.exclude = toArray(options.exclude)
@@ -20,7 +19,10 @@ function normalizeConfig<T extends CommonOptions>(options: T) {
2019
return options
2120
}
2221

23-
export async function resolveConfig<T extends CommonOptions>(options: T): Promise<T> {
22+
export async function resolveConfig<T extends CommonOptions>(
23+
options: T & { _?: (string | number)[] },
24+
): Promise<T> {
25+
const defaults = options?._?.[0] === 'usage' ? DEFAULT_USAGE_OPTIONS : DEFAULT_CHECK_OPTIONS
2426
options = normalizeConfig(options)
2527

2628
const loader = createConfigLoader<CommonOptions>({
@@ -44,10 +46,10 @@ export async function resolveConfig<T extends CommonOptions>(options: T): Promis
4446
const config = await loader.load()
4547

4648
if (!config.sources.length)
47-
return options
49+
return deepmerge(defaults, options as T) as T
4850

4951
debug(`config file found ${config.sources[0]}`)
5052
const configOptions = normalizeConfig(config.config)
5153

52-
return deepmerge(configOptions, options) as T
54+
return deepmerge(deepmerge(defaults, configOptions), options as T) as T
5355
}

‎src/constants.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { CheckOptions, CommonOptions, UsageOptions } from './types'
2+
3+
export const LOG_LEVELS = ['debug', 'info', 'warn', 'error', 'silent'] as const
4+
5+
export const MODE_CHOICES = ['default', 'major', 'minor', 'patch', 'latest', 'newest'] as const
6+
7+
export const DEFAULT_COMMON_OPTIONS: CommonOptions = {
8+
cwd: '',
9+
loglevel: 'info',
10+
failOnOutdated: false,
11+
silent: false,
12+
recursive: false,
13+
force: false,
14+
ignorePaths: '',
15+
include: '',
16+
exclude: '',
17+
dev: false,
18+
prod: false,
19+
}
20+
21+
export const DEFAULT_USAGE_OPTIONS: UsageOptions = {
22+
...DEFAULT_COMMON_OPTIONS,
23+
detail: false,
24+
recursive: true,
25+
}
26+
27+
export const DEFAULT_CHECK_OPTIONS: CheckOptions = {
28+
...DEFAULT_COMMON_OPTIONS,
29+
mode: 'default',
30+
write: false,
31+
global: false,
32+
// TODO: enable by default: !process.env.CI && process.stdout.isTTY,
33+
interactive: false,
34+
install: false,
35+
update: false,
36+
all: false,
37+
sort: 'diff-asc',
38+
includeLocked: false,
39+
}

‎src/io/packages.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function writePackage(pkg: PackageMeta, options: CommonOptions) {
4848
}
4949

5050
export async function loadPackage(relative: string, options: CommonOptions, shouldUpdate: (name: string) => boolean): Promise<PackageMeta> {
51-
const filepath = path.resolve(options.cwd, relative)
51+
const filepath = path.resolve(options.cwd ?? '', relative)
5252
const raw = await readJSON(filepath)
5353
let deps: RawDep[] = []
5454

‎src/log.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import process from 'node:process'
22
import c from 'picocolors'
33
import { MultiBar, Presets } from 'cli-progress'
4-
import { LOGLEVELS } from './config'
4+
import { LOG_LEVELS } from './constants'
55
import { visualLength, visualPadEnd, visualPadStart } from './render'
6+
import type { LogLevel } from './types'
67

78
interface Options {
89
columns: number
910
pending: number
1011
align: string
11-
loglevel: string
12+
loglevel: LogLevel
1213
}
1314

14-
export function shouldLog(level: string, messageLevel: string) {
15-
return LOGLEVELS.indexOf(level) <= LOGLEVELS.indexOf(messageLevel)
15+
export function shouldLog(level: LogLevel, messageLevel: LogLevel) {
16+
return LOG_LEVELS.indexOf(level) <= LOG_LEVELS.indexOf(messageLevel)
1617
}
1718

1819
export class TableLogger {

‎src/types.ts

+9-7
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,32 @@ export interface ResolvedDepChange extends RawDep {
4141
aliasName?: string
4242
}
4343

44+
export type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'
45+
4446
export interface CommonOptions {
45-
cwd: string
47+
cwd?: string
4648
recursive?: boolean
4749
ignorePaths?: string | string[]
4850
include?: string | string[]
4951
exclude?: string | string[]
5052
prod?: boolean
5153
dev?: boolean
52-
loglevel?: string
54+
loglevel?: LogLevel
5355
failOnOutdated?: boolean
5456
silent?: boolean
5557
force?: boolean
5658
packageMode?: { [name: string]: PackageMode }
5759
}
5860

5961
export interface UsageOptions extends CommonOptions {
60-
detail: boolean
61-
recursive: true
62+
detail?: boolean
63+
recursive?: true
6264
}
6365

6466
export interface CheckOptions extends CommonOptions {
65-
mode: string
66-
write: boolean
67-
all: boolean
67+
mode?: RangeMode
68+
write?: boolean
69+
all?: boolean
6870
sort?: SortOption
6971
interactive?: boolean
7072
install?: boolean

‎src/utils/sort.ts

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ export type SortKey = 'time' | 'diff' | 'name'
66
export type SortOrder = 'asc' | 'desc'
77

88
export type SortOption = `${SortKey}-${SortOrder}`
9+
export const SORT_CHOICES = [
10+
'time-asc',
11+
'time-desc',
12+
'diff-asc',
13+
'diff-desc',
14+
'name-asc',
15+
'name-desc',
16+
] as const
917

1018
export function parseSortOption(option: SortOption) {
1119
return option.split('-') as [SortKey, SortOrder]

0 commit comments

Comments
 (0)
Please sign in to comment.