-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
Copy pathtmsnippets.js
82 lines (76 loc) · 2.17 KB
/
tmsnippets.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
var fs = require('fs')
var plist = require('plist')
var snippets = [];
var path = process.argv[2] || process.cwd();
function readSnippet(path, name) {
if (name)
path += name
console.log(name)
if (!/\.(tmSnippet|sublime-snippet|plist)$/i.test(path))
return
console.log(name)
var plistString = fs.readFileSync(path, "utf8");
plist.parseString(plistString, function(_, plist){
snippets.push(plist)
})
}
// read
function readDir(path) {
if (fs.statSync(path).isDirectory()) {
path += "/"
fs.readdirSync(path).forEach(function(name) {
if (/snippets/i.test(name))
readSnippetsInDir(path + name)
else
readDir(path + name)
})
}
}
function readSnippetsInDir(path) {
if (fs.statSync(path).isDirectory()) {
path += "/"
snippets.push(path)
fs.readdirSync(path).forEach(function(name) {
readSnippet(path, name)
})
} else {
readSnippet(path)
}
}
readDir(path)
// transform
snippets = snippets.map(function(s) {
if (s.length == 1)
s = s[0]
if (s.scope)
s.scope = s.scope.replace(/source\./g, "")
delete s.uuid
return s
})
// stringify
var indent = ""
var text = JSON.stringify(snippets, null, 1)
// .replace(/(\n\s*)"(\w+)"\:/g, "$1$2:")
.replace(/(\n\s*)\},\n\s*{/g, "$1}, {")
.replace(/\[\n\s*\{\n/g, "[{\n").replace(/(\n\s*)\}\n\s*\]/g, "$1}]")
.replace(/\[\n\s*[^\[\{\}\]]{0,100}\]/g, function(x){return x.replace(/\n\s*/g, " ")})
.replace(/\:\s*\{\n\s*(.*)\n\s*\}/g, ": {$1}")
.split(/\n\s*/).map(function(x){
if (x[0] == "}" || x[0] == "]")
indent = indent.substr(1)
if (x.slice(-1) == "{" || x.slice(-1) == "[") {
indent += "\t"
return indent.substr(1) + x
}
return indent +x
}).join("\n")
.replace(/\\[\\tnr]/g, function(a){
if (a[1] == "\\")
return a
else if (a[1] == "t")
return "\t"
else
return "\\n"+"\\" + "\n"
})
fs.writeFileSync(path += "/./ace.snippets.js", text)
console.log(path)