Files
create/src/args.ts
S.Gromov f0daed180f feat: автодополнение и детект режима запуска
- Добавлены служебные команды __list-templates/__list-vars и генерация completion для bash/
    zsh/fish
  - Введён детект режимов запуска (npx/local/direct/global) и применён в проверке обновлений
  - Обновлены help и документация (README, FEATURES)
2026-01-27 13:07:44 +03:00

103 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ParsedArgs } from './types';
export function printHelp() {
const lines = [
'Использование:',
' npx @gromlab/create <шаблон> <имя> [путь] [опции]',
'',
'Аргументы:',
' <шаблон> Имя шаблона',
' <имя> Значение переменной name (обязательный аргумент)',
' [путь] Папка вывода (по умолчанию: текущая директория)',
'',
'Команды:',
' completion --shell <bash|zsh|fish> Сгенерировать скрипт автодополнения',
'',
'Опции:',
' --<var> <value> Переменная шаблона (поддерживается любой --key <value>)',
' --overwrite Перезаписывать существующие файлы',
' --skip-update Не проверять обновления CLI',
' -h, --help Показать эту справку',
'',
'Примеры:',
' npx @gromlab/create component Button',
' npx @gromlab/create component Button src/components'
];
console.log(lines.join('\n'));
}
function consumeValue(args: string[], index: number, key: string): string {
const next = args[index + 1];
if (!next || next.startsWith('-')) {
throw new Error(`Missing value for --${key}`);
}
return next;
}
export function parseArgs(argv: string[]): ParsedArgs {
const parsed: ParsedArgs = {
vars: {},
overwrite: false,
skipUpdate: false,
help: false,
extra: []
};
const args = argv.slice(2);
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '-h' || arg === '--help') {
parsed.help = true;
continue;
}
if (arg === '--overwrite') {
parsed.overwrite = true;
continue;
}
if (arg === '--skip-update') {
parsed.skipUpdate = true;
continue;
}
if (arg.startsWith('--')) {
const eqIndex = arg.indexOf('=');
const key = eqIndex === -1 ? arg.slice(2) : arg.slice(2, eqIndex);
const inlineValue = eqIndex === -1 ? undefined : arg.slice(eqIndex + 1);
if (!key) continue;
if (key === 'templates' || key === 'templatesPath' || key === 'templates-path') {
throw new Error('Опция --templates не поддерживается');
}
if (key === 'out' || key === 'output') {
throw new Error('Опция --out не поддерживается');
}
const value = inlineValue ?? consumeValue(args, i, key);
if (inlineValue === undefined) i++;
parsed.vars[key] = value;
continue;
}
if (!parsed.templateName) {
parsed.templateName = arg;
continue;
}
if (!parsed.positionalName) {
parsed.positionalName = arg;
continue;
}
if (!parsed.positionalOutDir) {
parsed.positionalOutDir = arg;
continue;
}
parsed.extra.push(arg);
}
return parsed;
}