Skip to content

Commit

Permalink
🎨 Unified code style
Browse files Browse the repository at this point in the history
  • Loading branch information
tw93 committed Jun 24, 2023
1 parent 789f49f commit 6785d1a
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 117 deletions.
15 changes: 15 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "lf",
"bracketSameLine": false,
"jsxSingleQuote": false,
"printWidth": 120,
"proseWrap": "preserve",
"quoteProps": "as-needed",
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "all",
"useTabs": false
}
33 changes: 14 additions & 19 deletions bin/builders/BaseBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import path from 'path';
import fsExtra from "fs-extra";
import chalk from "chalk";
import fsExtra from 'fs-extra';
import chalk from 'chalk';
import prompts from 'prompts';

import { PakeAppOptions } from '@/types';
import { checkRustInstalled, installRust } from '@/helpers/rust';
import { mergeConfig } from "@/helpers/merge";
import { mergeConfig } from '@/helpers/merge';
import tauriConfig from '@/helpers/tauriConfig';
import { npmDirectory } from '@/utils/dir';
import { getSpinner } from "@/utils/info";
import { getSpinner } from '@/utils/info';
import { shellExec } from '@/utils/shell';
import { isChinaDomain } from '@/utils/ip';
import { IS_MAC } from "@/utils/platform";
import { IS_MAC } from '@/utils/platform';
import logger from '@/options/logger';

