-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from Cyfrin/dev
dev --> main
- Loading branch information
Showing
2 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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); | ||
} | ||
}); |