完成配套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

198
dist/cli.js vendored
View File

@@ -47,6 +47,11 @@ const DEFAULT_PAKE_OPTIONS = {
fullscreen: false,
resizable: true,
transparent: false,
userAgent: '',
showMenu: false,
showSystemTray: false,
// iter_copy_file: false,
systemTrayIcon: '',
debug: false,
};
@@ -1624,7 +1629,9 @@ function promptText(message, initial) {
}
function mergeTauriConfig(url, options, tauriConf) {
return __awaiter(this, void 0, void 0, function* () {
const { width, height, fullscreen, transparent, resizable, identifier, name, } = options;
const { width, height, fullscreen, transparent, resizable, userAgent, showMenu, showSystemTray, systemTrayIcon,
// iter_copy_file,
identifier, name, } = options;
const tauriConfWindowOptions = {
width,
height,
@@ -1650,9 +1657,73 @@ function mergeTauriConfig(url, options, tauriConf) {
process.exit();
}
}
Object.assign(tauriConf.tauri.windows[0], Object.assign({ url }, tauriConfWindowOptions));
// logger.warn(JSON.stringify(tauriConf.pake.windows, null, 4));
Object.assign(tauriConf.pake.windows[0], Object.assign({ 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 = yield 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);
yield 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;
}
}
tauriConf.package.productName = name;
tauriConf.tauri.bundle.identifier = identifier;
// 处理应用图标
const exists = yield fs.stat(options.icon)
.then(() => true)
.catch(() => false);
@@ -1691,6 +1762,41 @@ function mergeTauriConfig(url, options, tauriConf) {
else {
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 = yield 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}`;
yield 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": {
@@ -1707,9 +1813,14 @@ function mergeTauriConfig(url, options, tauriConf) {
}
}
let bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } };
yield fs.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf), 'utf-8'));
yield fs.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8'));
const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json');
yield 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');
yield fs.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf), 'utf-8'));
yield fs.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8'));
});
}
@@ -1859,36 +1970,56 @@ function checkRustInstalled() {
}
var tauri$3 = {
windows: [
{
url: "https://weread.qq.com/",
transparent: true,
fullscreen: false,
width: 1200,
height: 780,
resizable: true
}
],
security: {
csp: null
},
updater: {
active: false
},
systemTray: {
iconPath: "/home/tlntin/data/code/rust_study/Pake/src-tauri/png/code_512.png",
iconAsTemplate: true
}
};
var build = {
devPath: "../dist",
distDir: "../dist",
beforeBuildCommand: "",
beforeDevCommand: ""
};
var CommonConf = {
"package": {
productName: "WeRead",
productName: "baidu",
version: "1.0.0"
},
tauri: tauri$3,
build: build
tauri: tauri$3
};
var windows = [
{
url: "https://www.baidu.com",
transparent: false,
fullscreen: false,
width: 1200,
height: 780,
resizable: true,
url_type: "web"
}
];
var user_agent = {
macos: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15",
linux: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
windows: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
};
var menu = {
macos: true,
linux: true,
windows: false
};
var system_tray = {
macos: false,
linux: true,
windows: true
};
var pakeConf = {
windows: windows,
user_agent: user_agent,
menu: menu,
system_tray: system_tray
};
var tauri$2 = {
@@ -1963,10 +2094,9 @@ var MacConf = {
var tauri = {
bundle: {
icon: [
"png/weread_256.ico",
"png/weread_512.png"
"/home/tlntin/data/code/rust_study/Pake/src-tauri/png/code_512.png"
],
identifier: "com.tw93.weread",
identifier: "pake-f9751d",
active: true,
category: "DeveloperTool",
copyright: "",
@@ -1982,10 +2112,7 @@ var tauri = {
"librsvg2-dev",
"gnome-video-effects",
"gnome-video-effects-extra"
],
files: {
"/usr/share/applications/com-tw93-weread.desktop": "assets/com-tw93-weread.desktop"
}
]
},
externalBin: [
],
@@ -2005,7 +2132,8 @@ var LinuxConf = {
let tauriConf = {
package: CommonConf.package,
tauri: CommonConf.tauri
tauri: CommonConf.tauri,
pake: pakeConf
};
switch (process.platform) {
case "win32": {
@@ -2301,6 +2429,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((url, options) => __awaiter(void 0, void 0, void 0, function* () {
checkUpdateTips();
@@ -2315,6 +2450,7 @@ program
const builder = BuilderFactory.create();
yield builder.prepare();
const appOptions = yield handleOptions(options, url);
// logger.warn(JSON.stringify(appOptions, null, 4));
builder.build(url, appOptions);
}));
program.parse();