Add the automatic replacement of domestic source function of pake-cli
This commit is contained in:
19
bin/builders/LinuxBuilder.ts
vendored
19
bin/builders/LinuxBuilder.ts
vendored
@@ -5,6 +5,7 @@ import { checkRustInstalled, installRust } from '@/helpers/rust.js';
|
||||
import { PakeAppOptions } from '@/types.js';
|
||||
import { IBuilder } from './base.js';
|
||||
import { shellExec } from '@/utils/shell.js';
|
||||
import {isChinaDomain} from '@/utils/ip_addr.js';
|
||||
// @ts-expect-error 加上resolveJsonModule rollup会打包报错
|
||||
// import tauriConf from '../../src-tauri/tauri.windows.conf.json';
|
||||
import tauriConf from './tauriConf.js';
|
||||
@@ -44,9 +45,23 @@ export default class LinuxBuilder implements IBuilder {
|
||||
async build(url: string, options: PakeAppOptions) {
|
||||
logger.debug('PakeAppOptions', options);
|
||||
const { name } = options;
|
||||
|
||||
await mergeTauriConfig(url, options, tauriConf);
|
||||
const _ = await shellExec(`cd ${npmDirectory} && npm install && npm run build`);
|
||||
const isChina = isChinaDomain("www.npmjs.com")
|
||||
|
||||
if (isChina) {
|
||||
// crates.io也顺便换源
|
||||
const rust_project_dir = path.join(npmDirectory, 'src-tauri', ".cargo");
|
||||
const project_cn_conf = path.join(rust_project_dir, "cn_config.bak");
|
||||
const project_conf = path.join(rust_project_dir, "config");
|
||||
fs.copyFile(project_cn_conf, project_conf);
|
||||
fs.unlink(project_cn_conf);
|
||||
|
||||
const _ = await shellExec(
|
||||
`cd ${npmDirectory} && npm install --registry=https://registry.npmmirror.com && npm run build`
|
||||
);
|
||||
} else {
|
||||
const _ = await shellExec(`cd ${npmDirectory} && npm install && npm run build`);
|
||||
}
|
||||
let arch: string;
|
||||
if (process.arch === "x64") {
|
||||
arch = "amd64";
|
||||
|
||||
26
bin/builders/MacBuilder.ts
vendored
26
bin/builders/MacBuilder.ts
vendored
@@ -11,6 +11,7 @@ import tauriConf from './tauriConf.js';
|
||||
import log from 'loglevel';
|
||||
import { mergeTauriConfig } from './common.js';
|
||||
import { npmDirectory } from '@/utils/dir.js';
|
||||
import {isChinaDomain} from '@/utils/ip_addr.js';
|
||||
import logger from '@/options/logger.js';
|
||||
|
||||
export default class MacBuilder implements IBuilder {
|
||||
@@ -41,10 +42,31 @@ export default class MacBuilder implements IBuilder {
|
||||
await mergeTauriConfig(url, options, tauriConf);
|
||||
let dmgName: string;
|
||||
if (options.multiArch) {
|
||||
await shellExec(`cd "${npmDirectory}" && npm install --verbose && npm run build:mac`);
|
||||
const isChina = isChinaDomain("www.npmjs.com")
|
||||
if (isChina) {
|
||||
// crates.io也顺便换源
|
||||
const rust_project_dir = path.join(npmDirectory, 'src-tauri', ".cargo");
|
||||
const project_cn_conf = path.join(rust_project_dir, "cn_config.bak");
|
||||
const project_conf = path.join(rust_project_dir, "config");
|
||||
fs.copyFile(project_cn_conf, project_conf);
|
||||
fs.unlink(project_cn_conf);
|
||||
|
||||
const _ = await shellExec(
|
||||
`cd ${npmDirectory} && npm install --registry=https://registry.npmmirror.com && npm run build:mac`
|
||||
);
|
||||
} else {
|
||||
const _ = await shellExec(`cd ${npmDirectory} && npm install && npm run build:mac`);
|
||||
}
|
||||
dmgName = `${name}_${tauriConf.package.version}_universal.dmg`;
|
||||
} else {
|
||||
await shellExec(`cd "${npmDirectory}" && npm install --verbose && npm run build`);
|
||||
const isChina = isChinaDomain("www.npmjs.com")
|
||||
if (isChina) {
|
||||
const _ = await shellExec(
|
||||
`cd ${npmDirectory} && npm install --registry=https://registry.npmmirror.com && npm run build`
|
||||
);
|
||||
} else {
|
||||
const _ = await shellExec(`cd ${npmDirectory} && npm install && npm run build`);
|
||||
}
|
||||
let arch = "x64";
|
||||
if (process.arch === "arm64") {
|
||||
arch = "aarch64";
|
||||
|
||||
17
bin/builders/WinBulider.ts
vendored
17
bin/builders/WinBulider.ts
vendored
@@ -13,6 +13,7 @@ import { fileURLToPath } from 'url';
|
||||
import logger from '@/options/logger.js';
|
||||
import { mergeTauriConfig } from './common.js';
|
||||
import { npmDirectory } from '@/utils/dir.js';
|
||||
import {isChinaDomain} from '@/utils/ip_addr.js';
|
||||
|
||||
export default class WinBuilder implements IBuilder {
|
||||
async prepare() {
|
||||
@@ -47,7 +48,21 @@ export default class WinBuilder implements IBuilder {
|
||||
|
||||
await mergeTauriConfig(url, options, tauriConf);
|
||||
|
||||
const _ = await shellExec(`cd ${npmDirectory} && npm install && npm run build`);
|
||||
const isChina = isChinaDomain("www.npmjs.com")
|
||||
if (isChina) {
|
||||
// crates.io也顺便换源
|
||||
const rust_project_dir = path.join(npmDirectory, 'src-tauri', ".cargo");
|
||||
const project_cn_conf = path.join(rust_project_dir, "cn_config.bak");
|
||||
const project_conf = path.join(rust_project_dir, "config");
|
||||
fs.copyFile(project_cn_conf, project_conf);
|
||||
fs.unlink(project_cn_conf);
|
||||
|
||||
const _ = await shellExec(
|
||||
`cd ${npmDirectory} && npm install --registry=https://registry.npmmirror.com && npm run build`
|
||||
);
|
||||
} else {
|
||||
const _ = await shellExec(`cd ${npmDirectory} && npm install && npm run build`);
|
||||
}
|
||||
const language = tauriConf.tauri.bundle.windows.wix.language[0];
|
||||
const arch = process.arch;
|
||||
const msiName = `${name}_${tauriConf.package.version}_${arch}_${language}.msi`;
|
||||
|
||||
14
bin/helpers/rust.ts
vendored
14
bin/helpers/rust.ts
vendored
@@ -2,9 +2,17 @@ import { IS_WIN } from '@/utils/platform.js';
|
||||
import ora from 'ora';
|
||||
import shelljs from 'shelljs';
|
||||
import { shellExec } from '../utils/shell.js';
|
||||
import {isChinaDomain} from '@/utils/ip_addr.js'
|
||||
|
||||
const RustInstallScriptFocMac =
|
||||
"curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y";
|
||||
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() {
|
||||
@@ -21,5 +29,5 @@ export async function installRust() {
|
||||
}
|
||||
|
||||
export function checkRustInstalled() {
|
||||
return shelljs.exec('rustc --version', { silent: true }).code === 0;
|
||||
return shelljs.exec('source "$HOME/.cargo/env" && rustc --version', { silent: true }).code === 0;
|
||||
}
|
||||
|
||||
35
bin/utils/ip_addr.ts
vendored
Normal file
35
bin/utils/ip_addr.ts
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
import { exec } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import dns from 'dns';
|
||||
|
||||
const resolve = promisify(dns.resolve);
|
||||
|
||||
async function isChinaDomain(domain: string): Promise<boolean> {
|
||||
try {
|
||||
// 解析域名为IP地址
|
||||
const [ip] = await resolve(domain);
|
||||
return await isChinaIP(ip);
|
||||
} catch (error) {
|
||||
// 域名无法解析,返回false
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function isChinaIP(ip: string): Promise<boolean> {
|
||||
return new Promise((resolve, reject) => {
|
||||
exec(`ping -c 1 -w 1 ${ip}`, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
// 命令执行出错,返回false
|
||||
resolve(false);
|
||||
} else {
|
||||
// 解析输出信息,提取延迟值
|
||||
const match = stdout.match(/time=(\d+\.\d+) ms/);
|
||||
const latency = match ? parseFloat(match[1]) : 0;
|
||||
// 判断延迟是否超过100ms
|
||||
resolve(latency > 100);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export { isChinaDomain, isChinaIP };
|
||||
14
src-tauri/.cargo/cn_config.bak
Normal file
14
src-tauri/.cargo/cn_config.bak
Normal file
@@ -0,0 +1,14 @@
|
||||
[source.crates-io]
|
||||
# To use sparse index, change 'rsproxy' to 'rsproxy-sparse'
|
||||
replace-with = 'rsproxy'
|
||||
|
||||
[source.rsproxy]
|
||||
registry = "https://rsproxy.cn/crates.io-index"
|
||||
[source.rsproxy-sparse]
|
||||
registry = "sparse+https://rsproxy.cn/index/"
|
||||
|
||||
[registries.rsproxy]
|
||||
index = "https://rsproxy.cn/crates.io-index"
|
||||
|
||||
[net]
|
||||
git-fetch-with-cli = true
|
||||
Reference in New Issue
Block a user