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 | 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 64x 64x 64x 64x 64x 64x 64x 427x 427x 427x 427x 427x 427x 68x 68x 68x 68x 1x 1x 1x 1x 1x 67x 67x 67x 1x 1x 1x 1x 1x 66x 66x 66x 66x 2x 2x 2x 2x 2x 64x 64x 68x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 248x 248x 248x 248x 248x 248x 248x 248x 6x 6x 37x 37x 37x 248x 36x 36x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 242x 205x 205x 248x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 4x 4x 4x 4x 4x 4x 8x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 10x 10x 10x 4x 4x 9x 1x 1x 5x 5x 5x 5x 4x 4x 4x 3x 3x 4x 4x 4x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 8x 8x 8x 8x 8x 10x 10x 10x 10x 2x 2x 10x 8x 8x 8x 10x 10x 10x 10x 10x 8x 8x 2x 2x 2x 2x 2x 2x 10x 10x 8x 8x 8x 8x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 417x 417x 417x 417x 417x 6x 6x 6x 6x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 34x 6x 6x 34x 4x 411x 411x 816x 816x 4x 4x 812x 812x 812x 2x 2x 816x 405x 405x 405x 417x 427x 427x 427x 427x 427x 427x 2x 2x 2x 427x 427x 427x 427x 427x 427x 427x 427x 427x 427x 49x 49x 47x 47x 2x 2x 2x 2x 2x 2x 49x 427x 427x 427x 427x 427x 427x 427x 427x 427x 7x 7x 7x 7x 427x 427x 427x 427x 427x 427x 427x 427x 9x 9x 24x 24x 24x 17x 17x 9x 9x 7x 9x 427x 427x 427x 427x 427x 427x 427x 427x | import fs from 'fs';
import path from 'path';
import readline from 'node:readline';
import { execSync } from 'child_process';
import { red, yellow, gray } from './colors.js';
/**
* Helper to parse input names into PascalCase and kebab-case.
* Supports camelCase, kebab-case, snake_case, and PascalCase.
* @param {string} inputName - The input name from CLI.
* @returns {{capitalizedName: string, folderFileName: string}}
*/
export function parseName(inputName) {
let processedName = inputName;
if (inputName === inputName.toUpperCase() && inputName !== inputName.toLowerCase()) {
processedName = inputName.toLowerCase();
}
const parts = processedName.split(/(?<=[a-z0-9])(?=[A-Z])|[-_]/).filter(Boolean);
const capitalizedName = parts.map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join('');
const folderFileName = parts.map((part) => part.toLowerCase()).join('-');
return { capitalizedName, folderFileName };
}
/**
* Validates that a scaffold name is a valid identifier / kebab / snake token
* and does not attempt path traversal or corrupt JS files.
*/
export function validateComponentName(name) {
if (!name || typeof name !== 'string') {
return {
valid: false,
reason: 'Name must not be empty. Example: "my-component" or "MyComponent".'
};
}
// Refuse whitespace
if (/\s/.test(name)) {
return {
valid: false,
reason: `Name "${name}" cannot contain whitespace. Example: "my-component" or "MyComponent".`
};
}
// Refuse leading digits
if (/^[0-9]/.test(name)) {
return {
valid: false,
reason: `Name "${name}" cannot start with a number. Example: "my-component" or "MyComponent".`
};
}
// Refuse path traversal, slashes, dots, or invalid identifier characters
// Only allow letters, digits, dashes, and underscores, starting with an ASCII letter
if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(name)) {
return {
valid: false,
reason: `Name "${name}" contains invalid characters or path separators. Example: "my-component" or "MyComponent".`
};
}
return { valid: true };
}
/**
* Checks if git status is clean or prompts user if there are unstaged changes.
*
* Scoped to the project directory rather than to whatever the shell's working
* directory happens to be: the guard exists to protect the files the command
* is about to write, and those live under the project root. Reading
* `process.cwd()` instead meant a command run from a subdirectory reported the
* status of an unrelated enclosing repository.
*
* Git's own stderr is discarded. Outside a repository git writes
* `fatal: not a git repository` to stderr, which `execSync` forwards to the
* parent by default -- so every `avenx init` in a plain directory printed a
* fatal-looking line above its own output while in fact succeeding.
* @param {string} [cwd] - The project root to inspect. Defaults to the process's directory.
* @returns {boolean|Promise<boolean>} True to proceed, false when the user declined.
*/
export function checkGitStatus(cwd = process.cwd()) {
try {
const output = execSync('git status --porcelain', {
cwd,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
});
if (!output.trim()) {
return true;
}
console.warn(yellow('⚠️ You have unstaged changes in your repository.'));
if (!process.stdin.isTTY || !process.stdout.isTTY) {
return true;
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise((resolve) => {
rl.question('Do you want to proceed? (y/N) ', (answer) => {
rl.close();
if (answer.trim().toLowerCase() === 'y') {
resolve(true);
} else {
console.log(gray('Operation cancelled.'));
resolve(false);
}
});
});
} catch {
return true;
}
}
/**
* Creates a shared prompt session that buffers stdin lines between questions.
*
* `readline.question()` only listens for the next line while that particular
* question is active. In a piped run, stdin can deliver several lines at once,
* so answers after the first one can otherwise be emitted before the next
* question starts and be lost. Keeping one interface for the whole wizard and
* queueing its `line` events preserves every supplied answer.
*
* EOF also settles any pending question with `null`, so callers never wait on
* a promise that can no longer receive input.
* @param {NodeJS.ReadableStream} [input] - Input stream.
* @param {NodeJS.WritableStream} [output] - Output stream.
* @returns {{question: function(string): Promise<string|null>, close: function(): void}}
*/
export function createPromptSession(input = process.stdin, output = process.stdout) {
const rl = readline.createInterface({ input, output });
const queuedAnswers = [];
const pendingQuestions = [];
let closed = false;
rl.on('line', (answer) => {
const resolve = pendingQuestions.shift();
if (resolve) {
resolve(answer);
} else {
queuedAnswers.push(answer);
}
});
rl.on('close', () => {
closed = true;
while (pendingQuestions.length > 0) {
pendingQuestions.shift()(null);
}
});
return {
question(query) {
output.write(query);
if (queuedAnswers.length > 0) {
return Promise.resolve(queuedAnswers.shift());
}
if (closed) {
return Promise.resolve(null);
}
return new Promise((resolve) => {
pendingQuestions.push(resolve);
});
},
close() {
if (!closed) {
rl.close();
}
},
};
}
/**
* Prompts the user with a question on the command line.
* @param {string} query - The question query.
* @param {string} [defaultValue] - The default response.
* @param {function(string): (boolean|string)} [validator] - Optional function validating input.
* @param {{question: function(string): Promise<string|null>, close: function(): void}} [session]
* Shared prompt session. When omitted, this function owns a temporary session.
* @returns {Promise<string>}
*/
export async function promptQuestion(query, defaultValue, validator = null, session = null) {
const promptSession = session || createPromptSession();
const ownsSession = !session;
try {
while (true) {
const answer = await promptSession.question(query);
let trimmed;
if (answer === null) {
if (defaultValue === undefined) {
const promptName = query.split('\n')[0].trim();
throw new Error(`Input ended before an answer was provided for: ${promptName}`);
}
trimmed = defaultValue;
} else {
trimmed = answer.trim();
if (trimmed === '' && defaultValue !== undefined) {
trimmed = defaultValue;
}
}
if (!validator) {
return trimmed;
}
const valid = validator(trimmed);
if (valid === true) {
return trimmed;
}
console.log(red(`❌ ${valid}`));
// Once stdin has reached EOF there is no value a retry could consume.
// A valid default has already returned above, so an invalid default must
// fail instead of spinning forever.
if (answer === null) {
throw new Error(`Input ended while retrying: ${query.split('\n')[0].trim()}`);
}
}
} finally {
if (ownsSession) {
promptSession.close();
}
}
}
/**
* Reads a template, checking custom template overrides in templatesDir and templates/ folder first.
* @param {string} baseDir
* @param {object} config
* @param {string} frameworkDir
* @param {string} subfolder
* @param {string} filename
* @param {string|null} [templateName]
* @returns {string}
*/
export function readTemplate(baseDir, config, frameworkDir, subfolder, filename, templateName = null) {
const dirs = [config?.templatesDir || '.avenxtemplates', 'templates'].filter(
(dir, idx, self) => dir && self.indexOf(dir) === idx,
);
if (templateName) {
const ext = path.extname(filename);
const basename = filename.replace(/\.template$/, '');
for (const dir of dirs) {
const candidatePaths = [
path.join(baseDir, dir, subfolder, templateName, filename),
path.join(baseDir, dir, subfolder, `${templateName}.${filename}`),
path.join(baseDir, dir, subfolder, `${basename}.${templateName}.template`),
path.join(baseDir, dir, templateName, filename),
path.join(baseDir, dir, `${templateName}.${filename}`),
path.join(baseDir, dir, `${templateName}.${subfolder}${ext}.template`),
path.join(baseDir, dir, `${templateName}${ext}.template`),
];
for (const candidatePath of candidatePaths) {
if (fs.existsSync(candidatePath)) {
return fs.readFileSync(candidatePath, 'utf-8');
}
}
}
}
for (const dir of dirs) {
const localStructuredPath = path.join(baseDir, dir, subfolder, filename);
if (fs.existsSync(localStructuredPath)) {
return fs.readFileSync(localStructuredPath, 'utf-8');
}
const localFlatPath = path.join(baseDir, dir, filename);
if (fs.existsSync(localFlatPath)) {
return fs.readFileSync(localFlatPath, 'utf-8');
}
}
const globalPath = path.join(frameworkDir, 'templates', subfolder, filename);
return fs.readFileSync(globalPath, 'utf-8');
}
/**
* Reports a CLI error and marks the process as failed.
* @param {string} message
*/
export function fail(message) {
console.error(red(`❌ Error: ${message}`));
process.exitCode = 1;
}
/**
* Stops generation if any target path already exists.
* @param {string} baseDir
* @param {string} type
* @param {string} name
* @param {string[]} targetPaths
* @returns {boolean}
*/
export function abortIfGeneratedPathExists(baseDir, type, name, targetPaths) {
const existingPath = targetPaths.find((targetPath) => fs.existsSync(targetPath));
if (!existingPath) {
return false;
}
fail(
`${type} '${name}' already exists at ${path.relative(baseDir, existingPath)}. ` +
'Remove the existing file or choose a different name.',
);
return true;
}
/**
* Cross-platform directory watcher with recursive support fallback.
* Node 18 on Linux does not support fs.watch(dir, { recursive: true }).
* @param {string} dirPath - Directory to watch.
* @param {Function} callback - Event callback (eventType, filename).
* @returns {{close: Function}|object} FSWatcher or compatible watcher object with close() method.
*/
export function watchDirectory(dirPath, callback) {
try {
return fs.watch(dirPath, { recursive: true }, callback);
} catch (err) {
if (err && err.code === 'ERR_FEATURE_UNAVAILABLE_ON_PLATFORM') {
return createRecursiveWatcherFallback(dirPath, callback);
}
throw err;
}
}
/**
* Watches each existing directory once and returns one handle for the group.
* @param {string[]} dirPaths - Directories to watch recursively.
* @param {Function} callback - Event callback shared by every watcher.
* @returns {{close: Function}|null} A closable group, or null when none exist.
*/
export function watchDirectories(dirPaths, callback) {
const paths = [...new Set(dirPaths.filter(Boolean).map((dirPath) => path.resolve(dirPath)))];
const watchers = paths.filter((dirPath) => {
try {
return fs.statSync(dirPath).isDirectory();
} catch {
return false;
}
}).map((dirPath) => watchDirectory(dirPath, callback));
if (watchers.length === 0) return null;
return { close: () => watchers.forEach((watcher) => watcher.close()) };
}
/**
* Fallback recursive watcher for platforms/Node versions lacking native recursive watch.
* Walks directory tree and registers individual fs.watch instances.
* @param {string} rootPath - Root directory to watch.
* @param {Function} callback - Event callback.
* @returns {{close: Function}}
*/
function createRecursiveWatcherFallback(rootPath, callback) {
const watchers = new Map();
function scanAndWatch(currentDir) {
if (!fs.existsSync(currentDir)) return;
if (!watchers.has(currentDir)) {
try {
const watcher = fs.watch(currentDir, (eventType, filename) => {
const relativeDir = path.relative(rootPath, currentDir);
const relativeFile = filename
? (relativeDir ? path.join(relativeDir, filename) : filename).replace(/\\/g, '/')
: relativeDir
? relativeDir.replace(/\\/g, '/')
: '';
const fullPath = filename ? path.join(currentDir, filename) : currentDir;
try {
if (fs.existsSync(fullPath) && fs.statSync(fullPath).isDirectory()) {
scanAndWatch(fullPath);
}
} catch {
// Ignore stat errors on deleted / inaccessible entries
}
callback(eventType, relativeFile);
});
watchers.set(currentDir, watcher);
} catch {
// Ignore watch errors on transient dirs or permission errors
}
}
try {
const entries = fs.readdirSync(currentDir, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
scanAndWatch(path.join(currentDir, entry.name));
}
}
} catch {
// Ignore read errors on inaccessible dirs
}
}
scanAndWatch(rootPath);
return {
close() {
for (const watcher of watchers.values()) {
try {
watcher.close();
} catch {
// Ignore close errors
}
}
watchers.clear();
},
};
}
|