Skip to content

Commit 648969a

Browse files
committed
Convert consolidate workflow script to node
1 parent 074d670 commit 648969a

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

.github/workflows/consolidate-snippets.yml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,18 @@ jobs:
1616
- name: Checkout repository
1717
uses: actions/checkout@v3
1818

19-
- name: Install jq (for JSON manipulation)
20-
run: sudo apt-get install -y jq
19+
- name: Set up Node.js
20+
uses: actions/setup-node@v3
21+
with:
22+
node-version: "16"
23+
24+
- name: Install dependencies
25+
run: |
26+
npm install
2127
2228
- name: Consolidate JSON files
2329
run: |
24-
mkdir -p public/consolidated
25-
echo "[" > public/consolidated/all_snippets.json
26-
find public/data -name "*.json" ! -name "_index.json" -exec sh -c '
27-
for file; do
28-
jq -c ".[]" "$file" | \
29-
sed "s/^/{\"language\": \"$(basename "$file" .json)\", /" | \
30-
sed "s/}$/}/" >> public/consolidated/all_snippets.json
31-
done
32-
' _ {} +
33-
sed -i '$ s/,$//' public/consolidated/all_snippets.json
34-
echo "]" >> public/consolidated/all_snippets.json
30+
node utils/consolidate.js # Run the script located in the utils/ folder
3531
3632
- name: Commit and push changes
3733
run: |

utils/consolidate.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { readdirSync, readFileSync, mkdirSync, writeFileSync } from "fs";
2+
import { basename, join } from "path";
3+
4+
const dataDir = "public/data";
5+
const outputFile = "public/consolidated/all_snippets.json";
6+
7+
const files = readdirSync(dataDir).filter(
8+
(f) => f.endsWith(".json") && f !== "_index.json"
9+
);
10+
11+
const consolidated = [];
12+
13+
files.forEach((file) => {
14+
const language = basename(file, ".json");
15+
const content = JSON.parse(readFileSync(join(dataDir, file)));
16+
17+
content.forEach((category) => {
18+
consolidated.push({
19+
language,
20+
categoryName: category.categoryName,
21+
snippets: category.snippets,
22+
});
23+
});
24+
});
25+
26+
mkdirSync("public/consolidated", { recursive: true });
27+
writeFileSync(outputFile, JSON.stringify(consolidated, null, 2));

0 commit comments

Comments
 (0)