2 var path = require('path')
3 var glob = require('glob')
5 // more popular packages should go here, maybe?
6 function isTestPkg (p) {
7 return !!p.match(/^(expresso|mocha|tap|coffee-script|coco|streamline)$/)
10 function niceName (n) {
11 return n.replace(/^node-|[.-]js$/g, '')
14 function readDeps (test) { return function (cb) {
15 fs.readdir('node_modules', function (er, dir) {
19 if (n === 0) return cb(null, deps)
20 dir.forEach(function (d) {
21 if (d.match(/^\./)) return next()
22 if (test !== isTestPkg(d))
25 var dp = path.join(dirname, 'node_modules', d, 'package.json')
26 fs.readFile(dp, 'utf8', function (er, p) {
28 try { p = JSON.parse(p) }
29 catch (e) { return next() }
30 if (!p.version) return next()
31 deps[d] = '~' + p.version
36 if (--n === 0) return cb(null, deps)
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')
49 exports.main = function (cb) {
50 fs.readdir(dirname, function (er, f) {
53 f = f.filter(function (f) {
54 return f.match(/\.js$/)
57 if (f.indexOf('index.js') !== -1)
59 else if (f.indexOf('main.js') !== -1)
61 else if (f.indexOf(basename + '.js') !== -1)
66 return cb(null, prompt('entry point', f || 'index.js'))
72 exports.bin = function (cb) {
73 fs.readdir(path.resolve(dirname, 'bin'), function (er, d) {
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$/)
84 exports.directories = function (cb) {
85 fs.readdir(dirname, function (er, dirs) {
88 dirs.forEach(function (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
96 if (Object.keys(res).length === 0) res = undefined
101 if (!package.dependencies) {
102 exports.dependencies = readDeps(false)
105 if (!package.devDependencies) {
106 exports.devDependencies = readDeps(true)
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)
119 function setupScripts (d, cb) {
120 // check to see what framework is in use, if any
122 return test || notest
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)
133 s.test = prompt('test command', tx)
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'))
144 gconf = gconf.split(/\r?\n/)
145 var i = gconf.indexOf('[remote "origin"]')
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 = /, '')
152 if (u && u.match(/^git@github.com:/))
153 u = u.replace(/^git@github.com:/, 'git://github.com/')
155 return cb(null, prompt('git repository', u))
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,]+/)
169 if (!package.author) {
170 exports.author = config.get('init.author.name')
172 "name" : config.get('init.author.name'),
173 "email" : config.get('init.author.email'),
174 "url" : config.get('init.author.url')
179 exports.license = prompt('license', 'BSD-2-Clause')