-
Notifications
You must be signed in to change notification settings - Fork 34
/
cli.js
126 lines (101 loc) · 2.5 KB
/
cli.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
'use strict'
var path = require('path')
var fs = require('fs')
var cwd = process.cwd()
var Pkg = require('./lib/pkg')
var extend = require('extend')
var minimist = require('minimist')
var napa = module.exports = {}
napa.cli = function (args, done) {
var parsedArgs = minimist(args)
args = parsedArgs['_']
var total = 0
var pkg = napa.readpkg()
var opts = napa._loadFromPkg('napa-config', {})
// Add flags to opts
var flags = parsedArgs
delete flags['_']
opts = extend(opts, flags)
if (pkg) {
args = args.map(napa.args).concat(pkg)
} else {
args = args.map(napa.args)
}
args.forEach(function (cmd) {
total++
opts.ref = cmd[2]
var pkg = new Pkg(cmd[0], cmd[1], opts)
pkg.install(close)
})
function close () {
total--
if (total < 1 && typeof done === 'function') {
return done()
}
}
}
napa.args = function (str) {
var url, name
var split = str.split(':')
if (split.length === 3) {
name = split[2]
url = split.slice(0, 2).join(':')
} else if (split.length === 2) {
if (split[1].slice(0, 2) === '//') {
url = split.join(':')
} else {
url = split[0]
name = split[1]
}
} else {
url = split.join(':')
}
if (!name) {
name = url.slice(url.lastIndexOf('/') + 1)
}
return [napa.url(url), name, napa.getref(str)]
}
napa.url = function (url) {
if (typeof url !== 'string') {
if (url.url) url = url.url
else return false
}
if (url.indexOf('#') !== -1) {
if (url.indexOf('://') === -1) {
var s = url.split('#')
url = 'https://github.com/' + s[0] + '/archive/' + s[1]
if (process.platform === 'win32') url += '.zip'
else url += '.tar.gz'
} else {
url = url.replace(/#.*?$/, '')
}
}
if (url.slice(0, 1) === '/') {
url = url.slice(1)
}
if (url.indexOf('://') === -1) {
url = 'git://github.com/' + url
}
return url
}
napa.readpkg = function () {
var repos = napa._loadFromPkg('napa') || {}
return Object.keys(repos).map(function (repo) {
var repoLocation = repos[repo]
return [napa.url(repoLocation), repo, napa.getref(repoLocation)]
})
}
napa._loadFromPkg = function (property, defaults) {
if (typeof defaults === 'undefined') {
defaults = false
}
var pkgPath = path.join(cwd, 'package.json')
if (!fs.existsSync(pkgPath)) {
return defaults
}
var pkg = require(pkgPath)
return pkg.hasOwnProperty(property) ? pkg[property] : defaults
}
napa.getref = function (url) {
return url.replace(/^[^#]*#?/, '')
}