-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.js
130 lines (110 loc) · 3.55 KB
/
plugin.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
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const slugify = require('slugify')
const copyDir = require('copy-dir')
const untildify = require('untildify')
const PACKAGE_DIST_PATH = path.resolve(__dirname, 'documenter')
module.exports.workspaceActions = [
{
icon: 'fa-cloud-upload',
label: 'Export HTML Documentation...',
action: async (context, { workspace }) => {
const rootPathConfigKey = `root_path_${workspace._id}`
const outputPathConfigKey = `output_path_${workspace._id}`
try {
let outputPath = await context.app.prompt(
'Output Path (e.g ~/Documents/My-Docs)',
{
submitName: 'Continue',
defaultValue: await context.store.getItem(outputPathConfigKey)
}
)
let rootPath = await context.app.prompt(
'Documentation Root Path (No trailing slash e.g /docs)',
{
submitName: 'Export',
defaultValue: await context.store.getItem(rootPathConfigKey)
}
)
if (!outputPath.length) {
outputPath = path.join(
context.app.getPath('desktop'),
`${slugify(workspace.name)}-API-Docs`
)
} else {
outputPath = untildify(outputPath)
}
if (rootPath === '/') {
rootPath = ''
}
await context.store.setItem(rootPathConfigKey, rootPath)
await context.store.setItem(outputPathConfigKey, outputPath)
try {
await mkdirp(outputPath)
} catch (e) {
await context.app.alert(
'Something went wrong!',
'Unable to create output folder.'
)
return
}
try {
copyDir.sync(PACKAGE_DIST_PATH, outputPath, {
utimes: false,
mode: false,
cover: true
})
const indexFilePath = path.join(outputPath, 'index.html')
fs.writeFileSync(
indexFilePath,
fs
.readFileSync(indexFilePath)
.toString()
.replace('src="bundle.js"', `src="${rootPath}/bundle.js"`)
.replace('href="bundle.css"', `href="${rootPath}/bundle.css"`)
.replace('href="favicon.ico"', `href="${rootPath}/favicon.ico"`)
.replace(
'id="app"',
`id="app"${
rootPath.length ? ' data-root="' + rootPath + '"' : ''
}`
)
)
const jsFilePath = path.join(outputPath, 'bundle.js')
fs.writeFileSync(
jsFilePath,
fs
.readFileSync(jsFilePath)
.toString()
.replace('logo.png', `${rootPath}/logo.png`)
)
} catch (_) {
await context.app.alert(
'Something went wrong!',
'Unable to copy documenter files to output path.'
)
return
}
try {
const data = await context.data.export.insomnia({
workspace,
format: 'json',
includePrivate: false
})
fs.writeFileSync(path.join(outputPath, 'insomnia.json'), data)
} catch (_) {
await context.app.alert(
'Something went wrong!',
'Unable to export workspace data.'
)
return
}
await context.app.alert(
'All Done!',
`Your documentation has been created at "${outputPath}" and it's ready to be deployed!`
)
} catch (_) {}
}
}
]