Skip to content

Commit

Permalink
Merge pull request #82 from AElfProject/fix/assert
Browse files Browse the repository at this point in the history
Fix/assert
  • Loading branch information
hzz780 authored Jul 30, 2024
2 parents bc17b9a + be9a27d commit 8094480
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aelf-command",
"version": "0.1.47-beta.8",
"version": "0.1.47-beta.9",
"description": "A CLI tools for AElf",
"main": "src/index.js",
"type": "module",
Expand Down
25 changes: 24 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,37 @@
*/
import { Command } from 'commander';
import chalk from 'chalk';
// @ts-ignore
import updateNotifier from 'update-notifier';
import check from 'check-node-version';
import { execSync } from 'child_process';
import commands from './command/index.js';
import RC from './rc/index.js';
import pkg from '../package.json' assert { type: 'json' };
import { readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import path from 'path';
import { logger } from './utils/myLogger.js';
import { userHomeDir } from './utils/userHomeDir.js';

const minVersion = '10.9.0';

export function getPackageJson() {
let dirname;
try {
// for test as we cannot use import.meta.url in Jest
dirname = __dirname;
} catch {
const __filename = fileURLToPath(import.meta.url);
dirname = path.dirname(__filename);
}
const filePath = path.resolve(dirname, '../package.json');
const data = readFileSync(filePath, 'utf-8');
const packageJson = JSON.parse(data);
return packageJson;
}

function init(options) {
const pkg = getPackageJson();
const commander = new Command();
// Configuration for test
if (options?.exitOverride) {
Expand Down Expand Up @@ -44,7 +63,9 @@ function init(options) {
});
commander.command('*').action(() => {
// change into help
// @ts-ignore
logger.warn('not a valid command\n');
// @ts-ignore
logger.info(execSync('aelf-command -h').toString());
});
const isTest = process.env.NODE_ENV === 'test';
Expand All @@ -67,6 +88,7 @@ function init(options) {
function run(args, options) {
check({ node: `>= ${minVersion}` }, (error, results) => {
if (error) {
// @ts-ignore
logger.error(error);
return;
}
Expand All @@ -77,6 +99,7 @@ function run(args, options) {
isTest ? { from: 'user' } : undefined
);
} else {
// @ts-ignore
logger.error('Your Node.js version is needed to >= %s', minVersion);
}
});
Expand Down
13 changes: 12 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import check from 'check-node-version';
import updateNotifier from 'update-notifier';
import { logger } from '../src/utils/myLogger.js';
import pkg from '../package.json' assert { type: 'json' };
import { getPackageJson } from '../src/index.js';

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

Expand All @@ -15,7 +15,18 @@ jest.mock('child_process', () => {
};
});
jest.mock('../src/utils/myLogger.js');

describe('test get packagon json', () => {
test('should return correct packagon json', () => {
const pkg = getPackageJson();
expect(pkg.name).toBe('aelf-command');
});
});
describe('test index', () => {
let pkg;
beforeEach(() => {
pkg = getPackageJson();
});
afterEach(() => {
// Restore any mocks
jest.restoreAllMocks();
Expand Down

0 comments on commit 8094480

Please sign in to comment.