export default abstract class BaseBuilder {
Expand All @@ -23,8 +23,8 @@ export default abstract class BaseBuilder {

async prepare() {
if (!IS_MAC) {
logger.info('⚙︎ The first use requires installing system dependencies.');
logger.info('⚙︎ See more in https://tauri.app/v1/guides/getting-started/prerequisites.');
logger.info(' The first use requires installing system dependencies.');
logger.info(' See more in https://tauri.app/v1/guides/getting-started/prerequisites.');
}

if (!checkRustInstalled()) {
Expand All @@ -42,14 +42,14 @@ export default abstract class BaseBuilder {
}
}

const isChina = await isChinaDomain("www.npmjs.com");
const isChina = await isChinaDomain('www.npmjs.com');
const spinner = getSpinner('Installing package...');
if (isChina) {
logger.info("⚙︎ Located in China, using npm/rsProxy CN mirror.");
const rustProjectDir = path.join(npmDirectory, 'src-tauri', ".cargo");
logger.info('✺ Located in China, using npm/rsProxy CN mirror.');
const rustProjectDir = path.join(npmDirectory, 'src-tauri', '.cargo');
await fsExtra.ensureDir(rustProjectDir);
const projectCnConf = path.join(npmDirectory, "src-tauri", "rust_proxy.toml");
const projectConf = path.join(rustProjectDir, "config");
const projectCnConf = path.join(npmDirectory, 'src-tauri', 'rust_proxy.toml');
const projectConf = path.join(rustProjectDir, 'config');
await fsExtra.copy(projectCnConf, projectConf);
await shellExec(`cd "${npmDirectory}" && npm install --registry=https://registry.npmmirror.com`);
} else {
Expand Down Expand Up @@ -89,19 +89,14 @@ export default abstract class BaseBuilder {
abstract getFileName(): string;

protected getBuildCommand(): string {
return "npm run build";
return 'npm run build';
}

protected getBasePath(): string {
return 'src-tauri/target/release/bundle/';
}

protected getBuildAppPath(npmDirectory: string, fileName: string, fileType: string): string {
return path.join(
npmDirectory,
this.getBasePath(),
fileType.toLowerCase(),
`${fileName}.${fileType}`
);
return path.join(npmDirectory, this.getBasePath(), fileType.toLowerCase(), `${fileName}.${fileType}`);
}
}
7 changes: 3 additions & 4 deletions bin/builders/LinuxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@ import { PakeAppOptions } from '@/types';
import tauriConfig from '@/helpers/tauriConfig';

export default class LinuxBuilder extends BaseBuilder {

constructor(options: PakeAppOptions) {
super(options);
}

getFileName(): string {
const { name } = this.options;
const arch = process.arch === "x64" ? "amd64" : process.arch;
const arch = process.arch === 'x64' ? 'amd64' : process.arch;
return `${name}_${tauriConfig.package.version}_${arch}`;
}

// Customize it, considering that there are all targets.
async build(url: string) {
const targetTypes = ["deb", "appimage"];
const targetTypes = ['deb', 'appimage'];
for (const target of targetTypes) {
if (this.options.targets === target || this.options.targets === "all") {
if (this.options.targets === target || this.options.targets === 'all') {
await this.buildAndCopy(url, target);
}
}
Expand Down
4 changes: 2 additions & 2 deletions bin/builders/MacBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BaseBuilder from './BaseBuilder';
export default class MacBuilder extends BaseBuilder {
constructor(options: PakeAppOptions) {
super(options);
this.options.targets = "dmg";
this.options.targets = 'dmg';
}

getFileName(): string {
Expand All @@ -14,7 +14,7 @@ export default class MacBuilder extends BaseBuilder {
if (this.options.multiArch) {
arch = 'universal';
} else {
arch = process.arch === "arm64" ? "aarch64" : process.arch;
arch = process.arch === 'arm64' ? 'aarch64' : process.arch;
}
return `${name}_${tauriConfig.package.version}_${arch}`;
}
Expand Down
2 changes: 1 addition & 1 deletion bin/builders/WinBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tauriConfig from '@/helpers/tauriConfig';
export default class WinBuilder extends BaseBuilder {
constructor(options: PakeAppOptions) {
super(options);
this.options.targets = "msi";
this.options.targets = 'msi';
}

getFileName(): string {
Expand Down
5 changes: 2 additions & 3 deletions bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ program
.option('--debug', 'Debug mode', DEFAULT.debug)
.version(packageJson.version, '-v, --version', 'Output the current version')
.action(async (url: string, options: PakeCliOptions) => {

await checkUpdateTips();

if (!url) {
program.outputHelp((str) => {
program.outputHelp(str => {
return str
.split('\n')
.filter((line) => !/((-h,|--help)|((-v|-V),|--version))\s+.+$/.test(line))
.filter(line => !/((-h,|--help)|((-v|-V),|--version))\s+.+$/.test(line))
.join('\n');
});
process.exit(0);
Expand Down
19 changes: 6 additions & 13 deletions bin/helpers/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import { npmDirectory } from '@/utils/dir';
import logger from '@/options/logger';
import { PakeAppOptions, PlatformMap } from '@/types';

export async function mergeConfig(
url: string,
options: PakeAppOptions,
tauriConf: any,
) {
export async function mergeConfig(url: string, options: PakeAppOptions, tauriConf: any) {
const {
width,
height,
Expand Down Expand Up @@ -61,7 +57,7 @@ export async function mergeConfig(

const filesToCopyBack = ['cli.js', 'about_pake.html'];
await Promise.all(
filesToCopyBack.map((file) =>
filesToCopyBack.map(file =>
fsExtra.copy(path.join(distBakDir, file), path.join(distDir, file)),
),
);
Expand All @@ -72,8 +68,7 @@ export async function mergeConfig(
} else {
tauriConf.pake.windows[0].url_type = 'web';
// Set the secure domain for calling window.__TAURI__ to the application domain that has been set.
tauriConf.tauri.security.dangerousRemoteDomainIpcAccess[0].domain =
new URL(url).hostname;
tauriConf.tauri.security.dangerousRemoteDomainIpcAccess[0].domain = new URL(url).hostname;
}

const platformMap: PlatformMap = {
Expand All @@ -95,7 +90,8 @@ export async function mergeConfig(
delete tauriConf.tauri.bundle.deb.files;
const validTargets = ['all', 'deb', 'appimage'];
if (validTargets.includes(options.targets)) {
tauriConf.tauri.bundle.targets = options.targets === 'all' ? ['deb', 'appimage'] : [options.targets];
tauriConf.tauri.bundle.targets =
options.targets === 'all' ? ['deb', 'appimage'] : [options.targets];
} else {
logger.warn(
`✼ The target must be one of ${validTargets.join(', ')}, the default 'deb' will be used.`,
Expand Down Expand Up @@ -165,9 +161,7 @@ export async function mergeConfig(
trayIconPath = `png/${name.toLowerCase()}${iconExt}`;
await fsExtra.copy(systemTrayIcon, trayIcoPath);
} else {
logger.warn(
`✼ System tray icon must be .ico or .png, but you provided ${iconExt}.`,
);
logger.warn(`✼ System tray icon must be .ico or .png, but you provided ${iconExt}.`);
logger.warn(`✼ Default system tray icon will be used.`);
}
} catch {
Expand All @@ -192,7 +186,6 @@ export async function mergeConfig(
const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json');
await fsExtra.writeJson(pakeConfigPath, tauriConf.pake, { spaces: 4 });


let tauriConf2 = JSON.parse(JSON.stringify(tauriConf));
delete tauriConf2.pake;
delete tauriConf2.tauri.bundle;
Expand Down
6 changes: 3 additions & 3 deletions bin/helpers/rust.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import chalk from "chalk";
import chalk from 'chalk';
import shelljs from 'shelljs';

import { getSpinner } from "@/utils/info";
import { getSpinner } from '@/utils/info';
import { IS_WIN } from '@/utils/platform';
import { shellExec } from '@/utils/shell';
import { isChinaDomain } from '@/utils/ip';

export async function installRust() {
const isInChina = await isChinaDomain("sh.rustup.rs");
const isInChina = await isChinaDomain('sh.rustup.rs');
const rustInstallScriptForMac = isInChina
? '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'
: "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y";
Expand Down
6 changes: 3 additions & 3 deletions bin/helpers/tauriConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import LinuxConf from '../../src-tauri/tauri.linux.conf.json';
const platformConfigs = {
win32: WinConf,
darwin: MacConf,
linux: LinuxConf
linux: LinuxConf,
};

const {platform} = process;
const { platform } = process;
// @ts-ignore
const platformConfig = platformConfigs[platform];

Expand All @@ -21,7 +21,7 @@ let tauriConfig = {
},
package: CommonConf.package,
build: CommonConf.build,
pake: pakeConf
pake: pakeConf,
};

export default tauriConfig;
11 changes: 7 additions & 4 deletions bin/options/icon.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import path from 'path';
import axios from 'axios';
import fsExtra from "fs-extra";
import fsExtra from 'fs-extra';
import chalk from 'chalk';
import { dir } from 'tmp-promise';


import logger from './logger';
import { npmDirectory } from '@/utils/dir';
import { IS_LINUX, IS_WIN } from '@/utils/platform';
import { getSpinner } from "@/utils/info";
import { getSpinner } from '@/utils/info';
import { fileTypeFromBuffer } from 'file-type';
import { PakeAppOptions } from '@/types';

Expand All @@ -21,7 +20,11 @@ export async function handleIcon(options: PakeAppOptions) {
}
} else {
logger.warn('✼ No icon given, default in use. For a custom icon, use --icon option.');
const iconPath = IS_WIN ? 'src-tauri/png/icon_256.ico' : IS_LINUX ? 'src-tauri/png/icon_512.png' : 'src-tauri/icons/icon.icns';
const iconPath = IS_WIN
? 'src-tauri/png/icon_256.ico'
: IS_LINUX
? 'src-tauri/png/icon_512.png'
: 'src-tauri/icons/icon.icns';
return path.join(npmDirectory, iconPath);
}
}
Expand Down
11 changes: 7 additions & 4 deletions bin/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fsExtra from "fs-extra";
import logger from "@/options/logger";
import fsExtra from 'fs-extra';
import logger from '@/options/logger';

import { handleIcon } from './icon';
import { getDomain } from '@/utils/url';
Expand All @@ -20,14 +20,17 @@ function isValidName(name: string, platform: NodeJS.Platform): boolean {
return !!name && reg.test(name);
}

export default async function handleOptions(options: PakeCliOptions, url: string): Promise<PakeAppOptions> {
export default async function handleOptions(
options: PakeCliOptions,
url: string,
): Promise<PakeAppOptions> {
const { platform } = process;
const isActions = process.env.GITHUB_ACTIONS;
let name = options.name;

const pathExists = await fsExtra.pathExists(url);
if (!options.name) {
const defaultName = pathExists ? "" : resolveAppName(url, platform);
const defaultName = pathExists ? '' : resolveAppName(url, platform);
const promptMessage = 'Enter your application name';
const namePrompt = await promptText(promptMessage, defaultName);
name = namePrompt || defaultName;
Expand Down
10 changes: 5 additions & 5 deletions bin/options/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import log from 'loglevel';

const logger = {
info(...msg: any[]) {
log.info(...msg.map((m) => chalk.white(m)));
log.info(...msg.map(m => chalk.white(m)));
},
debug(...msg: any[]) {
log.debug(...msg);
},
error(...msg: any[]) {
log.error(...msg.map((m) => chalk.red(m)));
log.error(...msg.map(m => chalk.red(m)));
},
warn(...msg: any[]) {
log.info(...msg.map((m) => chalk.yellow(m)));
log.info(...msg.map(m => chalk.yellow(m)));
},
success(...msg: any[]) {
log.info(...msg.map((m) => chalk.green(m)));
}
log.info(...msg.map(m => chalk.green(m)));
},
};

export default logger;
1 change: 0 additions & 1 deletion bin/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export interface PlatformMap {
[key: string]: any;
}
Expand Down
5 changes: 1 addition & 4 deletions bin/utils/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ import { fileURLToPath } from 'url';
const currentModulePath = fileURLToPath(import.meta.url);

// Resolve the parent directory of the current module
export const npmDirectory = path.join(
path.dirname(currentModulePath),
'..'
);
export const npmDirectory = path.join(path.dirname(currentModulePath), '..');
Loading

0 comments on commit 6785d1a

Please sign in to comment.