diff --git a/src/index.ts b/src/index.ts index f4d26d6..58b5c94 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ import { execaCommandSync } from "execa"; import { version } from "./config/index.js"; import { - checkRegistry, + checkTaobaoRegistry, detectPackageManager, updatePackages, } from "./utils/index.js"; @@ -29,7 +29,7 @@ cli const packageManager = detectPackageManager(); - checkRegistry(packageManager); + checkTaobaoRegistry(packageManager); const content = readFileSync(packageJSON, { encoding: "utf-8" }); @@ -54,12 +54,10 @@ cli const updateCommand = packageManager === "pnpm" ? `pnpm update` + : packageManager === "yarn1" + ? `yarn upgrade` : packageManager === "yarn" - ? execaCommandSync(`${packageManager} --version`).stdout.startsWith( - "1." - ) - ? `yarn upgrade` - : `yarn up` + ? `yarn up` : `npm update`; execaCommandSync(updateCommand, { stdout: "inherit" }); diff --git a/src/utils/checkRegistry.ts b/src/utils/checkRegistry.ts deleted file mode 100644 index 533ed40..0000000 --- a/src/utils/checkRegistry.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { execaCommandSync } from "execa"; - -import { type PackageManager } from "./packageManager.js"; - -const NPM_MIRROR_REGISTRY = "https://registry.npmmirror.com/"; - -const getUserRegistry = (packageManager: PackageManager): string => - execaCommandSync(`${packageManager} config get registry`).stdout; - -export const checkRegistry = (packageManager: PackageManager): void => { - const userRegistry = getUserRegistry(packageManager); - - if (/https:\/\/registry\.npm\.taobao\.org\/?/.test(userRegistry)) { - console.error( - "npm.taobao.org is no longer available, resetting it to npmmirror.com" - ); - - execaCommandSync( - `${packageManager} config set registry ${NPM_MIRROR_REGISTRY}` - ); - } -}; diff --git a/src/utils/getVersion.ts b/src/utils/getVersion.ts index 71a6061..4ce47d5 100644 --- a/src/utils/getVersion.ts +++ b/src/utils/getVersion.ts @@ -1,6 +1,6 @@ import { get } from "node:https"; import semver from "semver"; -import { getRegistry } from "./packageManager.js"; +import { getRegistry } from "./registry.js"; export const getVersion = async ( packageName: string, diff --git a/src/utils/index.ts b/src/utils/index.ts index dc957d6..4ee7049 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,3 @@ -export * from "./checkRegistry.js"; +export * from "./registry.js"; export * from "./packageManager.js"; export * from "./updatePackage.js"; diff --git a/src/utils/packageManager.ts b/src/utils/packageManager.ts index bc97b68..dea5b2d 100644 --- a/src/utils/packageManager.ts +++ b/src/utils/packageManager.ts @@ -2,7 +2,7 @@ import { existsSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { execaCommandSync } from "execa"; -export type PackageManager = "npm" | "yarn" | "pnpm"; +export type PackageManager = "npm" | "yarn" | "yarn1" | "pnpm"; const globalCache = new Map(); const localCache = new Map(); @@ -13,7 +13,12 @@ const PNPM_LOCK = "pnpm-lock.yaml"; const isInstalled = (packageManager: PackageManager): boolean => { try { - return execaCommandSync(`${packageManager} --version`).exitCode === 0; + const bin = packageManager === "yarn1" ? "yarn" : packageManager; + const excute = execaCommandSync(`${bin} --version`); + + if (packageManager === "yarn1") return excute.stdout.startsWith("1"); + + return excute.exitCode === 0; } catch (e) { return false; } @@ -57,9 +62,15 @@ export const getTypeofLockFile = ( } if (existsSync(resolve(cwd, YARN_LOCK))) { - localCache.set(key, "yarn"); + const packageManager = fs + .readFileSync(resolve(cwd, YARN_LOCK), { encoding: "utf-8" }) + .includes("yarn lockfile v1") + ? "yarn1" + : "yarn"; + + localCache.set(key, packageManager); - return "yarn"; + return packageManager; } if (existsSync(resolve(cwd, NPM_LOCK))) { @@ -81,9 +92,15 @@ export const getTypeofLockFile = ( } if (existsSync(resolve(dir, YARN_LOCK))) { - localCache.set(key, "yarn"); + const packageManager = fs + .readFileSync(resolve(dir, YARN_LOCK), { encoding: "utf-8" }) + .includes("yarn lockfile v1") + ? "yarn1" + : "yarn"; - return "yarn"; + localCache.set(key, packageManager); + + return packageManager; } if (existsSync(resolve(dir, NPM_LOCK))) { @@ -112,8 +129,3 @@ export const detectPackageManager = ( : "npm") ); }; - -export const getRegistry = (): string => - execaCommandSync( - `${detectPackageManager()} config get registry` - ).stdout.replace(/\/?$/, "/"); diff --git a/src/utils/registry.ts b/src/utils/registry.ts new file mode 100644 index 0000000..5784d2e --- /dev/null +++ b/src/utils/registry.ts @@ -0,0 +1,42 @@ +import { execaCommandSync } from "execa"; + +import { type PackageManager } from "./packageManager.js"; + +const NPM_MIRROR_REGISTRY = "https://registry.npmmirror.com/"; + +export const getRegistry = (packageManager: PackageManager): string => { + if ( + packageManager === "yarn" && + !execaCommandSync(`${packageManager} --version`).stdout.startsWith("1") + ) + return execaCommandSync( + `${packageManager} config get npmRegistryServer` + ).stdout.replace(/\/?$/, "/"); + + return execaCommandSync( + `${packageManager} config get registry` + ).stdout.replace(/\/?$/, "/"); +}; + +export const checkTaobaoRegistry = (packageManager: PackageManager): void => { + const userRegistry = getRegistry(packageManager); + + if (/https:\/\/registry\.npm\.taobao\.org\/?/.test(userRegistry)) { + console.error( + "npm.taobao.org is no longer available, resetting it to npmmirror.com" + ); + + if ( + packageManager === "yarn" && + !execaCommandSync(`${packageManager} --version`).stdout.startsWith("1") + ) { + execaCommandSync( + `${packageManager} config set npmRegistryServer ${NPM_MIRROR_REGISTRY}` + ); + } else { + execaCommandSync( + `${packageManager} config set registry ${NPM_MIRROR_REGISTRY}` + ); + } + } +};