🎨 replace shelljs to execa

This commit is contained in:
Tw93
2025-01-23 14:42:27 +08:00
parent 8fc6ca8cbd
commit d44e38122d

View File

@@ -1,91 +1,128 @@
import shelljs from 'shelljs';
import axios from 'axios';
import fs from 'fs'; import fs from 'fs';
import path from 'path';
import axios from 'axios';
import { execa } from 'execa';
const { exec, cd, mv } = shelljs; // Configuration logging
const logConfiguration = () => {
console.log('Welcome to use pake-cli to build app');
console.log('Node.js info in your localhost:', process.version);
console.log('\n=======================\n');
console.log('Pake parameters:');
console.log('url:', process.env.URL);
console.log('name:', process.env.NAME);
console.log('icon:', process.env.ICON);
console.log('height:', process.env.HEIGHT);
console.log('width:', process.env.WIDTH);
console.log('fullscreen:', process.env.FULLSCREEN);
console.log('hide-title-bar:', process.env.HIDE_TITLE_BAR);
console.log('is multi arch? only for Mac:', process.env.MULTI_ARCH);
console.log('targets type? only for Linux:', process.env.TARGETS);
console.log('===========================\n');
};
console.log('Welcome to use pake-cli to build app'); // Build parameters construction
console.log('Node.js info in your localhost ', process.version); const buildParameters = () => {
console.log('\n=======================\n'); const params = ['cli.js', process.env.URL, '--name', process.env.NAME, '--height', process.env.HEIGHT, '--width', process.env.WIDTH];
console.log('Pake parameters is: ');
console.log('url: ', process.env.URL);
console.log('name: ', process.env.NAME);
console.log('icon: ', process.env.ICON);
console.log('height: ', process.env.HEIGHT);
console.log('width: ', process.env.WIDTH);
console.log('fullscreen: ', process.env.FULLSCREEN);
console.log('hide-title-bar: ', process.env.HIDE_TITLE_BAR);
console.log('is multi arch? only for Mac: ', process.env.MULTI_ARCH);
console.log('targets type? only for Linux: ', process.env.TARGETS);
console.log('===========================\n');
cd('node_modules/pake-cli'); if (process.env.HIDE_TITLE_BAR === 'true') {
let params = `node cli.js ${process.env.URL} --name ${process.env.NAME} --height ${process.env.HEIGHT} --width ${process.env.WIDTH}`; params.push('--hide-title-bar');
}
if (process.env.HIDE_TITLE_BAR === 'true') { if (process.env.FULLSCREEN === 'true') {
params = `${params} --hide-title-bar`; params.push('--fullscreen');
} }
if (process.env.FULLSCREEN === 'true') { if (process.env.MULTI_ARCH === 'true') {
params = `${params} --fullscreen`; // We'll handle rustup separately since it's a different command
} params.push('--multi-arch');
}
if (process.env.MULTI_ARCH === 'true') { if (process.env.TARGETS) {
exec('rustup target add aarch64-apple-darwin'); params.push('--targets', process.env.TARGETS);
params = `${params} --multi-arch`; }
}
if (process.env.TARGETS) { if (process.platform === 'win32' || process.platform === 'linux') {
params = `${params} --targets ${process.env.TARGETS}`; params.push('--show-system-tray');
} }
if (process.platform === 'win32' || process.platform === 'linux') { return params;
params = `${params} --show-system-tray`; };
}
// Icon download handling
const downloadIcon = async iconFile => { const downloadIcon = async iconFile => {
try { try {
const response = await axios.get(process.env.ICON, { responseType: 'arraybuffer' }); const response = await axios.get(process.env.ICON, { responseType: 'arraybuffer' });
fs.writeFileSync(iconFile, response.data); fs.writeFileSync(iconFile, response.data);
return `${params} --icon ${iconFile}`; return ['--icon', iconFile];
} catch (error) { } catch (error) {
console.error('Error occurred during icon download: ', error); console.error('Error occurred during icon download:', error);
throw error;
} }
}; };
const main = async () => { // Get icon file name based on platform
if (process.env.ICON && process.env.ICON !== '') { const getIconFileName = () => {
let iconFile;
switch (process.platform) { switch (process.platform) {
case 'linux': case 'linux':
iconFile = 'icon.png'; return 'icon.png';
break;
case 'darwin': case 'darwin':
iconFile = 'icon.icns'; return 'icon.icns';
break;
case 'win32': case 'win32':
iconFile = 'icon.ico'; return 'icon.ico';
break;
default: default:
console.log("Unable to detect your OS system, won't download the icon!"); throw new Error('Unable to detect your OS system');
process.exit(1); }
};
// Main execution
const main = async () => {
try {
logConfiguration();
const cliPath = path.join(process.cwd(), 'node_modules/pake-cli');
process.chdir(cliPath);
let params = buildParameters();
if (process.env.MULTI_ARCH === 'true') {
await execa('rustup', ['target', 'add', 'aarch64-apple-darwin']);
} }
params = await downloadIcon(iconFile); if (process.env.ICON && process.env.ICON !== '') {
const iconFile = getIconFileName();
const iconParams = await downloadIcon(iconFile);
params.push(...iconParams);
} else { } else {
console.log("Won't download the icon as ICON environment variable is not defined!"); console.log("Won't download the icon as ICON environment variable is not defined!");
} }
console.log('Pake parameters is: ', params); console.log('Pake parameters:', params.join(' '));
console.log('Compile....'); console.log('Compiling....');
exec(params);
// Execute the CLI command
await execa('node', params, { stdio: 'inherit' });
// Create output directory if it doesn't exist
if (!fs.existsSync('output')) { if (!fs.existsSync('output')) {
fs.mkdirSync('output'); fs.mkdirSync('output');
} }
mv(`${process.env.NAME}.*`, 'output/');
// Move built files to output directory
const files = fs.readdirSync('.');
const namePattern = new RegExp(`^${process.env.NAME}\\..*$`);
for (const file of files) {
if (namePattern.test(file)) {
await execa('mv', [file, path.join('output', file)]);
}
}
console.log('Build Success'); console.log('Build Success');
cd('../..'); process.chdir('../..');
} catch (error) {
console.error('Build failed:', error);
process.exit(1);
}
}; };
main(); main();