-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin.js
executable file
·136 lines (113 loc) · 3.42 KB
/
bin.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
const path = require('path')
const ansi = require('ansi-escape-sequences')
const dedent = require('dedent')
const minimist = require('minimist')
const series = require('run-series')
const mapLimit = require('map-limit')
const rimraf = require('rimraf')
const lib = require('./')
const USAGE = `
A command line utility to assist in the creation of DS Boilerplate component
$ ${clr('dscomponent', 'bold')} ${clr('<component-name>', 'green')} [options]
Options:
-S, --skip skip creating file. Options: css, js, twig
-h, --help print usage
-v, --version print version
-q, --quiet don't output any logs
`.replace(/\n$/, '').replace(/^\n/, '')
const NOCOMPONENT = `
Please specify the component name:
${clr('$ dscomponent', 'cyan')} ${clr('<component-name>', 'green')}
For example:
${clr('$ dscomponent', 'cyan')} ${clr('my-component', 'green')}
Run ${clr('dscomponent --help', 'cyan')} to see all options.
`.replace(/\n$/, '').replace(/^\n/, '')
const argv = minimist(process.argv.slice(2), {
alias: {
skip: ['S', 's'],
help: 'h',
version: 'v',
quiet: 'q'
},
boolean: [
'help',
'version',
'quiet'
]
})
;(function main (argv) {
const component = argv._[0]
if (argv.help) {
console.log(USAGE)
} else if (argv.version) {
console.log(require('./package.json').version)
} else if (!component) {
console.log(NOCOMPONENT)
process.exit(1)
} else {
create(path.join(process.cwd(), component), argv)
}
})(argv)
function create (dir, argv) {
const written = []
const cmds = [
function (done) {
print(`Creating a new component directory in ${clr(dir, 'green')}`)
lib.mkdir(dir, done)
}
]
// psuedo schema for writing files
// filename: method to writeFile
const toWrite = {
'index.twig': 'writeTwig',
'styles.scss': 'writeCSS',
'index.js': 'writeJS',
'README.md': 'writeReadMe'
}
Object.keys(toWrite).forEach(function (filename) {
if (filename === 'index.js' && argv.skip === 'js') return
if (filename === 'index.twig' && argv.skip === 'twig') return
var isStyles = argv.skip === 'css' || argv.skip === 'scss'
if (filename === 'styles.scss' && isStyles) return
const method = toWrite[filename]
const component = argv._[0]
const fn = function (done) {
printFile(filename)
const file = path.join(dir, filename)
written.push(file)
lib[method](component, file, done)
}
cmds.push(fn)
})
series(cmds, function (err) {
if (err) {
print('\nAborting installation. The following error occured:')
print(` ${clr(err.message, 'red')}\n`)
mapLimit(written, 1, cleanFile, function (err) {
if (err) throw err
console.log('Cleanup completed, please try again sometime.')
process.exit(1)
})
} else {
var msg = dedent`
Component created in ${clr(dir, 'green')}.
${clr('All done, good job!', 'magenta')}
`.replace(/\n$/g, '')
print('\n' + msg)
}
})
function print (val) {
if (!argv.quiet) console.log(val)
}
function printFile (filename) {
print(`Creating file ${clr(filename, 'cyan')} in directory ${dir}`)
}
}
function clr (text, color) {
return process.stdout.isTTY ? ansi.format(text, color) : text
}
function cleanFile (file, cb) {
console.log('Deleting generated file… ' + clr(path.basename(file), 'cyan'))
rimraf(file, cb)
}