Skip to content

Commit 174ae0b

Browse files
committed
check
1 parent 89232a6 commit 174ae0b

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/Npm.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,21 @@ const Npm: React.FC = () => {
4646
},
4747
];
4848

49-
const [isLoading, setLoading] = useState(true);
49+
const [isLoading, setLoading] = useState<boolean>(true);
5050

5151
const [value, setValue] = useState<string>("");
5252
const [url, setUrl] = useState<string>("");
5353

54+
const [isDisabled, setIsDisabled] = useState<boolean>(true);
55+
5456
async function getNpmProgram() {
5557
const platformName = await platform();
56-
return /^win/i.test(platformName) ? "run-npm.cmd" : "run-npm";
58+
return /^win/i.test(platformName) ? "run-npm-cmd" : "run-npm";
5759
}
5860

5961
function getUrl(): Promise<string> {
6062
return new Promise<string>(async (resolve) => {
61-
const platformName = await platform();
62-
const program = /^win/i.test(platformName) ? "run-npm-cmd" : "run-npm";
63+
const program = await getNpmProgram();
6364
const command = new Command(program, ["config", "get", "registry"]);
6465
let url = "";
6566

@@ -109,7 +110,22 @@ const Npm: React.FC = () => {
109110
setValue(option?.value as string);
110111
};
111112

113+
async function check() {
114+
try {
115+
const program = await getNpmProgram();
116+
await new Command(program).spawn();
117+
setIsDisabled(false);
118+
} catch (e) {
119+
// 命令不存在的情况 "program not found"
120+
console.log(e);
121+
setLoading(false);
122+
setIsDisabled(true);
123+
}
124+
}
125+
112126
useEffect(() => {
127+
check();
128+
113129
getUrl().then(() => {
114130
setLoading(false);
115131
});
@@ -151,6 +167,7 @@ const Npm: React.FC = () => {
151167
return (
152168
<>
153169
<Radio.Group
170+
disabled={isDisabled}
154171
onChange={onChange}
155172
name="radiogroup"
156173
options={npmOptions}

0 commit comments

Comments
 (0)