优化本地文件打包功能

This commit is contained in:
Tlntin
2022-12-29 11:24:22 +08:00
parent c7717ffda5
commit 2f9e104b51
6 changed files with 74 additions and 60 deletions

View File

@@ -75,17 +75,16 @@ export async function mergeTauriConfig(
// } else { // } else {
// fs.rm // fs.rm
// } // }
let file_path = url.slice(8, url.length); const url_exists = await fs.stat(url)
const url_exists = await fs.stat(file_path)
.then(() => true) .then(() => true)
.catch(() => false); .catch(() => false);
if (url_exists) { if (url_exists) {
logger.warn("you input may a local file"); logger.warn("you input may a local file");
tauriConf.pake.windows[0].url_type = "local"; tauriConf.pake.windows[0].url_type = "local";
const file_name = path.basename(file_path); const file_name = path.basename(url);
// const dir_name = path.dirname(url); // const dir_name = path.dirname(url);
const url_path = path.join("dist/", file_name); const url_path = path.join("dist/", file_name);
await fs.copyFile(file_path, url_path); await fs.copyFile(url, url_path);
tauriConf.pake.windows[0].url = file_name; tauriConf.pake.windows[0].url = file_name;
tauriConf.pake.windows[0].url_type = "local"; tauriConf.pake.windows[0].url_type = "local";
} else { } else {

5
bin/cli.ts vendored
View File

@@ -9,6 +9,7 @@ import BuilderFactory from './builders/BuilderFactory.js';
import { checkUpdateTips } from './helpers/updater.js'; import { checkUpdateTips } from './helpers/updater.js';
// @ts-expect-error // @ts-expect-error
import packageJson from '../package.json'; import packageJson from '../package.json';
import logger from './options/logger.js';
program.version(packageJson.version).description('A cli application can package a web page to desktop application.'); program.version(packageJson.version).description('A cli application can package a web page to desktop application.');
@@ -49,9 +50,9 @@ program
const builder = BuilderFactory.create(); const builder = BuilderFactory.create();
await builder.prepare(); await builder.prepare();
// logger.warn("you input url is ", url);
const appOptions = await handleInputOptions(options, url); const appOptions = await handleInputOptions(options, url);
// logger.warn(JSON.stringify(appOptions, null, 4)); // logger.info(JSON.stringify(appOptions, null, 4));
builder.build(url, appOptions); builder.build(url, appOptions);
}); });

2
bin/defaults.ts vendored
View File

