const path = require('node:path'); const arg = require('arg'); const chalk = require('chalk'); const boxenModule = require('boxen'); const { runWizard } = require('./wizard'); const pkg = require('../package.json'); const boxen = boxenModule.default || boxenModule; function toPositiveInteger(value, fallback) { if (value === undefined || value === null) { return fallback; } const parsed = Number(value); if (!Number.isFinite(parsed) || parsed <= 0) { return fallback; } return Math.floor(parsed); } function parseCli() { return arg( { '--help': Boolean, '--version': Boolean, '--dry-run': Boolean, '--cwd': String, '--output': String, '--max-depth': Number, '--max-files': Number, '-h': '--help', '-v': '--version', '-o': '--output', }, { permissive: true, }, ); } function getHelpText() { const repository = typeof pkg.repository === 'object' && pkg.repository && pkg.repository.url ? pkg.repository.url.replace(/^git\+/, '').replace(/\.git$/, '') : pkg.homepage || 'https://github.com/elouangrimm/dotstop'; const title = `${chalk.bold.cyan('dotstop')} ${chalk.bold.white(`v${pkg.version}`)}`; return [ title, chalk.dim('Interactive desktop-entry wizard for Linux apps'), '', chalk.bold('USAGE'), ` ${chalk.cyan('dotstop')} Launch wizard in current directory`, ` ${chalk.cyan('dotstop ./my-app')} Launch wizard for another folder`, ` ${chalk.cyan('dotstop --output ./app.desktop')} Preselect output path`, ` ${chalk.cyan('dotstop --dry-run')} Generate preview without writing file`, '', chalk.bold('OPTIONS'), ` ${chalk.yellow('-h, --help')} Show help`, ` ${chalk.yellow('-v, --version')} Show version`, ` ${chalk.yellow('--dry-run')} Build desktop entry preview only`, ` ${chalk.yellow('--cwd ')} Project directory to scan`, ` ${chalk.yellow('-o, --output ')} Write path or target directory`, ` ${chalk.yellow('--max-depth ')} Scan depth for icon/binary detection`, ` ${chalk.yellow('--max-files ')} Max files to inspect while scanning`, '', chalk.bold('WHAT DOTSTOP AUTO-DETECTS'), ' - existing .desktop files in your project', ' - likely launch commands from package.json, bin/, and scripts', ' - icon candidates from png/svg/xpm/ico and files named icon/logo', '', `${chalk.bold('Docs:')} ${chalk.underline(repository)}`, ].join('\n'); } function getStartupBox(cwd, dryRun) { const lines = [ chalk.hex('#06b6d4').bold('dotstop setup wizard'), '', `${chalk.bold('- CWD:')} ${cwd}`, `${chalk.bold('- Mode:')} ${dryRun ? 'dry-run (preview only)' : 'interactive write mode'}`, `${chalk.bold('- Goal:')} create a polished Linux .desktop launcher`, ]; return boxen(lines.join('\n'), { padding: 1, margin: 1, borderStyle: 'single', borderColor: '#06b6d4', }); } async function run() { const parsed = parseCli(); if (parsed['--help']) { console.log(getHelpText()); return; } if (parsed['--version']) { console.log(pkg.version); return; } const positional = parsed._.slice(); const positionalCwd = positional[0] ? String(positional[0]) : ''; const cwd = path.resolve(parsed['--cwd'] || positionalCwd || process.cwd()); const dryRun = Boolean(parsed['--dry-run']); const outputArg = parsed['--output'] ? String(parsed['--output']) : undefined; const maxDepth = toPositiveInteger(parsed['--max-depth'], 4); const maxFiles = toPositiveInteger(parsed['--max-files'], 6000); if (!process.stdout.isTTY) { throw new Error('dotstop requires an interactive TTY. Use --help for non-interactive mode.'); } console.log(getStartupBox(cwd, dryRun)); await runWizard({ cwd, dryRun, outputArg, maxDepth, maxFiles, }); } module.exports = { run, };