-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·95 lines (87 loc) · 2.79 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
#!/usr/bin/env node
const program = require('commander')
const fs = require('fs')
const path = require('path')
const recursive = require("recursive-readdir")
const mdConverter = require('widdershins')
const yaml = require('js-yaml')
const shins = require('shins');
program
.arguments('<surceswaggerfile> <targethtmlpath>')
.option('-d, --debug <debuglevel>', 'The debug level')
.option('-i, --include <fileList>', 'List of files to include, comma separated')
.action(function(source, target, include, debug) {
sourceFile = path.resolve(source)
targetPath = path.resolve(target)
includeFile = '/Users/javier/shinner/examples/api_2.yaml'
})
.parse(process.argv)
function readSourceFile(file) {
console.log('[INFO] Reading : %s', file)
var result = {}
var s = fs.readFileSync(path.resolve(file),'utf8')
try {
result = {}
result.sourceFile = file
result.sourceContent = yaml.safeLoad(s,{json:true})
return result
}
catch(ex) {
console.error('[WARNING] Failed to parse YAML/JSON : %s', file)
console.error(ex.message)
return ''
}
}
function convertToMarkDown(input) {
console.log('[INFO] Converting : %s', input.sourceFile)
var options = {}
options.codeSamples = true
options.sample = true
console.error('[INFO] includeFile: %s', includeFile[0])
if (includeFile) options.includes = includeFile[0]
mdConverter.convert(input.sourceContent,options,function(err,output){
if (err) {
console.error('[WARNING] Error convertToMarkDown: %s', err)
input.markdown = ''
}
console.log('[INFO] Converted to MD: %s', input.sourceFile)
input.markdown = output
convertToHtml(input)
})
return input
}
function writeTargetFile(input) {
input.targetFile = targetPath + input.sourceFile.replace(sourceFile, '').replace('.yaml','.html')
console.log('[INFO] Writing : %s', input.targetFile)
try {
fs.writeFileSync(path.resolve(input.targetFile),input.html,'utf8')
} catch (err) {
console.error('[WARNING] Error writeTargetFile: %s', err)
} finally {
console.log('[INFO] Finish : %s', input.targetFile)
}
}
function convertToHtml(input) {
var options = {}
options.minify = false;
options.customCss = false;
options.inline = false;
console.log('[INFO] convertToHtml : %s', input.sourceFile)
shins.render(input.markdown, options, (err, html) => {
if (err) {
console.error('[WARNING] Error convertToHtml: %s', err)
return;
}
input.html = html
writeTargetFile(input)
});
}
convertToMarkDown( readSourceFile(sourceFile) )
// recursive(sourceFile, [(f,s) => !s.isDirectory() && path.extname(f) != ".yaml"])
// .then(
// files => files
// .map(readSourceFile)
// .map(convertToMarkDown)
// ,
// error => console.error("something exploded", error)
// )