🎨 Refactoring CLI
This commit is contained in:
106
bin/options/icon.ts
vendored
106
bin/options/icon.ts
vendored
@@ -1,98 +1,52 @@
|
||||
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';
|
||||
import axios from 'axios';
|
||||
import fsExtra from "fs-extra";
|
||||
import { dir } from 'tmp-promise';
|
||||
import { fileTypeFromBuffer } from 'file-type';
|
||||
|
||||
export async function handleIcon(options: PakeAppOptions, url: string) {
|
||||
import logger from './logger';
|
||||
import { PakeAppOptions } from '@/types';
|
||||
import { npmDirectory } from '@/utils/dir';
|
||||
import { IS_LINUX, IS_WIN } from '@/utils/platform';
|
||||
|
||||
export async function handleIcon(options: PakeAppOptions) {
|
||||
if (options.icon) {
|
||||
if (options.icon.startsWith('http')) {
|
||||
return downloadIcon(options.icon);
|
||||
} else {
|
||||
return path.resolve(options.icon);
|
||||
}
|
||||
}
|
||||
if (!options.icon) {
|
||||
return getDefaultIcon();
|
||||
} else {
|
||||
logger.info('No app icon provided, default icon used. Use --icon option to assign an icon.');
|
||||
const iconPath = IS_WIN ? 'src-tauri/png/icon_256.ico' : IS_LINUX ? 'src-tauri/png/icon_512.png' : 'src-tauri/icons/icon.icns';
|
||||
return path.join(npmDirectory, iconPath);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getDefaultIcon() {
|
||||
logger.info('You have not provided an app icon, use the default icon.(use --icon option to assign an icon)')
|
||||
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 getIconFromPageUrl(url: string) {
|
||||
// const icon = await pageIcon(url);
|
||||
// console.log(icon);
|
||||
// if (icon.ext === '.ico') {
|
||||
// const a = await ICO.parse(icon.data);
|
||||
// icon.data = Buffer.from(a[0].buffer);
|
||||
// }
|
||||
|
||||
// const iconDir = (await dir()).path;
|
||||
// const iconPath = path.join(iconDir, `/icon.icns`);
|
||||
|
||||
// const out = png2icons.createICNS(icon.data, png2icons.BILINEAR, 0);
|
||||
|
||||
// await fs.writeFile(iconPath, out);
|
||||
// return iconPath;
|
||||
// }
|
||||
|
||||
// export async function getIconFromMacosIcons(name: string) {
|
||||
// const data = {
|
||||
// query: name,
|
||||
// filters: 'approved:true',
|
||||
// hitsPerPage: 10,
|
||||
// page: 1,
|
||||
// };
|
||||
// const res = await axios.post('https://p1txh7zfb3-2.algolianet.com/1/indexes/macOSicons/query?x-algolia-agent=Algolia%20for%20JavaScript%20(4.13.1)%3B%20Browser', data, {
|
||||
// headers: {
|
||||
// 'x-algolia-api-key': '0ba04276e457028f3e11e38696eab32c',
|
||||
// 'x-algolia-application-id': 'P1TXH7ZFB3',
|
||||
// },
|
||||
// });
|
||||
// if (!res.data.hits.length) {
|
||||
// return '';
|
||||
// } else {
|
||||
// return downloadIcon(res.data.hits[0].icnsUrl);
|
||||
// }
|
||||
// }
|
||||
|
||||
export async function downloadIcon(iconUrl: string) {
|
||||
let iconResponse;
|
||||
try {
|
||||
iconResponse = await axios.get(iconUrl, {
|
||||
const iconResponse = await axios.get(iconUrl, {
|
||||
responseType: 'arraybuffer',
|
||||
});
|
||||
|
||||
const iconData = await iconResponse.data;
|
||||
if (!iconData) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const fileDetails = await fileTypeFromBuffer(iconData);
|
||||
if (!fileDetails) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { path: tempPath } = await dir();
|
||||
const iconPath = `${tempPath}/icon.${fileDetails.ext}`;
|
||||
await fsExtra.outputFile(iconPath, iconData);
|
||||
return iconPath;
|
||||
} 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;
|
||||
}
|
||||
|
||||
32
bin/options/index.ts
vendored
32
bin/options/index.ts
vendored
@@ -1,29 +1,25 @@
|
||||
import { promptText } from '@/builders/common.js';
|
||||
import { getDomain } from '@/utils/url.js';
|
||||
import { getIdentifier } from '../helpers/tauriConfig.js';
|
||||
import { PakeAppOptions, PakeCliOptions } from '../types.js';
|
||||
import { handleIcon } from './icon.js';
|
||||
import fs from 'fs/promises';
|
||||
import fsExtra from "fs-extra";
|
||||
|
||||
import { handleIcon } from './icon';
|
||||
import { getDomain } from '@/utils/url';
|
||||
import { getIdentifier, promptText } from '@/utils/info';
|
||||
import { PakeAppOptions, PakeCliOptions } from '@/types';
|
||||
|
||||
export default async function handleOptions(options: PakeCliOptions, url: string): Promise<PakeAppOptions> {
|
||||
const appOptions: PakeAppOptions = {
|
||||
...options,
|
||||
identifier: '',
|
||||
identifier: getIdentifier(url),
|
||||
};
|
||||
const url_exists = await fs.stat(url)
|
||||
.then(() => true)
|
||||
.catch(() => false);
|
||||
|
||||
let urlExists = await fsExtra.pathExists(url);
|
||||
|
||||
if (!appOptions.name) {
|
||||
if (!url_exists) {
|
||||
appOptions.name = await promptText('please input your application name', getDomain(url));
|
||||
} else {
|
||||
appOptions.name = await promptText('please input your application name', "");
|
||||
}
|
||||
const defaultName = urlExists ? "" : getDomain(url);
|
||||
const promptMessage = 'Enter your application name';
|
||||
appOptions.name = await promptText(promptMessage, defaultName);
|
||||
}
|
||||
|
||||
appOptions.identifier = getIdentifier(appOptions.name, url);
|
||||
|
||||
appOptions.icon = await handleIcon(appOptions, url);
|
||||
appOptions.icon = await handleIcon(appOptions);
|
||||
|
||||
return appOptions;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user