44 */
55import { Command } from 'commander' ;
66import chalk from 'chalk' ;
7+ // @ts -ignore
78import updateNotifier from 'update-notifier' ;
89import check from 'check-node-version' ;
910import { execSync } from 'child_process' ;
1011import commands from './command/index.js' ;
1112import RC from './rc/index.js' ;
12- import pkg from '../package.json' assert { type : 'json ' } ;
13+ import { readFileSync } from 'fs' ;
14+ import { fileURLToPath } from 'url' ;
15+ import path from 'path' ;
1316import { logger } from './utils/myLogger.js' ;
1417import { userHomeDir } from './utils/userHomeDir.js' ;
1518
1619const minVersion = '10.9.0' ;
1720
21+ export function getPackageJson ( ) {
22+ let dirname ;
23+ try {
24+ // for test as we cannot use import.meta.url in Jest
25+ dirname = __dirname ;
26+ } catch {
27+ const __filename = fileURLToPath ( import . meta. url ) ;
28+ dirname = path . dirname ( __filename ) ;
29+ }
30+ const filePath = path . resolve ( dirname , '../package.json' ) ;
31+ const data = readFileSync ( filePath , 'utf-8' ) ;
32+ const packageJson = JSON . parse ( data ) ;
33+ return packageJson ;
34+ }
35+
1836function init ( options ) {
37+ const pkg = getPackageJson ( ) ;
1938 const commander = new Command ( ) ;
2039 // Configuration for test
2140 if ( options ?. exitOverride ) {
@@ -44,7 +63,9 @@ function init(options) {
4463 } ) ;
4564 commander . command ( '*' ) . action ( ( ) => {
4665 // change into help
66+ // @ts -ignore
4767 logger . warn ( 'not a valid command\n' ) ;
68+ // @ts -ignore
4869 logger . info ( execSync ( 'aelf-command -h' ) . toString ( ) ) ;
4970 } ) ;
5071 const isTest = process . env . NODE_ENV === 'test' ;
@@ -67,6 +88,7 @@ function init(options) {
6788function run ( args , options ) {
6889 check ( { node : `>= ${ minVersion } ` } , ( error , results ) => {
6990 if ( error ) {
91+ // @ts -ignore
7092 logger . error ( error ) ;
7193 return ;
7294 }
@@ -77,6 +99,7 @@ function run(args, options) {
7799 isTest ? { from : 'user' } : undefined
78100 ) ;
79101 } else {
102+ // @ts -ignore
80103 logger . error ( 'Your Node.js version is needed to >= %s' , minVersion ) ;
81104 }
82105 } ) ;
0 commit comments