kawkab-frontend
Version:
Kawkab frontend is a frontend library for the Kawkab framework
74 lines (73 loc) ⢠4.37 kB
JavaScript
import fs from 'fs';
import path from 'path';
import chalk from 'chalk';
import { detectPackageManager, run } from '../utils/helpers';
export function crossPlatformCommand(program) {
program
.command('cross-platform')
.description('Add Capacitor to your project for mobile and desktop platforms.')
.action(() => {
console.log(chalk.bold.magenta('š Integrating Cross-Platform Support (Capacitor & Electron)...'));
const projectRoot = process.cwd();
if (fs.existsSync(path.join(projectRoot, 'capacitor.config.ts'))) {
console.log(chalk.green('ā
Cross-platform support already seems to be installed.'));
return;
}
try {
const packageManager = detectPackageManager();
const packageJson = JSON.parse(fs.readFileSync(path.join(projectRoot, 'package.json'), 'utf8'));
const appName = packageJson.name || 'my-kawkab-app';
const appId = `com.kawkab.app.${appName.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()}`;
const addCmd = packageManager === 'yarn' || packageManager === 'bun' ? 'add' : 'install';
const capCmd = packageManager === 'bun' ? 'bun cap' : 'npx cap';
// --- Step 1: Install Capacitor ---
console.log(chalk.blue('\n[1/4] Installing Capacitor dependencies...'));
run(`${packageManager} ${addCmd} @capacitor/core`);
run(`${packageManager} ${addCmd} -D @capacitor/cli`);
// --- Step 2: Init Capacitor & Add Platforms ---
console.log(chalk.blue('\n[2/4] Initializing Capacitor and adding platforms...'));
// We initialize with the CORRECT webDir from the start
run(`${capCmd} init "${appName}" "${appId}" --web-dir "web/client"`);
run(`${packageManager} ${addCmd} @capacitor/android @capacitor/ios`);
run(`${capCmd} add android`);
run(`${capCmd} add ios`);
// --- Step 3: Add Electron Support ---
console.log(chalk.blue('\n[3/4] Adding Electron for desktop support...'));
run(`${packageManager} ${addCmd} -D @capacitor-community/electron`);
run(`${capCmd} add @capacitor-community/electron`);
// --- Step 4: Patch Electron Live Runner for Windows Compatibility ---
console.log(chalk.blue('\n[4/5] Patching Electron live runner for Windows compatibility...'));
const liveRunnerPath = path.join(projectRoot, 'electron', 'live-runner.js');
if (fs.existsSync(liveRunnerPath)) {
let content = fs.readFileSync(liveRunnerPath, 'utf8');
const originalLine = "let tempChild = cp.spawn(npmCmd, ['run', 'build']);";
const patchedLine = "let tempChild = cp.spawn(npmCmd, ['run', 'build'], { shell: true });";
if (content.includes(originalLine)) {
content = content.replace(originalLine, patchedLine);
fs.writeFileSync(liveRunnerPath, content);
console.log(chalk.green(' ā Successfully patched electron/live-runner.js'));
}
else {
console.log(chalk.yellow(' - Patch already applied or file content is unexpected. Skipping.'));
}
}
else {
console.warn(chalk.yellow(' - Could not find electron/live-runner.js to patch.'));
}
// --- Step 5: Final Instructions ---
const runStaticCmd = `${packageManager} run static`;
console.log(chalk.bold.green('\n[5/5] š Cross-platform setup complete!'));
console.log(chalk.cyan('\nNext Steps:'));
console.log(chalk.yellow(`1. Build your web app: \`${runStaticCmd}\``));
console.log(chalk.yellow(`2. Sync with native platforms: \`kawkab-frontend sync\``));
console.log(chalk.yellow('3. Open, build, and run:'));
console.log(chalk.gray(' - For Mobile: `kawkab-frontend open android` or `... open ios`'));
console.log(chalk.gray(' - For Desktop: `kawkab-frontend open desktop`'));
}
catch (err) {
console.error(chalk.red('\nā Failed to install or configure cross-platform support.'));
console.error(err);
process.exit(1);
}
});
}