feat: 脚手架基本ok
This commit is contained in:
12
bin/builders/BuilderFactory.ts
Normal file
12
bin/builders/BuilderFactory.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { IS_MAC } from '@/utils/platform.js';
|
||||
import { IBuilder } from './base.js';
|
||||
import MacBuilder from './MacBuilder.js';
|
||||
|
||||
export default class BuilderFactory {
|
||||
static create(): IBuilder {
|
||||
if (IS_MAC) {
|
||||
return new MacBuilder();
|
||||
}
|
||||
throw new Error('The current system does not support');
|
||||
}
|
||||
}
|
||||
0
bin/builders/LinuxBuilder.ts
Normal file
0
bin/builders/LinuxBuilder.ts
Normal file
65
bin/builders/MacBuilder.ts
Normal file
65
bin/builders/MacBuilder.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import prompts from 'prompts';
|
||||
import { checkRustInstalled, installRust } from '@/helpers/rust.js';
|
||||
import { PakeAppOptions } from '@/types.js';
|
||||
import { IBuilder } from './base.js';
|
||||
import { shellExec } from '@/utils/shell.js';
|
||||
import tauriConf from '../../src-tauri/tauri.conf.json';
|
||||
import { fileURLToPath } from 'url';
|
||||
import log from 'loglevel';
|
||||
|
||||
export default class MacBuilder implements IBuilder {
|
||||
async prepare() {
|
||||
if (checkRustInstalled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const res = await prompts({
|
||||
type: 'confirm',
|
||||
message: 'Detect you have not installed Rust, install it now?',
|
||||
name: 'value',
|
||||
});
|
||||
|
||||
if (res.value) {
|
||||
// TODO 国内有可能会超时
|
||||
await installRust();
|
||||
} else {
|
||||
log.error('Error: Pake need Rust to package your webapp!!!');
|
||||
process.exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
async build(url: string, options: PakeAppOptions) {
|
||||
log.debug('PakeAppOptions', options);
|
||||
|
||||
const { width, height, fullscreen, transparent, resizable, identifier, name } = options;
|
||||
|
||||
const tauriConfWindowOptions = {
|
||||
width,
|
||||
height,
|
||||
fullscreen,
|
||||
transparent,
|
||||
resizable,
|
||||
};
|
||||
|
||||
// TODO 下面这块逻辑还可以再拆 目前比较简单
|
||||
Object.assign(tauriConf.tauri.windows[0], { url, ...tauriConfWindowOptions });
|
||||
tauriConf.package.productName = name;
|
||||
tauriConf.tauri.bundle.identifier = identifier;
|
||||
tauriConf.tauri.bundle.icon = [options.icon];
|
||||
|
||||
const npmDirectory = path.join(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json');
|
||||
await fs.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf), 'utf-8'));
|
||||
|
||||
const code = await shellExec(`cd ${npmDirectory} && npm run build`);
|
||||
const dmgName = `${name}_${'0.2.0'}_universal.dmg`;
|
||||
const appPath = this.getBuildedAppPath(npmDirectory, dmgName);
|
||||
await fs.copyFile(appPath, path.resolve(`${name}_universal.dmg`));
|
||||
}
|
||||
|
||||
getBuildedAppPath(npmDirectory: string, dmgName: string) {
|
||||
return path.join(npmDirectory, 'src-tauri/target/universal-apple-darwin/release/bundle/dmg', dmgName);
|
||||
}
|
||||
}
|
||||
0
bin/builders/WinBulider.ts
Normal file
0
bin/builders/WinBulider.ts
Normal file
16
bin/builders/base.ts
Normal file
16
bin/builders/base.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { PakeAppOptions } from '@/types.js';
|
||||
|
||||
/**
|
||||
* Builder接口
|
||||
* 不同平台打包过程需要实现 prepare 和 build 方法
|
||||
*/
|
||||
export interface IBuilder {
|
||||
/** 前置检查 */
|
||||
prepare(): Promise<void>;
|
||||
/**
|
||||
* 开始打包
|
||||
* @param url 打包url
|
||||
* @param options 配置参数
|
||||
*/
|
||||
build(url: string, options: PakeAppOptions): Promise<void>;
|
||||
}
|
||||
11
bin/builders/common.ts
Normal file
11
bin/builders/common.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import prompts from 'prompts';
|
||||
|
||||
export async function promptText(message: string, initial?: string) {
|
||||
const response = await prompts({
|
||||
type: 'text',
|
||||
name: 'content',
|
||||
message,
|
||||
initial,
|
||||
});
|
||||
return response.content;
|
||||
}
|
||||
Reference in New Issue
Block a user