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