Skip to content

Commit e5c609d

Browse files
authored
fix: checkNpmPackageInstalled not working in bun as npm (#13)
1 parent acd5bc9 commit e5c609d

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/index.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import { exec, execSync } from 'child_process';
1+
import { execSync } from 'child_process';
22
import type { IApi } from 'father';
33
import fs from 'fs-extra';
44
import path from 'path';
55

66
const cwd = process.cwd();
77

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+
);
1514
}
1615

1716
export default (api: IApi) => {
@@ -23,16 +22,18 @@ export default (api: IApi) => {
2322

2423
// Break if current project not install `@rc-component/np`
2524
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')) {
2728
console.log('Please install `@rc-component/np` instead of `np`.');
2829
process.exit(1);
2930
}
3031

3132
const inputFolder =
3233
api?.config?.esm?.input || api?.config?.esm?.input || 'src/';
3334

34-
const isInstalled = await checkNpmPackageInstalled('eslint');
35-
if (isInstalled) {
35+
const isEslintInstalled = checkNpmPackageDependency(packageJson, 'eslint');
36+
if (isEslintInstalled) {
3637
execSync(
3738
// Requires compatibility with Windows environment
3839
`npx eslint ${inputFolder} --ext .tsx,.ts --rule "@typescript-eslint/consistent-type-exports: error"`,

0 commit comments

Comments
 (0)