3 module.exports.yes = yes
5 var PZ = require('promzard').PromZard
6 var path = require('path')
7 var def = require.resolve('./default-input.js')
10 var semver = require('semver')
11 var read = require('read')
13 // to validate the data object at the end as a worthwhile package
14 // and assign default values for things.
15 // readJson.extras(file, data, cb)
16 var readJson = require('read-package-json')
20 conf.get('yes') || conf.get('y') ||
21 conf.get('force') || conf.get('f')
25 function init (dir, input, config, cb) {
26 if (typeof config === 'function')
27 cb = config, config = {}
29 // accept either a plain-jane object, or a config object
30 // with a "get" method.
31 if (typeof config.get !== 'function') {
43 var packageFile = path.resolve(dir, 'package.json')
44 input = path.resolve(input)
46 var ctx = { yes: yes(config) }
48 var es = readJson.extraSet
49 readJson.extraSet = es.filter(function (fn) {
50 return fn.name !== 'authors' && fn.name !== 'mans'
52 readJson(packageFile, function (er, d) {
53 readJson.extraSet = es
58 ctx.filename = packageFile
59 ctx.dirname = path.dirname(packageFile)
60 ctx.basename = path.basename(ctx.dirname)
61 if (!pkg.version || !semver.valid(pkg.version))
65 ctx.config = config || {}
67 // make sure that the input is valid.
68 // if not, use the default
69 var pz = new PZ(input, ctx)
72 pz.on('data', function (data) {
73 Object.keys(data).forEach(function (k) {
74 if (data[k] !== undefined && data[k] !== null) pkg[k] = data[k]
77 // only do a few of these.
78 // no need for mans or contributors if they're in the files
79 var es = readJson.extraSet
80 readJson.extraSet = es.filter(function (fn) {
81 return fn.name !== 'authors' && fn.name !== 'mans'
83 readJson.extras(packageFile, pkg, function (er, pkg) {
84 readJson.extraSet = es
85 if (er) return cb(er, pkg)
86 pkg = unParsePeople(pkg)
87 // no need for the readme now.
89 delete pkg.readmeFilename
91 // really don't want to have this lying around in the file
97 // if the repo is empty, remove it.
101 // readJson filters out empty descriptions, but init-package-json
102 // traditionally leaves them alone
103 if (!pkg.description)
104 pkg.description = data.description
106 var d = JSON.stringify(pkg, null, 2) + '\n'
107 function write (yes) {
108 fs.writeFile(packageFile, d, 'utf8', function (er) {
109 if (!er && yes && !config.get('silent')) {
110 console.log('Wrote to %s:\n\n%s\n', packageFile, d)
118 console.log('About to write to %s:\n\n%s\n', packageFile, d)
119 read({prompt:'Is this ok? ', default: 'yes'}, function (er, ok) {
120 if (!ok || ok.toLowerCase().charAt(0) !== 'y') {
121 console.log('Aborted.')
132 // turn the objects into somewhat more humane strings.
133 function unParsePeople (data) {
134 if (data.author) data.author = unParsePerson(data.author)
135 ;["maintainers", "contributors"].forEach(function (set) {
136 if (!Array.isArray(data[set])) return;
137 data[set] = data[set].map(unParsePerson)
142 function unParsePerson (person) {
143 if (typeof person === "string") return person
144 var name = person.name || ""
145 var u = person.url || person.web
146 var url = u ? (" ("+u+")") : ""
147 var e = person.email || person.mail
148 var email = e ? (" <"+e+">") : ""
149 return name+email+url