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 | 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 18x 18x 18x 18x 18x 18x 250x 250x 250x 250x 250x 250x 250x 250x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 222x 222x 17x 17x 17x 17x 17x 17x 17x 17x 239x 239x 239x 17x 17x 9x 17x 8x 8x 17x 17x 17x 8x 17x 9x 9x 17x 17x 17x 17x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 1x 1x 8x 9x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 9x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 7x 7x 7x 7x 7x 7x 8x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 250x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 17x 18x 2x 2x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 18x | import fs from 'fs';
import path from 'path';
import { parseName, readTemplate, abortIfGeneratedPathExists, fail } from '../utils.js';
import { yellow, cyan, green, gray } from '../colors.js';
/**
* Checks if test generation is enabled via CLI flag or avenx config.
* @param {object} cli
* @param {boolean} [withTestFlag]
* @param {boolean} [noTestFlag]
* @returns {boolean}
*/
function shouldGenerateTest(cli, withTestFlag, noTestFlag) {
if (noTestFlag) return false;
if (withTestFlag) return true;
if (cli?.config?.generate && typeof cli.config.generate.withTests === 'boolean') {
return cli.config.generate.withTests;
}
return false;
}
/**
* Automatically adds import and registration for a component in src/main.app.js.
* @param {object} cli
* @param {string} className
* @param {string} folderName
*/
export function registerInMainApp(cli, className, folderName) {
const mainPath = path.join(cli.baseDir, cli.config.srcDir, 'main.app.js');
if (!fs.existsSync(mainPath)) return;
const content = fs.readFileSync(mainPath, 'utf-8');
const importStatement = `import ${className} from './components/${folderName}/${folderName}.component.js';`;
const registerStatement = `app.register('${className}', ${className});`;
const lines = content.split('\n');
let lastImportIndex = -1;
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('import ')) lastImportIndex = i;
}
if (lastImportIndex !== -1) {
lines.splice(lastImportIndex + 1, 0, importStatement);
} else {
lines.unshift(importStatement);
}
let lastRegisterIndex = -1;
let appInstanceIndex = -1;
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('app.register(')) lastRegisterIndex = i;
if (lines[i].includes('new AvenxApp')) appInstanceIndex = i;
}
if (lastRegisterIndex !== -1) {
lines.splice(lastRegisterIndex + 1, 0, registerStatement);
} else if (appInstanceIndex !== -1) {
lines.splice(appInstanceIndex + 1, 0, '', registerStatement);
} else {
lines.push('', registerStatement);
}
const hasMount = lines.some((line) => line.includes('app.mount('));
if (!hasMount) {
lines.push(`\napp.mount('${className}');`);
} else {
lines.push(`// app.mount('${className}'); // Uncomment to mount this component`);
}
fs.writeFileSync(mainPath, lines.join('\n'));
console.log(green(`✅ Component '${className}' registered in ${cli.config.srcDir}/main.app.js`));
}
/**
* Generates a new Bridge class and template file.
* @param {object} cli
* @param {string} name
* @param {boolean|object} [dryRun]
* @param {boolean} [force]
* @param {string|null} [templateName]
*/
export function generateBridge(cli, name, dryRun = false, force = false, templateName = null) {
if (typeof dryRun === 'object' && dryRun !== null) {
force = dryRun.force || false;
templateName = dryRun.templateName || null;
dryRun = dryRun.dryRun || false;
}
if (!name) {
fail('Please provide a bridge name (e.g., avenx g bridge auth)');
return;
}
const { folderFileName: lowerName } = parseName(name);
// A bridge is identified by its module, so its name is simply the file name
// in camelCase — the same identifier you would import it under.
const bridgeName = lowerName
.split(/[-_.]/)
.filter(Boolean)
.map((part, index) => (index === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1)))
.join('');
const globalDir = path.join(cli.baseDir, cli.config.srcDir, 'global');
const bridgePath = path.join(globalDir, `${lowerName}.bridge.js`);
if (!force && abortIfGeneratedPathExists(cli.baseDir, 'Bridge', lowerName, [bridgePath])) {
return;
}
if (force && fs.existsSync(bridgePath)) {
console.warn(yellow(`⚠️ Force enabled: overwriting existing Bridge '${lowerName}'.`));
}
if (dryRun) {
console.log(
cyan(`🧪 [Dry Run] Bridge '${bridgeName}' would be created at ${cli.config.srcDir}/global/${lowerName}.bridge.js`),
);
console.log(cyan('🧪 [Dry Run] No files were written.'));
return;
}
if (!fs.existsSync(globalDir)) {
fs.mkdirSync(globalDir, { recursive: true });
}
const template = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'bridge', 'bridge.js.template', templateName);
fs.writeFileSync(
bridgePath,
template.replace(/{{ name }}/g, bridgeName).replace(/{{ file }}/g, lowerName),
);
console.log(green(`✅ Bridge '${bridgeName}' generated at ${cli.config.srcDir}/global/${lowerName}.bridge.js`));
console.log(
gray(`ℹ️ Import it where you need it: import ${bridgeName} from '<path>/global/${lowerName}.bridge.js';`),
);
}
/**
* Generates a new Guard class and template file.
* @param {object} cli
* @param {string} name
* @param {boolean|object} [dryRun]
* @param {boolean} [force]
* @param {string|null} [templateName]
*/
export function generateGuard(cli, name, dryRun = false, force = false, templateName = null) {
if (typeof dryRun === 'object' && dryRun !== null) {
force = dryRun.force || false;
templateName = dryRun.templateName || null;
dryRun = dryRun.dryRun || false;
}
if (!name) {
fail('Please provide a guard name (e.g., avenx g guard auth)');
return;
}
const { capitalizedName: baseName, folderFileName: lowerName } = parseName(name);
const capitalizedName = baseName + 'Guard';
const guardDir = path.join(cli.baseDir, cli.config.srcDir, 'guards');
const guardPath = path.join(guardDir, `${lowerName}.guard.js`);
if (!force && abortIfGeneratedPathExists(cli.baseDir, 'Guard', lowerName, [guardPath])) {
return;
}
if (force && fs.existsSync(guardPath)) {
console.warn(yellow(`⚠️ Force enabled: overwriting existing Guard '${lowerName}'.`));
}
if (dryRun) {
console.log(
cyan(`🧪 [Dry Run] Guard '${capitalizedName}' would be created at ${cli.config.srcDir}/guards/${lowerName}.guard.js`),
);
console.log(cyan('🧪 [Dry Run] No files were written.'));
return;
}
if (!fs.existsSync(guardDir)) {
fs.mkdirSync(guardDir, { recursive: true });
}
const template = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'guard', 'guard.js.template', templateName);
fs.writeFileSync(guardPath, template.replace(/{{ name }}/g, capitalizedName));
console.log(green(`✅ Guard '${capitalizedName}' generated at ${cli.config.srcDir}/guards/${lowerName}.guard.js`));
console.log(gray(`ℹ️ It can be used in your route configurations.`));
}
/**
* Generates a new Page class and template files.
* @param {object} cli
* @param {string} name
* @param {boolean|object} [dryRun]
* @param {boolean} [force]
* @param {string|null} [templateName]
*/
export function generatePage(cli, name, dryRun = false, force = false, templateName = null) {
if (typeof dryRun === 'object' && dryRun !== null) {
force = dryRun.force || false;
templateName = dryRun.templateName || null;
dryRun = dryRun.dryRun || false;
}
if (!name) {
fail('Please provide a page name (e.g., avenx g page home)');
return;
}
const { capitalizedName, folderFileName: lowerName } = parseName(name);
const pageDir = path.join(cli.baseDir, cli.config.srcDir, 'pages');
const jsPath = path.join(pageDir, `${lowerName}.page.js`);
const cssPath = path.join(pageDir, `${lowerName}.page.css`);
if (!force && abortIfGeneratedPathExists(cli.baseDir, 'Page', lowerName, [jsPath, cssPath])) {
return;
}
if (force && (fs.existsSync(jsPath) || fs.existsSync(cssPath))) {
console.warn(yellow(`⚠️ Force enabled: overwriting existing Page '${lowerName}'.`));
}
if (dryRun) {
console.log(cyan(`🧪 [Dry Run] Page '${capitalizedName}' would be created at:`));
console.log(` ${cli.config.srcDir}/pages/${lowerName}.page.js`);
console.log(` ${cli.config.srcDir}/pages/${lowerName}.page.css`);
console.log(cyan('🧪 [Dry Run] No files were written.'));
return;
}
if (!fs.existsSync(pageDir)) {
fs.mkdirSync(pageDir, { recursive: true });
}
const jsTemplate = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'page', 'page.js.template', templateName);
const cssTemplate = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'page', 'page.css.template', templateName);
fs.writeFileSync(jsPath, jsTemplate.replace(/{{ name }}/g, capitalizedName));
fs.writeFileSync(cssPath, cssTemplate);
console.log(green(`✅ Page '${capitalizedName}' generated at ${cli.config.srcDir}/pages/${lowerName}.page.js`));
reportPageWiring(cli, capitalizedName, lowerName);
}
/**
* Tells the developer what to add so the new page can actually be reached.
*
* The previous line read "it will be automatically registered and routed if
* you update src/main.app.js", which says both that nothing is required and
* that something is. Nothing is automatic: a page the route table does not
* name is compiled into the bundle and never rendered, and the application
* shows an empty container with no error. A developer who followed
* `avenx init` and `avenx generate page` reached exactly that, so this prints
* the line to add rather than alluding to it.
* @param {object} cli - The CLI instance.
* @param {string} className - The generated page's class name.
* @param {string} lowerName - The kebab-case file name.
* @returns {void}
*/
function reportPageWiring(cli, className, lowerName) {
const mainAppPath = path.join(cli.baseDir, cli.config.srcDir, 'main.app.js');
const route = lowerName === 'home' || lowerName === 'index' ? '' : `#/${lowerName}`;
let source;
try {
source = fs.readFileSync(mainAppPath, 'utf8');
} catch {
console.log(gray(`ℹ️ Add a route for it in ${cli.config.srcDir}/main.app.js.`));
return;
}
if (new RegExp(`['"\`]${className}['"\`]`).test(source)) {
console.log(gray(`ℹ️ ${cli.config.srcDir}/main.app.js already names '${className}'.`));
return;
}
if (/\binitRouter\s*\(/.test(source)) {
console.log(gray(`ℹ️ Add it to the route table in ${cli.config.srcDir}/main.app.js:`));
console.log(gray(` '${route}': '${className}',`));
return;
}
console.log(gray(`ℹ️ Nothing routes to it yet. Add to ${cli.config.srcDir}/main.app.js:`));
console.log(gray(` app.initRouter({ '${route}': '${className}' });`));
console.log(gray(' Without a route, the page is bundled but never rendered (AVX_W53).'));
}
/**
* Generates a new component folder and template files, and registers it in main.app.js.
* @param {object} cli
* @param {string} name
* @param {boolean|object} [dryRun]
* @param {boolean} [force]
* @param {string|null} [templateName]
* @param {boolean} [withTest]
* @param {boolean} [noTest]
*/
export function generateComponent(
cli,
name,
dryRun = false,
force = false,
templateName = null,
withTest = false,
noTest = false,
) {
if (typeof dryRun === 'object' && dryRun !== null) {
force = dryRun.force || false;
templateName = dryRun.templateName || null;
withTest = dryRun.withTest || false;
noTest = dryRun.noTest || false;
dryRun = dryRun.dryRun || false;
} else if (typeof force === 'boolean' && typeof templateName === 'boolean') {
// Backward compatibility if called as (cli, name, dryRun, withTest, noTest)
noTest = templateName;
withTest = force;
force = false;
templateName = null;
}
if (!name) {
fail('Please provide a component name (e.g., avenx g my-component)');
return;
}
const { capitalizedName, folderFileName: lowerName } = parseName(name);
const compDir = path.join(cli.baseDir, cli.config.srcDir, 'components', lowerName);
const willGenerateTest = shouldGenerateTest(cli, withTest, noTest);
const jsPath = path.join(compDir, `${lowerName}.component.js`);
const cssPath = path.join(compDir, `${lowerName}.component.css`);
const testPath = path.join(compDir, `${lowerName}.component.test.js`);
const pathsToCheck = [compDir];
if (!force && abortIfGeneratedPathExists(cli.baseDir, 'Component', lowerName, pathsToCheck)) {
return;
}
if (force && fs.existsSync(compDir)) {
console.warn(yellow(`⚠️ Force enabled: overwriting existing Component '${lowerName}'.`));
}
if (dryRun) {
console.log(cyan(`🧪 [Dry Run] Component '${lowerName}' would be created at:`));
console.log(` ${cli.config.srcDir}/components/${lowerName}/${lowerName}.component.js`);
console.log(` ${cli.config.srcDir}/components/${lowerName}/${lowerName}.component.css`);
if (willGenerateTest) {
console.log(` ${cli.config.srcDir}/components/${lowerName}/${lowerName}.component.test.js`);
}
console.log(cyan(`🧪 [Dry Run] ${cli.config.srcDir}/main.app.js would be updated with:`));
console.log(` import ${capitalizedName} from './components/${lowerName}/${lowerName}.component.js';`);
console.log(` app.register('${capitalizedName}', ${capitalizedName});`);
console.log(cyan('🧪 [Dry Run] No files were written.'));
return;
}
fs.mkdirSync(compDir, { recursive: true });
const jsTemplate = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'component', 'component.js.template', templateName);
const cssTemplate = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'component', 'component.css.template', templateName);
fs.writeFileSync(
jsPath,
jsTemplate.replace(/{{ name }}/g, capitalizedName),
);
fs.writeFileSync(cssPath, cssTemplate);
if (willGenerateTest) {
const testTemplate = readTemplate(cli.baseDir, cli.config, cli.frameworkDir, 'component', 'component.test.js.template', templateName);
fs.writeFileSync(
testPath,
testTemplate
.replace(/{{PascalCaseName}}/g, capitalizedName)
.replace(/{{name}}/g, lowerName),
);
}
console.log(green(`✅ Component '${lowerName}' generated at ${cli.config.srcDir}/components/${lowerName}/`));
registerInMainApp(cli, capitalizedName, lowerName);
}
|