Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev --> main #72

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
});