Skip to content

Commit 7c6a3a4

Browse files
committed
refactor: export pkg version as constant
1 parent 13d3b1d commit 7c6a3a4

File tree

5 files changed

+14
-16
lines changed

5 files changed

+14
-16
lines changed

src/cli.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import {
2424
minimist,
2525
fs,
2626
path,
27+
VERSION,
2728
} from './index.js'
2829
import { installDeps, parseDeps } from './deps.js'
29-
import { randomId, getVersion } from './util.js'
30+
import { randomId } from './util.js'
3031
import { createRequire } from './vendor.js'
3132

3233
const EXT = '.mjs'
@@ -44,7 +45,7 @@ isMain() &&
4445
export function printUsage() {
4546
// language=txt
4647
console.log(`
47-
${chalk.bold('zx ' + getVersion())}
48+
${chalk.bold('zx ' + VERSION)}
4849
A tool for writing better scripts
4950
5051
${chalk.bold('Usage')}
@@ -95,7 +96,7 @@ export async function main() {
9596
if (argv.prefix) $.prefix = argv.prefix
9697
if (argv.postfix) $.postfix = argv.postfix
9798
if (argv.version) {
98-
console.log(getVersion())
99+
console.log(VERSION)
99100
return
100101
}
101102
if (argv.help) {

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import { ProcessPromise } from './core.js'
16+
import { fs } from './vendor.js'
1617

1718
export * from './core.js'
1819
export * from './goods.js'
@@ -27,9 +28,13 @@ export {
2728
glob as globby,
2829
} from './vendor.js'
2930

31+
export const VERSION = fs.readJsonSync(
32+
new URL('../package.json', import.meta.url)
33+
).version
34+
export const version = VERSION
35+
3036
export {
3137
type Duration,
32-
getVersion,
3338
quote,
3439
quotePowerShell,
3540
tempdir,

src/util.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import os from 'node:os'
1616
import path from 'node:path'
1717
import fs from 'node:fs'
18-
import { createRequire } from './vendor.js'
1918
import { chalk } from './vendor-core.js'
2019

2120
export { isStringLiteral } from './vendor-core.js'
@@ -464,10 +463,6 @@ export const proxyOverride = <T extends object>(
464463
},
465464
}) as T
466465

467-
export function getVersion(): string {
468-
return createRequire(import.meta.url)('../package.json').version
469-
}
470-
471466
// https://stackoverflow.com/a/7888303
472467
export const camelToSnake = (str: string) =>
473468
str

test/index.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { describe, test } from 'node:test'
1717
import {
1818
nothrow,
1919
quiet,
20+
version,
21+
VERSION,
2022
$,
2123
log,
2224
cd,
@@ -62,6 +64,8 @@ describe('index', () => {
6264
// index
6365
assert(nothrow)
6466
assert(quiet)
67+
assert(version)
68+
assert.equal(version, VERSION)
6569

6670
// core
6771
assert($)

test/util.test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import {
2828
randomId,
2929
// normalizeMultilinePieces,
3030
getCallerLocationFromString,
31-
getVersion,
3231
tempdir,
3332
tempfile,
3433
preferLocalBin,
@@ -195,12 +194,6 @@ test('preferLocalBin()', () => {
195194
)
196195
})
197196

198-
test('getVersion return version from package.json', () => {
199-
const packageJsonContent = fs.readFileSync('./package.json', 'utf8')
200-
const packageInfo = JSON.parse(packageJsonContent)
201-
202-
assert.equal(getVersion(), packageInfo.version)
203-
204197
test('camelToSnake()', () => {
205198
assert.equal(camelToSnake('verbose'), 'VERBOSE')
206199
assert.equal(camelToSnake('nothrow'), 'NOTHROW')

0 commit comments

Comments
 (0)