Skip to content

Commit

Permalink
feature: --package-json CLI argument
Browse files Browse the repository at this point in the history
Support explicitly specified package.json in CLI.
  • Loading branch information
jfirebaugh committed Oct 23, 2024
1 parent 6611df1 commit 4ad8537
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
17 changes: 14 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import type swcType from '@swc/core'
import { convert } from './index'

const {
values: { filename, cwd, output, help, set: overrideValues },
values: {
filename,
'package-json': packageJson,
cwd,
output,
help,
set: overrideValues,
},
} = parseArgs({
options: {
filename: {
type: 'string',
short: 'f',
default: 'tsconfig.json',
},
'package-json': {
type: 'string',
},
cwd: {
type: 'string',
short: 'c',
Expand Down Expand Up @@ -42,7 +52,8 @@ Usage: tsconfig-to-swcconfig [options]
Alias: t2s [options]
Options:
-f, --filename <filename> filename to tsconfig (default: "tsconfig.json")
-f, --filename <filename> path to tsconfig (default: "tsconfig.json")
--package-json <package.json> path to package.json (default: search cwd)
-c, --cwd <cwd> cwd (default: "${process.cwd()}")
-o, --output <output> output file (default: stdout)
-s, --set <name>=<value> set additional swcrc options
Expand Down Expand Up @@ -75,7 +86,7 @@ const overrides = overrideValues?.reduce((all, a) => {
return all
}, {} as any) as swcType.Options

const swcConfig = convert(filename, cwd, overrides)
const swcConfig = convert(filename, cwd, overrides, packageJson)

if (output) {
writeFile(output, JSON.stringify(swcConfig, null, 2), (err) => {
Expand Down
23 changes: 8 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ export function convert(
cwd: string = process.cwd(),
/** swc configs to override */
swcOptions?: swcType.Options,
packageJsonPath?: string | undefined,
): swcType.Options {
const tsOptions = getTSOptions(filename, cwd) ?? {}
return convertTsConfig(tsOptions, swcOptions, cwd)
return convertTsConfig(tsOptions, swcOptions, cwd, packageJsonPath)
}

export function convertTsConfig(
tsOptions: TsConfigJson.CompilerOptions,
swcOptions: swcType.Options = {},
cwd: string = process.cwd(),
packageJsonPath?: string | undefined,
): swcType.Options {
// https://json.schemastore.org/tsconfig
const {
Expand Down Expand Up @@ -51,7 +53,7 @@ export function convertTsConfig(
{
sourceMaps: sourceMap,
module: {
type: moduleType(module, cwd),
type: moduleType(module, getPackageJson(packageJsonPath, cwd)),
strictMode: alwaysStrict || !noImplicitUseStrict,
noInterop: !esModuleInterop,
} satisfies swcType.ModuleConfig,
Expand Down Expand Up @@ -95,29 +97,20 @@ type Module = typeof availableModuleTypes[number]

function moduleType(
m: TsConfigJson.CompilerOptions.Module | undefined,
cwd: string = process.cwd(),
packageJson: { type?: string },
): Module {
const module = (m as unknown as string)?.toLowerCase()
if (availableModuleTypes.includes(module as any)) {
return module as Module
}

const es6Modules = [
'es2015',
'es2020',
'es2022',
'esnext',
'none',
] as const
const es6Modules = ['es2015', 'es2020', 'es2022', 'esnext', 'none'] as const
if (es6Modules.includes(module as any)) {
return 'es6'
}

const nodeModules = [
'node16',
'nodenext',
] as const
if (nodeModules.includes(module as any) && getPackageJson(cwd)?.type === 'module') {
const nodeModules = ['node16', 'nodenext'] as const
if (nodeModules.includes(module as any) && packageJson.type === 'module') {
return 'es6'
}

Expand Down
12 changes: 7 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ export function getTSOptions(
return config.compilerOptions ?? null
}

export function getPackageJson(cwd: string = process.cwd()) {
const result = findUpSync('package.json', { cwd })
export function getPackageJson(
packageJsonPath: string | undefined,
cwd: string,
): { type?: string } {
const result = findUpSync(packageJsonPath || 'package.json', { cwd })
if (!result) {
return null
return {}
}

const packageJson = JSON.parse(fs.readFileSync(result, 'utf-8'))
return packageJson
return JSON.parse(fs.readFileSync(result, 'utf-8'))
}

// the following code is copied from https://github.com/sindresorhus/find-up-simple/blob/ec263e63e3198ce3cdadd49decb9940ac9997bf3/index.js#L32C1-L53C2
Expand Down
22 changes: 22 additions & 0 deletions test/cli.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,26 @@ describe('cli', { concurrency: true }, () => {
match(stdout, /"not1": "1x"/)
match(stdout, /"not2": "x2"/)
})

it('should convert tsconfig.json with package.json path', async () => {
const { stdout, stderr } = await pExe('node', [
'dist/cli.js',
'--filename',
resolve(
__dirname,
'fixtures',
'tsconfig-with-package-json',
'tsconfig.json',
),
'--package-json',
resolve(
__dirname,
'fixtures',
'tsconfig-with-package-json',
'package.json',
),
])
strictEqual(stderr, '')
match(stdout, /"type": "es6"/)
})
})

0 comments on commit 4ad8537

Please sign in to comment.