From 38d8153c5cd72c988c454d37ef28cf2fb26196c1 Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Mon, 29 Jul 2024 15:54:37 +0800 Subject: [PATCH 1/5] feat: unexpected identifier assert --- package.json | 2 +- src/index.js | 2 +- test/index.test.js | 2 +- tsconfig.json | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e7aa117..75b9c48 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-command", - "version": "0.1.47-beta.8", + "version": "0.1.47-alpha.6", "description": "A CLI tools for AElf", "main": "src/index.js", "type": "module", diff --git a/src/index.js b/src/index.js index 8be7716..74a23cf 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,7 @@ 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 pkg from '../package.json'; import { logger } from './utils/myLogger.js'; import { userHomeDir } from './utils/userHomeDir.js'; diff --git a/test/index.test.js b/test/index.test.js index 6d55c16..f07689d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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 pkg from '../package.json'; const commandBin = path.resolve(__dirname, '../bin/aelf-command.js'); diff --git a/tsconfig.json b/tsconfig.json index 1744afa..88baacf 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,7 @@ "allowJs": true, "checkJs": true, "allowSyntheticDefaultImports": true, - "noEmit": true + "noEmit": true, + "resolveJsonModule": true } } From 012f09e7fba0691a59ff2e0b54186dfb72ccae1c Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Mon, 29 Jul 2024 19:10:46 +0800 Subject: [PATCH 2/5] feat: eslint --- package.json | 2 +- src/index.js | 9 ++++++++- test/index.test.js | 4 +++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 75b9c48..0a6fadd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-command", - "version": "0.1.47-alpha.6", + "version": "0.1.47-alpha.8", "description": "A CLI tools for AElf", "main": "src/index.js", "type": "module", diff --git a/src/index.js b/src/index.js index 74a23cf..e6e583e 100644 --- a/src/index.js +++ b/src/index.js @@ -4,12 +4,15 @@ */ 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'; +import { createRequire } from 'module'; // Bring in the ability to create the 'require' method +const require = createRequire(import.meta.url); // construct the require method +const pkg = require('../package.json'); import { logger } from './utils/myLogger.js'; import { userHomeDir } from './utils/userHomeDir.js'; @@ -44,7 +47,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'; @@ -67,6 +72,7 @@ function init(options) { function run(args, options) { check({ node: `>= ${minVersion}` }, (error, results) => { if (error) { + // @ts-ignore logger.error(error); return; } @@ -77,6 +83,7 @@ function run(args, options) { isTest ? { from: 'user' } : undefined ); } else { + // @ts-ignore logger.error('Your Node.js version is needed to >= %s', minVersion); } }); diff --git a/test/index.test.js b/test/index.test.js index f07689d..af711c4 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,7 +3,9 @@ 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'; +import { createRequire } from 'module'; // Bring in the ability to create the 'require' method +const require = createRequire(import.meta.url); // construct the require method +const pkg = require('../package.json'); const commandBin = path.resolve(__dirname, '../bin/aelf-command.js'); From 7ad70498f402644393c2b31236709c6bef2de857 Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Tue, 30 Jul 2024 09:59:52 +0800 Subject: [PATCH 3/5] feat: assert syntax for import attributes in Node.js 22 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0a6fadd..18847f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aelf-command", - "version": "0.1.47-alpha.8", + "version": "0.1.47-beta.9", "description": "A CLI tools for AElf", "main": "src/index.js", "type": "module", From 20d821dc3182472122ecdb61859849aaac980c10 Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Tue, 30 Jul 2024 10:01:14 +0800 Subject: [PATCH 4/5] feat: assert syntax for import attributes in Node.js 22 --- tsconfig.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tsconfig.json b/tsconfig.json index 88baacf..1744afa 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,6 @@ "allowJs": true, "checkJs": true, "allowSyntheticDefaultImports": true, - "noEmit": true, - "resolveJsonModule": true + "noEmit": true } } From be9a27d9698a4a10882ca37cf2a62554e555f408 Mon Sep 17 00:00:00 2001 From: AbigailDeng Date: Tue, 30 Jul 2024 13:06:44 +0800 Subject: [PATCH 5/5] feat: readFileSync --- src/index.js | 22 +++++++++++++++++++--- test/index.test.js | 15 ++++++++++++--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/index.js b/src/index.js index e6e583e..ccc1528 100644 --- a/src/index.js +++ b/src/index.js @@ -10,15 +10,31 @@ import check from 'check-node-version'; import { execSync } from 'child_process'; import commands from './command/index.js'; import RC from './rc/index.js'; -import { createRequire } from 'module'; // Bring in the ability to create the 'require' method -const require = createRequire(import.meta.url); // construct the require method -const pkg = require('../package.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) { diff --git a/test/index.test.js b/test/index.test.js index af711c4..d503b35 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,9 +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 { createRequire } from 'module'; // Bring in the ability to create the 'require' method -const require = createRequire(import.meta.url); // construct the require method -const pkg = require('../package.json'); +import { getPackageJson } from '../src/index.js'; const commandBin = path.resolve(__dirname, '../bin/aelf-command.js'); @@ -17,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();