Skip to content

Commit

Permalink
fix: support --set boolean params
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 23, 2023
1 parent b5181e2 commit babdfce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { writeFile } from 'node:fs'
import { parseArgs } from 'node:util'
import type swcType from '@swc/core'
import { convert } from './index'
import { parseParamValue } from './utils'

const {
values: { filename, cwd, output, help, set: overrideValues },
Expand Down Expand Up @@ -62,7 +63,7 @@ const overrides = overrideValues?.reduce((all, a) => {
return o[s]
}, all)

parent[key] = value
parent[key] = parseParamValue(prop, value)

return all
}, {} as any) as swcType.Options
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,13 @@ function resolveFile(
return null
}
}

export function parseParamValue(prop: string, value: string) {
if (value === "true" || value === "false") {
return value === "true"
}
if (!isNaN(Number(value))) {
return Number(value)
}
return value
}
31 changes: 31 additions & 0 deletions test/cli.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,35 @@ describe('cli', { concurrency: true }, () => {
match(stdout, /"target": "es2015"/)
match(stdout, /"type": "commonjs"/)
})

it('should convert tsconfig.json with boolean additions', async () => {
const { stdout, stderr } = await pExe('node', [
'dist/cli.js',
'--set',
'jsc.externalHelpers=true',
'--set',
'jsc.test2=false',
])
strictEqual(stderr, '')
match(stdout, /"test2": false/)
})

it('should convert tsconfig.json with numeric additions', async () => {
const { stdout, stderr } = await pExe('node', [
'dist/cli.js',
'--set',
'jsc.num1=1',
'--set',
'jsc.num2=2',
'--set',
'jsc.not1=1x',
'--set',
'jsc.not2=x2',
])
strictEqual(stderr, '')
match(stdout, /"num1": 1/)
match(stdout, /"num2": 2/)
match(stdout, /"not1": "1x"/)
match(stdout, /"not2": "x2"/)
})
})

0 comments on commit babdfce

Please sign in to comment.