-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstackd.ts
134 lines (111 loc) · 4.41 KB
/
stackd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env tsx
import { program } from 'commander';
import inquirer from 'inquirer';
import chalk from 'chalk';
import { spawn } from 'child_process';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const getPackageRoot = () => {
let currentDir = __dirname;
while (!fs.existsSync(join(currentDir, 'package.json'))) {
const parentDir = dirname(currentDir);
if (parentDir === currentDir) {
return process.cwd();
}
currentDir = parentDir;
}
return currentDir;
};
const packageRoot = getPackageRoot();
const showBanner = () => {
console.log(chalk.cyan(`
██████╗████████╗ █████╗ ██████╗██╗ ██╗'██████╗
██╔════╝╚══██╔══╝██╔══██╗██╔════╝██║ ██╔╝██╔══██╗
╚█████╗ ██║ ███████║██║ █████═╝ ██║ ██║
╚═══██╗ ██║ ██╔══██║██║ ██╔═██╗ ██║ ██║
██████╔╝ ██║ ██║ ██║╚██████╗██║ ██╗██████╔╝
╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚═════╝
`));
console.log(chalk.yellow.bold(' 🚀 Full Stack Project Generator\n'));
};
const createBorder = () => chalk.cyan('='.repeat(60));
const executeCommand = (command: string, cwd: string) => {
const [cmd, ...args] = command.split(' ');
const process = spawn(cmd, args, { cwd, stdio: 'inherit', shell: true });
process.on('error', (error) => {
console.error(chalk.red(`Error: ${error.message}`));
});
return process;
};
const getAppPath = (appName: string) => {
const devPath = join(packageRoot, 'apps', appName);
if (fs.existsSync(devPath)) {
return devPath;
}
const publishedPath = join(packageRoot, 'node_modules', '@shivasankaran','stackd','apps', appName);
if (fs.existsSync(publishedPath)) {
return publishedPath;
}
return join(packageRoot, appName);
};
const initializeProject = async () => {
showBanner();
console.log(createBorder());
const answer = await inquirer.prompt([
{
type: 'list',
name: 'interface',
message: chalk.magenta.bold('🎯 Choose your preferred interface:'),
choices: [
{ name: chalk.blue('CLI Interface'), value: 'cli' },
{ name: chalk.green('Web Interface'), value: 'web' }
]
}
]);
try {
if (answer.interface === 'cli') {
console.log(chalk.blue('\n📦 Starting CLI interface...\n'));
const cliAppPath = getAppPath('cli');
const cliScriptPath = join(cliAppPath, 'src', 'cli.ts');
console.log(cliScriptPath);
if (!fs.existsSync(cliScriptPath)) {
console.log(chalk.yellow(`\nCLI script not found at ${cliScriptPath}. Trying alternative paths...\n`));
const command = 'npx tsx @stackd/cli/src/cli.ts run';
executeCommand(command, process.cwd());
} else {
console.log("hello");
executeCommand(`npx tsx ${cliScriptPath} run`, process.cwd());
}
} else {
const webAppPath = getAppPath('web');
console.log(chalk.green(`\n🌐 Setting up Web Interface at ${webAppPath}...\n`));
if (!fs.existsSync(webAppPath)) {
console.error(chalk.red(`\n❌ Web app directory not found at: ${webAppPath}`));
process.exit(1);
}
await executeCommand('npm install', webAppPath);
console.log(chalk.green('\n🚀 Starting Web Interface...'));
console.log(chalk.green('\n🌐 Web interface available at http://localhost:3000\n'));
executeCommand('npm run dev', webAppPath);
}
} catch (error: any) {
console.error(chalk.red(`\n❌ Failed to start the selected interface: ${error.message}`));
process.exit(1);
}
};
program
.name('stackd')
.description('CLI tool for creating full-stack applications')
.version('6.0.0');
program
.command('init')
.action(initializeProject);
program
.action(() => {
console.log(chalk.yellow('\nNo command specified. Using "init" by default.\n'));
initializeProject();
});
program.parse(process.argv);