-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.coffee
68 lines (53 loc) · 1.86 KB
/
build.coffee
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
recursive = require 'recursive-readdir'
fs = require 'fs'
marked = require 'marked'
_ = require "underscore"
hcl2json = require "hcl-to-json"
child_process = require('child_process')
json2yml = require('json2yaml')
fuzzy = require "fuzzy"
replaceHclToYaml = (title,str)->
# check for ...'s
str = str.split("...").join("# ...")
a = str.split("```")
a.forEach (v,k) ->
if k%2 is 1
try
a[k] = json2yml.stringify hcl2json v
catch err
console.log title,v
return a.join("```")
recreateDocs = (callback) ->
fs.stat "/terraform",(err,res)->
if err
child_process.execSync "git clone https://github.com/hashicorp/terraform/ --depth 1; rm -rf ./terraform/.git"
recursive 'terraform/website/source/docs', (err, files)->
content =[]
files.forEach (path,k)->
data = fs.readFileSync path,'utf-8'
if path.indexOf(".md") > 0 or path.indexOf(".markdown") > 0
title = data.substring(data.indexOf('page_title:')+13,data.indexOf('sidebar_current:')-2)
content.push {value:title,path:path,data:replaceHclToYaml(title,data),marked:marked(data)}
fs.writeFileSync "./website/docs.json",JSON.stringify(content,null,"\t")
fs.writeFileSync "./docs.json",JSON.stringify(content,null,"\t")
# child_process.execSync "rm -rf ./terraform;"
callback null
docs = JSON.parse fs.readFileSync("./website/docs.json")
searchDocs = (query)->
results = []
docs.forEach (content)->
if content.data.indexOf(query) > 0
results.push [content.path]
return results
search = (query)->
options =
pre: '<'
post: '>'
extract: (el) -> return el.value
results = fuzzy.filter(query, docs, options);
matches = results.map((el)-> el.string)
return matches
#console.log args
# recreateDocs ->
# console.log "done."
module.exports = {recreateDocs,searchDocs,search}