-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·187 lines (170 loc) · 7.21 KB
/
index.js
File metadata and controls
executable file
·187 lines (170 loc) · 7.21 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env node
import { Command } from 'commander';
import chalk from 'chalk';
import { scaffoldMonorepo, addService, scaffoldPlugin } from './lib/scaffold.js';
import fs from 'fs';
import path from 'path';
import { renderServicesTable } from './lib/ui.js';
import { runDev } from './lib/dev.js';
import { startAdminDashboard } from './lib/admin.js';
import { runHotReload } from './lib/hotreload.js';
const program = new Command();
program
.name('create-polyglot')
.description('Scaffold a polyglot microservice monorepo');
// New explicit init subcommand (Task: add-init-command)
program
.command('init')
.argument('[project-name]', 'Name of the project (optional, will prompt if omitted)')
.option('-s, --services <services>', 'Comma separated list of services (node,python,go,java,frontend)')
.option('--preset <preset>', 'Add preset: turborepo | nx')
.option('--no-install', 'Skip installing dependencies at the root')
.option('--git', 'Initialize a git repository')
.option('--force', 'Overwrite if directory exists and not empty')
.option('--package-manager <pm>', 'npm | pnpm | yarn | bun (default: npm)')
.option('--frontend-generator', 'Use create-next-app to scaffold the frontend instead of the bundled template')
.option('--yes', 'Skip confirmation (assume yes) for non-interactive use')
.action(async (projectNameArg, options) => {
await scaffoldMonorepo(projectNameArg, options);
});
// Backward compatibility: calling the root command directly still scaffolds (deprecated path).
program
.argument('[project-name]', '(Deprecated: call `create-polyglot init <name>` instead) Project name')
.option('-s, --services <services>', '(Deprecated) Services list')
.option('--preset <preset>', '(Deprecated) Preset turborepo|nx')
.option('--no-install', '(Deprecated) Skip install')
.option('--git', '(Deprecated) Init git')
.option('--force', '(Deprecated) Overwrite directory')
.option('--package-manager <pm>', '(Deprecated) Package manager')
.option('--frontend-generator', '(Deprecated) Use create-next-app for frontend')
.option('--yes', '(Deprecated) Assume yes for prompts')
.action(async (projectNameArg, options) => {
if (!options._deprecatedNoticeShown) {
console.log(chalk.yellow('⚠️ Direct invocation is deprecated. Use `create-polyglot init` going forward.'));
options._deprecatedNoticeShown = true;
}
await scaffoldMonorepo(projectNameArg, options);
});
// Additional commands must be registered before final parse.
program
.command('add')
.description('Add a new service or plugin')
.argument('<entity>', 'service | plugin')
.argument('<name>', 'Name of the service or plugin')
.option('--type <type>', 'Service type (node|python|go|java|frontend)')
.option('--lang <type>', '(Deprecated) Alias of --type')
.option('--port <port>', 'Service port')
.option('--yes', 'Non-interactive defaults')
.action(async (entity, name, opts) => {
const projectDir = process.cwd();
try {
if (entity === 'service') {
let type = opts.type || opts.lang;
let port = opts.port ? Number(opts.port) : undefined;
if (!opts.yes) {
const promptsMod = await import('prompts');
const p = promptsMod.default;
if (!type) {
const ans = await p({ type: 'select', name: 'type', message: 'Service type:', choices: [
{ title: 'Node.js', value: 'node' },
{ title: 'Python', value: 'python' },
{ title: 'Go', value: 'go' },
{ title: 'Java', value: 'java' },
{ title: 'Frontend (Next.js)', value: 'frontend' }
] });
type = ans.type;
}
if (!port) {
const ans = await p({ type: 'text', name: 'port', message: 'Port (leave blank for default):', validate: v => !v || (/^\d+$/.test(v) && +v>0 && +v<=65535) ? true : 'Invalid port' });
if (ans.port) port = Number(ans.port);
}
}
const defaultPorts = { frontend: 3000, node: 3001, go: 3002, java: 3003, python: 3004 };
if (!type) throw new Error('Service type required');
if (!port) port = defaultPorts[type];
await addService(projectDir, { type, name, port }, opts);
} else if (entity === 'plugin') {
await scaffoldPlugin(projectDir, name);
} else {
console.log(chalk.red(`Unknown entity '${entity}'. Use service or plugin.`));
process.exit(1);
}
} catch (e) {
console.error(chalk.red('Failed to add:'), e.message);
process.exit(1);
}
});
program
.command('dev')
.description('Run services locally (Node & frontend) or use --docker for compose')
.option('--docker', 'Use docker compose up --build to start all services')
.action(async (opts) => {
await runDev({ docker: !!opts.docker });
});
program
.command('services')
.description('List services in the current workspace (table)')
.option('--json', 'Output raw JSON instead of table')
.action(async (opts) => {
try {
const cwd = process.cwd();
const cfgPath = path.join(cwd, 'polyglot.json');
if (!fs.existsSync(cfgPath)) {
console.log(chalk.red('polyglot.json not found. Run inside a generated workspace.'));
process.exit(1);
}
const cfg = JSON.parse(fs.readFileSync(cfgPath, 'utf-8'));
if (opts.json) {
console.log(JSON.stringify(cfg.services, null, 2));
} else {
renderServicesTable(cfg.services, { title: 'Workspace Services' });
}
} catch (e) {
console.error(chalk.red('Failed to list services:'), e.message);
process.exit(1);
}
});
program
.command('admin')
.description('Launch admin dashboard to monitor service status')
.option('-p, --port <port>', 'Dashboard port (default: 8080)', '8080')
.option('-r, --refresh <ms>', 'Refresh interval in milliseconds (default: 5000)', '5000')
.option('--no-open', 'Don\'t auto-open browser')
.action(async (opts) => {
try {
const port = parseInt(opts.port);
const refresh = parseInt(opts.refresh);
if (isNaN(port) || port < 1 || port > 65535) {
console.error(chalk.red('Invalid port number. Must be between 1-65535.'));
process.exit(1);
}
if (isNaN(refresh) || refresh < 1000) {
console.error(chalk.red('Invalid refresh interval. Must be at least 1000ms.'));
process.exit(1);
}
await startAdminDashboard({
port,
refresh,
open: opts.open
});
} catch (e) {
console.error(chalk.red('Failed to start admin dashboard:'), e.message);
process.exit(1);
}
});
// Unified hot reload aggregator
program
.command('hot')
.description('Unified hot reload across services (auto-restart / HMR)')
.option('-s, --services <list>', 'Subset of services (comma names or types)')
.option('--dry-run', 'Show what would run without starting processes')
.action(async (opts) => {
try {
const filter = opts.services ? opts.services.split(',').map(s => s.trim()).filter(Boolean) : [];
await runHotReload({ servicesFilter: filter, dryRun: !!opts.dryRun });
} catch (e) {
console.error(chalk.red('Failed to start hot reload:'), e.message);
process.exit(1);
}
});
program.parse();