From 368121f3d76149d42a431f13daf650122c2426fe Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 10 Oct 2023 10:03:35 +0800 Subject: [PATCH] refactor(utils): handle rapid mode for tnpm/cnpm detection --- packages/utils/src/npmClient.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/packages/utils/src/npmClient.ts b/packages/utils/src/npmClient.ts index f2c2a4cdd008..925763209304 100644 --- a/packages/utils/src/npmClient.ts +++ b/packages/utils/src/npmClient.ts @@ -1,4 +1,4 @@ -import { existsSync } from 'fs'; +import { existsSync, readFileSync } from 'fs'; import { join } from 'path'; export type NpmClient = 'npm' | 'cnpm' | 'tnpm' | 'yarn' | 'pnpm'; @@ -11,12 +11,29 @@ export enum NpmClientEnum { npm = 'npm', } export const getNpmClient = (opts: { cwd: string }): NpmClient => { + const tnpmRegistries = ['.alibaba-inc.', '.antgroup-inc.']; + const tcnpmLockPath = join(opts.cwd, 'node_modules', '.package-lock.json'); const chokidarPkg = require('chokidar/package.json'); - if (chokidarPkg.__npminstall_done) { - return chokidarPkg._resolved.includes('registry.npm.alibaba-inc.com') + + // detect tnpm/cnpm client + // all situations: + // - npminstall mode + native fs => generate _resolved field in package.json + // - npminstall mode + rapid fs => generate .package-lock.json in node_modules + // - npm mode + native fs => generate .package-lock.json in node_modules + // - npm mode + rapid fs => generate .package-lock.json in node_modules + // all conditions: + // - has _resolved field or .package-lock.json means tnpm/cnpm + // - _resolved field or .package-lock.json contains tnpm registry means tnpm + if (chokidarPkg._resolved) { + return tnpmRegistries.some((r) => chokidarPkg._resolved.includes(r)) ? 'tnpm' : 'cnpm'; + } else if (existsSync(tcnpmLockPath)) { + const tcnpmLock = readFileSync(tcnpmLockPath, 'utf-8'); + + return tnpmRegistries.some((r) => tcnpmLock.includes(r)) ? 'tnpm' : 'cnpm'; } + const chokidarPath = require.resolve('chokidar'); if ( chokidarPath.includes('.pnpm') ||