-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (69 loc) · 2.01 KB
/
index.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
const fs = require('fs')
const dedent = require('dedent')
const mkdirp = require('mkdirp')
const cama = require('cama')
var gitConfig = require('git-config')
exports.mkdir = function (dir, cb) {
mkdirp(dir, function (err) {
if (err) return cb(new Error('Could not create directory ' + dir))
fs.readdir(dir, function (err, files) {
if (err) return cb(new Error('Could not read directory ' + dir))
if (files.length) return cb(new Error('Directory contains files. This might create conflicts.'))
cb()
})
})
}
exports.writeTwig = function (component, filename, cb) {
// TODO: add logic for panels, buttons, mastheads, etc...
const data = dedent`
<section class='${component}'>
<!-- component-name markup goes here-->
</section>\n
`
write(filename, data, cb)
}
exports.writeCSS = function (component, filename, cb) {
// TODO: add logic for panels, buttons, mastheads, etc...
const data = dedent`
.${component} {
/* component styles go here */
}\n
`
write(filename, data, cb)
}
exports.writeJS = function (component, filename, cb) {
// TODO: add logic for panels, buttons, mastheads, etc...
const capitalize = component.replace(component.charAt(0), component.charAt(0).toUpperCase())
const ctor = cama(capitalize)
const data = dedent`
import $ from 'jquery'
class ${ctor} {
constructor(element, options) {
this.element = $(element)
}
init() {
if (!this.element.length) {
return
}
}
}
const ${cama(component)} = new ${ctor}()
${cama(component)}.init()\n
`
write(filename, data, cb)
}
exports.writeReadMe = function (component, filename, cb) {
gitConfig(function (err, config) {
if (err) return cb(err)
const data = dedent`
# Author: ${config.user.name}
`
write(filename, data, cb)
})
}
function write (filename, data, cb) {
fs.writeFile(filename, data, function (err) {
if (err) return cb(new Error('Could not write file ' + filename))
cb()
})
}