Skip to content

Commit 88b8400

Browse files
shevnvantongolub
andauthored
feat: export pkg version from index (google#986)
* feat: getVersion with version from package.json * refactor: export pkg version as constant --------- Co-authored-by: Anton Golub <antongolub@antongolub.com>
1 parent f65342e commit 88b8400

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

src/cli.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
minimist,
2525
fs,
2626
path,
27+
VERSION,
2728
} from './index.js'
2829
import { installDeps, parseDeps } from './deps.js'
2930
import { randomId } from './util.js'
@@ -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) {
@@ -294,10 +295,6 @@ export function transformMarkdown(buf: Buffer): string {
294295
return output.join('\n')
295296
}
296297

297-
export function getVersion(): string {
298-
return createRequire(import.meta.url)('../package.json').version
299-
}
300-
301298
export function isMain(
302299
metaurl: string = import.meta.url,
303300
scriptpath: string = process.argv[1]

src/index.ts

+6
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,6 +28,11 @@ 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,
3238
quote,

test/index.test.js

+4
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($)

0 commit comments

Comments
 (0)