Created
March 5, 2024 22:16
-
-
Save agritheory/b99a5dafd9c2bce111ce1bd4702441cd to your computer and use it in GitHub Desktop.
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
import { existsSync, readFile, writeFile } from "fs"; | |
import { remark } from 'remark'; | |
import RemarkLinkRewrite from './rewrite.js'; | |
import shelljs from "shelljs" | |
const { exec } = shelljs; | |
if (!existsSync("./content/")) { | |
exec("mkdir -p ./content"); | |
} | |
// pull the latest check run code | |
if (!existsSync("./doc_repos/check_run")) { | |
console.log("Check Run directory not found. Cloning repo..."); | |
exec("git clone https://github.com/agritheory/${repo_name}.git ./doc_repos/check_run"); | |
} else { | |
console.log("Check Run directory found. Pulling latest..."); | |
exec("cd ./doc_repos/check_run && git pull"); | |
} | |
// create check run assets folder if it doesn't exist | |
if (!existsSync("./public/documentation/check_run/assets/")) { | |
exec("mkdir -p ./public/documentation/check_run/assets/"); | |
} | |
// copy contents of every asset folder in check run docs to the public check run assets folder | |
exec(`find ./doc_repos/check_run/docs -name 'assets' | while read line; do | |
echo "Processing file '$line'" | |
cp -a "$line"/. ./public/documentation/check_run/assets/ | |
done`); | |
// copy checkrun docs into content folder | |
exec("cp -a ./doc_repos/check_run/docs/. ./content/check_run/"); | |
update_urls("./content/check_run/", "check_run") | |
// ################################################################# | |
// ########################### CLEANUP ############################# | |
// ################################################################# | |
// remove repos | |
exec("rm -rf ./doc_repos/check_run") | |
exec("rm -rf ./doc_repos/cloud_storage") | |
exec("rm -rf ./doc_repos/beam") | |
exec("rm -rf ./doc_repos/inventory_tools") | |
// exec("rm -rf ./doc_repos/electronic_payments") | |
exec("rm -rf ./doc_repos") | |
function update_urls(folder, repo){ | |
console.log('Update URLs', folder, repo) | |
shelljs.ls(`${folder}*.md`).forEach(file => { | |
readFile(file, (err, fileContent)=>{ | |
remark() | |
.use(RemarkLinkRewrite, { | |
replacer: (url) => { | |
console.log(url) | |
if (url.startsWith('./assets')) { | |
return url.replace('./assets', `/documentation/${repo}/assets`) | |
} | |
if (!url.startsWith('./assets') && !url.startsWith('./index') && url.startsWith('./')) { | |
if(url.endsWith('.md')){ | |
url = url.replace('.md', '') | |
} | |
return url.replace('./', `/documentation/${repo}/`) | |
} | |
return url | |
} | |
}) | |
.process(fileContent) | |
.then(({ value }) => { | |
writeFile(file, value, (err) => { if (err) throw err }) | |
}) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment