Skip to content

Commit

Permalink
fixup some bug in China mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlntin committed Apr 29, 2023
1 parent 87dae7b commit 460ab7b
Show file tree
Hide file tree
Showing 9 changed files with 2,354 additions and 2,336 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ dist
!dist/cli.js
!dist/.gitkeep
src-tauri/.cargo/config
src-tauri/.cargo/
.next
15 changes: 9 additions & 6 deletions bin/builders/LinuxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ export default class LinuxBuilder implements IBuilder {
logger.debug('PakeAppOptions', options);
const { name } = options;
await mergeTauriConfig(url, options, tauriConf);
const isChina = isChinaDomain("www.npmjs.com")

const isChina = await isChinaDomain("www.npmjs.com");
if (isChina) {
// crates.io也顺便换源
logger.info("it's in China, use npm/rust cn mirror")
const rust_project_dir = path.join(npmDirectory, 'src-tauri', ".cargo");
const project_cn_conf = path.join(rust_project_dir, "cn_config.bak");
const e1 = fs.access(rust_project_dir);
if (e1) {
await fs.mkdir(rust_project_dir, { recursive: true });
}
const project_cn_conf = path.join(npmDirectory, "src-tauri", "cn_config.bak");
const project_conf = path.join(rust_project_dir, "config");
fs.copyFile(project_cn_conf, project_conf);

const _ = await shellExec(
`cd "${npmDirectory}" && npm install --registry=https://registry.npmmirror.com && npm run build`
);
Expand Down Expand Up @@ -95,4 +98,4 @@ export default class LinuxBuilder implements IBuilder {
packageName
);
}
}
}
10 changes: 7 additions & 3 deletions bin/builders/MacBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ export default class MacBuilder implements IBuilder {
await mergeTauriConfig(url, options, tauriConf);
let dmgName: string;
if (options.multiArch) {
const isChina = isChinaDomain("www.npmjs.com")
const isChina = await isChinaDomain("www.npmjs.com");
if (isChina) {
// crates.io也顺便换源
logger.info("it's in China, use npm/rust cn mirror")
const rust_project_dir = path.join(npmDirectory, 'src-tauri', ".cargo");
const project_cn_conf = path.join(rust_project_dir, "cn_config.bak");
const e1 = fs.access(rust_project_dir);
if (e1) {
await fs.mkdir(rust_project_dir, { recursive: true });
}
const project_cn_conf = path.join(npmDirectory, "src-tauri", "cn_config.bak");
const project_conf = path.join(rust_project_dir, "config");
fs.copyFile(project_cn_conf, project_conf);

Expand Down
12 changes: 7 additions & 5 deletions bin/builders/WinBulider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ export default class WinBuilder implements IBuilder {
async build(url: string, options: PakeAppOptions) {
logger.debug('PakeAppOptions', options);
const { name } = options;

await mergeTauriConfig(url, options, tauriConf);

const isChina = isChinaDomain("www.npmjs.com")
const isChina = await isChinaDomain("www.npmjs.com")
if (isChina) {
// crates.io也顺便换源
logger.info("it's in China, use npm/rust cn mirror")
const rust_project_dir = path.join(npmDirectory, 'src-tauri', ".cargo");
const project_cn_conf = path.join(rust_project_dir, "cn_config.bak");
const e1 = fs.access(rust_project_dir);
if (e1) {
await fs.mkdir(rust_project_dir, { recursive: true });
}
const project_cn_conf = path.join(npmDirectory, "src-tauri", "cn_config.bak");
const project_conf = path.join(rust_project_dir, "config");
fs.copyFile(project_cn_conf, project_conf);

Expand Down
22 changes: 12 additions & 10 deletions bin/helpers/rust.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { IS_WIN } from '@/utils/platform.js';
import ora from 'ora';
import shelljs from 'shelljs';
import logger from '@/options/logger.js';
import { shellExec } from '../utils/shell.js';
import {isChinaDomain} from '@/utils/ip_addr.js'

const is_china = isChinaDomain("sh.rustup.rs");
let RustInstallScriptFocMac = "";
if (is_china) {
RustInstallScriptFocMac =
'export RUSTUP_DIST_SERVER="https://rsproxy.cn" && export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup" && curl --proto "=https" --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh';
} else {
RustInstallScriptFocMac =
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y";
}
const RustInstallScriptForWin = 'winget install --id Rustlang.Rustup';

export async function installRust() {
const is_china = await isChinaDomain("sh.rustup.rs");
let RustInstallScriptFocMac = "";
if (is_china) {
logger.info("it's in China, use rust cn mirror to install rust");
RustInstallScriptFocMac =
'export RUSTUP_DIST_SERVER="https://rsproxy.cn" && export RUSTUP_UPDATE_ROOT="https://rsproxy.cn/rustup" && curl --proto "=https" --tlsv1.2 -sSf https://rsproxy.cn/rustup-init.sh | sh';
} else {
RustInstallScriptFocMac =
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y";
}
const RustInstallScriptForWin = 'winget install --id Rustlang.Rustup';
const spinner = ora('Downloading Rust').start();
try {
await shellExec(IS_WIN ? RustInstallScriptForWin : RustInstallScriptFocMac);
Expand Down
22 changes: 9 additions & 13 deletions bin/utils/ip_addr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,16 @@ async function isChinaDomain(domain: string): Promise<boolean> {
}

async function isChinaIP(ip: string, domain: string): Promise<boolean> {
return new Promise((resolve, reject) => {
// exec(`ping -c -w 1 ${ip}`, (error, stdout, stderr) => {
ping(ip)
.then((declay)=>{
logger.info(`${domain} latency is ${declay} ms`);
try {
const delay = await ping(ip);
logger.info(`${domain} latency is ${delay} ms`);
// 判断延迟是否超过500ms
resolve(declay > 500);
})
.catch((error)=>{
return delay > 500;
} catch (error) {
// 命令执行出错,返回false
logger.info(`ping ${domain} failed!, is not in China!`);
resolve(false);
})
});
logger.info(`ping ${domain} failed!, is not in China!`);
return false;
}
}

export { isChinaDomain, isChinaIP };
export { isChinaDomain, isChinaIP };
Loading

0 comments on commit 460ab7b

Please sign in to comment.