Skip to content

Commit

Permalink
🎨 replace shelljs to execa
Browse files Browse the repository at this point in the history
  • Loading branch information
tw93 committed Jan 23, 2025
1 parent 8fc6ca8 commit d44e381
Showing 1 changed file with 108 additions and 71 deletions.
179 changes: 108 additions & 71 deletions script/build_with_pake_cli.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,128 @@
import shelljs from 'shelljs';
import axios from 'axios';
import fs from 'fs';
import path from 'path';
import axios from 'axios';
import { execa } from 'execa';

// 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');
};

// Build parameters construction
const buildParameters = () => {
const params = ['cli.js', process.env.URL, '--name', process.env.NAME, '--height', process.env.HEIGHT, '--width', process.env.WIDTH];

if (process.env.HIDE_TITLE_BAR === 'true') {
params.push('--hide-title-bar');
}

if (process.env.FULLSCREEN === 'true') {
params.push('--fullscreen');
}

if (process.env.MULTI_ARCH === 'true') {
// We'll handle rustup separately since it's a different command
params.push('--multi-arch');
}

if (process.env.TARGETS) {
params.push('--targets', process.env.TARGETS);
}

if (process.platform === 'win32' || process.platform === 'linux') {
params.push('--show-system-tray');
}

const { exec, cd, mv } = shelljs;

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 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');
let params = `node cli.js ${process.env.URL} --name ${process.env.NAME} --height ${process.env.HEIGHT} --width ${process.env.WIDTH}`;

if (process.env.HIDE_TITLE_BAR === 'true') {
params = `${params} --hide-title-bar`;
}

if (process.env.FULLSCREEN === 'true') {
params = `${params} --fullscreen`;
}

if (process.env.MULTI_ARCH === 'true') {
exec('rustup target add aarch64-apple-darwin');
params = `${params} --multi-arch`;
}

if (process.env.TARGETS) {
params = `${params} --targets ${process.env.TARGETS}`;
}

if (process.platform === 'win32' || process.platform === 'linux') {
params = `${params} --show-system-tray`;
}
return params;
};

// Icon download handling
const downloadIcon = async iconFile => {
try {
const response = await axios.get(process.env.ICON, { responseType: 'arraybuffer' });
fs.writeFileSync(iconFile, response.data);
return `${params} --icon ${iconFile}`;
return ['--icon', iconFile];
} catch (error) {
console.error('Error occurred during icon download: ', error);
console.error('Error occurred during icon download:', error);
throw error;
}
};

// Get icon file name based on platform
const getIconFileName = () => {
switch (process.platform) {
case 'linux':
return 'icon.png';
case 'darwin':
return 'icon.icns';
case 'win32':
return 'icon.ico';
default:
throw new Error('Unable to detect your OS system');
}
};

// Main execution
const main = async () => {
if (process.env.ICON && process.env.ICON !== '') {
let iconFile;
switch (process.platform) {
case 'linux':
iconFile = 'icon.png';
break;
case 'darwin':
iconFile = 'icon.icns';
break;
case 'win32':
iconFile = 'icon.ico';
break;
default:
console.log("Unable to detect your OS system, won't download the icon!");
process.exit(1);
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);
} else {
console.log("Won't download the icon as ICON environment variable is not defined!");
}
if (process.env.ICON && process.env.ICON !== '') {
const iconFile = getIconFileName();
const iconParams = await downloadIcon(iconFile);
params.push(...iconParams);
} else {
console.log("Won't download the icon as ICON environment variable is not defined!");
}

console.log('Pake parameters:', params.join(' '));
console.log('Compiling....');

console.log('Pake parameters is: ', params);
console.log('Compile....');
exec(params);
// Execute the CLI command
await execa('node', params, { stdio: 'inherit' });

// Create output directory if it doesn't exist
if (!fs.existsSync('output')) {
fs.mkdirSync('output');
}

if (!fs.existsSync('output')) {
fs.mkdirSync('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');
process.chdir('../..');
} catch (error) {
console.error('Build failed:', error);
process.exit(1);
}
mv(`${process.env.NAME}.*`, 'output/');
console.log('Build Success');
cd('../..');
};

main();

0 comments on commit d44e381

Please sign in to comment.