|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const ignore = require('ignore'); |
| 4 | + |
| 5 | +// 대상 디렉토리와 출력 파일 경로 설정 |
| 6 | +const targetDirectory = './'; // 사용하려는 디렉토리 경로로 변경 |
| 7 | +const outputFile = 'output.txt'; |
| 8 | + |
| 9 | +// ignore 인스턴스 생성 |
| 10 | +const ig = ignore(); |
| 11 | + |
| 12 | +// 1. .gitignore 파일 존재 시 그 내용도 추가 |
| 13 | +const gitignorePath = path.join(targetDirectory, '.gitignore'); |
| 14 | +if (fs.existsSync(gitignorePath)) { |
| 15 | + const gitignoreContent = fs.readFileSync(gitignorePath, 'utf8'); |
| 16 | + ig.add(gitignoreContent); |
| 17 | + console.log('.gitignore 파일의 규칙을 추가합니다.'); |
| 18 | +} else { |
| 19 | + console.log('.gitignore 파일이 없어 건너뜁니다.'); |
| 20 | +} |
| 21 | + |
| 22 | +// 2. 사용자 정의 ignore 배열 추가 |
| 23 | +const customIgnorePatterns = [ |
| 24 | + '.git/', // 디렉토리 |
| 25 | + '.devcontainer/', // 디렉토리 |
| 26 | + '.github/', // 디렉토리 |
| 27 | + '*.md', // Markdown 파일 |
| 28 | + 'package-lock.json', // package-lock.json 파일 |
| 29 | + '*.png', '*.jpg', '*.jpeg', '*.gif', '*.bmp', '*.tiff', // 이미지 파일들 |
| 30 | + '*.xml', // XML 파일 |
| 31 | + '*.ico', // 아이콘 파일 |
| 32 | +]; |
| 33 | +ig.add(customIgnorePatterns); |
| 34 | +console.log('Custom ignore 패턴을 추가합니다.'); |
| 35 | + |
| 36 | +// 현재 실행 중인 스크립트 파일 자체(자기 자신)는 별도로 체크 |
| 37 | +const selfFilePath = path.resolve(__filename); |
| 38 | + |
| 39 | +// 재귀적으로 디렉토리 내 모든 파일 경로 수집 (ignore 규칙 적용) |
| 40 | +function getFiles(dir, baseDir = dir) { |
| 41 | + let results = []; |
| 42 | + const list = fs.readdirSync(dir); |
| 43 | + list.forEach(file => { |
| 44 | + const fullPath = path.join(dir, file); |
| 45 | + const relativePath = path.relative(baseDir, fullPath); |
| 46 | + |
| 47 | + // 실행 파일 자체 제외 |
| 48 | + if (path.resolve(fullPath) === selfFilePath) { |
| 49 | + console.log(`자기 자신 파일(${relativePath}) 제외`); |
| 50 | + return; |
| 51 | + } |
| 52 | + // 출력 파일도 제외 |
| 53 | + if (path.resolve(fullPath) === path.resolve(outputFile)) { |
| 54 | + console.log(`출력 파일(${relativePath}) 제외`); |
| 55 | + return; |
| 56 | + } |
| 57 | + // ignore 패턴 적용 |
| 58 | + if (ig.ignores(relativePath)) { |
| 59 | + console.log(`제외: ${relativePath}`); |
| 60 | + return; |
| 61 | + } |
| 62 | + const stat = fs.statSync(fullPath); |
| 63 | + if (stat && stat.isDirectory()) { |
| 64 | + results = results.concat(getFiles(fullPath, baseDir)); |
| 65 | + } else { |
| 66 | + results.push(fullPath); |
| 67 | + } |
| 68 | + }); |
| 69 | + return results; |
| 70 | +} |
| 71 | + |
| 72 | +// 내보내기 대상 파일 리스트 획득 |
| 73 | +const files = getFiles(targetDirectory); |
| 74 | +let outputContent = ''; |
| 75 | + |
| 76 | +// 각 파일의 경로와 내용을 지정 형식으로 누적 |
| 77 | +files.forEach(file => { |
| 78 | + const fileContent = fs.readFileSync(file, 'utf8'); |
| 79 | + outputContent += `# ${file}\n\`\`\`\n${fileContent}\n\`\`\`\n\n`; |
| 80 | +}); |
| 81 | + |
| 82 | +// 누적된 내용을 output.txt 파일로 저장 |
| 83 | +fs.writeFileSync(outputFile, outputContent); |
| 84 | +console.log(`모든 파일의 내용이 ${outputFile}에 저장되었습니다.`); |
0 commit comments