Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
feat: support yarn2 and above
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Nov 12, 2023
1 parent ddd32b5 commit 9a51d5a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 42 deletions.
12 changes: 5 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { execaCommandSync } from "execa";

import { version } from "./config/index.js";
import {
checkRegistry,
checkTaobaoRegistry,
detectPackageManager,
updatePackages,
} from "./utils/index.js";
Expand All @@ -29,7 +29,7 @@ cli

const packageManager = detectPackageManager();

checkRegistry(packageManager);
checkTaobaoRegistry(packageManager);

const content = readFileSync(packageJSON, { encoding: "utf-8" });

Expand All @@ -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" });
Expand Down
22 changes: 0 additions & 22 deletions src/utils/checkRegistry.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/getVersion.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./checkRegistry.js";
export * from "./registry.js";
export * from "./packageManager.js";
export * from "./updatePackage.js";
34 changes: 23 additions & 11 deletions src/utils/packageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, boolean>();
const localCache = new Map<string, PackageManager>();
Expand All @@ -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;
}
Expand Down Expand Up @@ -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))) {
Expand All @@ -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))) {
Expand Down Expand Up @@ -112,8 +129,3 @@ export const detectPackageManager = (
: "npm")
);
};

export const getRegistry = (): string =>
execaCommandSync(
`${detectPackageManager()} config get registry`
).stdout.replace(/\/?$/, "/");
42 changes: 42 additions & 0 deletions src/utils/registry.ts
Original file line number Diff line number Diff line change
@@ -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}`
);
}
}
};

0 comments on commit 9a51d5a

Please sign in to comment.