-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
190 lines (147 loc) · 6.62 KB
/
index.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
187
188
189
190
const path = require('path')
const fs = require('fs')
const MetadocPlugin = require('@author.io/metadoc-plugin')
class ApiGenerator extends MetadocPlugin {
constructor () {
super(...arguments)
this.NAME = require(path.join(__dirname, 'package.json')).name
this.APIROOT = this.getCLIArg('--root') || '/'
if (this.APIROOT.trim().substr(this.APIROOT.trim().length - 1, 1) !== '/') {
this.APIROOT = `${this.APIROOT}/`
}
let version = this.getCLIArg('--version') || null
if (version) {
if (version.toLowerCase() === 'auto') {
try {
version = JSON.parse(fs.readFileSync(path.join(this.source, 'package.json')).toString()).version
} catch (e) {
try {
version = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json')).toString()).version
} catch (ee) {
throw new Error(`Could not find package.json in "${this.source}" or "${process.cwd()}". Autodetection failed.`)
process.exit(1)
}
}
}
this.APIROOT = `${this.APIROOT}${version}/`
this.output = path.join(this.output, version)
}
}
get root () {
return this.APIROOT
}
set root (value) {
this.APIROOT = value
}
process () {
this.verifyOutputDirectory()
// Generate class data (in namespaces)
Object.keys(this.SOURCE.namespaces).forEach(namespace => this.processNamespace(namespace, this.SOURCE.namespaces[namespace]))
// Generate the primary manifest
let meta = Object.assign({}, this.SOURCE)
meta.namespaces = Object.keys(meta.namespaces).map(ns => {
return `${this.APIROOT}/${ns}/index.json`.replace(/\/+/gi, '/').replace(':/', '://')
})
fs.writeFileSync(path.join(this.mkdirp(this.output), 'api.json'), JSON.stringify(this.data, null, 2))
fs.writeFileSync(path.join(this.mkdirp(this.output), 'api.classes.json'), JSON.stringify(this.classList, null, 2))
fs.writeFileSync(path.join(this.mkdirp(this.output), 'api.exceptions.json'), JSON.stringify(this.data.exceptions, null, 2))
fs.writeFileSync(path.join(this.mkdirp(this.output), 'api.bus.json'), JSON.stringify(this.data.bus, null, 2))
fs.writeFileSync(path.join(this.mkdirp(this.output), 'api.types.json'), JSON.stringify(this.data.types, null, 2))
fs.writeFileSync(path.join(this.mkdirp(this.output), 'api.namespaces.json'), JSON.stringify(this.manifest, null, 2))
let globals = this.manifest
if (this.manifest.hasOwnProperty('NGN')) {
globals.Events = { href: `${this.APIROOT}api.bus.json` }
globals.Exceptions = { href: `${this.APIROOT}api.exceptions.json` }
}
this.manifest.global.classes.forEach(cls => globals[cls.name] = this.classList[cls.name])
delete globals.global
console.log(globals)
fs.writeFileSync(path.join(this.mkdirp(this.output), 'index.json'), JSON.stringify({
globals,
full_spec_href: `${this.APIROOT}api.json`,
class_href: `${this.APIROOT}api.classes.json`,
// namespace_href: `${this.APIROOT}api.namespaces.json`,
type_href: `${this.APIROOT}api.types.json`
}, null, 2))
this.emit('complete')
}
processNamespace (namespace, data, root = '/') {
let dir = this.mkdirp(path.join(this.output, root, namespace))
// Write the manifest for the namespace
let meta = Object.assign({}, data)
meta.classes = (Array.isArray(meta.classes) ? meta.classes : Object.keys(meta.classes || {})).map(ClassName => {
return {
href: `${this.APIROOT}/${ClassName.replace(/\./gi, '/')}${ClassName.split('.').pop().toLowerCase() === 'index' ? '_class' : ''}.json`.replace(/\/+/gi, '/'),
name: ClassName
}
})
meta.namespaces = Object.keys(meta.namespaces || {})
meta.namespaces.forEach(ns => {
meta.namespaces[ns] = `${this.APIROOT}/${root}/${ns}/index.json`.replace(/\/+/gi, '/')
})
if (meta.sourcefile) {
meta.sourcefile = meta.sourcefile.replace(/\/+|\\+|\\+\/+|\/+\\+/gi, '/').replace(/\/+/gi, '/').replace('./', '')
}
fs.writeFileSync(path.join(dir, 'index.json'), JSON.stringify(meta, null, 2))
// Generate the class data
let Classes = new Set(data.classes)
Classes.forEach(ClassName => {
if (this.SOURCE.classes.hasOwnProperty(ClassName)) {
let classdata = this.SOURCE.classes[ClassName]
let name = ClassName.split('.').pop()
name = name.toLowerCase() === 'index' ? `${name}_class` : name
if (classdata.sourcefile) {
classdata.sourcefile = classdata.sourcefile.replace(/\/+|\\+|\\+\/+|\/+\\+/gi, '/').replace(/\/+/gi, '/').replace('./', '')
}
console.log(`Writing static metadoc API file: ${path.join(dir, name + '.json').replace(process.cwd(), '.').replace(/\\/gi, '/')}`)
fs.writeFileSync(path.join(dir, `${name}.json`), JSON.stringify(classdata, null, 2))
} else {
console.error(`Missing class: ${ClassName}`)
}
})
meta.namespaces.forEach(subNamespace => this.processNamespace(subNamespace, data.namespaces[subNamespace], namespace.replace(/\/+/gi, '/')))
}
get classList () {
let data = {}
Object.keys(this.data.classes).forEach(cls => {
data[cls] = {
href: `${this.APIROOT}${cls.split('.').length === 1 ? 'global/' : ''}${cls.replace(/\./gi, '/')}.json`,
description: this.data.classes[cls].description
}
})
return data
}
// Generates a manifest/tree of all classes and namespaces based on the output of the process
get manifest () {
let data = this.data//JSON.parse(fs.readFileSync(path.join(this.output, 'api.json')))
let namespaces = Object.assign({}, data.namespaces)
let output = {}
Object.keys(namespaces).forEach(namespace => {
output = Object.assign(output, this.generateManifestNamespace(namespaces[namespace]))
})
return output
}
generateManifestNamespace (namespace, namespacePath = []) {
let data = {}
if (namespace) {
namespacePath.push(namespace.label)
data[namespace.label] = {
href: `${this.APIROOT}${namespacePath.join('/')}/index.json`,
description: namespace.description,
classes: namespace.classes.map(cls => {
return {
name: cls,
href: `${this.APIROOT}${namespacePath.join('/')}/${cls.replace(namespacePath.join('.') + '.', '')}.json`
}
}),
namespaces: {}
}
Object.keys(namespace.namespaces).forEach(ns => {
let subspace = this.generateManifestNamespace(namespace.namespaces[ns], namespacePath.slice(0))
data[namespace.label].namespaces = Object.assign(data[namespace.label].namespaces, subspace)
})
}
return data
}
}
module.exports = ApiGenerator