Skip to content

Commit

Permalink
feat: add cli overrides param
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 21, 2023
1 parent 093b86e commit 53eec43
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import { parseArgs } from 'node:util'
import { writeFile } from 'node:fs'
import type swcType from '@swc/core'
import { convert } from './index'

const {
values: { filename, cwd, output, help },
values: { filename, cwd, output, help, set: overrideValues },
} = parseArgs({
options: {
filename: {
Expand All @@ -22,6 +23,11 @@ const {
type: 'string',
short: 'o',
},
set: {
type: 'string',
short: 's',
multiple: true,
},
help: {
type: 'boolean',
short: 'h',
Expand All @@ -39,13 +45,26 @@ 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
`)

process.exit(0)
}

const swcConfig = convert(filename, cwd)
const overrides = overrideValues?.reduce((all, a) => {
const [prop, value] = a.split("=", 2)
const props = prop.split(".")
const parents = props.slice(0, -1)
const key = props[props.length - 1]
const parent = parents.reduce((o, s) => o[s] ??= {}, all)

parent[key] = value

return all
}, {} as any) as swcType.Options

const swcConfig = convert(filename, cwd, overrides)

if (output) {
writeFile(output, JSON.stringify(swcConfig, null, 2), (err) => {
Expand Down
19 changes: 19 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,23 @@ describe.concurrent('cli', () => {
expect(result.status).toBe(0)
expect(result.stdout.toString()).toMatch(/"target": "es2018"/)
})

it('should convert --filename', ({ expect }) => {
const result = spawnSync('node', ['dist/cli.js', '--filename', path.resolve(__dirname, 'fixtures', 'tsconfig', 'tsconfig-es6.json')])
expect(result.status).toBe(0)
expect(result.stdout.toString()).toMatch(/"target": "es2015"/)
})

it('should convert tsconfig.json with additions', ({ expect }) => {
const result = spawnSync('node', ['dist/cli.js', '--set', 'module.target=es2015'])
expect(result.status).toBe(0)
expect(result.stdout.toString()).toMatch(/"target": "es2015"/)
})

it('should convert tsconfig.json with additions', ({ expect }) => {
const result = spawnSync('node', ['dist/cli.js', '--filename', path.resolve(__dirname, 'fixtures', 'tsconfig', 'tsconfig-es2022.json'), '--set', 'module.target=es2015', '--set', 'module.type=commonjs'])
expect(result.status).toBe(0)
expect(result.stdout.toString()).toMatch(/"target": "es2015"/)
expect(result.stdout.toString()).toMatch(/"type": "commonjs"/)
})
})

0 comments on commit 53eec43

Please sign in to comment.