forked from mafintosh/create-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·171 lines (132 loc) · 4.04 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
var tar = require('tar-fs')
var gunzip = require('gunzip-maybe')
var request = require('request')
var minimist = require('minimist')
var format = require('streaming-format')
var read = require('read')
var fs = require('fs')
var path = require('path')
var CONFIG = path.join(process.env.HOME || process.env.USERPROFILE, '.config/bretzel-start.json')
var conf = fs.existsSync(CONFIG) ? require(CONFIG) : {}
var argv = minimist(process.argv.slice(2), {alias:{configure:'c'}, boolean:'c'})
var dir = argv._[0]
var repo = argv._[1]
var name = dir && path.basename(dir)
var parse = function(str) {
var branch = str.split('#')[1] || 'master'
var parts = str.split('#')[0].replace(/\.git$/, '').split(/[:\/]/)
if (parts.length < 2) return null
var repo = parts.pop()
var user = parts.pop()
return {
user: user,
repo: repo,
branch: branch
}
}
if (argv.configure) {
var defaults = conf.defaults || {}
var keys = Object.keys(defaults)
read({
prompt: 'Set repository: ',
default: conf.github && conf.github.user+'/'+conf.github.repo+'#'+conf.github.branch
}, function(err, repo) {
if (err) return
if (!repo) {
console.error('Repository is required')
process.exit(1)
return
}
repo = parse(repo)
if (!repo) {
console.error('Repository is malformed (should be user/repo(#branch)?)')
process.exit(2)
return
}
conf.github = repo
conf.defaults = {}
var store = function() {
try {
fs.mkdirSync(path.dirname(CONFIG))
} catch (err) {
// do nothing
}
fs.writeFileSync(CONFIG, JSON.stringify(conf, null, 2))
}
var loop = function() {
read({
prompt: 'Set key=value: ',
default: keys.length ? keys[0]+'='+defaults[keys[0]] : 'blank to skip'
}, function(err, def) {
if (err) return
if (def === 'blank to skip' || !def.trim()) return store()
keys.shift()
def = def.trim().split(/\s*=\s*/)
var k = def[0].trim() || oldK
var v = (def[1] || '').trim()
if (v) conf.defaults[k] = v
else delete conf.defaults[k]
loop()
})
}
loop()
})
return
}
var usage = function() {
var def = conf.github ? conf.github.user+'/'+conf.github.repo+'#'+conf.github.branch : 'user/repo#branch'
console.error('bretzel-start [name] [%s]', def)
console.error('')
console.error(' --configure,-c to set default repo/parameters')
console.error(' --[key]=[val] to pass format parameters')
console.error('')
var keys = Object.keys(conf.defaults || {})
if (keys.length) {
console.error('Default parameters are:')
console.error()
keys.forEach(function(key) {
console.log(' %s=%s', key, conf.defaults[key])
})
console.error('')
}
process.exit(1)
}
var gzipStream, gzipSrc;
var failedHttp = function(response) {
if (response.statusCode !== 200) {
console.log('Fetch failed! Status code: '+response.statusCode)
process.exit(4)
}
}
if (argv.file) {
gzipSrc = argv.file;
gzipStream = fs.createReadStream(argv.file);
}
else if (argv.url) {
gzipSrc = argv.url;
gzipStream = request(argv.url)
gzipStream.on('response', failedHttp)
} else {
if (repo && repo.indexOf('/') === -1 && conf.github) repo = conf.github.user+'/'+conf.github.repo+'#'+repo
if (repo) repo = parse(repo)
else if (conf.github) repo = conf.github
if (!repo || !name) return usage()
gzipSrc = repo.user+'/'+repo.repo+'#'+repo.branch
gzipStream = request('https://github.com/'+repo.user+'/'+repo.repo+'/archive/'+repo.branch+'.tar.gz')
gzipStream.on('response', failedHttp)
}
console.log('Creating new project %s from %s', name, gzipSrc)
var formatter = function(stream) {
var def = conf.defaults || {}
def.name = name
return stream.pipe(format(function(name) {
return argv[name] || def[name] || '{{'+name+'}}'
}))
}
gzipStream
.pipe(gunzip())
.pipe(tar.extract(name, {strip:1, mapStream:formatter}))
.on('finish', function() {
console.log('Project created in %s', path.resolve(dir))
})