Skip to content

Commit

Permalink
fix: 优化 update 安装时包管理器调用逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
beezen committed Jan 9, 2024
1 parent 1e6ef79 commit 648ca44
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/actions/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import shell from "shelljs";
import inquirer from "inquirer";
import { getLangMessage, setConfig } from "../common";
import { baseInitConfig } from "../common/env";
import { compareVersion } from "../utils/index";
import { compareVersion, getPackageManager } from "../utils/index";
import { addUser, removeUser } from "./base";
/**
* 更新版本
Expand All @@ -26,9 +26,12 @@ export function updateVersion(option, curVersion) {
}
const status = compareVersion(curVersion, latestVersion);
if (status === -1) {
const packageManager = getPackageManager();
const updateCmd =
packageManager === "yarn" ? "yarn global add nucm@latest" : "npm install -g nucm@latest";
if (option.silent) {
console.log(getLangMessage("MSG_updateTips").red);
shell.exec("npm install -g nucm@latest"); // 更新最新版本
shell.exec(updateCmd); // 更新最新版本
return;
}
// 存在新版本
Expand All @@ -44,9 +47,9 @@ export function updateVersion(option, curVersion) {
name: "result"
}
])
.then(answers => {
.then((answers) => {
if (answers.result) {
shell.exec("npm install -g nucm@latest"); // 更新最新版本
shell.exec(updateCmd); // 更新最新版本
}
});
} else {
Expand Down Expand Up @@ -80,7 +83,7 @@ export function searchToSave() {
}
const accountList = fileConfig.nucm[registryConfig.registryName] || {};
const account = Object.keys(accountList).filter(
name => accountList[name] && accountList[name]["access-tokens"] === registryConfig._authtoken
(name) => accountList[name] && accountList[name]["access-tokens"] === registryConfig._authtoken
);
const tokenTag = `nucm_${Date.now()}`;

Expand All @@ -93,7 +96,7 @@ export function searchToSave() {
name: "check"
}
])
.then(answers => {
.then((answers) => {
if (answers.check) {
inquirer
.prompt([
Expand All @@ -104,7 +107,7 @@ export function searchToSave() {
default: tokenTag
}
])
.then(a => {
.then((a) => {
if (a.name) {
removeUser(account[0]);
addUser(a.name, registryConfig._authtoken);
Expand All @@ -122,7 +125,7 @@ export function searchToSave() {
default: tokenTag
}
])
.then(answers => {
.then((answers) => {
if (answers.name) {
addUser(answers.name, registryConfig._authtoken);
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import shell from "shelljs";
/**
* 链接符号
* @param str 字符传
Expand Down Expand Up @@ -49,3 +50,13 @@ export function compareVersion(v1, v2) {
}
return 0;
}

/** 获取包管理器 */
export function getPackageManager() {
// 校验 yarn 是否存在
const yarnVersion = shell.exec("yarn --version", { silent: true }).stdout.trim();
if (yarnVersion) {
return "yarn";
}
return "npm";
}

0 comments on commit 648ca44

Please sign in to comment.