launch-express
Version:
CLI tool to setup a new Launch Express project
107 lines (103 loc) • 5.24 kB
JavaScript
import chalk from 'chalk';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import path from 'path';
import fs from 'fs';
import * as newCommand from './commands/new.js';
import * as updateCommand from './commands/update.js';
import * as generateSchemaCommand from './commands/generate-schema.js';
import { select } from '@clack/prompts';
import dedent from 'dedent';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Read package.json for version
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json'), 'utf8'));
function showHelp() {
console.log(dedent `
${chalk.yellow('Usage:')}
npx launch-express new [project-name]
npx launch-express update
npx launch-express generate-schema
${chalk.yellow('Options:')}
-h, --help Show this help message
-v, --version Show the current version
${chalk.yellow('Commands:')}
new [project-name] Create a new project (use 'new -h' for more info)
update Update current project from template (use 'update -h' for more info)
generate-schema Generate and merge Better Auth schema (use 'generate-schema -h' for more info)
${chalk.yellow('Example:')}
npx launch-express new my-awesome-app
npx launch-express update
npx launch-express generate-schema
`);
process.exit(0);
}
function showVersion() {
console.log(`v${packageJson.version}`);
process.exit(0);
}
async function main() {
console.log(`
██╗ █████╗ ██╗ ██╗███╗ ██╗ ██████╗██╗ ██╗ ███████╗██╗ ██╗██████╗ ██████╗ ███████╗███████╗███████╗
██║ ██╔══██╗██║ ██║████╗ ██║██╔════╝██║ ██║ ██╔════╝╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝██╔════╝██╔════╝
██║ ███████║██║ ██║██╔██╗ ██║██║ ███████║ █████╗ ╚███╔╝ ██████╔╝██████╔╝█████╗ ███████╗███████╗
██║ ██╔══██║██║ ██║██║╚██╗██║██║ ██╔══██║ ██╔══╝ ██╔██╗ ██╔═══╝ ██╔══██╗██╔══╝ ╚════██║╚════██║
███████╗██║ ██║╚██████╔╝██║ ╚████║╚██████╗██║ ██║ ███████╗██╔╝ ██╗██║ ██║ ██║███████╗███████║███████║
╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝
`);
console.log(chalk.gray(dedent `
Start your project with Launch Express CLI.
Website: ${chalk.bold('https://launch-express.com')}
`));
console.log();
// Handle command line arguments
const arg = process.argv[2];
const subArg = process.argv[3];
// Parse command line arguments
const command = arg ||
(await select({
message: 'What would you like to do?',
options: [
{ value: 'new', label: 'Create a new project' },
{ value: 'update', label: 'Update current project from template' },
{ value: 'generate-schema', label: 'Generate and merge Better Auth schema' },
{ value: 'help', label: 'Show help' },
{ value: 'version', label: 'Show version' },
],
}));
if (arg === '-h' || arg === '--help' || command === 'help') {
showHelp();
}
if (arg === '-v' || arg === '--version' || command === 'version') {
showVersion();
}
if (arg === 'new' || command === 'new') {
if (subArg === '-h' || subArg === '--help') {
newCommand.showHelp();
}
await newCommand.execute(subArg);
return;
}
if (arg === 'update' || command === 'update') {
if (subArg === '-h' || subArg === '--help') {
updateCommand.showHelp();
}
await updateCommand.execute();
return;
}
if (arg === 'generate-schema' || command === 'generate-schema') {
if (subArg === '-h' || subArg === '--help') {
generateSchemaCommand.showHelp();
}
await generateSchemaCommand.execute();
return;
}
console.log(chalk.red('Invalid command. Use --help to see available commands.'));
process.exit(1);
}
main().catch((error) => {
console.error(chalk.red('An error occurred:'));
console.error(error);
process.exit(1);
});