-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
135 lines (109 loc) · 3.49 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
shell.config.fatal = true
// User defined input
var branch = core.getInput("branch")
var history = core.getInput("history")
const jazzyVersion = core.getInput("version")
const configFilePath = core.getInput("config")
const jazzyArgs = core.getInput("args")
const token = core.getInput("personal_access_token")
if (branch == '' || branch == null || branch == undefined) {
branch = "gh-pages"
}
if (history == '' || history == null || history == undefined) {
history = true
}
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 = () => {
let command = `jazzy`
if (jazzyArgs) {
command += ` ${jazzyArgs}`
}
if (configFilePath) {
command += ` --config ${configFilePath}`
}
return command
}
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 (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")
}
}
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
}
}
return "docs"
}
const generateAndDeploy = () => {
shell.exec(generateJazzyInstallCommand())
shell.exec(generateJazzyArguments())
var folder = getDocumentationFolder()
if (folder.charAt(folder.length - 1) != '/') {
folder += '/'
}
shell.exec("mkdir -p ../.staging/" + folder)
// we don't want it to nest on move
shell.exec("rm -r ../.staging/" + folder)
shell.mv(folder, "../.staging/" + folder)
shell.exec("mkdir ../.docs")
shell.cd("../.docs")
if (history) {
shell.exec(`git clone ${remote} .`)
shell.exec(`git checkout ${branch}`)
} else {
shell.exec("git init")
shell.exec(`git checkout -b ${branch}`)
}
shell.exec("mkdir -p " + folder, {fatal: false})
// we don't want it to nest on move
shell.exec("rm -rf " + folder)
shell.mv("../.staging/" + folder, folder)
shell.exec("rm " + folder + "undocumented.json")
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} ${branch}`)
shell.cd(process.env.GITHUB_WORKSPACE)
}
try {
generateAndDeploy()
} catch (error) {
core.setFailed(error.message)
}