From e1879b696034707ef08b344187748e5c850e21f8 Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 14 Sep 2024 23:41:48 +0800 Subject: [PATCH 1/3] fix: skip if eslint is not installed --- src/index.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index c617877..78d7c5f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,15 +11,19 @@ export default (api: IApi) => { api?.config?.esm?.input || api?.config?.esm?.input || 'src/'; const { execSync } = require('child_process'); - execSync( - `npx eslint ${inputFolder} --ext .tsx,.ts --rule '@typescript-eslint/consistent-type-exports: error'`, - { - cwd: process.cwd(), - env: process.env, - stdio: [process.stdin, process.stdout, process.stderr], - encoding: 'utf-8', - }, - ); + try { + execSync( + `npx eslint ${inputFolder} --ext .tsx,.ts --rule '@typescript-eslint/consistent-type-exports: error'`, + { + cwd: process.cwd(), + env: process.env, + stdio: [process.stdin, process.stdout, process.stderr], + encoding: 'utf-8', + }, + ); + } catch (error) { + console.log('ESLint is not installed, skip.'); + } }); // modify default build config for all rc projects From 797bf84b5bd607b5a1363b38d79809e070cfb7cd Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 14 Sep 2024 23:55:23 +0800 Subject: [PATCH 2/3] fix: skip if eslint is not installed --- src/index.ts | 22 ++++++++++++++++++---- tsconfig.json | 1 + 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 78d7c5f..deafc03 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,22 @@ import type { IApi } from 'father'; +import { exec, execSync } from 'child_process'; + +// 检查是否已安装 npm 包 +function checkNpmPackageInstalled(packageName: string) { + return new Promise((resolve, reject) => { + exec(`npm list --depth=0 ${packageName}`, (error: Error) => { + if (error) { + reject(false); // 未安装 + } else { + resolve(true); // 已安装 + } + }); + }); +} export default (api: IApi) => { // Compile break if export type without consistent - api.onStart(() => { + api.onStart(async () => { if (api.name !== 'build') { return; } @@ -10,8 +24,8 @@ export default (api: IApi) => { const inputFolder = api?.config?.esm?.input || api?.config?.esm?.input || 'src/'; - const { execSync } = require('child_process'); - try { + const isInstalled = await checkNpmPackageInstalled('eslint'); + if (isInstalled) { execSync( `npx eslint ${inputFolder} --ext .tsx,.ts --rule '@typescript-eslint/consistent-type-exports: error'`, { @@ -21,7 +35,7 @@ export default (api: IApi) => { encoding: 'utf-8', }, ); - } catch (error) { + } else { console.log('ESLint is not installed, skip.'); } }); diff --git a/tsconfig.json b/tsconfig.json index 3df9be7..767e369 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "strict": true, + "lib": ["ES2015"], "skipLibCheck": true, "esModuleInterop": true, "baseUrl": "./" From b19922d1ad4ffbd3ff7523ba529ef25d572dacf0 Mon Sep 17 00:00:00 2001 From: afc163 Date: Sat, 14 Sep 2024 23:58:49 +0800 Subject: [PATCH 3/3] Update src/index.ts --- src/index.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index deafc03..cb84c86 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,13 +3,9 @@ import { exec, execSync } from 'child_process'; // 检查是否已安装 npm 包 function checkNpmPackageInstalled(packageName: string) { - return new Promise((resolve, reject) => { + return new Promise((resolve) => { exec(`npm list --depth=0 ${packageName}`, (error: Error) => { - if (error) { - reject(false); // 未安装 - } else { - resolve(true); // 已安装 - } + resolve(!error); }); }); }