Welcome to mirror list, hosted at ThFree Co, Russian Federation.

init-package-json.js « lib « init-package-json « node_modules - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f13a34ce6ff4e5327e81c95fe09b384730a7d420 (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

module.exports = init
module.exports.yes = yes

var PZ = require('promzard').PromZard
var path = require('path')
var def = require.resolve('./default-input.js')

var fs = require('fs')
var semver = require('semver')
var read = require('read')

// to validate the data object at the end as a worthwhile package
// and assign default values for things.
var readJson = require('read-package-json')

function yes (conf) {
  return !!(
    conf.get('yes') || conf.get('y') ||
    conf.get('force') || conf.get('f')
  )
}

function init (dir, input, config, cb) {
  if (typeof config === 'function') {
    cb = config
    config = {}
  }

  // accept either a plain-jane object, or a config object
  // with a "get" method.
  if (typeof config.get !== 'function') {
    var data = config
    config = {
      get: function (k) {
        return data[k]
      },
      toJSON: function () {
        return data
      },
    }
  }

  var packageFile = path.resolve(dir, 'package.json')
  input = path.resolve(input)
  var pkg
  var ctx = { yes: yes(config) }

  var es = readJson.extraSet
  readJson.extraSet = es.filter(function (fn) {
    return fn.name !== 'authors' && fn.name !== 'mans'
  })
  readJson(packageFile, function (er, d) {
    readJson.extraSet = es

    if (er) {
      pkg = {}
    } else {
      pkg = d
    }

    ctx.filename = packageFile
    ctx.dirname = path.dirname(packageFile)
    ctx.basename = path.basename(ctx.dirname)
    if (!pkg.version || !semver.valid(pkg.version)) {
      delete pkg.version
    }

    ctx.package = pkg
    ctx.config = config || {}

    // make sure that the input is valid.
    // if not, use the default
    var pz = new PZ(input, ctx)
    pz.backupFile = def
    pz.on('error', cb)
    pz.on('data', function (data) {
      Object.keys(data).forEach(function (k) {
        if (data[k] !== undefined && data[k] !== null) {
          pkg[k] = data[k]
        }
      })

      // only do a few of these.
      // no need for mans or contributors if they're in the files
      var es = readJson.extraSet
      readJson.extraSet = es.filter(function (fn) {
        return fn.name !== 'authors' && fn.name !== 'mans'
      })
      readJson.extras(packageFile, pkg, function (er, pkg) {
        if (er) {
          return cb(er, pkg)
        }
        readJson.extraSet = es
        pkg = unParsePeople(pkg)
        // no need for the readme now.
        delete pkg.readme
        delete pkg.readmeFilename

        // really don't want to have this lying around in the file
        delete pkg._id

        // ditto
        delete pkg.gitHead

        // if the repo is empty, remove it.
        if (!pkg.repository) {
          delete pkg.repository
        }

        // readJson filters out empty descriptions, but init-package-json
        // traditionally leaves them alone
        if (!pkg.description) {
          pkg.description = data.description
        }

        var d = JSON.stringify(updateDeps(pkg), null, 2) + '\n'
        function write (yes) {
          fs.writeFile(packageFile, d, 'utf8', function (er) {
            if (!er && yes && !config.get('silent')) {
              console.log('Wrote to %s:\n\n%s\n', packageFile, d)
            }
            return cb(er, pkg)
          })
        }
        if (ctx.yes) {
          return write(true)
        }
        console.log('About to write to %s:\n\n%s\n', packageFile, d)
        read({ prompt: 'Is this OK? ', default: 'yes' }, function (er, ok) {
          if (er) {
            return cb(er)
          }
          if (!ok || ok.toLowerCase().charAt(0) !== 'y') {
            console.log('Aborted.')
          } else {
            return write()
          }
        })
      })
    })
  })
}

function updateDeps (depsData) {
  // optionalDependencies don't need to be repeated in two places
  if (depsData.dependencies) {
    if (depsData.optionalDependencies) {
      for (const name of Object.keys(depsData.optionalDependencies)) {
        delete depsData.dependencies[name]
      }
    }
    if (Object.keys(depsData.dependencies).length === 0) {
      delete depsData.dependencies
    }
  }

  return depsData
}

// turn the objects into somewhat more humane strings.
function unParsePeople (data) {
  if (data.author) {
    data.author = unParsePerson(data.author)
  }['maintainers', 'contributors'].forEach(function (set) {
    if (!Array.isArray(data[set])) {
      return
    }
    data[set] = data[set].map(unParsePerson)
  })
  return data
}

function unParsePerson (person) {
  if (typeof person === 'string') {
    return person
  }
  var name = person.name || ''
  var u = person.url || person.web
  var url = u ? (' (' + u + ')') : ''
  var e = person.email || person.mail
  var email = e ? (' <' + e + '>') : ''
  return name + email + url
}