Skip to content

Commit

Permalink
refactor: handle extensions without dot
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Nov 8, 2024
1 parent 4d2bd15 commit 76991e8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{
"name": "all",
"path": "build/*",
"limit": "833.1 kB",
"limit": "833.6 kB",
"brotli": false,
"gzip": false
}
Expand Down
7 changes: 7 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const argv = minimist(process.argv.slice(2), {

export async function main() {
await import('./globals.js')
argv.ext = normalizeExt(argv.ext)
if (argv.cwd) $.cwd = argv.cwd
if (argv.verbose) $.verbose = true
if (argv.quiet) $.quiet = true
Expand Down Expand Up @@ -302,3 +303,9 @@ export function isMain(

return false
}

export function normalizeExt(ext?: string) {
if (!ext) return
if (!/^\.?\w+(\.\w+)*$/.test(ext)) throw new Error(`Invalid extension ${ext}`)
return ext[0] === '.' ? ext : `.${ext}`
}
9 changes: 8 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import assert from 'node:assert'
import { test, describe, before, after } from 'node:test'
import { fileURLToPath } from 'node:url'
import '../build/globals.js'
import { isMain } from '../build/cli.js'
import { isMain, normalizeExt } from '../build/cli.js'

const __filename = fileURLToPath(import.meta.url)
const spawn = $.spawn
Expand Down Expand Up @@ -267,4 +267,11 @@ describe('cli', () => {
}
})
})

test('normalizeExt()', () => {
assert.equal(normalizeExt('.ts'), '.ts')
assert.equal(normalizeExt('ts'), '.ts')
assert.equal(normalizeExt(), undefined)
assert.throws(() => normalizeExt('.'))
})
})

0 comments on commit 76991e8

Please sign in to comment.