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

fix: support --set boolean and numberic params #20

Merged
merged 2 commits into from
Sep 23, 2023
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Options:
-f, --filename <filename> filename to tsconfig (default: "tsconfig.json")
-c, --cwd <cwd> cwd (default: process.cwd())
-o, --output <output> output file (default: stdout)
-s, --set <name>=<value> set additional swcrc options
-h, --help display help for command
```

Expand Down
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
}
32 changes: 32 additions & 0 deletions test/cli.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,36 @@ 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, /"externalHelpers": true/)
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"/)
})
})
Loading