完成配套pake-cli命令

This commit is contained in:
Tlntin
2022-12-28 23:26:53 +08:00
parent cd2985a191
commit 1504149635
6 changed files with 323 additions and 36 deletions

128
bin/builders/common.ts vendored
View File

@@ -15,6 +15,7 @@ export async function promptText(message: string, initial?: string) {
return response.content;
}
export async function mergeTauriConfig(
url: string,
options: PakeAppOptions,
@@ -26,6 +27,11 @@ export async function mergeTauriConfig(
fullscreen,
transparent,
resizable,
userAgent,
showMenu,
showSystemTray,
systemTrayIcon,
// iter_copy_file,
identifier,
name,
} = options;
@@ -56,10 +62,84 @@ export async function mergeTauriConfig(
}
}
// logger.warn(JSON.stringify(tauriConf.pake.windows, null, 4));
Object.assign(tauriConf.pake.windows[0], { url, ...tauriConfWindowOptions });
// 判断一下url类型是文件还是网站
// 如果是文件则需要将该文件以及所在文件夹下的所有文件拷贝到src目录下待做
// const src_exists = await fs.stat("src")
// .then(() => true)
// .catch(() => false);
// if (!src_exists) {
// fs.mkdir("src")
// } else {
// fs.rm
// }
const url_exists = await fs.stat(url)
.then(() => true)
.catch(() => false);
if (url_exists) {
tauriConf.pake.windows[0].url_type = "local";
const file_name = path.basename(url);
// const dir_name = path.dirname(url);
const url_path = path.join("dist/", file_name);
await fs.copyFile(url, url_path);
tauriConf.pake.windows[0].url = file_name;
tauriConf.pake.windows[0].url_type = "local";
} else {
tauriConf.pake.windows[0].url_type = "web";
}
// 处理user-agent
logger.warn(userAgent);
if (userAgent.length > 0) {
if (process.platform === "win32") {
tauriConf.pake.user_agent.windows = userAgent;
}
if (process.platform === "linux") {
tauriConf.pake.user_agent.linux = userAgent;
}
if (process.platform === "darwin") {
tauriConf.pake.user_agent.macos = userAgent;
}
}
// 处理菜单栏
if (showMenu) {
if (process.platform === "win32") {
tauriConf.pake.menu.windows = true;
}
if (process.platform === "linux") {
tauriConf.pake.menu.linux = true;
}
if (process.platform === "darwin") {
tauriConf.pake.user_agent.macos = true;
}
}
// 处理托盘
if (showSystemTray) {
if (process.platform === "win32") {
tauriConf.pake.system_tray.windows = true;
}
if (process.platform === "linux") {
tauriConf.pake.system_tray.linux = true;
}
if (process.platform === "darwin") {
tauriConf.pake.system_tray.macos = true;
}
}
Object.assign(tauriConf.tauri.windows[0], { url, ...tauriConfWindowOptions });
tauriConf.package.productName = name;
tauriConf.tauri.bundle.identifier = identifier;
// 处理应用图标
const exists = await fs.stat(options.icon)
.then(() => true)
.catch(() => false);
@@ -97,7 +177,40 @@ export async function mergeTauriConfig(
logger.warn("the custom icon path may not exists. we will use default icon to replace it");
}
// 处理托盘自定义图标
let useDefaultIcon = true; // 是否使用默认托盘图标
if (systemTrayIcon.length > 0) {
const icon_exists = await fs.stat(systemTrayIcon)
.then(() => true)
.catch(() => false);
if (icon_exists) {
// 需要判断图标格式默认只支持ico和png两种
let iconExt = path.extname(systemTrayIcon).toLowerCase();
if (iconExt == ".png" || iconExt == ".icon") {
useDefaultIcon = false;
const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`);
tauriConf.tauri.systemTray.iconPath = `png/${name.toLowerCase()}${iconExt}`;
await fs.copyFile(systemTrayIcon, trayIcoPath);
} else {
logger.warn(`file type for system tray icon mut be .ico or .png , but you give ${iconExt}`);
logger.warn(`system tray icon file will not change with default.`);
}
} else {
logger.warn(`${systemTrayIcon} not exists!`)
logger.warn(`system tray icon file will not change with default.`);
}
}
// 处理托盘默认图标
if (useDefaultIcon) {
if (process.platform === "linux" || process.platform === "win32") {
tauriConf.tauri.systemTray.iconPath = tauriConf.tauri.bundle.icon[0];
} else {
tauriConf.tauri.systemTray.iconPath = "png/icon_512.png";
}
}
// 保存配置文件
let configPath = "";
switch (process.platform) {
case "win32": {
@@ -117,13 +230,22 @@ export async function mergeTauriConfig(
let bundleConf = {tauri: {bundle: tauriConf.tauri.bundle}};
await fs.writeFile(
configPath,
Buffer.from(JSON.stringify(bundleConf), 'utf-8')
Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8')
);
const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json')
await fs.writeFile(
pakeConfigPath,
Buffer.from(JSON.stringify(tauriConf.pake, null, 4), 'utf-8')
);
let tauriConf2 = JSON.parse(JSON.stringify(tauriConf));
delete tauriConf2.pake;
delete tauriConf2.tauri.bundle;
const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json')
await fs.writeFile(
configJsonPath,
Buffer.from(JSON.stringify(tauriConf), 'utf-8')
Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8')
);
}