Created
November 12, 2024 03:38
-
-
Save suarezafelipe/def0c73fabb829abda800a5699605b71 to your computer and use it in GitHub Desktop.
folder2txt as a GItHub Action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Concatenate Files | |
on: | |
push: | |
branches: [ master ] | |
paths: | |
- 'src/**' | |
- '.github/workflows/**' | |
pull_request: | |
types: [ closed ] | |
branches: [ master ] | |
paths: | |
- 'src/**' | |
- '.github/workflows/**' | |
jobs: | |
concatenate: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '20' | |
- name: Create concatenated file | |
run: | | |
node -e ' | |
const fs = require("fs"); | |
const path = require("path"); | |
const ignorePatterns = { | |
folders: ["node_modules", ".git", "dist", "bin", "obj", ".angular", ".idea", "assets"], | |
files: [".DS_Store", "*.dll", "*.exe", "*.pdb", "appsettings*.json", "*.user", "*.Designer.cs", ".gitignore", "package-lock.json", ".env.*", "environment.ts", "environment.*.ts",] | |
}; | |
function matchWildcard(str, pattern) { | |
const regexPattern = "^" + pattern.split("*").map(p => p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*") + "$"; | |
return new RegExp(regexPattern).test(str); | |
} | |
function shouldIgnore(filePath, isFolder = false) { | |
const name = path.basename(filePath); | |
const patterns = isFolder ? ignorePatterns.folders : ignorePatterns.files; | |
return patterns.some(p => p === name || matchWildcard(name, p)); | |
} | |
function processFolder(folderPath, output) { | |
const files = fs.readdirSync(folderPath); | |
for (const file of files) { | |
const filePath = path.join(folderPath, file); | |
const stats = fs.statSync(filePath); | |
if (stats.isFile() && !shouldIgnore(filePath)) { | |
console.log(`Processing ${filePath}`); | |
output.write(`\n${filePath}:\n`); | |
output.write(fs.readFileSync(filePath)); | |
} else if (stats.isDirectory() && !shouldIgnore(filePath, true)) { | |
processFolder(filePath, output); | |
} | |
} | |
} | |
const output = fs.createWriteStream("output.txt"); | |
output.write("Repository Files:\n"); | |
processFolder(".", output); | |
output.end(); | |
' | |
- name: Commit and push if changed | |
run: | | |
git config --global user.name 'GitHub Action' | |
git config --global user.email '[email protected]' | |
git add output.txt | |
git diff --quiet && git diff --staged --quiet || (git commit -m "Update concatenated files [skip ci]" && git push) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment