Skip to content

Commit

Permalink
Merge pull request #72 from Cyfrin/dev
Browse files Browse the repository at this point in the history
dev --> main
  • Loading branch information
solhosty authored Jan 26, 2024
2 parents 55f839b + af25be4 commit 247c6c9
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/update-json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Update JSON on Markdown Change

on:
pull_request:
paths:
- '**/*.md'

jobs:
update-json:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'

- name: Run script to update JSON
run: node update_json.js

- name: Commit and push if changed
run: |
git config --global user.email "actions@github.com"
git config --global user.name "GitHub Actions"
git add -u
git commit -m "Updated JSON file via GitHub Actions" || exit 0 # This will not fail if no changes
git push
57 changes: 57 additions & 0 deletions update_json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");

const coursesPath = "courses";
const jsonFilesPath = "content/courses";

// Function to update the corresponding JSON file for a changed Markdown file
function updateJsonForMarkdown(mdFilePath) {
// Extract course slug from the Markdown file path
const courseSlug = path.basename(path.dirname(path.dirname(mdFilePath)));

// Construct the path to the corresponding JSON file
const jsonFilePath = path.join(jsonFilesPath, courseSlug + ".json");

// Check if the JSON file exists
if (!fs.existsSync(jsonFilePath)) {
console.log(`JSON file not found for course: ${courseSlug}`);
return;
}

// Read the Markdown file
const markdownContent = fs.readFileSync(mdFilePath, "utf8");

// Read and update the JSON file
const jsonContent = JSON.parse(fs.readFileSync(jsonFilePath, "utf8"));

// Iterate through sections and lessons to find the correct one to update
let updated = false;
jsonContent.sections.forEach((section) => {
section.lessons.forEach((lesson) => {
if (lesson.rawMarkdownUrl === mdFilePath.replace(coursesPath, "")) {
lesson.markdownContent = markdownContent;
updated = true;
}
});
});

if (!updated) {
console.log(`Lesson not found in JSON for Markdown file: ${mdFilePath}`);
return;
}

// Write updated JSON back to file
fs.writeFileSync(jsonFilePath, JSON.stringify(jsonContent, null, 2));
}

// Get a list of changed Markdown files
const changedFiles = execSync("git diff --name-only HEAD HEAD~1")
.toString()
.split("\n");
changedFiles.forEach((file) => {
if (file.startsWith(coursesPath) && path.extname(file) === ".md") {
console.log(`Updating JSON for changed file: ${file}`);
updateJsonForMarkdown(file);
}
});

0 comments on commit 247c6c9

Please sign in to comment.