forked from Steven0351/publish-jazzy-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
106 lines (82 loc) · 2.65 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const core = require("@actions/core")
const github = require("@actions/github")
const shell = require("shelljs")
const yaml = require("js-yaml")
const fs = require("fs")
const context = github.context
const branch = "gh-pages"
// User defined input
const jazzyVersion = core.getInput("version")
const configFilePath = core.getInput("config")
const jazzyArgs = core.getInput("args")
const token = core.getInput("personal_access_token")
const remote = `https://${token}@github.com/${context.repo.owner}/${context.repo.repo}.git`
const generateJazzyInstallCommand = () => {
let gemInstall = "sudo gem install jazzy"
if (jazzyVersion) {
gemInstall += ` -v ${jazzyVersion}`
}
return gemInstall
}
const generateJazzyArguments = () => {
if (configFilePath) {
return `jazzy --config ${configFilePath}`
}
if (jazzyArgs) {
return `jazzy ${jazzyArgs}`
}
return "jazzy"
}
const sliceDocumentsFromJazzyArgs = (outputArg) => {
const startIndexOfDocsDir = jazzyArgs.indexOf(outputArg) + outputArg.length + 1
const endIndexOfDocsDir = jazzyArgs.indexOf(" ", startIndexOfDocsDir)
if (endIndexOfDocsDir != -1) {
return jazzyArgs.slice(startIndexOfDocsDir, endIndexOfDocsDir)
} else {
return jazzyArgs.slice(startIndexOfDocsDir)
}
}
const getDocumentationFolder = () => {
if (configFilePath) {
let config
const fileExt = configFilePath.split(".").pop().toLowerCase()
if (fileExt === "yml" || fileExt === "yaml") {
config = yaml.safeLoad(fs.readFileSync(configFilePath, "utf8"))
} else if (fileExt === "json") {
const rawData = fs.readFileSync(configFilePath)
config = JSON.parse(rawData)
}
if (config.output) {
return config.output
}
}
if (jazzyArgs) {
// --output needs to be checked first, because --output includes -o
if (jazzyArgs.includes("--output")) {
return sliceDocumentsFromJazzyArgs("--output")
}
if (jazzyArgs.includes("-o")) {
return sliceDocumentsFromJazzyArgs("-o")
}
}
return "docs"
}
const generateAndDeploy = () => {
shell.exec(generateJazzyInstallCommand())
shell.exec(generateJazzyArguments())
shell.exec("mkdir ../.docs")
shell.cp("-r", `${getDocumentationFolder()}/*`, "../.docs/")
shell.cd("../.docs")
shell.exec("git init")
shell.exec(`git config user.name ${context.actor}`)
shell.exec(`git config user.email ${context.actor}@users.noreply.github.com`)
shell.exec("git add .")
shell.exec("git commit -m 'Deploying Updated Jazzy Docs'")
shell.exec(`git push --force ${remote} master:${branch}`)
shell.cd(process.env.GITHUB_WORKSPACE)
}
try {
generateAndDeploy()
} catch (error) {
core.setFailed(error.message)
}