🎨 Optimize the use of CLI.
This commit is contained in:
3
bin/builders/BaseBuilder.ts
vendored
3
bin/builders/BaseBuilder.ts
vendored
@@ -1,3 +1,4 @@
|
||||
import ora from "ora";
|
||||
import path from 'path';
|
||||
import fsExtra from "fs-extra";
|
||||
import prompts from 'prompts';
|
||||
@@ -39,6 +40,8 @@ export default abstract class BaseBuilder {
|
||||
}
|
||||
|
||||
protected async runBuildCommand(directory: string, command: string) {
|
||||
const spinner = ora('Building...').start();
|
||||
setTimeout(() => spinner.succeed(), 5000);
|
||||
const isChina = await isChinaDomain("www.npmjs.com");
|
||||
if (isChina) {
|
||||
logger.info("Located in China, using npm/Rust CN mirror.");
|
||||
|
||||
21
bin/builders/BuilderProvider.ts
vendored
21
bin/builders/BuilderProvider.ts
vendored
@@ -3,19 +3,20 @@ import MacBuilder from './MacBuilder';
|
||||
import WinBuilder from './WinBuilder';
|
||||
import LinuxBuilder from './LinuxBuilder';
|
||||
|
||||
import { IS_MAC, IS_WIN, IS_LINUX } from '@/utils/platform';
|
||||
const { platform } = process;
|
||||
|
||||
const buildersMap: Record<string, new () => BaseBuilder> = {
|
||||
darwin: MacBuilder,
|
||||
win32: WinBuilder,
|
||||
linux: LinuxBuilder,
|
||||
};
|
||||
|
||||
export default class BuilderProvider {
|
||||
static create(): BaseBuilder {
|
||||
if (IS_MAC) {
|
||||
return new MacBuilder();
|
||||
const Builder = buildersMap[platform];
|
||||
if (!Builder) {
|
||||
throw new Error('The current system is not supported!');
|
||||
}
|
||||
if (IS_WIN) {
|
||||
return new WinBuilder();
|
||||
}
|
||||
if (IS_LINUX) {
|
||||
return new LinuxBuilder();
|
||||
}
|
||||
throw new Error('The current system is not supported!');
|
||||
return new Builder();
|
||||
}
|
||||
}
|
||||
|
||||
4
bin/cli.ts
vendored
4
bin/cli.ts
vendored
@@ -1,3 +1,4 @@
|
||||
import ora from "ora";
|
||||
import log from 'loglevel';
|
||||
import { program } from 'commander';
|
||||
|
||||
@@ -46,10 +47,11 @@ program
|
||||
log.setLevel('debug');
|
||||
}
|
||||
|
||||
const spinner = ora('Preparing...').start();
|
||||
const builder = BuilderProvider.create();
|
||||
await builder.prepare();
|
||||
|
||||
const appOptions = await handleInputOptions(options, url);
|
||||
spinner.succeed();
|
||||
|
||||
log.debug('PakeAppOptions', appOptions);
|
||||
|
||||
|
||||
6
bin/helpers/merge.ts
vendored
6
bin/helpers/merge.ts
vendored
@@ -127,19 +127,19 @@ export async function mergeConfig(
|
||||
const platformIconMap: PlatformMap = {
|
||||
win32: {
|
||||
fileExt: '.ico',
|
||||
path: `png/${name.toLowerCase()}_32.ico`,
|
||||
path: `png/${name.toLowerCase()}_256.ico`,
|
||||
defaultIcon: 'png/icon_256.ico',
|
||||
message: 'Windows icon must be .ico and 256x256px.',
|
||||
},
|
||||
linux: {
|
||||
fileExt: '.png',
|
||||
path: `png/${name.toLowerCase()}_32.png`,
|
||||
path: `png/${name.toLowerCase()}_512.png`,
|
||||
defaultIcon: 'png/icon_512.png',
|
||||
message: 'Linux icon must be .png and 512x512px.',
|
||||
},
|
||||
darwin: {
|
||||
fileExt: '.icns',
|
||||
path: `icons/${name.toLowerCase()}_32.icns`,
|
||||
path: `icons/${name.toLowerCase()}.icns`,
|
||||
defaultIcon: 'icons/icon.icns',
|
||||
message: 'MacOS icon must be .icns type.',
|
||||
},
|
||||
|
||||
6
bin/options/icon.ts
vendored
6
bin/options/icon.ts
vendored
@@ -25,11 +25,10 @@ export async function handleIcon(options: PakeAppOptions) {
|
||||
|
||||
export async function downloadIcon(iconUrl: string) {
|
||||
try {
|
||||
const iconResponse = await axios.get(iconUrl, {
|
||||
responseType: 'arraybuffer',
|
||||
});
|
||||
const iconResponse = await axios.get(iconUrl, { responseType: 'arraybuffer' });
|
||||
|
||||
const iconData = await iconResponse.data;
|
||||
|
||||
if (!iconData) {
|
||||
return null;
|
||||
}
|
||||
@@ -42,6 +41,7 @@ export async function downloadIcon(iconUrl: string) {
|
||||
const { path: tempPath } = await dir();
|
||||
const iconPath = `${tempPath}/icon.${fileDetails.ext}`;
|
||||
await fsExtra.outputFile(iconPath, iconData);
|
||||
|
||||
return iconPath;
|
||||
} catch (error) {
|
||||
if (error.response && error.response.status === 404) {
|
||||
|
||||
18
bin/utils/ip.ts
vendored
18
bin/utils/ip.ts
vendored
@@ -10,7 +10,8 @@ const ping = async (host: string) => {
|
||||
const ip = await lookup(host);
|
||||
const start = new Date();
|
||||
|
||||
return new Promise<number>((resolve, reject) => {
|
||||
// Prevent timeouts from affecting user experience.
|
||||
const requestPromise = new Promise<number>((resolve, reject) => {
|
||||
const req = http.get(`http://${ip.address}`, (res) => {
|
||||
const delay = new Date().getTime() - start.getTime();
|
||||
res.resume();
|
||||
@@ -21,14 +22,23 @@ const ping = async (host: string) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
const timeoutPromise = new Promise<number>((_, reject) => {
|
||||
setTimeout(() => {
|
||||
reject(new Error('Request timed out after 3 seconds'));
|
||||
}, 3000);
|
||||
});
|
||||
|
||||
return Promise.race([requestPromise, timeoutPromise]);
|
||||
};
|
||||
|
||||
|
||||
async function isChinaDomain(domain: string): Promise<boolean> {
|
||||
try {
|
||||
const [ip] = await resolve(domain);
|
||||
return await isChinaIP(ip, domain);
|
||||
} catch (error) {
|
||||
logger.info(`${domain} can't be parse!`);
|
||||
logger.debug(`${domain} can't be parse!`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -36,10 +46,10 @@ async function isChinaDomain(domain: string): Promise<boolean> {
|
||||
async function isChinaIP(ip: string, domain: string): Promise<boolean> {
|
||||
try {
|
||||
const delay = await ping(ip);
|
||||
logger.info(`${domain} latency is ${delay} ms`);
|
||||
logger.debug(`${domain} latency is ${delay} ms`);
|
||||
return delay > 500;
|
||||
} catch (error) {
|
||||
logger.info(`ping ${domain} failed!`);
|
||||
logger.debug(`ping ${domain} failed!`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user