62 lines
1.7 KiB
TypeScript
Vendored
62 lines
1.7 KiB
TypeScript
Vendored
import axios from 'axios';
|
|
import { fileTypeFromBuffer } from 'file-type';
|
|
import { PakeAppOptions } from '../types.js';
|
|
import { dir } from 'tmp-promise';
|
|
import path from 'path';
|
|
import fs from 'fs/promises';
|
|
import logger from './logger.js';
|
|
import { npmDirectory } from '@/utils/dir.js';
|
|
import { IS_LINUX, IS_WIN } from '@/utils/platform.js';
|
|
|
|
export async function handleIcon(options: PakeAppOptions, url: string) {
|
|
if (options.icon) {
|
|
if (options.icon.startsWith('http')) {
|
|
return downloadIcon(options.icon);
|
|
} else {
|
|
return path.resolve(options.icon);
|
|
}
|
|
}
|
|
if (!options.icon) {
|
|
return getDefaultIcon();
|
|
}
|
|
}
|
|
|
|
export async function getDefaultIcon() {
|
|
logger.info('You haven\'t provided an app icon, so the default icon will be used. To assign a custom icon, please use the --icon option.')
|
|
let iconPath = 'src-tauri/icons/icon.icns';
|
|
if (IS_WIN) {
|
|
iconPath = 'src-tauri/png/icon_256.ico';
|
|
} else if (IS_LINUX) {
|
|
iconPath = 'src-tauri/png/icon_512.png';
|
|
}
|
|
|
|
return path.join(npmDirectory, iconPath);
|
|
}
|
|
|
|
export async function downloadIcon(iconUrl: string) {
|
|
let iconResponse;
|
|
try {
|
|
iconResponse = await axios.get(iconUrl, {
|
|
responseType: 'arraybuffer',
|
|
});
|
|
} catch (error) {
|
|
if (error.response && error.response.status === 404) {
|
|
return null;
|
|
}
|
|
throw error;
|
|
}
|
|
|
|
const iconData = await iconResponse.data;
|
|
if (!iconData) {
|
|
return null;
|
|
}
|
|
const fileDetails = await fileTypeFromBuffer(iconData);
|
|
if (!fileDetails) {
|
|
return null;
|
|
}
|
|
const { path } = await dir();
|
|
const iconPath = `${path}/icon.${fileDetails.ext}`;
|
|
await fs.writeFile(iconPath, iconData);
|
|
return iconPath;
|
|
}
|