Files
CodeListing/index.ts

113 lines
3.2 KiB
TypeScript
Raw Permalink 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 { Config } from './config/config';
import { logger } from './config/logger';
import { DocBuilder } from './docx/doc-builder';
import { RepoToDoc } from './fs';
import { GitService } from './git';
import fs from 'node:fs/promises';
import * as path from 'path';
import { confirm } from '@clack/prompts';
import * as p from '@clack/prompts';
import pc from 'picocolors';
const defaultIgnore = [
'node_modules',
'.git',
'.env',
'.gitignore',
'.DS_Store',
'package-lock.json',
'bun.lockb',
'.vscode',
];
async function hello() {
p.intro(pc.red(pc.bold('CodeListing')));
await new Promise((resolve) => setTimeout(resolve, 700));
p.note(
`
- ${pc.green('указываете конфигурацию в .env файле')}
${pc.gray('*файл создасться при первом запуске')}
- ${pc.green('запускаете')}
${pc.gray('*если репо не приватный и...')}
- ${pc.green('листинг кода в .docx файле появляется там, где вы указазиы')}
`,
);
p.note(pc.gray('не работает с приватными репозиториями'));
await new Promise((resolve) => setTimeout(resolve, 700));
const shouldContinue = await confirm({
message: 'Do you want to continue?',
});
return shouldContinue;
}
async function checkAndCreateEnvFile() {
try {
const envPath = path.join(process.cwd(), '.env');
try {
await fs.access(envPath);
logger.info('Файл .env найден, используем существующие настройки');
} catch (err) {
const envTemplate = `# URL репозитория для клонирования
GIT_REPO_PATH=https://github.com/nodegit/nodegit.git
PATH_TO_SAVE=./repo
PATH_TO_SAVE_DOCX=./report
BRANCH_NAME=main # try origin/main or main
`;
await fs.writeFile(envPath, envTemplate);
logger.info(
'Создан новый файл .env с примером настроек. Пожалуйста, отредактируйте его и запустите программу снова.',
);
process.exit(0);
}
} catch (error) {
logger.error('Ошибка при проверке файла .env:', error);
throw error;
}
}
async function main() {
try {
if (!(await hello())) {
throw new Error('вы решили уйти');
}
await checkAndCreateEnvFile();
logger.info('app started');
const config = Config.loadConfig();
const gitService = new GitService(config);
await gitService.getRepo(config.branch);
const builder = new DocBuilder();
const ignore = await gitService.getIgnore(true);
const repoToDoc = new RepoToDoc(
Array.isArray(ignore) && ignore.length ? ignore : defaultIgnore,
config.pathToSave,
builder,
);
await repoToDoc.processFiles();
await builder.saveFile(config.pathToSaveDocx);
logger.info(`Отчет сохранен в: ${config.pathToSaveDocx}/lab.docx`);
logger.info('End');
} catch (e: unknown) {
logger.error(`Error: ${e instanceof Error ? e.message : e}`);
p.outro(pc.green('пока'));
process.exit(1);
}
}
main();