From 5b6cfd32437b0f5bbb6414003366cf7ae76e9f3d Mon Sep 17 00:00:00 2001 From: Tlntin Date: Sun, 11 Dec 2022 05:08:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0pake-cli=20=E5=9B=BE=E6=A0=87?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/builders/common.ts | 57 ++++++++++++++++++------ dist/cli.js | 99 ++++++++++++++++++++++++++++-------------- 2 files changed, 111 insertions(+), 45 deletions(-) diff --git a/bin/builders/common.ts b/bin/builders/common.ts index 4ba578f..b3fab61 100644 --- a/bin/builders/common.ts +++ b/bin/builders/common.ts @@ -3,6 +3,7 @@ import prompts from 'prompts'; import path from 'path'; import fs from 'fs/promises'; import { npmDirectory } from '@/utils/dir.js'; +import logger from '@/options/logger.js'; export async function promptText(message: string, initial?: string) { const response = await prompts({ @@ -40,20 +41,50 @@ export async function mergeTauriConfig( Object.assign(tauriConf.tauri.windows[0], { url, ...tauriConfWindowOptions }); tauriConf.package.productName = name; tauriConf.tauri.bundle.identifier = identifier; - tauriConf.tauri.bundle.icon = [options.icon]; - if (process.platform === "win32") { - const ico_path = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}_32.ico`); - tauriConf.tauri.bundle.resources = [`png/${name.toLowerCase()}_32.ico`]; - await fs.copyFile(options.icon, ico_path); - } - if (process.platform === "linux") { - const installSrc = `/usr/share/applications/${name}.desktop`; - const assertSrc = `src-tauri/assets/${name}.desktop`; - const assertPath = path.join(npmDirectory, assertSrc); - tauriConf.tauri.bundle.deb.files = { - [installSrc]: assertPath + const exists = await fs.stat(options.icon) + .then(() => true) + .catch(() => false); + if (exists) { + let updateIconPath = true; + let customIconExt = path.extname(options.icon).toLowerCase(); + if (process.platform === "win32") { + if (customIconExt === ".ico") { + const ico_path = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}_32.ico`); + tauriConf.tauri.bundle.resources = [`png/${name.toLowerCase()}_32.ico`]; + await fs.copyFile(options.icon, ico_path); + } else { + updateIconPath = false; + logger.warn(`icon file in Windows must be 256 * 256 pix with .ico type, but you give ${customIconExt}`); + } } - + if (process.platform === "linux") { + tauriConf.package.productName = name.toLowerCase(); + if (customIconExt === ".png") { + const installSrc = `/usr/share/applications/${name.toLowerCase()}.desktop`; + const assertSrc = `src-tauri/assets/${name.toLowerCase()}.desktop`; + const assertPath = path.join(npmDirectory, assertSrc); + tauriConf.tauri.bundle.deb.files = { + [installSrc]: assertPath + } + } else { + updateIconPath = false; + logger.warn(`icon file in Linux must be 512 * 512 pix with .png type, but you give ${customIconExt}`); + } + } + + if (process.platform === "darwin") { + if (customIconExt !== ".icns") { + updateIconPath = false; + logger.warn(`icon file in MacOS must be .icns type, but you give ${customIconExt}`); + } + } + if (updateIconPath) { + tauriConf.tauri.bundle.icon = [options.icon]; + } else { + logger.warn(`icon file will not change with default.`); + } + } else { + logger.warn("the custom icon path may not exists. we will use default icon to replace it"); } diff --git a/dist/cli.js b/dist/cli.js index c9b023a..3197890 100644 --- a/dist/cli.js +++ b/dist/cli.js @@ -6,11 +6,11 @@ import isurl from 'is-url'; import prompts from 'prompts'; import path from 'path'; import fs from 'fs/promises'; +import chalk from 'chalk'; import crypto from 'crypto'; import axios from 'axios'; import { fileTypeFromBuffer } from 'file-type'; import { dir } from 'tmp-promise'; -import chalk from 'chalk'; import ora from 'ora'; import shelljs from 'shelljs'; import updateNotifier from 'update-notifier'; @@ -1593,6 +1593,24 @@ function validateUrlInput(url) { const npmDirectory = path.join(path.dirname(fileURLToPath(import.meta.url)), '..'); +const logger = { + info(...msg) { + log.info(...msg.map((m) => chalk.blue.bold(m))); + }, + debug(...msg) { + log.debug(...msg); + }, + error(...msg) { + log.error(...msg.map((m) => chalk.red.bold(m))); + }, + warn(...msg) { + log.info(...msg.map((m) => chalk.yellow.bold(m))); + }, + success(...msg) { + log.info(...msg.map((m) => chalk.green.bold(m))); + } +}; + function promptText(message, initial) { return __awaiter(this, void 0, void 0, function* () { const response = yield prompts({ @@ -1617,19 +1635,53 @@ function mergeTauriConfig(url, options, tauriConf) { Object.assign(tauriConf.tauri.windows[0], Object.assign({ url }, tauriConfWindowOptions)); tauriConf.package.productName = name; tauriConf.tauri.bundle.identifier = identifier; - tauriConf.tauri.bundle.icon = [options.icon]; - if (process.platform === "win32") { - const ico_path = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}_32.ico`); - tauriConf.tauri.bundle.resources = [`png/${name.toLowerCase()}_32.ico`]; - yield fs.copyFile(options.icon, ico_path); + const exists = yield fs.stat(options.icon) + .then(() => true) + .catch(() => false); + if (exists) { + let updateIconPath = true; + let customIconExt = path.extname(options.icon).toLowerCase(); + if (process.platform === "win32") { + if (customIconExt === ".ico") { + const ico_path = path.join(npmDirectory, `src-tauri/png/${name.toLowerCase()}_32.ico`); + tauriConf.tauri.bundle.resources = [`png/${name.toLowerCase()}_32.ico`]; + yield fs.copyFile(options.icon, ico_path); + } + else { + updateIconPath = false; + logger.warn(`icon file in Windows must be 256 * 256 pix with .ico type, but you give ${customIconExt}`); + } + } + if (process.platform === "linux") { + tauriConf.package.productName = name.toLowerCase(); + if (customIconExt === ".png") { + const installSrc = `/usr/share/applications/${name.toLowerCase()}.desktop`; + const assertSrc = `src-tauri/assets/${name.toLowerCase()}.desktop`; + const assertPath = path.join(npmDirectory, assertSrc); + tauriConf.tauri.bundle.deb.files = { + [installSrc]: assertPath + }; + } + else { + updateIconPath = false; + logger.warn(`icon file in Linux must be 512 * 512 pix with .png type, but you give ${customIconExt}`); + } + } + if (process.platform === "darwin") { + if (customIconExt !== ".icns") { + updateIconPath = false; + logger.warn(`icon file in MacOS must be .icns type, but you give ${customIconExt}`); + } + } + if (updateIconPath) { + tauriConf.tauri.bundle.icon = [options.icon]; + } + else { + logger.warn(`icon file will not change with default.`); + } } - if (process.platform === "linux") { - const installSrc = `/usr/share/applications/${name}.desktop`; - const assertSrc = `src-tauri/assets/${name}.desktop`; - const assertPath = path.join(npmDirectory, assertSrc); - tauriConf.tauri.bundle.deb.files = { - [installSrc]: assertPath - }; + else { + logger.warn("the custom icon path may not exists. we will use default icon to replace it"); } let configPath = ""; switch (process.platform) { @@ -1660,24 +1712,6 @@ function getIdentifier(name, url) { return `pake-${postFixHash}`; } -const logger = { - info(...msg) { - log.info(...msg.map((m) => chalk.blue.bold(m))); - }, - debug(...msg) { - log.debug(...msg); - }, - error(...msg) { - log.error(...msg.map((m) => chalk.red.bold(m))); - }, - warn(...msg) { - log.info(...msg.map((m) => chalk.yellow.bold(m))); - }, - success(...msg) { - log.info(...msg.map((m) => chalk.green.bold(m))); - } -}; - const IS_MAC = process.platform === 'darwin'; const IS_WIN = process.platform === 'win32'; const IS_LINUX = process.platform === 'linux'; @@ -1876,7 +1910,8 @@ var tauri$2 = { wix: { language: [ "en-US" - ] + ], + template: "assets/main.wxs" } } }