-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdoclets.js
executable file
·151 lines (137 loc) · 3.78 KB
/
doclets.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
#!/usr/bin/env node
/* vim: set syntax=javascript */
var prompt = require('prompt')
require('colors')
var path = require('path')
var fs = require('fs')
var yaml = require('js-yaml')
var repoName = require('git-repo-name')
prompt.message = 'doclets'.blue
var gather = require('../lib/gather')
var log = function (message) {
console.log('doclets: '.blue + message)
}
var logerr = function (message) {
console.error('doclets: '.blue + message.red)
}
var getDefaultConfig = function () {
try {
console.log('doclets: Reading package.json ok'.blue)
var config = JSON.parse(fs.readFileSync('package.json'))
config.repository = config.repository || {}
if (config.main && fs.lstatSync(config.main).isFile()) {
config.main = path.dirname(config.main)
}
return config
} catch (err) {
log('Could not read package.json'.yellow)
return {}
}
}
var config = function () {
var writeConfigYaml = function (inputs) {
var config = {}
config.dir = inputs.dir
config.flavor = 'jsdoc'
if (inputs.readme && inputs.readme !== '') {
config.articles = []
var articleEntry = {}
articleEntry[inputs.readme] = 'README.md'
config.articles.push(articleEntry)
}
fs.writeFileSync('.doclets.yml', yaml.dump(config))
log('.doclets.yml created'.green)
log('commit and push to generate initial documentation'.green)
}
var configDefaults = getDefaultConfig()
var configSchema = {
properties: {
dir: {
description: 'Root folder of source code',
default: configDefaults.main || 'lib'
}
}
}
try {
fs.accessSync('README.md')
configSchema.properties.readme = {
description: 'Title of README.md article',
default: 'Readme',
required: true
}
} catch (err) {
console.log('no readme')
}
prompt.get(configSchema, function (err, result) {
if (err) {
console.error('Sorry, information not complete'.red)
process.exit(1)
} else {
writeConfigYaml(result)
}
})
}
var preview = function () {
try {
fs.readFileSync('.doclets.yml')
} catch (err) {
logerr('Could not read ./doclets.yml')
logerr('Please run:\n\t $ doclets config'.green)
return
}
var configDefaults = getDefaultConfig()
var repoOwner
if (configDefaults.repository && configDefaults.repository.url) {
var match = configDefaults.repository.url.match(/github\.com\/([^\/]+)\/.*/)
if (match) {
repoOwner = match[1]
}
}
var previewSchema = {
properties: {
repository: {
description: 'GitHub repository name',
required: true,
default: repoName.sync()
},
owner: {
description: 'GitHub repository owner',
required: true,
default: repoOwner
},
port: {
description: 'Preview webserver port',
required: true,
default: 8081
}
}
}
prompt.get(previewSchema, function (err, result) {
if (err) {
console.error('Sorry, information not complete'.red)
process.exit(1)
} else {
var db = require('../lib/db-fake')
process.env.NODE_ENV = 'test'
var server = require('../lib/server')
server.init(result.port, db)
var docData = gather.gatherDocletsAndMeta('./')
db.put(result.owner + '/' + result.repository, 'local', {data: docData}, function () {})
log('finished'.yellow)
log('starting webserver...'.yellow)
log('visit:')
log('http://localhost:' + result.port + '/' + result.owner + '/' + result.repository + '/local')
}
})
}
var commands = {
config: config,
preview: preview
}
var command = process.argv[2]
if (!command || Object.keys(commands).indexOf(command) === -1) {
console.log('usage: doclets config | preview'.yellow)
process.exit(1)
}
prompt.start()
commands[command]()