From eb74fb40f8ae67cd9fc4cb1bfd1aa39102dff624 Mon Sep 17 00:00:00 2001 From: "Mr.Hope" Date: Mon, 13 Nov 2023 09:52:05 +0800 Subject: [PATCH] fix: bug fixes --- src/index.ts | 5 +++-- src/utils/getVersion.ts | 6 +++++- src/utils/packageManager.ts | 15 ++++++++------- src/utils/registry.ts | 5 +---- src/utils/updatePackage.ts | 26 ++++++++++++++++++++++---- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/index.ts b/src/index.ts index 58b5c94..f179770 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ #!/usr/bin/env node import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { resolve } from "node:path"; + import { cac } from "cac"; import { execaCommandSync } from "execa"; @@ -37,10 +38,10 @@ cli await Promise.all([ packageJSONContent.dependencies - ? updatePackages(packageJSONContent.dependencies) + ? updatePackages(packageManager, packageJSONContent.dependencies) : Promise.resolve(), packageJSONContent.devDependencies - ? updatePackages(packageJSONContent.devDependencies) + ? updatePackages(packageManager, packageJSONContent.devDependencies) : Promise.resolve(), ]); diff --git a/src/utils/getVersion.ts b/src/utils/getVersion.ts index 4ce47d5..85e953f 100644 --- a/src/utils/getVersion.ts +++ b/src/utils/getVersion.ts @@ -1,13 +1,17 @@ import { get } from "node:https"; + import semver from "semver"; + import { getRegistry } from "./registry.js"; +import { type PackageManager } from "./packageManager.js"; export const getVersion = async ( + packageManager: PackageManager, packageName: string, tag: "latest" | "next" | "auto" = "auto", retries = 3 ): Promise => { - const registry = getRegistry(); + const registry = getRegistry(packageManager); const infoUrl = `${registry}-/package/${packageName}/dist-tags`; const getVersionInfo = (): Promise> => diff --git a/src/utils/packageManager.ts b/src/utils/packageManager.ts index dea5b2d..0f0be1d 100644 --- a/src/utils/packageManager.ts +++ b/src/utils/packageManager.ts @@ -1,5 +1,6 @@ -import { existsSync } from "node:fs"; +import { existsSync, readFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; + import { execaCommandSync } from "execa"; export type PackageManager = "npm" | "yarn" | "yarn1" | "pnpm"; @@ -62,9 +63,9 @@ export const getTypeofLockFile = ( } if (existsSync(resolve(cwd, YARN_LOCK))) { - const packageManager = fs - .readFileSync(resolve(cwd, YARN_LOCK), { encoding: "utf-8" }) - .includes("yarn lockfile v1") + const packageManager = readFileSync(resolve(cwd, YARN_LOCK), { + encoding: "utf-8", + }).includes("yarn lockfile v1") ? "yarn1" : "yarn"; @@ -92,9 +93,9 @@ export const getTypeofLockFile = ( } if (existsSync(resolve(dir, YARN_LOCK))) { - const packageManager = fs - .readFileSync(resolve(dir, YARN_LOCK), { encoding: "utf-8" }) - .includes("yarn lockfile v1") + const packageManager = readFileSync(resolve(dir, YARN_LOCK), { + encoding: "utf-8", + }).includes("yarn lockfile v1") ? "yarn1" : "yarn"; diff --git a/src/utils/registry.ts b/src/utils/registry.ts index 5784d2e..de1c881 100644 --- a/src/utils/registry.ts +++ b/src/utils/registry.ts @@ -26,10 +26,7 @@ export const checkTaobaoRegistry = (packageManager: PackageManager): void => { "npm.taobao.org is no longer available, resetting it to npmmirror.com" ); - if ( - packageManager === "yarn" && - !execaCommandSync(`${packageManager} --version`).stdout.startsWith("1") - ) { + if (packageManager === "yarn") { execaCommandSync( `${packageManager} config set npmRegistryServer ${NPM_MIRROR_REGISTRY}` ); diff --git a/src/utils/updatePackage.ts b/src/utils/updatePackage.ts index ffd0d5e..56e63b5 100644 --- a/src/utils/updatePackage.ts +++ b/src/utils/updatePackage.ts @@ -6,23 +6,41 @@ import { VUE_RELATED_PACKAGES, } from "../config/index.js"; import { getVersion } from "./getVersion.js"; +import { PackageManager } from "./packageManager.js"; export const updatePackages = async ( + packageManager: PackageManager, dependencies: Record ): Promise => { await Promise.all( Object.keys(dependencies).map(async (dependency) => { if (VUE_RELATED_PACKAGES.includes(dependency)) { - dependencies[dependency] = `^${await getVersion(dependency, "latest")}`; + dependencies[dependency] = `^${await getVersion( + packageManager, + dependency, + "latest" + )}`; } else if (OFFICIAL_PACKAGES.includes(dependency)) { - dependencies[dependency] = await getVersion(dependency, "next"); + dependencies[dependency] = await getVersion( + packageManager, + dependency, + "next" + ); } else if (OFFICIAL_PLUGINS.includes(dependency)) { - dependencies[dependency] = await getVersion(dependency, "next"); + dependencies[dependency] = await getVersion( + packageManager, + dependency, + "next" + ); } else if ( THIRD_PARTY_PLUGINS.test(dependency) || THIRD_PARTY_THEMES.test(dependency) ) { - dependencies[dependency] = await getVersion(dependency, "auto"); + dependencies[dependency] = await getVersion( + packageManager, + dependency, + "auto" + ); } }) );