-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·161 lines (138 loc) · 6.49 KB
/
index.js
File metadata and controls
executable file
·161 lines (138 loc) · 6.49 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
const { Command } = require('commander');
const chalk = require('chalk');
const { ContainerDetector } = require('./lib/detectors');
const { EXIT_CODES, loadContext, autoDetect, printDetection, printPlan } = require('./lib/utils');
const program = new Command();
const log = {
info: (msg) => console.log(chalk.blue('[info]'), msg),
success: (msg) => console.log(chalk.green('[ok]'), msg),
error: (msg) => console.log(chalk.red('[error]'), msg),
warn: (msg) => console.log(chalk.yellow('[warn]'), msg),
};
// Main dispatcher
async function main() {
program
.name('cloud')
.description('Ship any container to the cloud with a single command')
.version('0.0.1')
.argument('[target]', 'Target to ship (image, directory, etc.)')
.option('--ship', 'Ship to cloud after preparation')
.option('--plan', 'Show what would happen (dry run)')
.option('--detect', 'Show detected project type and exit')
.option('--type <type>', 'Force specific project type (container)')
.option('--name <name>', 'Project name override')
.option('--class <class>', 'Container class name override')
.option('--max-instances <number>', 'Maximum container instances', '10')
.option('--migration-tag <tag>', 'Override migration tag (e.g. v4)')
.option('--no-prompt', 'Non-interactive mode for CI')
.option('--force', 'Overwrite existing files')
.option('--verbose', 'Verbose logging')
.action(async (target, opts) => {
try {
const ctx = await loadContext(target ? [target] : []);
const detectors = [new ContainerDetector()];
// Apply config overrides
Object.assign(opts, ctx.config);
// Force specific detector if requested
const forcedDetector = opts.type ? detectors.find(d => d.id === opts.type) : null;
let result;
if (forcedDetector) {
result = await forcedDetector.detect(ctx, opts);
if (!result) {
log.error(`No ${opts.type} project detected`);
process.exit(EXIT_CODES.CONFIG);
}
} else {
result = await autoDetect(detectors, ctx, opts);
if (!result) {
console.log(chalk.red('\nNo supported project type detected\n'));
console.log(chalk.yellow('Supported types:'));
console.log(chalk.gray(' - Container (Dockerfile, docker-compose.yml, image reference)'));
console.log(chalk.yellow('\nTry:'));
console.log(chalk.gray(' - cloud --type container <image>'));
console.log(chalk.gray(' - cloud --detect (to see what was found)'));
process.exit(EXIT_CODES.USAGE);
}
}
// Handle detection-only mode
if (opts.detect) {
printDetection(result);
return;
}
// Handle plan mode
if (opts.plan) {
printPlan(result, ctx, opts);
return;
}
// Execute scaffold
log.info(`Preparing ${result.detector.name} project...`);
const scaffoldResult = await result.detector.scaffold(ctx, opts, result);
if (!scaffoldResult) {
log.error('Scaffolding failed');
process.exit(EXIT_CODES.CONFIG);
}
log.success('Project preparation completed!');
// Execute ship if requested
if (opts.ship) {
const shipSuccess = await result.detector.deploy(ctx, opts, scaffoldResult);
if (!shipSuccess) {
log.warn('Shipping failed, but scaffolding was successful');
process.exit(EXIT_CODES.PARTIAL);
}
} else {
console.log(chalk.blue('\nNext steps:'));
console.log(chalk.gray(' - Review generated files'));
console.log(chalk.gray(' - Run: cloud --ship (to deploy to cloud)'));
console.log(chalk.gray(' - Or run: npm run deploy'));
}
} catch (error) {
log.error(`Error: ${error.message}`);
if (opts.verbose) {
console.error(error.stack);
}
process.exit(EXIT_CODES.CONFIG);
}
});
// Help command
program
.command('help')
.description('Show detailed help and examples')
.action(() => {
console.log(chalk.blue.bold('\nCloud CLI - Deploy Any Container to Cloudflare\n'));
console.log(chalk.yellow('Basic Usage:'));
console.log(' cloud Auto-detect and prepare project');
console.log(' cloud --ship Auto-detect, prepare, and deploy to cloud');
console.log(' cloud --plan Show what would happen (dry run)');
console.log(' cloud --detect Show detected project type');
console.log(chalk.yellow('\nContainer Examples:'));
console.log(' cloud nginx:alpine Deploy remote image');
console.log(' cloud ./Dockerfile --ship Deploy local container');
console.log(' cloud --type container --ship Force container detection');
console.log(' cloud redis:alpine --name my-cache --ship');
console.log(chalk.yellow('\nOptions:'));
console.log(' --ship Deploy to cloud after preparation');
console.log(' --plan Dry run (show plan)');
console.log(' --detect Show detection results');
console.log(' --type <type> Force project type (container)');
console.log(' --name <name> Override project name');
console.log(' --class <class> Container class name override');
console.log(' --max-instances <number> Maximum container instances (default: 10)');
console.log(' --migration-tag <tag> Override migration tag (e.g. v4)');
console.log(' --no-prompt Non-interactive (CI mode)');
console.log(' --force Overwrite existing files');
console.log(' --verbose Detailed logging');
console.log(chalk.yellow('\nSupported Project Types:'));
console.log(' Container Dockerfile, images, docker-compose');
console.log(chalk.green('\nDeploy any container to Cloudflare with one command!'));
});
await program.parseAsync();
}
// Run the CLI
if (require.main === module) {
main().catch(error => {
console.error(chalk.red('Fatal error:'), error.message);
process.exit(EXIT_CODES.CONFIG);
});
}
module.exports = { main, loadContext, autoDetect };