@@ -10,7 +10,7 @@ export const DEFAULT_PAKE_OPTIONS: PakeCliOptions = {
userAgent: '', userAgent: '',
showMenu: false, showMenu: false,
showSystemTray: false, showSystemTray: false,
targets: '', targets: 'deb',
// iter_copy_file: false, // iter_copy_file: false,
systemTrayIcon: '', systemTrayIcon: '',
debug: false, debug: false,

11
bin/options/index.ts vendored
View File

@@ -3,15 +3,22 @@ import { getDomain } from '@/utils/url.js';
import { getIdentifier } from '../helpers/tauriConfig.js'; import { getIdentifier } from '../helpers/tauriConfig.js';
import { PakeAppOptions, PakeCliOptions } from '../types.js'; import { PakeAppOptions, PakeCliOptions } from '../types.js';
import { handleIcon } from './icon.js'; import { handleIcon } from './icon.js';
import fs from 'fs/promises';
export default async function handleOptions(options: PakeCliOptions, url: string): Promise<PakeAppOptions> { export default async function handleOptions(options: PakeCliOptions, url: string): Promise<PakeAppOptions> {
const appOptions: PakeAppOptions = { const appOptions: PakeAppOptions = {
...options, ...options,
identifier: '', identifier: '',
}; };
const url_exists = await fs.stat(url)
.then(() => true)
.catch(() => false);
if (!appOptions.name) { if (!appOptions.name) {
appOptions.name = await promptText('please input your application name', getDomain(url)); if (!url_exists) {
appOptions.name = await promptText('please input your application name', getDomain(url));
} else {
appOptions.name = await promptText('please input your application name', "");
}
} }
appOptions.identifier = getIdentifier(appOptions.name, url); appOptions.identifier = getIdentifier(appOptions.name, url);

View File

@@ -1,5 +1,6 @@
import * as Commander from 'commander'; import * as Commander from 'commander';
import { normalizeUrl } from './url.js'; import { normalizeUrl } from './url.js';
import fs from 'fs';
export function validateNumberInput(value: string) { export function validateNumberInput(value: string) {
const parsedValue = Number(value); const parsedValue = Number(value);
@@ -10,9 +11,13 @@ export function validateNumberInput(value: string) {
} }
export function validateUrlInput(url: string) { export function validateUrlInput(url: string) {
if(!fs.existsSync(url)) {
try { try {
return normalizeUrl(url) return normalizeUrl(url)
} catch (error) { } catch (error) {
throw new Commander.InvalidArgumentError(error.message); throw new Commander.InvalidArgumentError(error.message);
} }
} else {
return url;
}
} }

104
dist/cli.js vendored
View File

@@ -3,9 +3,10 @@ import { program } from 'commander';
import log from 'loglevel'; import log from 'loglevel';
import url, { fileURLToPath } from 'url'; import url, { fileURLToPath } from 'url';
import isurl from 'is-url'; import isurl from 'is-url';
import fs from 'fs';
import prompts from 'prompts'; import prompts from 'prompts';
import path from 'path'; import path from 'path';
import fs from 'fs/promises'; import fs$1 from 'fs/promises';
import chalk from 'chalk'; import chalk from 'chalk';
import crypto from 'crypto'; import crypto from 'crypto';
import axios from 'axios'; import axios from 'axios';
@@ -50,7 +51,7 @@ const DEFAULT_PAKE_OPTIONS = {
userAgent: '', userAgent: '',
showMenu: false, showMenu: false,
showSystemTray: false, showSystemTray: false,
targets: '', targets: 'deb',
// iter_copy_file: false, // iter_copy_file: false,
systemTrayIcon: '', systemTrayIcon: '',
debug: false, debug: false,
@@ -1589,11 +1590,16 @@ function validateNumberInput(value) {
return parsedValue; return parsedValue;
} }
function validateUrlInput(url) { function validateUrlInput(url) {
try { if (!fs.existsSync(url)) {
return normalizeUrl(url); try {
return normalizeUrl(url);
}
catch (error) {
throw new Commander.InvalidArgumentError(error.message);
}
} }
catch (error) { else {
throw new Commander.InvalidArgumentError(error.message); return url;
} }
} }
@@ -1670,17 +1676,16 @@ function mergeTauriConfig(url, options, tauriConf) {
// } else { // } else {
// fs.rm // fs.rm
// } // }
let file_path = url.slice(8, url.length); const url_exists = yield fs$1.stat(url)
const url_exists = yield fs.stat(file_path)
.then(() => true) .then(() => true)
.catch(() => false); .catch(() => false);
if (url_exists) { if (url_exists) {
logger.warn("you input may a local file"); logger.warn("you input may a local file");
tauriConf.pake.windows[0].url_type = "local"; tauriConf.pake.windows[0].url_type = "local";
const file_name = path.basename(file_path); const file_name = path.basename(url);
// const dir_name = path.dirname(url); // const dir_name = path.dirname(url);
const url_path = path.join("dist/", file_name); const url_path = path.join("dist/", file_name);
yield fs.copyFile(file_path, url_path); yield fs$1.copyFile(url, url_path);
tauriConf.pake.windows[0].url = file_name; tauriConf.pake.windows[0].url = file_name;
tauriConf.pake.windows[0].url_type = "local"; tauriConf.pake.windows[0].url_type = "local";
} }
@@ -1760,7 +1765,7 @@ function mergeTauriConfig(url, options, tauriConf) {
tauriConf.package.productName = name; tauriConf.package.productName = name;
tauriConf.tauri.bundle.identifier = identifier; tauriConf.tauri.bundle.identifier = identifier;
// 处理应用图标 // 处理应用图标
const exists = yield fs.stat(options.icon) const exists = yield fs$1.stat(options.icon)
.then(() => true) .then(() => true)
.catch(() => false); .catch(() => false);
if (exists) { if (exists) {
@@ -1770,7 +1775,7 @@ function mergeTauriConfig(url, options, tauriConf) {
if (customIconExt === ".ico") { if (customIconExt === ".ico") {
const ico_path = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}_32.ico`); const ico_path = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}_32.ico`);
tauriConf.tauri.bundle.resources = [`png/${name.toLowerCase()}_32.ico`]; tauriConf.tauri.bundle.resources = [`png/${name.toLowerCase()}_32.ico`];
yield fs.copyFile(options.icon, ico_path); yield fs$1.copyFile(options.icon, ico_path);
} }
else { else {
updateIconPath = false; updateIconPath = false;
@@ -1801,7 +1806,7 @@ function mergeTauriConfig(url, options, tauriConf) {
// 处理托盘自定义图标 // 处理托盘自定义图标
let useDefaultIcon = true; // 是否使用默认托盘图标 let useDefaultIcon = true; // 是否使用默认托盘图标
if (systemTrayIcon.length > 0) { if (systemTrayIcon.length > 0) {
const icon_exists = yield fs.stat(systemTrayIcon) const icon_exists = yield fs$1.stat(systemTrayIcon)
.then(() => true) .then(() => true)
.catch(() => false); .catch(() => false);
if (icon_exists) { if (icon_exists) {
@@ -1811,7 +1816,7 @@ function mergeTauriConfig(url, options, tauriConf) {
useDefaultIcon = false; useDefaultIcon = false;
const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`); const trayIcoPath = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}${iconExt}`);
tauriConf.tauri.systemTray.iconPath = `png/${name.toLowerCase()}${iconExt}`; tauriConf.tauri.systemTray.iconPath = `png/${name.toLowerCase()}${iconExt}`;
yield fs.copyFile(systemTrayIcon, trayIcoPath); yield fs$1.copyFile(systemTrayIcon, trayIcoPath);
} }
else { else {
logger.warn(`file type for system tray icon mut be .ico or .png , but you give ${iconExt}`); logger.warn(`file type for system tray icon mut be .ico or .png , but you give ${iconExt}`);
@@ -1849,14 +1854,14 @@ function mergeTauriConfig(url, options, tauriConf) {
} }
} }
let bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } }; let bundleConf = { tauri: { bundle: tauriConf.tauri.bundle } };
yield fs.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8')); yield fs$1.writeFile(configPath, Buffer.from(JSON.stringify(bundleConf, null, 4), 'utf-8'));
const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json'); const pakeConfigPath = path.join(npmDirectory, 'src-tauri/pake.json');
yield fs.writeFile(pakeConfigPath, Buffer.from(JSON.stringify(tauriConf.pake, null, 4), 'utf-8')); yield fs$1.writeFile(pakeConfigPath, Buffer.from(JSON.stringify(tauriConf.pake, null, 4), 'utf-8'));
let tauriConf2 = JSON.parse(JSON.stringify(tauriConf)); let tauriConf2 = JSON.parse(JSON.stringify(tauriConf));
delete tauriConf2.pake; delete tauriConf2.pake;
delete tauriConf2.tauri.bundle; delete tauriConf2.tauri.bundle;
const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json'); const configJsonPath = path.join(npmDirectory, 'src-tauri/tauri.conf.json');
yield fs.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8')); yield fs$1.writeFile(configJsonPath, Buffer.from(JSON.stringify(tauriConf2, null, 4), 'utf-8'));
}); });
} }
@@ -1955,7 +1960,7 @@ function downloadIcon(iconUrl) {
} }
const { path } = yield dir(); const { path } = yield dir();
const iconPath = `${path}/icon.${fileDetails.ext}`; const iconPath = `${path}/icon.${fileDetails.ext}`;
yield fs.writeFile(iconPath, iconData); yield fs$1.writeFile(iconPath, iconData);
return iconPath; return iconPath;
}); });
} }
@@ -1963,8 +1968,16 @@ function downloadIcon(iconUrl) {
function handleOptions(options, url) { function handleOptions(options, url) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const appOptions = Object.assign(Object.assign({}, options), { identifier: '' }); const appOptions = Object.assign(Object.assign({}, options), { identifier: '' });
const url_exists = yield fs$1.stat(url)
.then(() => true)
.catch(() => false);
if (!appOptions.name) { if (!appOptions.name) {
appOptions.name = yield promptText('please input your application name', getDomain(url)); if (!url_exists) {
appOptions.name = yield promptText('please input your application name', getDomain(url));
}
else {
appOptions.name = yield promptText('please input your application name', "");
}
} }
appOptions.identifier = getIdentifier(appOptions.name, url); appOptions.identifier = getIdentifier(appOptions.name, url);
appOptions.icon = yield handleIcon(appOptions); appOptions.icon = yield handleIcon(appOptions);
@@ -2013,29 +2026,22 @@ var tauri$3 = {
active: false active: false
}, },
systemTray: { systemTray: {
iconPath: "png/weread_512.png", iconPath: "/home/tlntin/data/code/rust_study/Pake/src-tauri/png/icon_512.png",
iconAsTemplate: true iconAsTemplate: true
} }
}; };
var build = {
devPath: "../dist",
distDir: "../dist",
beforeBuildCommand: "",
beforeDevCommand: ""
};
var CommonConf = { var CommonConf = {
"package": { "package": {
productName: "WeRead", productName: "ccc",
version: "1.0.0" version: "1.0.0"
}, },
tauri: tauri$3, tauri: tauri$3
build: build
}; };
var windows = [ var windows = [
{ {
url: "https://weread.qq.com/", url: "/home/tlntin/下载/AriaNg-1.3.2-AllInOne/index.html",
transparent: true, transparent: false,
fullscreen: false, fullscreen: false,
width: 1200, width: 1200,
height: 780, height: 780,
@@ -2137,10 +2143,9 @@ var MacConf = {
var tauri = { var tauri = {
bundle: { bundle: {
icon: [ icon: [
"png/weread_256.ico", "/home/tlntin/data/code/rust_study/Pake/src-tauri/png/icon_512.png"
"png/weread_512.png"
], ],
identifier: "com.tw93.weread", identifier: "pake-34d841",
active: true, active: true,
category: "DeveloperTool", category: "DeveloperTool",
copyright: "", copyright: "",
@@ -2156,10 +2161,7 @@ var tauri = {
"librsvg2-dev", "librsvg2-dev",
"gnome-video-effects", "gnome-video-effects",
"gnome-video-effects-extra" "gnome-video-effects-extra"
], ]
files: {
"/usr/share/applications/com-tw93-weread.desktop": "assets/com-tw93-weread.desktop"
}
}, },
externalBin: [ externalBin: [
], ],
@@ -2168,8 +2170,7 @@ var tauri = {
], ],
shortDescription: "", shortDescription: "",
targets: [ targets: [
"deb", "deb"
"appimage"
] ]
} }
}; };
@@ -2234,8 +2235,8 @@ class MacBuilder {
const dmgName = `${name}_${tauriConf.package.version}_${arch}.dmg`; const dmgName = `${name}_${tauriConf.package.version}_${arch}.dmg`;
const appPath = this.getBuildedAppPath(npmDirectory, dmgName); const appPath = this.getBuildedAppPath(npmDirectory, dmgName);
const distPath = path.resolve(`${name}.dmg`); const distPath = path.resolve(`${name}.dmg`);
yield fs.copyFile(appPath, distPath); yield fs$1.copyFile(appPath, distPath);
yield fs.unlink(appPath); yield fs$1.unlink(appPath);
logger.success('Build success!'); logger.success('Build success!');
logger.success('You can find the app installer in', distPath); logger.success('You can find the app installer in', distPath);
}); });
@@ -2279,8 +2280,8 @@ class WinBuilder {
const msiName = `${name}_${tauriConf.package.version}_${arch}_${language}.msi`; const msiName = `${name}_${tauriConf.package.version}_${arch}_${language}.msi`;
const appPath = this.getBuildedAppPath(npmDirectory, msiName); const appPath = this.getBuildedAppPath(npmDirectory, msiName);
const distPath = path.resolve(`${name}.msi`); const distPath = path.resolve(`${name}.msi`);
yield fs.copyFile(appPath, distPath); yield fs$1.copyFile(appPath, distPath);
yield fs.unlink(appPath); yield fs$1.unlink(appPath);
logger.success('Build success!'); logger.success('Build success!');
logger.success('You can find the app installer in', distPath); logger.success('You can find the app installer in', distPath);
}); });
@@ -2330,24 +2331,24 @@ class LinuxBuilder {
const debPath = this.getBuildedAppPath(npmDirectory, "deb", debName); const debPath = this.getBuildedAppPath(npmDirectory, "deb", debName);
const distPath = path.resolve(`${name}.deb`); const distPath = path.resolve(`${name}.deb`);
// 增加文件是否存在验证再决定是否copy文件 // 增加文件是否存在验证再决定是否copy文件
const debExists = yield fs.stat(debPath) const debExists = yield fs$1.stat(debPath)
.then(() => true) .then(() => true)
.catch(() => false); .catch(() => false);
if (debExists) { if (debExists) {
yield fs.copyFile(debPath, distPath); yield fs$1.copyFile(debPath, distPath);
yield fs.unlink(debPath); yield fs$1.unlink(debPath);
logger.success('Build success!'); logger.success('Build success!');
logger.success('You can find the deb app installer in', distPath); logger.success('You can find the deb app installer in', distPath);
} }
const appImageName = `${name}_${tauriConf.package.version}_${arch}.AppImage`; const appImageName = `${name}_${tauriConf.package.version}_${arch}.AppImage`;
const appImagePath = this.getBuildedAppPath(npmDirectory, "appimage", appImageName); const appImagePath = this.getBuildedAppPath(npmDirectory, "appimage", appImageName);
const distAppPath = path.resolve(`${name}.AppImage`); const distAppPath = path.resolve(`${name}.AppImage`);
const appExists = yield fs.stat(appImagePath) const appExists = yield fs$1.stat(appImagePath)
.then(() => true) .then(() => true)
.catch(() => false); .catch(() => false);
if (appExists) { if (appExists) {
yield fs.copyFile(appImagePath, distAppPath); yield fs$1.copyFile(appImagePath, distAppPath);
yield fs.unlink(appImagePath); yield fs$1.unlink(appImagePath);
logger.success('Build success!'); logger.success('Build success!');
logger.success('You can find the Appimage app installer in', distAppPath); logger.success('You can find the Appimage app installer in', distAppPath);
} }
@@ -2509,8 +2510,9 @@ program
} }
const builder = BuilderFactory.create(); const builder = BuilderFactory.create();
yield builder.prepare(); yield builder.prepare();
// logger.warn("you input url is ", url);
const appOptions = yield handleOptions(options, url); const appOptions = yield handleOptions(options, url);
// logger.warn(JSON.stringify(appOptions, null, 4)); // logger.info(JSON.stringify(appOptions, null, 4));
builder.build(url, appOptions); builder.build(url, appOptions);
})); }));
program.parse(); program.parse();