Commit 88b8400

Nikolay <shevnv@yandex-team.ru>
2024-12-18 15:06:32
feat: export pkg version from index (#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
Changed files (3)
src/cli.ts
@@ -24,6 +24,7 @@ import {
   minimist,
   fs,
   path,
+  VERSION,
 } from './index.js'
 import { installDeps, parseDeps } from './deps.js'
 import { randomId } from './util.js'
@@ -44,7 +45,7 @@ isMain() &&
 export function printUsage() {
   // language=txt
   console.log(`
- ${chalk.bold('zx ' + getVersion())}
+ ${chalk.bold('zx ' + VERSION)}
    A tool for writing better scripts
 
  ${chalk.bold('Usage')}
@@ -95,7 +96,7 @@ export async function main() {
   if (argv.prefix) $.prefix = argv.prefix
   if (argv.postfix) $.postfix = argv.postfix
   if (argv.version) {
-    console.log(getVersion())
+    console.log(VERSION)
     return
   }
   if (argv.help) {
@@ -294,10 +295,6 @@ export function transformMarkdown(buf: Buffer): string {
   return output.join('\n')
 }
 
-export function getVersion(): string {
-  return createRequire(import.meta.url)('../package.json').version
-}
-
 export function isMain(
   metaurl: string = import.meta.url,
   scriptpath: string = process.argv[1]
src/index.ts
@@ -13,6 +13,7 @@
 // limitations under the License.
 
 import { ProcessPromise } from './core.js'
+import { fs } from './vendor.js'
 
 export * from './core.js'
 export * from './goods.js'
@@ -27,6 +28,11 @@ export {
   glob as globby,
 } from './vendor.js'
 
+export const VERSION = fs.readJsonSync(
+  new URL('../package.json', import.meta.url)
+).version
+export const version = VERSION
+
 export {
   type Duration,
   quote,
test/index.test.js
@@ -17,6 +17,8 @@ import { describe, test } from 'node:test'
 import {
   nothrow,
   quiet,
+  version,
+  VERSION,
   $,
   log,
   cd,
@@ -62,6 +64,8 @@ describe('index', () => {
     // index
     assert(nothrow)
     assert(quiet)
+    assert(version)
+    assert.equal(version, VERSION)
 
     // core
     assert($)