tmsnippets.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var fs = require('fs')
  2. var plist = require('plist')
  3. var snippets = [];
  4. var path = process.argv[2] || process.cwd();
  5. function readSnippet(path, name) {
  6. if (name)
  7. path += name
  8. console.log(name)
  9. if (!/\.(tmSnippet|sublime-snippet|plist)$/i.test(path))
  10. return
  11. console.log(name)
  12. var plistString = fs.readFileSync(path, "utf8");
  13. plist.parseString(plistString, function(_, plist){
  14. snippets.push(plist)
  15. })
  16. }
  17. // read
  18. function readDir(path) {
  19. if (fs.statSync(path).isDirectory()) {
  20. path += "/"
  21. fs.readdirSync(path).forEach(function(name) {
  22. if (/snippets/i.test(name))
  23. readSnippetsInDir(path + name)
  24. else
  25. readDir(path + name)
  26. })
  27. }
  28. }
  29. function readSnippetsInDir(path) {
  30. if (fs.statSync(path).isDirectory()) {
  31. path += "/"
  32. snippets.push(path)
  33. fs.readdirSync(path).forEach(function(name) {
  34. readSnippet(path, name)
  35. })
  36. } else {
  37. readSnippet(path)
  38. }
  39. }
  40. readDir(path)
  41. // transform
  42. snippets = snippets.map(function(s) {
  43. if (s.length == 1)
  44. s = s[0]
  45. if (s.scope)
  46. s.scope = s.scope.replace(/source\./g, "")
  47. delete s.uuid
  48. return s
  49. })
  50. // stringify
  51. var indent = ""
  52. var text = JSON.stringify(snippets, null, 1)
  53. // .replace(/(\n\s*)"(\w+)"\:/g, "$1$2:")
  54. .replace(/(\n\s*)\},\n\s*{/g, "$1}, {")
  55. .replace(/\[\n\s*\{\n/g, "[{\n").replace(/(\n\s*)\}\n\s*\]/g, "$1}]")
  56. .replace(/\[\n\s*[^\[\{\}\]]{0,100}\]/g, function(x){return x.replace(/\n\s*/g, " ")})
  57. .replace(/\:\s*\{\n\s*(.*)\n\s*\}/g, ": {$1}")
  58. .split(/\n\s*/).map(function(x){
  59. if (x[0] == "}" || x[0] == "]")
  60. indent = indent.substr(1)
  61. if (x.slice(-1) == "{" || x.slice(-1) == "[") {
  62. indent += "\t"
  63. return indent.substr(1) + x
  64. }
  65. return indent +x
  66. }).join("\n")
  67. .replace(/\\[\\tnr]/g, function(a){
  68. if (a[1] == "\\")
  69. return a
  70. else if (a[1] == "t")
  71. return "\t"
  72. else
  73. return "\\n"+"\\" + "\n"
  74. })
  75. fs.writeFileSync(path += "/./ace.snippets.js", text)
  76. console.log(path)