-
Notifications
You must be signed in to change notification settings - Fork 3
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 #12 from MLH-Fellowship/dev
Prototype Script
- Loading branch information
Showing
7 changed files
with
172 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -102,3 +102,9 @@ dist | |
|
||
# TernJS port file | ||
.tern-port | ||
|
||
# Misc. | ||
.DS_Store | ||
|
||
# Docs output | ||
docs |
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 |
---|---|---|
@@ -1,9 +1,21 @@ | ||
const express = require("express"); | ||
|
||
const util = require("./util"); | ||
const app = express(); | ||
const port = 3000; | ||
|
||
app.get("/", (req, res) => res.send("Hello World!")); | ||
|
||
app.get("/:user/:repo/compile", (req, res) => { | ||
const user = req.params.user; | ||
const repo = req.params.repo; | ||
|
||
util.getReadme(user, repo).then(() => { | ||
util.serveDocs(app, user, repo); | ||
res.send(`Successfully Compiled Docs! View them at /${user}/${repo}`); | ||
}); | ||
}); | ||
|
||
app.listen(port, () => | ||
console.log(`Example app listening at http://localhost:${port}`) | ||
); |
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,5 @@ | ||
function generateHTML(user, repo) { | ||
return `<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><title>${repo}</title><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/><meta name="description" content="Description"/> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0"/> <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css"/> </head> <body> <div id="app"></div><script>window.$docsify={name: '${repo}', repo: '${user}/${repo}'}; </script> <script src="//cdn.jsdelivr.net/npm/docsify@4"></script> </body></html>`; | ||
} | ||
|
||
module.exports = { generateHTML }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -27,6 +27,8 @@ | |
} | ||
}, | ||
"dependencies": { | ||
"express": "^4.17.1" | ||
"axios": "^0.19.2", | ||
"express": "^4.17.1", | ||
"hasha": "^5.2.0" | ||
} | ||
} |
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 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Document</title> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> | ||
<meta name="description" content="Description" /> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, initial-scale=1.0, minimum-scale=1.0" | ||
/> | ||
<link | ||
rel="stylesheet" | ||
href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css" | ||
/> | ||
</head> | ||
|
||
<body> | ||
<div id="app"></div> | ||
<script> | ||
window.$docsify = { | ||
name: "", | ||
repo: "", | ||
}; | ||
</script> | ||
<!-- Docsify v4 --> | ||
<script src="//cdn.jsdelivr.net/npm/docsify@4"></script> | ||
</body> | ||
</html> |
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,70 @@ | ||
const express = require("express"); | ||
const axios = require("axios"); | ||
const fs = require("fs"); | ||
const hasha = require("hasha"); | ||
|
||
const docsify = require("./docsify"); | ||
|
||
// Compares local and remote readme for changes | ||
function isDiffReadme(user, repo, readmeData) { | ||
if (fs.existsSync(`./docs/${user}-${repo}/README.md`)) { | ||
const localReadmeHash = hasha.fromFileSync( | ||
`./docs/${user}-${repo}/README.md`, | ||
{ algorithm: "md5" } | ||
); | ||
const remoteReadmeHash = hasha(readmeData, { algorithm: "md5" }); | ||
|
||
if (localReadmeHash == remoteReadmeHash) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
// Writes all the required files | ||
function writeDocs(user, repo, readmeData) { | ||
const htmlData = docsify.generateHTML(user, repo); | ||
|
||
fs.writeFileSync(`./docs/${user}-${repo}/README.md`, readmeData, function ( | ||
err | ||
) { | ||
if (err) throw err; | ||
console.log("Generated ReadMe!"); | ||
}); | ||
|
||
fs.writeFileSync(`./docs/${user}-${repo}/index.html`, htmlData, function ( | ||
err | ||
) { | ||
if (err) throw err; | ||
console.log("Generated HTML!"); | ||
}); | ||
} | ||
|
||
// Gets readme contents and generates /docs/<files> with docsify enabled to render | ||
async function getReadme(user, repo) { | ||
const readme = await axios.get( | ||
`https://api.github.com/repos/${user}/${repo}/readme` | ||
); | ||
const readmeUrl = readme.data.download_url; | ||
const readmeData = await axios.get(readmeUrl); | ||
|
||
if (!fs.existsSync("./docs/")) { | ||
fs.mkdirSync("./docs/"); | ||
} | ||
|
||
if (!fs.existsSync(`./docs/${user}-${repo}/`)) { | ||
fs.mkdirSync(`./docs/${user}-${repo}/`); | ||
} | ||
|
||
// Write file only if github readme is different from local copy | ||
if (isDiffReadme(user, repo, readmeData.data)) { | ||
writeDocs(user, repo, readmeData.data); | ||
} | ||
} | ||
|
||
// Serves the compiled docs | ||
function serveDocs(app, user, repo) { | ||
app.use(`/${user}/${repo}`, express.static(`./docs/${user}-${repo}`)); | ||
} | ||
|
||
module.exports = { getReadme, serveDocs }; |