Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/assert #82

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading