summaryrefslogtreecommitdiff
path: root/dot-scls/run.js
blob: 119592d16c20069ac9d8e501163ecf4177167f54 (plain)
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
/*

写 Rust 真的是浪费生命!!!

*/
const { homedir } = require('os')
const { join, parse } = require('path')
const { execSync } = require('child_process')
const fs = require('fs')

const rootDir = join(homedir(), '.scls')
const cfgFile = join(rootDir, 'external-snippets.toml')
const externalSnippets = join(rootDir, 'external-snippets')

function clone(url, destination) {
  const cmd = `git clone ${url} ${destination}`
  console.log(cmd)
  execSync(cmd, { cwd: externalSnippets })
}

function mkdirp(path) {
  if (fs.existsSync(path)) {
    return
  }
  mkdirp(join(path, '..'))
  fs.mkdirSync(path)
}

function isDir(path) {
  return fs.existsSync(path) && fs.lstatSync(path).isDirectory()
}

function realName(filename) {
  return parse(filename).name
}

/**
 * 生成配置文件
 * @param {string} file 
 * @param {string} workspace
 */
function createConfigFile(file, workspace) {
  let content = `[[sources]]
name = "friendly-snippets"
git = "https://github.com/rafamadriz/friendly-snippets.git"
`

  function addSnippet(lang, path) {
return `
[[sources.paths]]
scope = ["${lang}"]
path = "snippets/${path}"
`
  }

  const frameworks = 'frameworks'

  function append(workspace, always = false) {
    fs.readdirSync(workspace).forEach(filename=> {
      if (filename.startsWith('.') || (filename == frameworks && !always)) return
      let name = realName(filename)
      const filepath = join(workspace, filename)
      if (!isDir(filepath)) {
        if (always) {
          filename = join(frameworks, filename)
        }
        content += addSnippet(name, filename)
      } else {
        fs.readdirSync(filepath).forEach(subname=> {
          let filepath = join(filename, subname)
          if (always) {
            filepath = join(frameworks, filepath)
          }
          content += addSnippet(filename, filepath) 
        })
      }
    })
  }

  append(workspace)
  append(join(workspace, frameworks), true)

  fs.writeFileSync(file, content)
}


function main() {
  mkdirp(externalSnippets)
  const name = 'friendly-snippets'
  const workspace = join(externalSnippets, name)
  if (!fs.existsSync(workspace)) {
    clone('https://github.com/rafamadriz/friendly-snippets', name)
    if (!fs.existsSync(workspace)) {
      throw new Error('Failed to clone external snippets')
    }
  }
  if (fs.existsSync(cfgFile)) return
  createConfigFile(cfgFile, join(workspace, 'snippets'))
}

main()