1
- import { exec , execSync } from 'child_process' ;
1
+ import { execSync } from 'child_process' ;
2
2
import type { IApi } from 'father' ;
3
3
import fs from 'fs-extra' ;
4
4
import path from 'path' ;
5
5
6
6
const cwd = process . cwd ( ) ;
7
7
8
- // 检查是否已安装 npm 包
9
- function checkNpmPackageInstalled ( packageName : string ) {
10
- return new Promise ( ( resolve ) => {
11
- exec ( `npm list --depth=0 ${ packageName } ` , ( error : Error ) => {
12
- resolve ( ! error ) ;
13
- } ) ;
14
- } ) ;
8
+ // 检查 package.json 中是否有指定的 npm 包依赖
9
+ function checkNpmPackageDependency ( packageJson : any , packageName : string ) {
10
+ return ! ! (
11
+ ( packageJson . dependencies && packageJson . dependencies [ packageName ] ) ||
12
+ ( packageJson . devDependencies && packageJson . devDependencies [ packageName ] )
13
+ ) ;
15
14
}
16
15
17
16
export default ( api : IApi ) => {
@@ -23,16 +22,18 @@ export default (api: IApi) => {
23
22
24
23
// Break if current project not install `@rc-component/np`
25
24
const packageJson = await fs . readJson ( path . join ( cwd , 'package.json' ) ) ;
26
- if ( ! packageJson . devDependencies [ '@rc-component/np' ] ) {
25
+
26
+ // Break if current project not install `@rc-component/np`
27
+ if ( ! checkNpmPackageDependency ( packageJson , '@rc-component/np' ) ) {
27
28
console . log ( 'Please install `@rc-component/np` instead of `np`.' ) ;
28
29
process . exit ( 1 ) ;
29
30
}
30
31
31
32
const inputFolder =
32
33
api ?. config ?. esm ?. input || api ?. config ?. esm ?. input || 'src/' ;
33
34
34
- const isInstalled = await checkNpmPackageInstalled ( 'eslint' ) ;
35
- if ( isInstalled ) {
35
+ const isEslintInstalled = checkNpmPackageDependency ( packageJson , 'eslint' ) ;
36
+ if ( isEslintInstalled ) {
36
37
execSync (
37
38
// Requires compatibility with Windows environment
38
39
`npx eslint ${ inputFolder } --ext .tsx,.ts --rule "@typescript-eslint/consistent-type-exports: error"` ,
0 commit comments