-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
build_docs.js
187 lines (157 loc) · 7.07 KB
/
build_docs.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const fs = require('fs')
const os = require('os');
const path = require('path');
const crypto = require('crypto');
const axios = require('axios').default;
const { execSync } = require('child_process')
const ghpages = require('gh-pages');
/**
* Working directory structure
* /tmp/<random_string>
* /<branch_name>
* /graphql-docs
*/
// Constant variables
const CURRENT_DIRECTORY = process.cwd()
const GITHUB_REPO = 'swiftwave-org/swiftwave'
const GRAPHQL_DOCUMENTATION_BRANCH = 'docs/graphql'
const CURRENT_BRANCH = execSync(`git branch | grep \\* | cut -d ' ' -f2`, { cwd: CURRENT_DIRECTORY, stdio: 'pipe' }).toString().trim()
// Index page for documentation
const INDEX_PAGE = ``
// Build docs for GraphQL
async function buildGraphQlDocs() {
const WORKING_DIRECTORY = path.join(CURRENT_DIRECTORY, ".build_docs_tmp")
// Create a working directory in the system's temp folder
fs.mkdirSync(WORKING_DIRECTORY, { recursive: true })
console.log(`Created temporary directory: ${WORKING_DIRECTORY}`)
// Repo URL
const REPO_URL = `https://codeload.github.com/${GITHUB_REPO}/zip/refs/heads/${GRAPHQL_DOCUMENTATION_BRANCH}`
console.log(`Repo URL: ${REPO_URL}`)
// Create the folder to move existing docs
const GRAPHQL_DOCUMENTATION_FOLDER = path.join(WORKING_DIRECTORY, 'graphql-docs')
fs.mkdirSync(GRAPHQL_DOCUMENTATION_FOLDER, { recursive: true })
console.log(`Created folder: ${GRAPHQL_DOCUMENTATION_FOLDER}`)
// Check if the branch exists in the repo by HEAD request
let isExistGraphQLDocsBranch = false
try {
const response = await axios.head(REPO_URL)
if (response.status === 200) {
isExistGraphQLDocsBranch = true
} else {
throw new Error('Branch does not exist')
}
}
catch (err) {
if (err.response.status === 404) {
isExistGraphQLDocsBranch = false
} else {
throw err
}
}
// If the branch exists, download the zip file
if (isExistGraphQLDocsBranch) {
const zipFilePath = path.join(WORKING_DIRECTORY, 'graphql-docs.zip')
const writer = fs.createWriteStream(zipFilePath)
const response = await axios({
url: REPO_URL,
method: 'GET',
responseType: 'stream'
})
response.data.pipe(writer)
await new Promise((resolve, reject) => {
writer.on('finish', resolve)
writer.on('error', reject)
})
console.log('Downloaded zip file')
// Extract the zip file in a tmp folder
const extract = require('extract-zip')
const extractPath = path.join(WORKING_DIRECTORY, 'downloaded-zip-content')
await extract(zipFilePath, { dir: extractPath })
console.log('Extracted zip file')
// Move extractPath/swiftwave-<branch_name>/ to WORKING_DIRECTORY/graphql-docs
const docs_branch_folder = GRAPHQL_DOCUMENTATION_BRANCH.split('/').join('-')
const extractedFolder = path.join(extractPath, `swiftwave-${docs_branch_folder}`)
fs.renameSync(extractedFolder, GRAPHQL_DOCUMENTATION_FOLDER)
console.log('Moved extracted folder to graphql-docs')
}
// Generate the GraphQL documentation
// Run `npx magidoc generate`
const magidocCommand = 'npx magidoc generate --file ' + generateMjs(CURRENT_BRANCH)
execSync(magidocCommand, { cwd: CURRENT_DIRECTORY, stdio: 'inherit' })
console.log('Generated GraphQL documentation')
// Check if CURRENT_DIRECTORY/graphql-docs exists
const CURRENT_DIRECTORY_GRAPHQL_DOCUMENTATION_FOLDER = path.join(CURRENT_DIRECTORY, 'graphql-docs')
const isExistCurrentDirectoryGraphqlDocs = fs.existsSync(CURRENT_DIRECTORY_GRAPHQL_DOCUMENTATION_FOLDER)
if (isExistCurrentDirectoryGraphqlDocs === false) {
console.log("Failed to generate GraphQL documentation")
return
}
// Delete if GRAPHQL_DOCUMENTATION_FOLDER/<branch_name> exists
const GRAPHQL_DOCUMENTATION_CURRENT_BRANCH_FOLDER = path.join(GRAPHQL_DOCUMENTATION_FOLDER, CURRENT_BRANCH)
const isExistGraphqlDocsBranchFolder = fs.existsSync(GRAPHQL_DOCUMENTATION_CURRENT_BRANCH_FOLDER)
if (isExistGraphqlDocsBranchFolder) {
fs.rmSync(GRAPHQL_DOCUMENTATION_CURRENT_BRANCH_FOLDER, { recursive: true })
console.log(`Deleted ${GRAPHQL_DOCUMENTATION_CURRENT_BRANCH_FOLDER}`)
}
// Create the folder GRAPHQL_DOCUMENTATION_CURRENT_BRANCH_FOLDER
fs.mkdirSync(GRAPHQL_DOCUMENTATION_CURRENT_BRANCH_FOLDER, { recursive: true })
// Move CURRENT_DIRECTORY/graphql-docs to WORKING_DIRECTORY/graphql-docs/
execSync(`mv ${CURRENT_DIRECTORY_GRAPHQL_DOCUMENTATION_FOLDER}/* ${GRAPHQL_DOCUMENTATION_CURRENT_BRANCH_FOLDER}`, { stdio: 'inherit' })
// Copy `graphql.docs.html` to `GRAPHQL_DOCUMENTATION_FOLDER/index.html`
execSync(`cp ./graphql.docs.html ${GRAPHQL_DOCUMENTATION_FOLDER}/index.html`, { stdio: 'inherit' })
// Publish the GraphQL documentation
await ghpages.publish(GRAPHQL_DOCUMENTATION_FOLDER, {
branch: GRAPHQL_DOCUMENTATION_BRANCH
})
console.log('Published GraphQL documentation')
// Delete the temporary directory [if it exists]
try {
fs.accessSync(WORKING_DIRECTORY, fs.constants.F_OK)
fs.rmSync(WORKING_DIRECTORY, { recursive: true })
console.log("Temporary folder deleted")
}
catch (err) {
console.log("Temporary folder failed to delete > ", WORKING_DIRECTORY)
}
}
// Function to generate mjs for specific branch
function generateMjs(branchName) {
const mjsContent = `
export default {
introspection: {
type: 'sdl',
paths: ['swiftwave_service/graphql/schema/**/*.graphqls'],
},
website: {
template: 'carbon-multi-page',
output: './graphql-docs',
options: {
siteRoot: '/${branchName}',
appTitle: 'Swiftwave GraphQL Documentation',
appLogo: 'https://github.com/swiftwave-org.png',
siteMeta: {
description: "Documentation for Swiftwave's GraphQL API.",
'og:description': "Documentation for Swiftwave's GraphQL API.",
},
pages: [
{
title: 'Welcome',
content: \`# 👋 Hi
Welcome to the documentation of the Swiftwave GraphQL API.
## Don't know about Swiftwave?
SwiftWave is a self-hosted lightweight PaaS solution to deploy and manage your applications on any VPS without any hassle 👀
## Want to support us?
Star ⭐ our [GitHub repository](https://github.com/swiftwave-org/swiftwave) and join our [Discord community](https://community.swiftwave.org) to get help and support from our team.\`,
}
],
}
}
}`
const tmpFolder = path.join(os.tmpdir(), crypto.randomBytes(20).toString('hex'))
fs.mkdirSync(tmpFolder, { recursive: true })
const mjsFilePath = path.join(tmpFolder, 'magidoc.mjs')
fs.writeFileSync(mjsFilePath, mjsContent)
return mjsFilePath
}
buildGraphQlDocs()
.then(() => console.log('Done!'))