Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 70x 70x 2x 70x 70x 70x 70x 70x 70x 2x 2x 2x 2x 2x 2x 2x 70x 418x 418x 418x 418x 418x 418x 418x 68x 68x 68x 68x 68x 4x 4x 4x 4x 4x 64x 64x 68x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 6x 6x 6x 6x 3x 3x 6x 6x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 412x 412x 412x 412x 412x 412x 412x 412x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 418x 419x 26x 26x 26x 393x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 419x 393x 11x 393x 382x 8x 8x 393x 393x 393x 419x 2x 393x 391x 391x 1x 1x 391x 393x 393x 393x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 307x 393x 393x 393x 393x 393x 393x 393x 80x 77x 77x 77x 80x 80x 80x 419x 419x 61x 50x 50x 50x 61x 1x 1x 60x 61x 61x 3x 3x 57x 57x 6x 57x 4x 51x 18x 47x 24x 29x 5x 5x 57x 57x 419x 419x 9x 8x 8x 8x 9x 1x 1x 8x 9x 9x 1x 1x 7x 7x 1x 7x 1x 6x 1x 5x 4x 4x 7x 7x 419x 419x 130x 113x 113x 1x 1x 1x 1x 1x 1x 113x 129x 130x 130x 130x 419x 2x 2x 419x 419x 27x 27x 419x 419x 419x 419x 2x 2x 419x 419x 6x 6x 419x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 16x 5x 5x 16x 8x 8x 8x 8x 8x 8x 8x 3x 3x 8x 8x 8x 8x 8x 5x 8x 8x 8x 8x 8x 4x 4x 8x 8x 8x 8x 8x 8x 419x 419x 419x 8x 8x 419x 20x 20x 419x 10x 10x 419x 7x 7x 419x 4x 4x 419x 9x 9x 9x 9x 9x 419x 3x 3x 419x 7x 7x 7x 7x 7x 7x 1x 1x 1x 6x 6x 6x 419x 419x 418x | import path from 'path';
import { fileURLToPath } from 'url';
import loadConfig, { getClosestKey } from '../lib/config.js';
import { loadEnv } from '../lib/env.js';
import { checkGitStatus, validateComponentName } from './utils.js';
import { createSeverityFormatter, cyan, gray, red } from './colors.js';
import { initProject } from './commands/init.js';
import { generateComponent, generatePage, generateBridge, generateGuard } from './commands/generate.js';
import { destroyComponent, destroyPage, destroyBridge, destroyGuard } from './commands/destroy.js';
import { buildProject, cleanProject, checkProject } from './commands/build.js';
import { runFormat } from './commands/format.js';
import { serveProject, watchProject } from './commands/serve.js';
import { printHelp } from './commands/help.js';
import { runDoctor } from './commands/doctor.js';
import { runInspect } from './commands/inspect.js';
import { runStats } from './commands/stats.js';
import { runEnv } from './commands/env.js';
import { explainDiagnostic } from './commands/explain.js';
import { runTrace } from './commands/trace.js';
import { runAtlas, runQuery } from './commands/atlas.js';
/**
* The scaffold kinds `generate` and `destroy` accept as their first argument.
* @type {string[]}
*/
const SCAFFOLD_TYPES = ['component', 'c', 'page', 'p', 'bridge', 'guard'];
/**
* Refuses a scaffold command whose type is not one this CLI knows.
*
* `avenx g <name>` is the documented shorthand for generating a component, so
* an unrecognised first argument is normally a name. It cannot be a name when a
* second positional argument follows it: `avenx g pge home` can only have meant
* `avenx g page home`, and reading it as the shorthand created a component
* called "pge", registered it in `src/main.app.js`, discarded "home" and exited
* 0.
* @param {string} command - The command being run, for the message.
* @param {string} type - The first positional argument.
* @param {string|undefined} name - The second positional argument, if any.
* @returns {boolean} True when the command should not proceed.
*/
function refuseUnknownScaffoldType(command, type, name) {
if (!name || SCAFFOLD_TYPES.includes(type)) return false;
const verb = command === 'd' || command === 'destroy' ? 'remove' : 'scaffold';
const closest = getClosestKey(type, SCAFFOLD_TYPES);
console.error(red(`❌ Error: Unknown type "${type}".`));
console.error(gray(' Expected one of: component, page, bridge, guard.'));
if (closest) {
console.error(gray(` Did you mean "avenx ${command} ${closest} ${name}"?`));
}
console.error(
gray(` To ${verb} a component called "${type}", run "avenx ${command} ${type}" with no second argument.`),
);
process.exitCode = 1;
return true;
}
/**
* Refuses a scaffold command whose name is invalid, contains whitespace,
* starts with a digit, or attempts directory traversal.
* @param {string} command The command being run ('g' or 'd').
* @param {string} name The component/item name to validate.
* @returns {boolean} True when the command should not proceed.
*/
function refuseInvalidScaffoldName(command, name) {
if (!name) return false;
const result = validateComponentName(name);
if (!result.valid) {
console.error(red(`❌ Error: Invalid name "${name}".`));
console.error(gray(` ${result.reason}`));
process.exitCode = 1;
return true;
}
return false;
}
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const findProjectRoot = loadConfig.findProjectRoot;
/**
* Every command name and alias the CLI dispatches on.
*
* Used only to suggest a correction for a mistyped command, so an entry missing
* here costs a suggestion rather than a command.
* @type {string[]}
*/
export const KNOWN_COMMANDS = [
'init',
'generate',
'g',
'destroy',
'd',
'build',
'b',
'clean',
'check',
'lint',
'doctor',
'env',
'explain',
'inspect',
'i',
'stats',
's',
'atlas',
'impact',
'why',
'trace',
'serve',
'watch',
'w',
'format',
'help',
];
/**
* Reports a command the CLI does not recognise, with a correction when one is near.
* @param {string} command - The command as typed.
* @returns {void}
*/
function reportUnknownCommand(command) {
const closest = getClosestKey(command, KNOWN_COMMANDS);
console.error(red(`Unknown command: "${command}"`));
if (closest) {
console.error(gray(`Did you mean "avenx ${closest}"?`));
}
console.error('');
}
/**
* Avenx CLI - Command Line Interface router for Avenx-JS.
*/
export class AvenxCLI {
/**
* Creates an instance of AvenxCLI.
* Initializes the base directory and framework directory paths.
* @param {object} [options]
*/
constructor(options = {}) {
this.baseDir = options.baseDir || findProjectRoot(process.cwd());
loadEnv(this.baseDir);
this.frameworkDir = path.join(__dirname, '..');
this.config = { ...loadConfig(this.baseDir), ...options };
// Tint compiler warnings yellow and errors red for every command that compiles
// (build, check, watch, serve). Stays inert when colors are unsupported.
this.config.logging = { ...this.config.logging, formatter: createSeverityFormatter() };
}
/**
* Serves the project on specified port and host.
* @param {number} port
* @param {string} host
* @param {boolean} [open] - Whether to open the browser automatically.
*/
serveProject(port, host, open) {
return serveProject(this, port, host, open);
}
/**
* Executes a given CLI command with provided arguments.
* @param {string} command - The command to run (e.g., 'init', 'generate', 'build', 'serve', 'help').
* @param {string[]} args - Additional arguments for the command.
*/
async run(command, args = []) {
if (args.includes('--help')) {
printHelp();
return;
}
const dryRun = args.includes('--dry-run') || args.includes('-d');
const force = args.includes('--force') || args.includes('-f');
const withTest = args.includes('--with-test');
const noTest = args.includes('--no-test');
// `avenx build` is production by default; `--dev` opts out. `--prod` is
// accepted so a script can state the intent it relies on. `serve` and
// `watch` are development unless the flag says otherwise.
const explicitDev = args.includes('--dev') || args.includes('--development');
const explicitProd = args.includes('--prod') || args.includes('--production');
// `--csp-strict` is additive to the mode rather than a mode of its own. A
// CSP-strict development build is the case that makes the flag worth
// having: `avenx serve` is where the interpreter -- and with it the last
// `new Function` -- used to be unavoidable, and a developer whose target
// deployment forbids 'unsafe-eval' wants to find out at their desk rather
// than in a staging security review.
if (args.includes('--csp-strict')) {
this.config.csp = { ...(this.config.csp || {}), strict: true };
}
if (explicitDev) {
this.config.mode = 'development';
} else if (explicitProd) {
this.config.mode = 'production';
} else if (command === 'serve' || command === 'watch' || command === 'w') {
this.config.mode = 'development';
}
let templateName = null;
const templateIndex = args.findIndex((arg) => arg === '--template' || arg === '-t');
if (templateIndex !== -1 && templateIndex + 1 < args.length) {
templateName = args[templateIndex + 1];
} else {
const templateInline = args.find((arg) => arg.startsWith('--template=') || arg.startsWith('-t='));
if (templateInline) {
templateName = templateInline.split('=').slice(1).join('=');
}
}
const filteredArgs = args.filter(
(arg, idx) =>
arg !== '--dry-run' &&
arg !== '-d' &&
arg !== '--force' &&
arg !== '-f' &&
arg !== '--no-color' &&
arg !== '--no-colors' &&
arg !== '--dev' &&
arg !== '--development' &&
arg !== '--prod' &&
arg !== '--production' &&
arg !== '--template' &&
arg !== '-t' &&
!(templateIndex !== -1 && idx === templateIndex + 1) &&
!arg.startsWith('--template=') &&
!arg.startsWith('-t=') &&
arg !== '--with-test' &&
arg !== '--csp-strict' &&
arg !== '--no-test',
);
const type = filteredArgs[0];
const name = filteredArgs[1];
switch (command) {
case 'init':
if (!force) {
const proceed = await checkGitStatus(this.baseDir);
if (!proceed) {
// Declining is a refusal to run, not a successful no-op. Exiting 0
// here made `avenx build && deploy` deploy whatever was already in
// dist/, which contradicts the documented contract that build exits
// 0 only on a successful build.
process.exit(1);
}
}
await initProject(this, args);
process.exit(0);
break;
case 'generate':
case 'g': {
if (!force) {
const proceed = await checkGitStatus(this.baseDir);
if (!proceed) {
process.exit(1);
}
}
if (refuseUnknownScaffoldType('g', type, name)) {
break;
}
const targetName = SCAFFOLD_TYPES.includes(type) ? name : type;
if (refuseInvalidScaffoldName('g', targetName)) {
break;
}
if (type === 'bridge') {
generateBridge(this, name, dryRun, force, templateName);
} else if (type === 'guard') {
generateGuard(this, name, dryRun, force, templateName);
} else if (type === 'page' || type === 'p') {
generatePage(this, name, dryRun, force, templateName);
} else if (type === 'component' || type === 'c') {
generateComponent(this, name, dryRun, force, templateName, withTest, noTest);
} else {
generateComponent(this, type, dryRun, force, templateName, withTest, noTest);
}
break;
}
case 'destroy':
case 'd': {
if (!force) {
const proceed = await checkGitStatus(this.baseDir);
if (!proceed) {
process.exit(1);
}
}
if (refuseUnknownScaffoldType('d', type, name)) {
break;
}
const targetDestroyName = SCAFFOLD_TYPES.includes(type) ? name : (name || type);
if (refuseInvalidScaffoldName('d', targetDestroyName)) {
break;
}
if (type === 'bridge') {
destroyBridge(this, name, dryRun);
} else if (type === 'guard') {
destroyGuard(this, name, dryRun);
} else if (type === 'page' || type === 'p') {
destroyPage(this, name, dryRun);
} else if (type === 'component' || type === 'c') {
destroyComponent(this, name, dryRun);
} else {
destroyComponent(this, name || type, dryRun);
}
break;
}
case 'build':
case 'b':
if (!force) {
const proceed = await checkGitStatus(this.baseDir);
if (!proceed) {
// Declining is a refusal to run, not a successful no-op. Exiting 0
// here made `avenx build && deploy` deploy whatever was already in
// dist/, which contradicts the documented contract that build exits
// 0 only on a successful build.
process.exit(1);
}
}
buildProject(this, {
codeSplitting: args.includes('--code-splitting') || args.includes('--split') ? true : undefined,
});
break;
case 'clean':
cleanProject(this);
break;
case 'check':
case 'lint':
checkProject(this, args);
break;
case 'doctor':
runDoctor(this);
break;
case 'env':
runEnv(this);
break;
case 'inspect':
case 'i':
runInspect(this);
break;
case 'stats':
case 's':
runStats(this, args);
break;
case 'serve': {
// Recording is opt-in and development-only. It is read here rather
// than from config alone so that turning it on is always a visible,
// deliberate act at the command line.
this.traceEnabled = args.includes('--trace');
const portIdx = args.findIndex(
(a) => a === '--port' || a === '-p' || a.startsWith('--port=') || a.startsWith('-p='),
);
const hostIdx = args.findIndex(
(a) => a === '--host' || a === '-h' || a.startsWith('--host=') || a.startsWith('-h='),
);
const open = args.includes('--open') || args.includes('-o');
if (args.includes('--no-live-reload') || args.includes('--live-reload=false')) {
this.config.server.liveReload = false;
}
// The overlay rides on the live-reload channel, so turning live reload
// off turns the overlay off with it; the flag is for keeping the
// reloads while silencing the overlay.
if (args.includes('--no-error-overlay') || args.includes('--error-overlay=false')) {
this.config.server.errorOverlay = false;
}
// The positional port is the first bare argument that is not itself the
// value of a flag. Only args[0] used to be considered, so any leading
// flag — `serve --trace 8080`, `serve --open 8080` — silently fell back
// to the default port.
const flagValueIndexes = new Set();
for (const idx of [portIdx, hostIdx]) {
if (idx !== -1 && !args[idx].includes('=')) {
flagValueIndexes.add(idx + 1);
}
}
const positionalPort = args.find(
(arg, idx) => !arg.startsWith('-') && !flagValueIndexes.has(idx) && /^\d+$/.test(arg.trim()),
);
const rawPortVal =
portIdx !== -1
? args[portIdx].includes('=')
? args[portIdx].split('=').slice(1).join('=')
: args[portIdx + 1]
: (positionalPort ?? null);
const rawPort = rawPortVal?.replace(/[^0-9]/g, '');
const port = rawPort
? parseInt(rawPort, 10)
: (process.env.PORT ? parseInt(String(process.env.PORT).replace(/[^0-9]/g, ''), 10) : null) ||
this.config.server?.port ||
3000;
const rawHostVal =
hostIdx !== -1
? args[hostIdx].includes('=')
? args[hostIdx].split('=').slice(1).join('=')
: args[hostIdx + 1]
: null;
const host = rawHostVal ? rawHostVal.trim().replace(/\/+$/g, '') : 'localhost';
this.serveProject(port, host, open);
break;
}
case 'watch':
case 'w':
console.log(cyan(`👀 Watching for changes in ${this.config.srcDir}/...\n`));
buildProject(this, { watch: true });
watchProject(this);
process.on('SIGINT', () => {
console.log(`\n${gray('Stopping watch...')}`);
process.exit(0);
});
break;
case 'format':
runFormat(this, args);
break;
case 'trace':
runTrace(this, args);
break;
case 'atlas':
runAtlas(this, args);
break;
case 'impact':
runQuery(this, args, 'in');
break;
case 'why':
runQuery(this, args, 'out');
break;
case 'explain': {
const asJson = args.includes('--json');
const codeArg = args.find((a) => !a.startsWith('-'));
explainDiagnostic(this, codeArg, asJson);
break;
}
case 'help':
printHelp();
break;
default:
// No command at all is a request for help, and exits 0. An
// *unrecognised* command is a mistake, and used to do the same thing --
// so `avenx buidl` printed the help text and exited 0, which meant
// `avenx buidl && deploy` deployed whatever was already in dist/. A
// mistyped command now says so and exits non-zero.
if (!command || command.startsWith('-')) {
printHelp();
break;
}
reportUnknownCommand(command);
printHelp();
process.exit(1);
}
}
}
|