From 0fb74f7bb85db47f6dd03697fd500d518e4907e5 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Tue, 14 Feb 2023 17:49:55 +0800 Subject: [PATCH] fix: add scripts dir to exclude (#219) closes https://github.com/eggjs/egg-bin/issues/218 --- .eslintignore | 2 -- package.json | 5 ++--- scripts/.eslintrc | 4 ++++ scripts/{postinstall => postinstall.js} | 11 +++++++++-- scripts/{start-cluster => start-cluster.js} | 2 -- src/bin/cli.ts | 10 +++++++++- src/cmd/dev.ts | 2 +- test/cmd/cov.test.ts | 3 --- test/cmd/debug.test.ts | 3 --- test/cmd/dev.test.ts | 3 --- test/cmd/test.test.ts | 3 --- test/ts.test.ts | 3 --- 12 files changed, 25 insertions(+), 26 deletions(-) create mode 100644 scripts/.eslintrc rename scripts/{postinstall => postinstall.js} (78%) rename scripts/{start-cluster => start-cluster.js} (92%) diff --git a/.eslintignore b/.eslintignore index 2b166231..25fbf5a1 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,4 +1,2 @@ node_modules/ coverage/ -scripts/ -*.js diff --git a/package.json b/package.json index 5a9b8fb8..882888ef 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,6 @@ "eslint": "^8.16.0", "eslint-config-egg": "^12.0.0", "git-contributor": "2", - "mm": "^3.2.0", "typescript": "^4.9.5" }, "repository": { @@ -65,7 +64,7 @@ "homepage": "https://github.com/eggjs/egg-bin", "author": "fengmk2 (https://github.com/fengmk2)", "scripts": { - "postinstall": "node scripts/postinstall", + "postinstall": "node scripts/postinstall.js", "contributor": "git-contributor", "lint": "eslint . --cache --ext ts", "test": "npm run lint -- --fix && npm run test-local", @@ -73,7 +72,7 @@ "test-local-with-ts-node-transpile-only": "node -r ts-node/register/transpile-only src/bin/cli.ts test", "test-local-with-swc": "node -r @swc-node/register src/bin/cli.ts test", "test-local-with-esbuild": "node -r esbuild-register src/bin/cli.ts test", - "test-tsc": "npm run clean && npm run tsc && rm -rf src && node dist/bin/cli.js && node dist/bin/cli.js test --base test/fixtures/example-ts", + "test-tsc": "npm run clean && npm run tsc && node dist/bin/cli.js && node dist/bin/cli.js test --base test/fixtures/example-ts && node dist/bin/cli.js dev --base test/fixtures/example-ts", "cov": "c8 -r lcov -r text-summary -x 'test/**' npm run test-local -- --timeout 120000", "ci-test-only": "npm run test-local -- test/cmd/cov.test.ts", "ci": "npm run lint && npm run cov && npm run test-tsc", diff --git a/scripts/.eslintrc b/scripts/.eslintrc new file mode 100644 index 00000000..1f6ab679 --- /dev/null +++ b/scripts/.eslintrc @@ -0,0 +1,4 @@ +{ + "root": true, + "extends": "eslint-config-egg" +} diff --git a/scripts/postinstall b/scripts/postinstall.js similarity index 78% rename from scripts/postinstall rename to scripts/postinstall.js index d2a6fb94..d4517037 100644 --- a/scripts/postinstall +++ b/scripts/postinstall.js @@ -1,5 +1,4 @@ -#!/usr/bin/env node - +const debug = require('node:util').debuglog('egg-bin:postinstall'); const path = require('node:path'); const fs = require('node:fs'); const runscript = require('runscript'); @@ -11,6 +10,14 @@ const frameworkPackageName = process.argv[3] || 'egg'; // try to use INIT_CWD env https://docs.npmjs.com/cli/v9/commands/npm-run-script // npm_rootpath is npminstall const npmRunRoot = process.env.INIT_CWD || process.env.npm_rootpath; + +debug('process.argv: %o', process.argv); +debug('process.env.INIT_CWD: %o', process.env.INIT_CWD); +debug('process.env.npm_rootpath: %o', process.env.npm_rootpath); +debug('etsBinFile: %o', etsBinFile); +debug('frameworkPackageName: %o', frameworkPackageName); +debug('npmRunRoot: %o', npmRunRoot); + if (npmRunRoot) { const pkgFile = path.join(npmRunRoot, 'package.json'); if (fs.existsSync(pkgFile)) { diff --git a/scripts/start-cluster b/scripts/start-cluster.js similarity index 92% rename from scripts/start-cluster rename to scripts/start-cluster.js index fbf1bdb6..279efc2b 100755 --- a/scripts/start-cluster +++ b/scripts/start-cluster.js @@ -1,5 +1,3 @@ -#!/usr/bin/env node - const debug = require('util').debuglog('egg-bin:lib:start-cluster'); debug('argv: %o', process.argv); diff --git a/src/bin/cli.ts b/src/bin/cli.ts index 8b89046a..97f26a7c 100644 --- a/src/bin/cli.ts +++ b/src/bin/cli.ts @@ -2,4 +2,12 @@ import { start } from '@artus-cli/artus-cli'; -start(); +const isBuildJavascriptFile = __filename.endsWith('.js'); +const exclude = [ 'scripts', 'bin', 'test', 'coverage' ]; +if (isBuildJavascriptFile) { + exclude.push('*.ts'); +} else { + exclude.push('dist'); +} + +start({ exclude }); diff --git a/src/cmd/dev.ts b/src/cmd/dev.ts index 7c10d939..4ca7f645 100644 --- a/src/cmd/dev.ts +++ b/src/cmd/dev.ts @@ -42,7 +42,7 @@ export class DevCommand extends BaseCommand { debug('run dev: %o', this.args); this.ctx.env.NODE_ENV = this.ctx.env.NODE_ENV ?? 'development'; this.ctx.env.EGG_MASTER_CLOSE_TIMEOUT = '1000'; - const serverBin = path.join(__dirname, '../../scripts/start-cluster'); + const serverBin = path.join(__dirname, '../../scripts/start-cluster.js'); const args = await this.formatEggStartArgs(); const serverCmd = `${serverBin} '${JSON.stringify(args)}'`; const requires = await this.formatRequires(); diff --git a/test/cmd/cov.test.ts b/test/cmd/cov.test.ts index a225164f..5aa8778e 100644 --- a/test/cmd/cov.test.ts +++ b/test/cmd/cov.test.ts @@ -1,7 +1,6 @@ import assert from 'node:assert'; import path from 'node:path'; import fs from 'node:fs/promises'; -import mm from 'mm'; import assertFile from 'assert-file'; import coffee from '../coffee'; @@ -17,8 +16,6 @@ describe('test/cmd/cov.test.ts', () => { assertFile(path.join(baseDir, 'coverage/lcov.info')); } - afterEach(mm.restore); - describe('egg-bin cov', () => { it('should success on js', async () => { await coffee.fork(eggBin, [ 'cov', '--ts=false' ], { cwd, env: { TESTS: 'test/**/*.test.js' } }) diff --git a/test/cmd/debug.test.ts b/test/cmd/debug.test.ts index 2d38b5ed..25b0704e 100644 --- a/test/cmd/debug.test.ts +++ b/test/cmd/debug.test.ts @@ -1,5 +1,4 @@ import path from 'node:path'; -import mm from 'mm'; import coffee from '../coffee'; describe('test/cmd/debug.test.ts', () => { @@ -7,8 +6,6 @@ describe('test/cmd/debug.test.ts', () => { const fixtures = path.join(__dirname, '../fixtures'); const cwd = path.join(fixtures, 'demo-app'); - afterEach(mm.restore); - it('should startCluster success', () => { return coffee.fork(eggBin, [ 'debug' ], { cwd }) // .debug() diff --git a/test/cmd/dev.test.ts b/test/cmd/dev.test.ts index 3ca7aa0c..c681ce3a 100644 --- a/test/cmd/dev.test.ts +++ b/test/cmd/dev.test.ts @@ -1,6 +1,5 @@ import path from 'node:path'; import net from 'node:net'; -import mm from 'mm'; import detect from 'detect-port'; import coffee from '../coffee'; @@ -9,8 +8,6 @@ describe('test/cmd/dev.test.ts', () => { const fixtures = path.join(__dirname, '../fixtures'); const cwd = path.join(fixtures, 'demo-app'); - afterEach(mm.restore); - it('should startCluster success', () => { return coffee.fork(eggBin, [ 'dev' ], { cwd }) // .debug() diff --git a/test/cmd/test.test.ts b/test/cmd/test.test.ts index ad299958..bea3c0f7 100644 --- a/test/cmd/test.test.ts +++ b/test/cmd/test.test.ts @@ -1,5 +1,4 @@ import path from 'node:path'; -import mm from 'mm'; import coffee from '../coffee'; describe('test/cmd/test.test.ts', () => { @@ -7,8 +6,6 @@ describe('test/cmd/test.test.ts', () => { const fixtures = path.join(__dirname, '../fixtures'); const cwd = path.join(fixtures, 'test-files'); - afterEach(mm.restore); - describe('egg-bin test', () => { it('should success js', () => { return coffee.fork(eggBin, [ 'test' ], { cwd }) diff --git a/test/ts.test.ts b/test/ts.test.ts index 0a13dc3a..fb27583a 100644 --- a/test/ts.test.ts +++ b/test/ts.test.ts @@ -1,7 +1,6 @@ import assert from 'node:assert'; import path from 'node:path'; import fs from 'node:fs/promises'; -import mm from 'mm'; import runscript from 'runscript'; import coffee from './coffee'; @@ -14,8 +13,6 @@ describe('test/ts.test.ts', () => { const fixtures = path.join(__dirname, 'fixtures'); let cwd: string; - afterEach(mm.restore); - it('should support ts', () => { cwd = path.join(fixtures, 'ts'); return coffee.fork(eggBin, [ 'dev' ], { cwd, env: { NODE_ENV: 'development' } })