Skip to content

Commit 8094480

Browse files
authored
Merge pull request #82 from AElfProject/fix/assert
Fix/assert
2 parents bc17b9a + be9a27d commit 8094480

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aelf-command",
3-
"version": "0.1.47-beta.8",
3+
"version": "0.1.47-beta.9",
44
"description": "A CLI tools for AElf",
55
"main": "src/index.js",
66
"type": "module",

src/index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,37 @@
44
*/
55
import { Command } from 'commander';
66
import chalk from 'chalk';
7+
// @ts-ignore
78
import updateNotifier from 'update-notifier';
89
import check from 'check-node-version';
910
import { execSync } from 'child_process';
1011
import commands from './command/index.js';
1112
import 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';
1316
import { logger } from './utils/myLogger.js';
1417
import { userHomeDir } from './utils/userHomeDir.js';
1518

1619
const 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+
1836
function 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) {
6788
function 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
});

test/index.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'path';
33
import check from 'check-node-version';
44
import updateNotifier from 'update-notifier';
55
import { logger } from '../src/utils/myLogger.js';
6-
import pkg from '../package.json' assert { type: 'json' };
6+
import { getPackageJson } from '../src/index.js';
77

88
const commandBin = path.resolve(__dirname, '../bin/aelf-command.js');
99

@@ -15,7 +15,18 @@ jest.mock('child_process', () => {
1515
};
1616
});
1717
jest.mock('../src/utils/myLogger.js');
18+
19+
describe('test get packagon json', () => {
20+
test('should return correct packagon json', () => {
21+
const pkg = getPackageJson();
22+
expect(pkg.name).toBe('aelf-command');
23+
});
24+
});
1825
describe('test index', () => {
26+
let pkg;
27+
beforeEach(() => {
28+
pkg = getPackageJson();
29+
});
1930
afterEach(() => {
2031
// Restore any mocks
2132
jest.restoreAllMocks();

0 commit comments

Comments
 (0)