-
Notifications
You must be signed in to change notification settings - Fork 453
/
vue-beautifier.coffee
59 lines (55 loc) · 1.92 KB
/
vue-beautifier.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
"use strict"
Beautifier = require('./beautifier')
module.exports = class VueBeautifier extends Beautifier
name: "Vue Beautifier"
link: "https://github.com/Glavin001/atom-beautify/blob/master/src/beautifiers/vue-beautifier.coffee"
options:
Vue: true
beautify: (text, language, options) ->
return new @Promise((resolve, reject) =>
prettydiff = require("prettydiff2")
_ = require('lodash')
regexp = /(^<(template|script|style)[^>]*>)((\s|\S)*?)^<\/\2>/gim
results = text.replace(regexp, (match, begin, type, text) =>
lang = /lang\s*=\s*['"](\w+)["']/.exec(begin)?[1]
replaceText = text
text = text.trim()
beautifiedText = (switch type
when "template"
switch lang
when "pug", "jade"
require("pug-beautify")(text, options)
when undefined
require("js-beautify").html(text, options)
else
undefined
when "script"
require("js-beautify")(text, options)
when "style"
switch lang
when "scss"
options = _.merge(options,
source: text
lang: "scss"
mode: "beautify"
)
prettydiff(options)
when "less"
options = _.merge(options,
source: text
lang: "less"
mode: "beautify"
)
prettydiff(options)
when undefined
require("js-beautify").css(text, options)
else
undefined
)
result = if beautifiedText then match.replace(replaceText, "\n#{beautifiedText.trim()}\n") else match
@verbose("Vue part", match, begin, type, text, lang, result)
return result
)
@verbose("Vue final results", results)
resolve(results)
)