Skip to content

Commit aacd0fe

Browse files
committed
Consistent handling of argv parameters
1 parent 4959c9e commit aacd0fe

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const expandGlob = async (patterns) => {
3333
}
3434

3535
let input = argv._
36-
const { dir, output } = argv
36+
const { dir, output, replace } = argv
3737

3838
if (argv.map) argv.map = { inline: false }
3939

@@ -85,7 +85,7 @@ const cliConfig = {
8585
: [],
8686
}
8787

88-
if (argv.watch && !(argv.output || argv.replace || argv.dir)) {
88+
if (argv.watch && !(output || replace || dir)) {
8989
error('Cannot write to stdout in watch mode')
9090
}
9191

@@ -96,11 +96,11 @@ if (input && input.length) {
9696
error('Input Error: You must pass a valid list of files to parse')
9797
}
9898

99-
if (input.length > 1 && !argv.dir && !argv.replace) {
99+
if (input.length > 1 && !dir && !replace) {
100100
error('Input Error: Must use --dir or --replace with multiple input files')
101101
}
102102
} else {
103-
if (argv.replace || argv.dir) {
103+
if (replace || dir) {
104104
error('Input Error: Cannot use --dir or --replace when reading from stdin')
105105
}
106106

@@ -203,32 +203,30 @@ async function css(css, file) {
203203
basename: path.basename(file),
204204
extname: path.extname(file),
205205
}
206-
207-
if (!argv.config) argv.config = path.dirname(file)
208206
}
209207

210208
const relativePath =
211209
file !== 'stdin' ? path.relative(path.resolve(), file) : file
212210

213-
if (!argv.config) argv.config = process.cwd()
211+
const configDir = argv.config || ctx.file?.dirname || process.cwd()
214212

215213
const time = process.hrtime()
216214

217215
printVerbose(pc.cyan(`Processing ${pc.bold(relativePath)}...`))
218216

219-
const config = (await rc(ctx, argv.config)) || cliConfig
217+
const config = (await rc(ctx, configDir)) || cliConfig
220218
const options = { ...config.options }
221219

222220
if (file === 'stdin' && output) file = output
223221

224222
// TODO: Unit test this
225223
options.from = file === 'stdin' ? path.join(process.cwd(), 'stdin') : file
226224

227-
if (output || dir || argv.replace) {
225+
if (output || dir || replace) {
228226
const base = argv.base
229227
? file.replace(path.resolve(argv.base), '')
230228
: path.basename(file)
231-
options.to = output || (argv.replace ? file : path.join(dir, base))
229+
options.to = output || (replace ? file : path.join(dir, base))
232230

233231
if (argv.ext) {
234232
options.to = options.to.replace(path.extname(options.to), argv.ext)

0 commit comments

Comments
 (0)