Skip to content

Commit

Permalink
refactor: error management
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored and hyf0 committed Mar 20, 2024
1 parent 7abf29a commit 872d974
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
5 changes: 2 additions & 3 deletions packages/cli/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jitiFactory from 'jiti'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { ERR_UNSUPPORTED_CONFIG_FORMAT } from './errors.js'

const __filename = fileURLToPath(import.meta.url)

Expand All @@ -12,9 +13,7 @@ const __filename = fileURLToPath(import.meta.url)
*/
export function loadConfig(configPath) {
if (!isSupportedFormat(configPath)) {
throw new Error(
`Unsupported config format. please use '.js', '.mjs' and '.ts' format`,
)
throw new Error(ERR_UNSUPPORTED_CONFIG_FORMAT)
}
return lazyJiti()(configPath)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/lib/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const ERR_CLI_META_DATA = 'cli meta data error'
export const ERR_UNSUPPORTED_CONFIG_FORMAT =
"Unsupported config format. please use '.js', '.mjs' and '.ts' format"
3 changes: 2 additions & 1 deletion packages/cli/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { ERR_CLI_META_DATA } from './errors.js'

/**
* get package.json
Expand All @@ -13,7 +14,7 @@ export function getPackageJSON(target) {
const raw = readFileSync(path.join(target, 'package.json'), 'utf8')
const pkg = JSON.parse(raw)
if (!pkg.name || !pkg.version || !pkg.description) {
throw new Error('cli meta data error')
throw new Error(ERR_CLI_META_DATA)
}
return pkg
}
7 changes: 5 additions & 2 deletions packages/cli/test/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { describe, test } from 'node:test'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { loadConfig } from '../lib/config.js'
import { ERR_UNSUPPORTED_CONFIG_FORMAT } from '../lib/errors.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

describe('loadConfig', () => {
const RE_ERR = RegExp(ERR_UNSUPPORTED_CONFIG_FORMAT)

test('js', () => {
const config = loadConfig(
path.resolve(__dirname, 'fixtures/rolldown.config.js'),
Expand Down Expand Up @@ -38,12 +41,12 @@ describe('loadConfig', () => {
test('cjs', () => {
assert.throws(() => {
loadConfig(path.resolve(__dirname, 'fixtures/rolldown.config.cjs'))
}, /Unsupported config format. please use '.js', '.mjs' and '.ts' format/)
}, RE_ERR)
})

test('other format', () => {
assert.throws(() => {
loadConfig(path.join(__dirname, 'fixtures/rolldown.config.json'))
}, /Unsupported config format. please use '.js', '.mjs' and '.ts' format/)
}, RE_ERR)
})
})
3 changes: 2 additions & 1 deletion packages/cli/test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, test } from 'node:test'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { getPackageJSON } from '../lib/utils.js'
import { ERR_CLI_META_DATA } from '../lib/errors.js'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

Expand All @@ -17,7 +18,7 @@ describe('utils', () => {
test('invalid package.json', () => {
assert.throws(() => {
getPackageJSON(path.resolve(__dirname, './fixtures'))
}, /cli meta data error/)
}, new RegExp(ERR_CLI_META_DATA))
})

test('failure', () => {
Expand Down

0 comments on commit 872d974

Please sign in to comment.