Completed
Push — master ( 2cb7f1...dc89da )
by Thomas
31s
created

module.exports   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
c 0
b 0
f 0
nc 16
nop 0
dl 0
loc 36
rs 8.439
1
'use strict'
2
3
const util = require('../util')
4
const output = require('../output')
5
const email = require('git-user-email')
6
const name = require('git-user-name')
7
const path = require('path')
8
const fs = require('fs')
9
const Git = require('nodegit')
10
const jsonfile = require('jsonfile')
11
const rimraf = require('rimraf')
12
13
module.exports = function (slug) {
14
  var template = 'basic'
15
16
  var userName = (util.getSystemUsername() || 'user').toLowerCase()
17
  if (slug.match(/^[a-z0-9-]+\/[a-z0-9-]+$/)) {
18
    userName = slug.split('/')[0]
19
    slug = slug.split('/')[1]
20
  }
21
22
  if (!slug.match(/^[a-z0-9-]+$/)) {
23
    output.err('Module name should only contain lowercase letters, numbers and dashes.')
24
    return
25
  }
26
27
  var repo = 'includable-modules/starter-' + template
28
  var stopSpinner = output.wait('Cloning ' + repo)
29
30
  try {
31
    fs.mkdirSync(slug)
32
    Git.Clone('https://github.com/' + repo, slug)
33
      .then(function () {
34
        stopSpinner(true)
35
36
        // Write module.json
37
        var json = util.moduleJSON(slug, userName, name({
38
          type: 'global'
39
        }), email({
40
          type: 'global'
41
        }), jsonfile.readFileSync(path.join(slug, 'module.json')))
42
        fs.writeFileSync(path.join(slug, 'module.json'), JSON.stringify(json, null, 3), 'utf8')
43
        fs.writeFileSync(path.join(slug, 'composer.json'), JSON.stringify({
44
          'require': {}
45
        }, null, 3), 'utf8')
46
47
        // Remove stock files
48
        try { fs.unlinkSync(path.join(slug, 'LICENSE.md')) } catch (e) {}
49
        try { fs.unlinkSync(path.join(slug, 'LICENSE')) } catch (e) {}
1 ignored issue
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
50
        try { fs.unlinkSync(path.join(slug, 'README')) } catch (e) {}
1 ignored issue
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
51
        try { rimraf.sync(path.join(slug, '.git')) } catch (e) {}
1 ignored issue
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
52
53
        // Write readme.md
54
        fs.writeFileSync(path.join(slug, 'README.md'), '# ' +
55
          util.boxNameDisplay(slug) + '\n\nA Incluable app for ' + util.boxNameDisplay(slug) + '.', 'utf8')
56
57
        // Git init
58
        var gitSpinner = output.wait('Initializing local Git repository')
59
        Git.Repository.init(path.resolve(slug), 0).then(function () {
60
          gitSpinner(true)
61
        })
62
63
        // Download IDE helper
64
        var ideSpinner = output.wait('Downloading code completion helper')
65
        util.downloadHelper(function () {
66
          ideSpinner(true)
67
        }, slug)
68
      })
69
      .catch(function (e) {
70
        stopSpinner(e)
71
      })
72
  } catch (e) {
73
    stopSpinner(e)
74
  }
75
}
76