-
Notifications
You must be signed in to change notification settings - Fork 10
/
verbfile.js
129 lines (110 loc) · 3.24 KB
/
verbfile.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
'use strict'
const path = require('path')
const pify = require('pify')
const cson = require('cson-parser')
const each = require('each-promise')
const fs = pify(require('fs'))
const camelcase = require('camelcase')
const vscodeFile = './vscode/snippets.json'
const SNIPPETS_DIR = './atom'
const TOP_LEVEL_KEY = '.source.js, .source.jsx, .source.ts'
module.exports = function (verb) {
verb.use(require('verb-generate-readme'))
verb.asyncHelper('generateSnippetDocs', generateSnippetDocs)
/**
* Generate VSCode snippets
* from Atom CSON snippets definition
*/
verb.task('vscode', () => {
return generateVSCodeSnippets()
.then((data) => {
return fs.writeFile(vscodeFile, JSON.stringify(data, null, 2))
})
.then(() => {
console.log(`VSCode snippets generated in ${vscodeFile}`)
console.log(`from Atom CSON snippets definitions.`)
console.log('done')
})
})
verb.task('default', ['readme', 'vscode'])
}
const generateSnippetDocs = (cb) => {
parseFiles(SNIPPETS_DIR)
.then((files) => {
let buf = `\n\n`
files.forEach((file) => {
const category = path.basename(file.path, path.extname(file.path))
const ghpath = `./${SNIPPETS_DIR}/${path.basename(file.path)}`
buf = `${buf}### ${category}\n`
buf = `${buf}> All [${category}](${ghpath}) snippets\n\n`
for (let desc in file.contents) {
const data = file.contents[desc]
buf = `${buf}#### \`${data.prefix}⇥\` ${desc}`
if (data.prefix === 'xa') {
buf = `${buf}\n\n> See [extend-shallow][] lib`
}
let fence = '\n\n'
fence += '```js\n'
fence += data.body.trim() + '\n'
fence += '```'
buf = buf + fence + '\n\n'
}
buf = `${buf}**[back to top](#readme)**\n\n`
})
cb(null, buf)
}, cb)
.catch(cb)
}
const generateVSCodeSnippets = () =>
new Promise((resolve, reject) => {
const snippets = {}
parseFiles(SNIPPETS_DIR).then((files) => {
files.forEach((file) => {
for (let desc in file.contents) {
const name = camelcase(desc).replace(/\(|\)/g, '')
const snippet = file.contents[desc]
snippet.description = desc
snippets[name] = snippet
}
})
resolve(snippets)
})
})
const parseFiles = (dir) => {
return readFiles(dir).then((files) =>
files.map((file) => {
let data = cson.parse(file.contents)[TOP_LEVEL_KEY]
data = data || cson.parse(file.contents)['.source.json']
return {
path: file.path,
contents: data
}
})
)
}
const readFiles = (dir) =>
readdir(dir).then((fps) => {
if (!fps.length) return fps
return each.serial(
fps.map((filepath) => () => {
return fs.readFile(filepath, 'utf8').then((contents) => ({
path: filepath,
contents: contents
}))
})
)
})
const readdir = (dir) =>
new Promise((resolve, reject) => {
const files = []
fs
.readdir(dir)
.then((filepaths) => {
return each.serial(
filepaths.map((fp) => () => {
files.push(path.resolve(dir, fp))
})
)
})
.then(() => resolve(files), reject)
})