Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

init-package-json.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. module.exports = init
  2. module.exports.yes = yes
  3. var PZ = require('promzard').PromZard
  4. var path = require('path')
  5. var def = require.resolve('./default-input.js')
  6. var fs = require('fs')
  7. var semver = require('semver')
  8. var read = require('read')
  9. // to validate the data object at the end as a worthwhile package
  10. // and assign default values for things.
  11. // readJson.extras(file, data, cb)
  12. var readJson = require('read-package-json')
  13. function yes (conf) {
  14. return !!(
  15. conf.get('yes') || conf.get('y') ||
  16. conf.get('force') || conf.get('f')
  17. )
  18. }
  19. function init (dir, input, config, cb) {
  20. if (typeof config === 'function')
  21. cb = config, config = {}
  22. // accept either a plain-jane object, or a config object
  23. // with a "get" method.
  24. if (typeof config.get !== 'function') {
  25. var data = config
  26. config = {
  27. get: function (k) {
  28. return data[k]
  29. },
  30. toJSON: function () {
  31. return data
  32. }
  33. }
  34. }
  35. var packageFile = path.resolve(dir, 'package.json')
  36. input = path.resolve(input)
  37. var pkg
  38. var ctx = { yes: yes(config) }
  39. var es = readJson.extraSet
  40. readJson.extraSet = es.filter(function (fn) {
  41. return fn.name !== 'authors' && fn.name !== 'mans'
  42. })
  43. readJson(packageFile, function (er, d) {
  44. readJson.extraSet = es
  45. if (er) pkg = {}
  46. else pkg = d
  47. ctx.filename = packageFile
  48. ctx.dirname = path.dirname(packageFile)
  49. ctx.basename = path.basename(ctx.dirname)
  50. if (!pkg.version || !semver.valid(pkg.version))
  51. delete pkg.version
  52. ctx.package = pkg
  53. ctx.config = config || {}
  54. // make sure that the input is valid.
  55. // if not, use the default
  56. var pz = new PZ(input, ctx)
  57. pz.backupFile = def
  58. pz.on('error', cb)
  59. pz.on('data', function (data) {
  60. Object.keys(data).forEach(function (k) {
  61. if (data[k] !== undefined && data[k] !== null) pkg[k] = data[k]
  62. })
  63. // only do a few of these.
  64. // no need for mans or contributors if they're in the files
  65. var es = readJson.extraSet
  66. readJson.extraSet = es.filter(function (fn) {
  67. return fn.name !== 'authors' && fn.name !== 'mans'
  68. })
  69. readJson.extras(packageFile, pkg, function (er, pkg) {
  70. readJson.extraSet = es
  71. if (er) return cb(er, pkg)
  72. pkg = unParsePeople(pkg)
  73. // no need for the readme now.
  74. delete pkg.readme
  75. delete pkg.readmeFilename
  76. // really don't want to have this lying around in the file
  77. delete pkg._id
  78. // ditto
  79. delete pkg.gitHead
  80. // if the repo is empty, remove it.
  81. if (!pkg.repository)
  82. delete pkg.repository
  83. // readJson filters out empty descriptions, but init-package-json
  84. // traditionally leaves them alone
  85. if (!pkg.description)
  86. pkg.description = data.description
  87. var d = JSON.stringify(pkg, null, 2) + '\n'
  88. function write (yes) {
  89. fs.writeFile(packageFile, d, 'utf8', function (er) {
  90. if (!er && yes && !config.get('silent')) {
  91. console.log('Wrote to %s:\n\n%s\n', packageFile, d)
  92. }
  93. return cb(er, pkg)
  94. })
  95. }
  96. if (ctx.yes) {
  97. return write(true)
  98. }
  99. console.log('About to write to %s:\n\n%s\n', packageFile, d)
  100. read({prompt:'Is this OK? ', default: 'yes'}, function (er, ok) {
  101. if (er) {
  102. return cb(er)
  103. }
  104. if (!ok || ok.toLowerCase().charAt(0) !== 'y') {
  105. console.log('Aborted.')
  106. } else {
  107. return write()
  108. }
  109. })
  110. })
  111. })
  112. })
  113. }
  114. // turn the objects into somewhat more humane strings.
  115. function unParsePeople (data) {
  116. if (data.author) data.author = unParsePerson(data.author)
  117. ;["maintainers", "contributors"].forEach(function (set) {
  118. if (!Array.isArray(data[set])) return;
  119. data[set] = data[set].map(unParsePerson)
  120. })
  121. return data
  122. }
  123. function unParsePerson (person) {
  124. if (typeof person === "string") return person
  125. var name = person.name || ""
  126. var u = person.url || person.web
  127. var url = u ? (" ("+u+")") : ""
  128. var e = person.email || person.mail
  129. var email = e ? (" <"+e+">") : ""
  130. return name+email+url
  131. }