diff --git a/package.json b/package.json index 0daf256..7045884 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,8 @@ "port" ], "bin": { - "detect": "bin/detect-port.cjs", - "detect-port": "bin/detect-port.cjs" + "detect": "dist/commonjs/bin/detect-port.js", + "detect-port": "dist/commonjs//detect-port.js" }, "main": "./dist/commonjs/index.js", "files": [ @@ -37,10 +37,12 @@ "typescript": "^5.2.2" }, "scripts": { - "test": "npm run lint -- --fix && egg-bin test", + "pretest": "npm run lint -- --fix && npm run prepublishOnly", + "test": "egg-bin test", "lint": "eslint src test --ext ts", "ci": "npm run lint && npm run cov && npm run prepublishOnly", "prepublishOnly": "tshy && tshy-after", + "precov": "npm run prepublishOnly", "cov": "egg-bin cov" }, "engines": { diff --git a/bin/detect-port.cjs b/src/bin/detect-port.ts similarity index 88% rename from bin/detect-port.cjs rename to src/bin/detect-port.ts index 3660e2c..96c9df8 100755 --- a/bin/detect-port.cjs +++ b/src/bin/detect-port.ts @@ -1,10 +1,10 @@ #!/usr/bin/env node -const path = require('node:path'); -const { readFileSync } = require('node:fs'); -const { detectPort } = require('../'); +import path from 'node:path'; +import { readFileSync } from 'node:fs'; +import detectPort from '../detect-port.js'; -const pkgFile = path.join(__dirname, '../package.json'); +const pkgFile = path.join(__dirname, '../../../package.json'); const pkg = JSON.parse(readFileSync(pkgFile, 'utf-8')); const args = process.argv.slice(2); @@ -12,11 +12,10 @@ let arg_0 = args[0]; if (arg_0 && [ '-v', '--version' ].includes(arg_0.toLowerCase())) { console.log(pkg.version); - process.exit(0); } -const removeByValue = (arr, val) => { +const removeByValue = (arr: string[], val: string) => { for (let i = 0; i < arr.length; i++) { if (arr[i] === val) { arr.splice(i, 1); diff --git a/test/cli.test.ts b/test/cli.test.ts index c87995e..0489400 100644 --- a/test/cli.test.ts +++ b/test/cli.test.ts @@ -10,8 +10,8 @@ const __dirname = path.dirname(__filename); const pkgFile = path.join(__dirname, '../package.json'); const pkg = JSON.parse(readFileSync(pkgFile, 'utf-8')); -describe.skip('test/cli.test.ts', async () => { - const binFile = path.join(__dirname, '../bin/detect-port.cjs'); +describe('test/cli.test.ts', async () => { + const binFile = path.join(__dirname, '../dist/commonjs/bin/detect-port.js'); it('should show version', async () => { let res = await execaNode(binFile, [ '-v' ]); diff --git a/test/detect-port.test.ts b/test/detect-port.test.ts index 6a6d704..1cd2a5a 100644 --- a/test/detect-port.test.ts +++ b/test/detect-port.test.ts @@ -101,18 +101,17 @@ describe('test/detect-port.test.ts', () => { assert.equal(realPort, 24001); }); - it('work with listening next port 24001 because 24000 was listened to ' + ip(), async () => { + it('work with listening next port 24001 because 24000 was listened', async () => { const port = 24000; const realPort = await detectPort(port); - - assert(realPort === 24001); + assert.equal(realPort, 24001); }); it('work with listening next port 28081 because 28080 was listened to 0.0.0.0:28080', async () => { const port = 28080; const realPort = await detectPort(port); - assert(realPort === 28081); + assert.equal(realPort, 28081); }); it('work with listening random port when try port hit maxPort', async () => { diff --git a/test/wait-port.test.ts b/test/wait-port.test.ts index a25ce28..88edabb 100644 --- a/test/wait-port.test.ts +++ b/test/wait-port.test.ts @@ -26,7 +26,7 @@ describe('test/wait-port.test.ts', () => { try { const port = 9093; await waitPort(port, { retries: 3, retryInterval: 100 }); - } catch (err:any) { + } catch (err: any) { assert.equal(err.message, 'retries exceeded'); } });