🎨 Optimize the code structure

This commit is contained in:
Tw93
2023-08-04 15:58:08 +08:00
parent c89b65694f
commit 1ac67605ba
13 changed files with 579 additions and 568 deletions

716
dist/cli.js vendored
View File

@@ -1,307 +1,313 @@
import chalk from 'chalk';
import { InvalidArgumentError, program } from 'commander';
import log from 'loglevel';
import path from 'path';
import fsExtra from 'fs-extra';
import prompts from 'prompts';
import shelljs from 'shelljs';
import crypto from 'crypto';
import ora from 'ora';
import { fileURLToPath } from 'url';
import dns from 'dns';
import http from 'http';
import { promisify } from 'util';
import updateNotifier from 'update-notifier';
import axios from 'axios';
import { dir } from 'tmp-promise';
import { fileTypeFromBuffer } from 'file-type';
import psl from 'psl';
import isUrl from 'is-url';
import fs from 'fs';
var name = "pake-cli";
var version = "2.2.5";
var description = "🤱🏻 Turn any webpage into a desktop app with Rust. 🤱🏻 很简单的用 Rust 打包网页生成很小的桌面 App。";
var engines = {
node: ">=16.0.0"
};
var bin = {
pake: "./cli.js"
};
var repository = {
type: "git",
url: "https://github.com/tw93/pake.git"
};
var author = {
name: "Tw93",
email: "tw93@qq.com"
};
var keywords = [
"pake",
"pake-cli",
"rust",
"tauri",
"no-electron",
"productivity"
];
var files = [
"dist",
"src-tauri",
"cli.js"
];
var scripts = {
start: "npm run dev",
dev: "npm run tauri dev",
build: "npm run tauri build --release",
"build:mac": "npm run tauri build -- --target universal-apple-darwin",
"build:all-unix": "chmod +x ./script/build.sh && ./script/build.sh",
"build:all-windows": "pwsh ./script/build.ps1",
analyze: "cd src-tauri && cargo bloat --release --crates",
tauri: "tauri",
cli: "rollup -c rollup.config.js --watch",
"cli:build": "cross-env NODE_ENV=production rollup -c rollup.config.js",
prepublishOnly: "npm run cli:build"
};
var type = "module";
var exports = "./dist/pake.js";
var license = "MIT";
var dependencies = {
"@tauri-apps/api": "^1.4.0",
"@tauri-apps/cli": "^1.4.0",
axios: "^1.1.3",
chalk: "^5.1.2",
commander: "^11.0.0",
"file-type": "^18.0.0",
"fs-extra": "^11.1.0",
"is-url": "^1.2.4",
loglevel: "^1.8.1",
ora: "^6.1.2",
prompts: "^2.4.2",
psl: "^1.9.0",
shelljs: "^0.8.5",
"tmp-promise": "^3.0.3",
"update-notifier": "^6.0.2"
};
var devDependencies = {
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-commonjs": "^23.0.2",
"@rollup/plugin-json": "^5.0.2",
"@rollup/plugin-terser": "^0.1.0",
"@types/fs-extra": "^9.0.13",
"@types/is-url": "^1.2.30",
"@types/page-icon": "^0.3.4",
"@types/prompts": "^2.4.1",
"@types/psl": "^1.1.0",
"@types/shelljs": "^0.8.11",
"@types/tmp": "^0.2.3",
"@types/update-notifier": "^6.0.1",
"app-root-path": "^3.1.0",
"cross-env": "^7.0.3",
rollup: "^3.3.0",
"rollup-plugin-typescript2": "^0.34.1",
tslib: "^2.4.1",
typescript: "^4.9.3"
};
var packageJson = {
name: name,
version: version,
description: description,
engines: engines,
bin: bin,
repository: repository,
author: author,
keywords: keywords,
files: files,
scripts: scripts,
type: type,
exports: exports,
license: license,
dependencies: dependencies,
devDependencies: devDependencies
};
var windows = [
{
url: "https://weread.qq.com/",
transparent: true,
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: false,
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$3 = {
security: {
csp: null,
dangerousRemoteDomainIpcAccess: [
{
domain: "weread.qq.com",
windows: [
"pake"
],
enableTauriAPI: true
}
]
},
updater: {
active: false
},
systemTray: {
iconPath: "png/weread_512.png",
iconAsTemplate: true
},
allowlist: {
all: true,
fs: {
all: true,
scope: [
"$DOWNLOAD/*"
]
}
}
};
var build = {
withGlobalTauri: true,
devPath: "../dist",
distDir: "../dist",
beforeBuildCommand: "",
beforeDevCommand: ""
};
var CommonConf = {
"package": {
productName: "WeRead",
version: "1.0.0"
},
tauri: tauri$3,
build: build
};
var tauri$2 = {
bundle: {
icon: [
"png/weread_256.ico",
"png/weread_32.ico"
],
identifier: "com.pake.weread",
active: true,
category: "DeveloperTool",
copyright: "",
externalBin: [
],
longDescription: "",
resources: [
"png/weread_32.ico"
],
shortDescription: "",
targets: [
"msi"
],
windows: {
certificateThumbprint: null,
digestAlgorithm: "sha256",
timestampUrl: "",
wix: {
language: [
"en-US"
],
template: "assets/main.wxs"
}
}
}
};
var WinConf = {
tauri: tauri$2
};
var tauri$1 = {
bundle: {
icon: [
"icons/weread.icns"
],
identifier: "com.pake.weread",
active: true,
category: "DeveloperTool",
copyright: "",
externalBin: [
],
longDescription: "",
macOS: {
entitlements: null,
exceptionDomain: "",
frameworks: [
],
providerShortName: null,
signingIdentity: null
},
resources: [
],
shortDescription: "",
targets: [
"dmg"
]
}
};
var MacConf = {
tauri: tauri$1
};
var tauri = {
bundle: {
icon: [
"png/weread_512.png"
],
identifier: "com.pake.weread",
active: true,
category: "DeveloperTool",
copyright: "",
deb: {
depends: [
"curl",
"wget"
],
files: {
"/usr/share/applications/com-pake-weread.desktop": "assets/com-pake-weread.desktop"
}
},
externalBin: [
],
longDescription: "",
resources: [
],
shortDescription: "",
targets: [
"deb",
"appimage"
]
}
};
var LinuxConf = {
tauri: tauri
};
import chalk from 'chalk';
import { InvalidArgumentError, program } from 'commander';
import log from 'loglevel';
import path from 'path';
import fsExtra from 'fs-extra';
import prompts from 'prompts';
import shelljs from 'shelljs';
import crypto from 'crypto';
import ora from 'ora';
import { fileURLToPath } from 'url';
import dns from 'dns';
import http from 'http';
import { promisify } from 'util';
import fs from 'fs';
import updateNotifier from 'update-notifier';
import axios from 'axios';
import { dir } from 'tmp-promise';
import { fileTypeFromBuffer } from 'file-type';
import psl from 'psl';
import isUrl from 'is-url';
var name = "pake-cli";
var version = "2.2.5";
var description = "🤱🏻 Turn any webpage into a desktop app with Rust. 🤱🏻 用 Rust 轻松构建轻量级多端桌面应用。";
var engines = {
node: ">=16.0.0"
};
var bin = {
pake: "./cli.js"
};
var repository = {
type: "git",
url: "https://github.com/tw93/pake.git"
};
var author = {
name: "Tw93",
email: "tw93@qq.com"
};
var keywords = [
"pake",
"pake-cli",
"rust",
"tauri",
"no-electron",
"productivity"
];
var files = [
"dist",
"src-tauri",
"cli.js"
];
var scripts = {
start: "npm run dev",
dev: "npm run tauri dev",
build: "npm run tauri build --release",
"build:debug": "npm run tauri build -- --debug",
"build:mac": "npm run tauri build -- --target universal-apple-darwin",
"build:all-unix": "chmod +x ./script/build.sh && ./script/build.sh",
"build:all-windows": "pwsh ./script/build.ps1",
analyze: "cd src-tauri && cargo bloat --release --crates",
tauri: "tauri",
cli: "rollup -c rollup.config.js --watch",
"cli:dev": "cross-env NODE_ENV=development rollup -c rollup.config.js -w",
"cli:build": "cross-env NODE_ENV=production rollup -c rollup.config.js",
prepublishOnly: "npm run cli:build"
};
var type = "module";
var exports = "./dist/pake.js";
var license = "MIT";
var dependencies = {
"@tauri-apps/api": "^1.4.0",
"@tauri-apps/cli": "^1.4.0",
axios: "^1.1.3",
chalk: "^5.1.2",
commander: "^11.0.0",
"file-type": "^18.0.0",
"fs-extra": "^11.1.0",
"is-url": "^1.2.4",
loglevel: "^1.8.1",
ora: "^6.1.2",
prompts: "^2.4.2",
psl: "^1.9.0",
shelljs: "^0.8.5",
"tmp-promise": "^3.0.3",
"update-notifier": "^6.0.2"
};
var devDependencies = {
"@rollup/plugin-alias": "^4.0.2",
"@rollup/plugin-commonjs": "^23.0.2",
"@rollup/plugin-json": "^5.0.2",
"@rollup/plugin-replace": "^5.0.2",
"@rollup/plugin-terser": "^0.1.0",
"@types/fs-extra": "^9.0.13",
"@types/is-url": "^1.2.30",
"@types/page-icon": "^0.3.4",
"@types/prompts": "^2.4.1",
"@types/psl": "^1.1.0",
"@types/shelljs": "^0.8.11",
"@types/tmp": "^0.2.3",
"@types/update-notifier": "^6.0.1",
"app-root-path": "^3.1.0",
"cross-env": "^7.0.3",
rollup: "^3.3.0",
"rollup-plugin-typescript2": "^0.34.1",
tslib: "^2.4.1",
typescript: "^4.9.3"
};
var packageJson = {
name: name,
version: version,
description: description,
engines: engines,
bin: bin,
repository: repository,
author: author,
keywords: keywords,
files: files,
scripts: scripts,
type: type,
exports: exports,
license: license,
dependencies: dependencies,
devDependencies: devDependencies
};
var windows = [
{
url: "https://weread.qq.com/",
transparent: true,
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: false,
linux: false,
windows: false
};
var system_tray = {
macos: false,
linux: true,
windows: true
};
var inject = [
];
var pakeConf = {
windows: windows,
user_agent: user_agent,
menu: menu,
system_tray: system_tray,
inject: inject
};
var tauri$3 = {
security: {
csp: null,
dangerousRemoteDomainIpcAccess: [
{
domain: "weread.qq.com",
windows: [
"pake"
],
enableTauriAPI: true
}
]
},
updater: {
active: false
},
systemTray: {
iconPath: "png/icon_512.png",
iconAsTemplate: true
},
allowlist: {
all: true,
fs: {
all: true,
scope: [
"$DOWNLOAD/*"
]
}
}
};
var build = {
withGlobalTauri: true,
devPath: "../dist",
distDir: "../dist",
beforeBuildCommand: "",
beforeDevCommand: ""
};
var CommonConf = {
"package": {
productName: "WeRead",
version: "1.0.0"
},
tauri: tauri$3,
build: build
};
var tauri$2 = {
bundle: {
icon: [
"png/weread_256.ico",
"png/weread_32.ico"
],
identifier: "com.pake.weread",
active: true,
category: "DeveloperTool",
copyright: "",
externalBin: [
],
longDescription: "",
resources: [
"png/weread_32.ico"
],
shortDescription: "",
targets: [
"msi"
],
windows: {
certificateThumbprint: null,
digestAlgorithm: "sha256",
timestampUrl: "",
wix: {
language: [
"en-US"
],
template: "assets/main.wxs"
}
}
}
};
var WinConf = {
tauri: tauri$2
};
var tauri$1 = {
bundle: {
icon: [
"icons/weread.icns"
],
identifier: "com.pake.weread",
active: true,
category: "DeveloperTool",
copyright: "",
externalBin: [
],
longDescription: "",
macOS: {
entitlements: null,
exceptionDomain: "",
frameworks: [
],
providerShortName: null,
signingIdentity: null
},
resources: [
],
shortDescription: "",
targets: [
"dmg"
]
}
};
var MacConf = {
tauri: tauri$1
};
var tauri = {
bundle: {
icon: [
"png/weread_512.png"
],
identifier: "com.pake.weread",
active: true,
category: "DeveloperTool",
copyright: "",
deb: {
depends: [
"curl",
"wget"
],
files: {
"/usr/share/applications/com-pake-weread.desktop": "assets/com-pake-weread.desktop"
}
},
externalBin: [
],
longDescription: "",
resources: [
],
shortDescription: "",
targets: [
"deb",
"appimage"
]
}
};
var LinuxConf = {
tauri: tauri
};
const platformConfigs = {
win32: WinConf,
darwin: MacConf,
@@ -318,8 +324,8 @@ let tauriConfig = {
package: CommonConf.package,
build: CommonConf.build,
pake: pakeConf,
};
};
// Generates an identifier based on the given URL.
function getIdentifier(url) {
const postFixHash = crypto.createHash('md5').update(url).digest('hex').substring(0, 6);
@@ -347,19 +353,19 @@ function getSpinner(text) {
spinner: loadingType,
color: 'cyan',
}).start();
}
}
const { platform: platform$1 } = process;
const IS_MAC = platform$1 === 'darwin';
const IS_WIN = platform$1 === 'win32';
const IS_LINUX = platform$1 === 'linux';
const IS_LINUX = platform$1 === 'linux';
// Convert the current module URL to a file path
const currentModulePath = fileURLToPath(import.meta.url);
// Resolve the parent directory of the current module
const npmDirectory = path.join(path.dirname(currentModulePath), '..');
const tauriConfigDirectory = path.join(npmDirectory, 'src-tauri');
const tauriConfigDirectory = path.join(npmDirectory, 'src-tauri');
function shellExec(command) {
return new Promise((resolve, reject) => {
shelljs.exec(command, { async: true, silent: false, cwd: npmDirectory }, code => {
@@ -371,8 +377,8 @@ function shellExec(command) {
}
});
});
}
}
const logger = {
info(...msg) {
log.info(...msg.map(m => chalk.white(m)));
@@ -389,8 +395,8 @@ const logger = {
success(...msg) {
log.info(...msg.map(m => chalk.green(m)));
},
};
};
const resolve = promisify(dns.resolve);
const ping = async (host) => {
const lookup = promisify(dns.lookup);
@@ -434,8 +440,8 @@ async function isChinaIP(ip, domain) {
logger.debug(`ping ${domain} failed!`);
return true;
}
}
}
async function installRust() {
const isActions = process.env.GITHUB_ACTIONS;
const isInChina = await isChinaDomain('sh.rustup.rs');
@@ -456,8 +462,8 @@ async function installRust() {
}
function checkRustInstalled() {
return shelljs.exec('rustc --version', { silent: true }).code === 0;
}
}
async function combineFiles(files, output) {
const contents = files.map(file => {
const fileContent = fs.readFileSync(file);
@@ -468,8 +474,8 @@ async function combineFiles(files, output) {
});
fs.writeFileSync(output, contents.join('\n'));
return files;
}
}
async function mergeConfig(url, options, tauriConf) {
const { width, height, fullscreen, transparent, userAgent, showMenu, showSystemTray, systemTrayIcon, iterCopyFile, identifier, name, resizable = true, inject, safeDomain, } = options;
const { platform } = process;
@@ -619,7 +625,7 @@ async function mergeConfig(url, options, tauriConf) {
}
}
tauriConf.tauri.systemTray.iconPath = trayIconPath;
const injectFilePath = path.join(npmDirectory, `src-tauri/src/inject/_INJECT_.js`);
const injectFilePath = path.join(npmDirectory, `src-tauri/src/inject/custom.js`);
// inject js or css files
if (inject?.length > 0) {
if (!inject.every(item => item.endsWith('.css') || item.endsWith('.js'))) {
@@ -650,8 +656,8 @@ async function mergeConfig(url, options, tauriConf) {
delete tauriConf2.tauri.bundle;
const configJsonPath = path.join(tauriConfigDirectory, 'tauri.conf.json');
await fsExtra.outputJSON(configJsonPath, tauriConf2, { spaces: 4 });
}
}
class BaseBuilder {
constructor(options) {
this.options = options;
@@ -733,8 +739,8 @@ class BaseBuilder {
getBuildAppPath(npmDirectory, fileName, fileType) {
return path.join(npmDirectory, this.getBasePath(), fileType.toLowerCase(), `${fileName}.${fileType}`);
}
}
}
class MacBuilder extends BaseBuilder {
constructor(options) {
super(options);
@@ -759,8 +765,8 @@ class MacBuilder extends BaseBuilder {
? 'src-tauri/target/universal-apple-darwin/release/bundle'
: super.getBasePath();
}
}
}
class WinBuilder extends BaseBuilder {
constructor(options) {
super(options);
@@ -772,8 +778,8 @@ class WinBuilder extends BaseBuilder {
const language = tauriConfig.tauri.bundle.windows.wix.language[0];
return `${name}_${tauriConfig.package.version}_${arch}_${language}`;
}
}
}
class LinuxBuilder extends BaseBuilder {
constructor(options) {
super(options);
@@ -798,8 +804,8 @@ class LinuxBuilder extends BaseBuilder {
}
return super.getFileType(target);
}
}
}
const { platform } = process;
const buildersMap = {
darwin: MacBuilder,
@@ -814,8 +820,8 @@ class BuilderProvider {
}
return new Builder(options);
}
}
}
const DEFAULT_PAKE_OPTIONS = {
icon: '',
height: 780,
@@ -833,12 +839,12 @@ const DEFAULT_PAKE_OPTIONS = {
debug: false,
inject: [],
safeDomain: [],
};
};
async function checkUpdateTips() {
updateNotifier({ pkg: packageJson, updateCheckInterval: 1000 * 60 }).notify({ isGlobal: true });
}
}
async function handleIcon(options) {
if (options.icon) {
if (options.icon.startsWith('http')) {
@@ -891,8 +897,8 @@ async function downloadIcon(iconUrl) {
}
throw error;
}
}
}
// Extracts the domain from a given URL.
function getDomain(inputUrl) {
try {
@@ -930,8 +936,8 @@ function normalizeUrl(urlToNormalize) {
else {
throw new Error(`Your url "${urlWithProtocol}" is invalid`);
}
}
}
function resolveAppName(name, platform) {
const domain = getDomain(name) || 'pake';
return platform !== 'linux' ? capitalizeFirstLetter(domain) : domain;
@@ -975,8 +981,8 @@ async function handleOptions(options, url) {
};
appOptions.icon = await handleIcon(appOptions);
return appOptions;
}
}
function validateNumberInput(value) {
const parsedValue = Number(value);
if (isNaN(parsedValue)) {
@@ -995,14 +1001,14 @@ function validateUrlInput(url) {
}
}
return url;
}
}
const { green, yellow } = chalk;
const logo = `${chalk.green(' ____ _')}
${green('| _ \\ __ _| | _____')}
${green('| |_) / _` | |/ / _ \\')}
${green('| __/ (_| | < __/')} ${yellow('https://github.com/tw93/pake')}
${green('|_| \\__,_|_|\\_\\___| can turn any webpage into a desktop app with Rust.')}
const logo = `${chalk.green(' ____ _')}
${green('| _ \\ __ _| | _____')}
${green('| |_) / _` | |/ / _ \\')}
${green('| __/ (_| | < __/')} ${yellow('https://github.com/tw93/pake')}
${green('|_| \\__,_|_|\\_\\___| can turn any webpage into a desktop app with Rust.')}
`;
program
.addHelpText('beforeAll', logo)
@@ -1023,8 +1029,8 @@ program
.option('--iter-copy-file', 'Copy files when URL is a local file', DEFAULT_PAKE_OPTIONS.iterCopyFile)
.option('--multi-arch', 'Only for Mac, supports both Intel and M1', DEFAULT_PAKE_OPTIONS.multiArch)
.option('--targets <string>', 'Only for Linux, option "deb" or "appimage"', DEFAULT_PAKE_OPTIONS.targets)
.option('--inject [injects...]', 'inject .js or .css for this app', DEFAULT_PAKE_OPTIONS.inject)
.option('--safe-domain [domains...]', 'domains that can call window.__TAURI__ and use ipc', DEFAULT_PAKE_OPTIONS.safeDomain)
.option('--inject [injects...]', 'Injection of .js or .css Files', DEFAULT_PAKE_OPTIONS.inject)
.option('--safe-domain [domains...]', 'Domains that Require Security Configuration"', DEFAULT_PAKE_OPTIONS.safeDomain)
.option('--debug', 'Debug mode', DEFAULT_PAKE_OPTIONS.debug)
.version(packageJson.version, '-v, --version', 'Output the current version')
.action(async (url, options) => {
@@ -1048,4 +1054,4 @@ program
await builder.prepare();
await builder.build(url);
});
program.parse();
program.parse();