🎨 CLI is more user-friendly.

This commit is contained in:
Tw93
2023-06-23 16:51:18 +08:00
parent 6a4bb69631
commit 0431bc7c12
10 changed files with 402 additions and 376 deletions

View File

@@ -2,20 +2,25 @@ import path from 'path';
import fsExtra from "fs-extra";
import prompts from 'prompts';
import logger from '@/options/logger';
import { PakeAppOptions } from '@/types';
import { checkRustInstalled, installRust } from '@/helpers/rust';
import { mergeConfig } from "@/helpers/merge";
import tauriConfig from '@/helpers/tauriConfig';
import { npmDirectory } from '@/utils/dir';
import { getSpinner } from "@/utils/info";
import { shellExec } from '@/utils/shell';
import { isChinaDomain } from '@/utils/ip';
import { getSpinner } from "@/utils/info";
import { npmDirectory } from '@/utils/dir';
import { IS_MAC } from "@/utils/platform";
import { checkRustInstalled, installRust } from '@/helpers/rust';
import { PakeAppOptions } from '@/types';
import logger from '@/options/logger';
export default abstract class BaseBuilder {
abstract build(url: string, options: PakeAppOptions): Promise<void>;
protected options: PakeAppOptions;
protected constructor(options: PakeAppOptions) {
this.options = options;
}
async prepare() {
// Windows and Linux need to install necessary build tools.
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#installing.');
@@ -52,9 +57,50 @@ export default abstract class BaseBuilder {
spinner.succeed('Package installed.');
}
protected async runBuildCommand(command: string = "npm run build") {
async buildAndCopy(url: string) {
const { name } = this.options;
await mergeConfig(url, this.options, tauriConfig);
await this.runBuildCommand();
const fileName = this.getFileName();
const appPath = this.getBuildAppPath(npmDirectory, fileName);
const distPath = path.resolve(`${name}.${this.getExtension()}`);
await fsExtra.copy(appPath, distPath);
await fsExtra.remove(appPath);
logger.success('✔ Build success!');
logger.success('✔ App installer located in', distPath);
}
abstract build(url: string): Promise<void>;
abstract getFileName(): string;
abstract getExtension(): string;
protected getArch() {
return process.arch === "x64" ? "amd64" : process.arch;
}
protected getBuildCommand(): string {
return "npm run build";
}
protected runBuildCommand() {
const spinner = getSpinner('Building app...');
setTimeout(() => spinner.stop(), 3000);
await shellExec(`cd "${npmDirectory}" && ${command}`);
return shellExec(`cd ${npmDirectory} && ${this.getBuildCommand()}`);
}
protected getBasePath(): string {
return 'src-tauri/target/release/bundle/';
}
protected getBuildAppPath(npmDirectory: string, fileName: string): string {
return path.join(
npmDirectory,
this.getBasePath(),
this.getExtension().toLowerCase(),
`${fileName}.${this.getExtension()}`
);
}
}