Skip to content

Commit 3fc4efa

Browse files
committed
shell
1 parent de7f820 commit 3fc4efa

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

src-tauri/tauri.conf.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313
"tauri": {
1414
"allowlist": {
1515
"all": true,
16+
"shell": {
17+
"execute": true,
18+
"sidecar": true,
19+
"scope": [
20+
{
21+
"name": "run-npm",
22+
"cmd": "npm.cmd",
23+
"args": true
24+
}
25+
]
26+
},
1627
"fs": {
1728
"scope": [
1829
"$HOME/.m2/settings.xml",

src/Npm.tsx

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import React, { useEffect, useState } from "react";
22
import type { RadioChangeEvent } from "antd";
33
import { Input, Radio, Space } from "antd";
4-
import { readFileFromAppConfig, writeFileToAppConfig } from "./fileUtils.ts";
5-
import { XmlUtils } from "./XMLUtil.ts";
6-
import { homeDir } from "@tauri-apps/api/path";
4+
import { Command } from "@tauri-apps/api/shell";
75

86
const Npm: React.FC = () => {
9-
const [customUrl, setCustomUrl] = useState("");
10-
const [disabled, setDisabled] = useState(true);
7+
const [customUrl, setCustomUrl] = useState<string>("");
8+
const [disabled, setDisabled] = useState<boolean>(true);
119

1210
const npmOptions = [
1311
{
@@ -47,19 +45,31 @@ const Npm: React.FC = () => {
4745
},
4846
];
4947

50-
const [value, setValue] = useState("");
48+
const [value, setValue] = useState<string>("");
49+
const [url, setUrl] = useState<string>("");
5150

52-
const [npmConfigFile, setNpmConfigFile] = useState("");
51+
async function getUrl(): Promise<string> {
52+
return new Promise<string>((resolve) => {
53+
const command = new Command("run-npm", ["config", "get", "registry"]);
54+
let url = "";
55+
56+
command.stdout.on("data", (line) => {
57+
url = line;
58+
});
59+
60+
command.on("close", () => {
61+
resolve(url);
62+
setUrl(url);
63+
});
64+
65+
command.spawn();
66+
});
67+
}
5368

5469
useEffect(() => {
70+
getUrl();
5571
const readConfigFile = async () => {
5672
try {
57-
const homeDirPath = await homeDir();
58-
setNpmConfigFile(homeDirPath + ".npmrc");
59-
60-
const fileContent = await readFileFromAppConfig(homeDirPath + ".npmrc");
61-
const url = await XmlUtils.extractRegistry(fileContent);
62-
6373
const option = npmOptions.find(
6474
(option) =>
6575
option.url != npmOptions[0].url &&
@@ -68,8 +78,12 @@ const Npm: React.FC = () => {
6878
);
6979

7080
if (url && !option) {
81+
if (url.includes("https://registry.npmjs.org")) {
82+
setValue(npmOptions[0].value);
83+
return;
84+
}
7185
setValue(npmOptions[npmOptions.length - 1].value);
72-
setCustomUrl(url);
86+
setCustomUrl(url as string);
7387
setDisabled(false);
7488
return;
7589
}
@@ -86,23 +100,20 @@ const Npm: React.FC = () => {
86100
};
87101

88102
readConfigFile();
89-
}, []);
103+
}, [url]);
90104

91105
async function writeUrlToFile(url: string) {
92-
let text = await readFileFromAppConfig(npmConfigFile);
93-
const replacedText = await XmlUtils.replaceRegistry(text, url);
94-
writeFileToAppConfig(npmConfigFile, replacedText);
106+
const command = new Command("run-npm", ["config", "set", "registry", url]);
107+
await command.spawn();
95108
}
96109

97110
const onChange = async (e: RadioChangeEvent) => {
98111
let checkedValue = e.target.value;
99112
setValue(checkedValue);
100113

101114
if (npmOptions[0].value === checkedValue) {
102-
const text = await readFileFromAppConfig(npmConfigFile);
103-
if (text && text != "") {
104-
const replacedText = await XmlUtils.removeRegistryLine(text);
105-
writeFileToAppConfig(npmConfigFile, replacedText);
115+
if (url && url != "") {
116+
await writeUrlToFile("");
106117
}
107118
return;
108119
}

0 commit comments

Comments
 (0)