完成配套pake-cli命令
This commit is contained in:
128
bin/builders/common.ts
vendored
128
bin/builders/common.ts
vendored
@@ -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')
|
||||
);
|
||||
}
|
||||
|
||||
4
bin/builders/tauriConf.js
vendored
4
bin/builders/tauriConf.js
vendored
@@ -1,11 +1,13 @@
|
||||
import CommonConf from '../../src-tauri/tauri.conf.json';
|
||||
import pakeConf from '../../src-tauri/pake.json';
|
||||
import WinConf from '../../src-tauri/tauri.windows.conf.json';
|
||||
import MacConf from '../../src-tauri/tauri.macos.conf.json';
|
||||
import LinuxConf from '../../src-tauri/tauri.linux.conf.json';
|
||||
|
||||
let tauriConf = {
|
||||
package: CommonConf.package,
|
||||
tauri: CommonConf.tauri
|
||||
tauri: CommonConf.tauri,
|
||||
pake: pakeConf
|
||||
}
|
||||
switch (process.platform) {
|
||||
case "win32": {
|
||||
|
||||
9
bin/cli.ts
vendored
9
bin/cli.ts
vendored
@@ -23,6 +23,13 @@ program
|
||||
.option('--no-resizable', 'whether the window can be resizable', DEFAULT_PAKE_OPTIONS.resizable)
|
||||
.option('--fullscreen', 'makes the packaged app start in full screen', DEFAULT_PAKE_OPTIONS.fullscreen)
|
||||
.option('--transparent', 'transparent title bar', DEFAULT_PAKE_OPTIONS.transparent)
|
||||
.option('--user-agent <string>', 'custom user agent', DEFAULT_PAKE_OPTIONS.userAgent)
|
||||
.option('--show-menu', 'show menu in app', DEFAULT_PAKE_OPTIONS.showMenu)
|
||||
.option('--show-system-tray', 'show system tray in app', DEFAULT_PAKE_OPTIONS.showSystemTray)
|
||||
.option('--system-tray-icon <string>', 'custom system tray icon', DEFAULT_PAKE_OPTIONS.systemTrayIcon)
|
||||
// .option('--iter-copy-file',
|
||||
// 'copy all static file to pake app when url is a static file',
|
||||
// DEFAULT_PAKE_OPTIONS.iter_copy_file)
|
||||
.option('--debug', 'debug', DEFAULT_PAKE_OPTIONS.transparent)
|
||||
.action(async (url: string, options: PakeCliOptions) => {
|
||||
checkUpdateTips();
|
||||
@@ -41,7 +48,7 @@ program
|
||||
await builder.prepare();
|
||||
|
||||
const appOptions = await handleInputOptions(options, url);
|
||||
|
||||
// logger.warn(JSON.stringify(appOptions, null, 4));
|
||||
builder.build(url, appOptions);
|
||||
});
|
||||
|
||||
|
||||
5
bin/defaults.ts
vendored
5
bin/defaults.ts
vendored
@@ -7,6 +7,11 @@ export const DEFAULT_PAKE_OPTIONS: PakeCliOptions = {
|
||||
fullscreen: false,
|
||||
resizable: true,
|
||||
transparent: false,
|
||||
userAgent: '',
|
||||
showMenu: false,
|
||||
showSystemTray: false,
|
||||
// iter_copy_file: false,
|
||||
systemTrayIcon: '',
|
||||
debug: false,
|
||||
};
|
||||
|
||||
|
||||
15
bin/types.ts
vendored
15
bin/types.ts
vendored
@@ -20,6 +20,21 @@ export interface PakeCliOptions {
|
||||
/** 是否开启沉浸式头部,默认为 false 不开启 ƒ*/
|
||||
transparent: boolean;
|
||||
|
||||
/** 自定义UA,默认为不开启 ƒ*/
|
||||
userAgent: string;
|
||||
|
||||
/** 开启菜单栏,MacOS默认开启,Windows,Linux默认不开启 ƒ*/
|
||||
showMenu: boolean;
|
||||
|
||||
/** 开启系统托盘,MacOS默认不开启,Windows,Linux默认开启 ƒ*/
|
||||
showSystemTray: boolean;
|
||||
|
||||
/** 托盘图标, Windows、Linux默认和应用图标共用一样的,MacOS需要提别提供, 格式为png或者ico */
|
||||
systemTrayIcon: string;
|
||||
|
||||
// /** 递归拷贝,当url为本地文件路径时候,将文件所在文件夹下面的所有子文件都拷贝到pake静态文件夹,默认不开启 */
|
||||
// iter_copy_file: false;
|
||||
|
||||
/** 调试模式,会输出更多日志 */
|
||||
debug: boolean;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user