80b20a94a7b115006f02d892fe821b57aedbe71c
[platform/upstream/nodejs.git] / deps / npm / node_modules / init-package-json / default-input.js
1 var fs = require('fs')
2 var path = require('path')
3 var glob = require('glob')
4
5 // more popular packages should go here, maybe?
6 function isTestPkg (p) {
7   return !!p.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/)
8 }
9
10 function niceName (n) {
11   return n.replace(/^node-|[.-]js$/g, '')
12 }
13
14 function readDeps (test) { return function (cb) {
15   fs.readdir('node_modules', function (er, dir) {
16     if (er) return cb()
17     var deps = {}
18     var n = dir.length
19     if (n === 0) return cb(null, deps)
20     dir.forEach(function (d) {
21       if (d.match(/^\./)) return next()
22       if (test !== isTestPkg(d))
23         return next()
24
25       var dp = path.join(dirname, 'node_modules', d, 'package.json')
26       fs.readFile(dp, 'utf8', function (er, p) {
27         if (er) return next()
28         try { p = JSON.parse(p) }
29         catch (e) { return next() }
30         if (!p.version) return next()
31         deps[d] = '~' + p.version
32         return next()
33       })
34     })
35     function next () {
36       if (--n === 0) return cb(null, deps)
37     }
38   })
39 }}
40
41
42 exports.name = prompt('name', package.name || basename)
43 exports.version = prompt('version', package.version || '0.0.0')
44 if (!package.description) {
45   exports.description = prompt('description')
46 }
47
48 if (!package.main) {
49   exports.main = function (cb) {
50     fs.readdir(dirname, function (er, f) {
51       if (er) f = []
52
53       f = f.filter(function (f) {
54         return f.match(/\.js$/)
55       })
56
57       if (f.indexOf('index.js') !== -1)
58         f = 'index.js'
59       else if (f.indexOf('main.js') !== -1)
60         f = 'main.js'
61       else if (f.indexOf(basename + '.js') !== -1)
62         f = basename + '.js'
63       else
64         f = f[0]
65
66       return cb(null, prompt('entry point', f || 'index.js'))
67     })
68   }
69 }
70
71 if (!package.bin) {
72   exports.bin = function (cb) {
73     fs.readdir(path.resolve(dirname, 'bin'), function (er, d) {
74       // no bins
75       if (er) return cb()
76       // just take the first js file we find there, or nada
77       return cb(null, d.filter(function (f) {
78         return f.match(/\.js$/)
79       })[0])
80     })
81   }
82 }
83
84 exports.directories = function (cb) {
85   fs.readdir(dirname, function (er, dirs) {
86     if (er) return cb(er)
87     var res = {}
88     dirs.forEach(function (d) {
89       switch (d) {
90         case 'example': case 'examples': return res.example = d
91         case 'test': case 'tests': return res.test = d
92         case 'doc': case 'docs': return res.doc = d
93         case 'man': return res.man = d
94       }
95     })
96     if (Object.keys(res).length === 0) res = undefined
97     return cb(null, res)
98   })
99 }
100
101 if (!package.dependencies) {
102   exports.dependencies = readDeps(false)
103 }
104
105 if (!package.devDependencies) {
106   exports.devDependencies = readDeps(true)
107 }
108
109 // MUST have a test script!
110 var s = package.scripts || {}
111 var notest = 'echo "Error: no test specified" && exit 1'
112 if (!package.scripts) {
113   exports.scripts = function (cb) {
114     fs.readdir(path.join(dirname, 'node_modules'), function (er, d) {
115       setupScripts(d || [], cb)
116     })
117   }
118 }
119 function setupScripts (d, cb) {
120   // check to see what framework is in use, if any
121   function tx (test) {
122     return test || notest
123   }
124
125   if (!s.test || s.test === notest) {
126     if (d.indexOf('tap') !== -1)
127       s.test = prompt('test command', 'tap test/*.js', tx)
128     else if (d.indexOf('expresso') !== -1)
129       s.test = prompt('test command', 'expresso test', tx)
130     else if (d.indexOf('mocha') !== -1)
131       s.test = prompt('test command', 'mocha', tx)
132     else
133       s.test = prompt('test command', tx)
134   }
135
136   return cb(null, s)
137 }
138
139 if (!package.repository) {
140   exports.repository = function (cb) {
141     fs.readFile('.git/config', 'utf8', function (er, gconf) {
142       if (er || !gconf) return cb(null, prompt('git repository'))
143
144       gconf = gconf.split(/\r?\n/)
145       var i = gconf.indexOf('[remote "origin"]')
146       if (i !== -1) {
147         var u = gconf[i + 1]
148         if (!u.match(/^\s*url =/)) u = gconf[i + 2]
149         if (!u.match(/^\s*url =/)) u = null
150         else u = u.replace(/^\s*url = /, '')
151       }
152       if (u && u.match(/^git@github.com:/))
153         u = u.replace(/^git@github.com:/, 'git://github.com/')
154
155       return cb(null, prompt('git repository', u))
156     })
157   }
158 }
159
160 if (!package.keywords) {
161   exports.keywords = prompt('keywords', function (s) {
162     if (!s) return undefined
163     if (Array.isArray(s)) s = s.join(' ')
164     if (typeof s !== 'string') return s
165     return s.split(/[\s,]+/)
166   })
167 }
168
169 if (!package.author) {
170   exports.author = config.get('init.author.name')
171   ? {
172       "name" : config.get('init.author.name'),
173       "email" : config.get('init.author.email'),
174       "url" : config.get('init.author.url')
175     }
176   : prompt('author')
177 }
178
179 exports.license = prompt('license', 'BSD-2-Clause')