fixer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. var isValidSemver = require('semver/functions/valid')
  2. var cleanSemver = require('semver/functions/clean')
  3. var validateLicense = require('validate-npm-package-license')
  4. var hostedGitInfo = require('hosted-git-info')
  5. var isBuiltinModule = require('is-core-module')
  6. var depTypes = ['dependencies', 'devDependencies', 'optionalDependencies']
  7. var extractDescription = require('./extract_description')
  8. var url = require('url')
  9. var typos = require('./typos.json')
  10. module.exports = {
  11. // default warning function
  12. warn: function () {},
  13. fixRepositoryField: function (data) {
  14. if (data.repositories) {
  15. this.warn('repositories')
  16. data.repository = data.repositories[0]
  17. }
  18. if (!data.repository) {
  19. return this.warn('missingRepository')
  20. }
  21. if (typeof data.repository === 'string') {
  22. data.repository = {
  23. type: 'git',
  24. url: data.repository,
  25. }
  26. }
  27. var r = data.repository.url || ''
  28. if (r) {
  29. var hosted = hostedGitInfo.fromUrl(r)
  30. if (hosted) {
  31. r = data.repository.url
  32. = hosted.getDefaultRepresentation() === 'shortcut' ? hosted.https() : hosted.toString()
  33. }
  34. }
  35. if (r.match(/github.com\/[^/]+\/[^/]+\.git\.git$/)) {
  36. this.warn('brokenGitUrl', r)
  37. }
  38. },
  39. fixTypos: function (data) {
  40. Object.keys(typos.topLevel).forEach(function (d) {
  41. if (Object.prototype.hasOwnProperty.call(data, d)) {
  42. this.warn('typo', d, typos.topLevel[d])
  43. }
  44. }, this)
  45. },
  46. fixScriptsField: function (data) {
  47. if (!data.scripts) {
  48. return
  49. }
  50. if (typeof data.scripts !== 'object') {
  51. this.warn('nonObjectScripts')
  52. delete data.scripts
  53. return
  54. }
  55. Object.keys(data.scripts).forEach(function (k) {
  56. if (typeof data.scripts[k] !== 'string') {
  57. this.warn('nonStringScript')
  58. delete data.scripts[k]
  59. } else if (typos.script[k] && !data.scripts[typos.script[k]]) {
  60. this.warn('typo', k, typos.script[k], 'scripts')
  61. }
  62. }, this)
  63. },
  64. fixFilesField: function (data) {
  65. var files = data.files
  66. if (files && !Array.isArray(files)) {
  67. this.warn('nonArrayFiles')
  68. delete data.files
  69. } else if (data.files) {
  70. data.files = data.files.filter(function (file) {
  71. if (!file || typeof file !== 'string') {
  72. this.warn('invalidFilename', file)
  73. return false
  74. } else {
  75. return true
  76. }
  77. }, this)
  78. }
  79. },
  80. fixBinField: function (data) {
  81. if (!data.bin) {
  82. return
  83. }
  84. if (typeof data.bin === 'string') {
  85. var b = {}
  86. var match
  87. if (match = data.name.match(/^@[^/]+[/](.*)$/)) {
  88. b[match[1]] = data.bin
  89. } else {
  90. b[data.name] = data.bin
  91. }
  92. data.bin = b
  93. }
  94. },
  95. fixManField: function (data) {
  96. if (!data.man) {
  97. return
  98. }
  99. if (typeof data.man === 'string') {
  100. data.man = [data.man]
  101. }
  102. },
  103. fixBundleDependenciesField: function (data) {
  104. var bdd = 'bundledDependencies'
  105. var bd = 'bundleDependencies'
  106. if (data[bdd] && !data[bd]) {
  107. data[bd] = data[bdd]
  108. delete data[bdd]
  109. }
  110. if (data[bd] && !Array.isArray(data[bd])) {
  111. this.warn('nonArrayBundleDependencies')
  112. delete data[bd]
  113. } else if (data[bd]) {
  114. data[bd] = data[bd].filter(function (bd) {
  115. if (!bd || typeof bd !== 'string') {
  116. this.warn('nonStringBundleDependency', bd)
  117. return false
  118. } else {
  119. if (!data.dependencies) {
  120. data.dependencies = {}
  121. }
  122. if (Object.prototype.hasOwnProperty.call(data.dependencies, bd)) {
  123. this.warn('nonDependencyBundleDependency', bd)
  124. data.dependencies[bd] = '*'
  125. }
  126. return true
  127. }
  128. }, this)
  129. }
  130. },
  131. fixDependencies: function (data, strict) {
  132. objectifyDeps(data, this.warn)
  133. addOptionalDepsToDeps(data, this.warn)
  134. this.fixBundleDependenciesField(data)
  135. ;['dependencies', 'devDependencies'].forEach(function (deps) {
  136. if (!(deps in data)) {
  137. return
  138. }
  139. if (!data[deps] || typeof data[deps] !== 'object') {
  140. this.warn('nonObjectDependencies', deps)
  141. delete data[deps]
  142. return
  143. }
  144. Object.keys(data[deps]).forEach(function (d) {
  145. var r = data[deps][d]
  146. if (typeof r !== 'string') {
  147. this.warn('nonStringDependency', d, JSON.stringify(r))
  148. delete data[deps][d]
  149. }
  150. var hosted = hostedGitInfo.fromUrl(data[deps][d])
  151. if (hosted) {
  152. data[deps][d] = hosted.toString()
  153. }
  154. }, this)
  155. }, this)
  156. },
  157. fixModulesField: function (data) {
  158. if (data.modules) {
  159. this.warn('deprecatedModules')
  160. delete data.modules
  161. }
  162. },
  163. fixKeywordsField: function (data) {
  164. if (typeof data.keywords === 'string') {
  165. data.keywords = data.keywords.split(/,\s+/)
  166. }
  167. if (data.keywords && !Array.isArray(data.keywords)) {
  168. delete data.keywords
  169. this.warn('nonArrayKeywords')
  170. } else if (data.keywords) {
  171. data.keywords = data.keywords.filter(function (kw) {
  172. if (typeof kw !== 'string' || !kw) {
  173. this.warn('nonStringKeyword')
  174. return false
  175. } else {
  176. return true
  177. }
  178. }, this)
  179. }
  180. },
  181. fixVersionField: function (data, strict) {
  182. // allow "loose" semver 1.0 versions in non-strict mode
  183. // enforce strict semver 2.0 compliance in strict mode
  184. var loose = !strict
  185. if (!data.version) {
  186. data.version = ''
  187. return true
  188. }
  189. if (!isValidSemver(data.version, loose)) {
  190. throw new Error('Invalid version: "' + data.version + '"')
  191. }
  192. data.version = cleanSemver(data.version, loose)
  193. return true
  194. },
  195. fixPeople: function (data) {
  196. modifyPeople(data, unParsePerson)
  197. modifyPeople(data, parsePerson)
  198. },
  199. fixNameField: function (data, options) {
  200. if (typeof options === 'boolean') {
  201. options = {strict: options}
  202. } else if (typeof options === 'undefined') {
  203. options = {}
  204. }
  205. var strict = options.strict
  206. if (!data.name && !strict) {
  207. data.name = ''
  208. return
  209. }
  210. if (typeof data.name !== 'string') {
  211. throw new Error('name field must be a string.')
  212. }
  213. if (!strict) {
  214. data.name = data.name.trim()
  215. }
  216. ensureValidName(data.name, strict, options.allowLegacyCase)
  217. if (isBuiltinModule(data.name)) {
  218. this.warn('conflictingName', data.name)
  219. }
  220. },
  221. fixDescriptionField: function (data) {
  222. if (data.description && typeof data.description !== 'string') {
  223. this.warn('nonStringDescription')
  224. delete data.description
  225. }
  226. if (data.readme && !data.description) {
  227. data.description = extractDescription(data.readme)
  228. }
  229. if (data.description === undefined) {
  230. delete data.description
  231. }
  232. if (!data.description) {
  233. this.warn('missingDescription')
  234. }
  235. },
  236. fixReadmeField: function (data) {
  237. if (!data.readme) {
  238. this.warn('missingReadme')
  239. data.readme = 'ERROR: No README data found!'
  240. }
  241. },
  242. fixBugsField: function (data) {
  243. if (!data.bugs && data.repository && data.repository.url) {
  244. var hosted = hostedGitInfo.fromUrl(data.repository.url)
  245. if (hosted && hosted.bugs()) {
  246. data.bugs = {url: hosted.bugs()}
  247. }
  248. } else if (data.bugs) {
  249. var emailRe = /^.+@.*\..+$/
  250. if (typeof data.bugs === 'string') {
  251. if (emailRe.test(data.bugs)) {
  252. data.bugs = {email: data.bugs}
  253. /* eslint-disable-next-line node/no-deprecated-api */
  254. } else if (url.parse(data.bugs).protocol) {
  255. data.bugs = {url: data.bugs}
  256. } else {
  257. this.warn('nonEmailUrlBugsString')
  258. }
  259. } else {
  260. bugsTypos(data.bugs, this.warn)
  261. var oldBugs = data.bugs
  262. data.bugs = {}
  263. if (oldBugs.url) {
  264. /* eslint-disable-next-line node/no-deprecated-api */
  265. if (typeof (oldBugs.url) === 'string' && url.parse(oldBugs.url).protocol) {
  266. data.bugs.url = oldBugs.url
  267. } else {
  268. this.warn('nonUrlBugsUrlField')
  269. }
  270. }
  271. if (oldBugs.email) {
  272. if (typeof (oldBugs.email) === 'string' && emailRe.test(oldBugs.email)) {
  273. data.bugs.email = oldBugs.email
  274. } else {
  275. this.warn('nonEmailBugsEmailField')
  276. }
  277. }
  278. }
  279. if (!data.bugs.email && !data.bugs.url) {
  280. delete data.bugs
  281. this.warn('emptyNormalizedBugs')
  282. }
  283. }
  284. },
  285. fixHomepageField: function (data) {
  286. if (!data.homepage && data.repository && data.repository.url) {
  287. var hosted = hostedGitInfo.fromUrl(data.repository.url)
  288. if (hosted && hosted.docs()) {
  289. data.homepage = hosted.docs()
  290. }
  291. }
  292. if (!data.homepage) {
  293. return
  294. }
  295. if (typeof data.homepage !== 'string') {
  296. this.warn('nonUrlHomepage')
  297. return delete data.homepage
  298. }
  299. /* eslint-disable-next-line node/no-deprecated-api */
  300. if (!url.parse(data.homepage).protocol) {
  301. data.homepage = 'http://' + data.homepage
  302. }
  303. },
  304. fixLicenseField: function (data) {
  305. const license = data.license || data.licence
  306. if (!license) {
  307. return this.warn('missingLicense')
  308. }
  309. if (
  310. typeof (license) !== 'string' ||
  311. license.length < 1 ||
  312. license.trim() === ''
  313. ) {
  314. return this.warn('invalidLicense')
  315. }
  316. if (!validateLicense(license).validForNewPackages) {
  317. return this.warn('invalidLicense')
  318. }
  319. },
  320. }
  321. function isValidScopedPackageName (spec) {
  322. if (spec.charAt(0) !== '@') {
  323. return false
  324. }
  325. var rest = spec.slice(1).split('/')
  326. if (rest.length !== 2) {
  327. return false
  328. }
  329. return rest[0] && rest[1] &&
  330. rest[0] === encodeURIComponent(rest[0]) &&
  331. rest[1] === encodeURIComponent(rest[1])
  332. }
  333. function isCorrectlyEncodedName (spec) {
  334. return !spec.match(/[/@\s+%:]/) &&
  335. spec === encodeURIComponent(spec)
  336. }
  337. function ensureValidName (name, strict, allowLegacyCase) {
  338. if (name.charAt(0) === '.' ||
  339. !(isValidScopedPackageName(name) || isCorrectlyEncodedName(name)) ||
  340. (strict && (!allowLegacyCase) && name !== name.toLowerCase()) ||
  341. name.toLowerCase() === 'node_modules' ||
  342. name.toLowerCase() === 'favicon.ico') {
  343. throw new Error('Invalid name: ' + JSON.stringify(name))
  344. }
  345. }
  346. function modifyPeople (data, fn) {
  347. if (data.author) {
  348. data.author = fn(data.author)
  349. }['maintainers', 'contributors'].forEach(function (set) {
  350. if (!Array.isArray(data[set])) {
  351. return
  352. }
  353. data[set] = data[set].map(fn)
  354. })
  355. return data
  356. }
  357. function unParsePerson (person) {
  358. if (typeof person === 'string') {
  359. return person
  360. }
  361. var name = person.name || ''
  362. var u = person.url || person.web
  363. var url = u ? (' (' + u + ')') : ''
  364. var e = person.email || person.mail
  365. var email = e ? (' <' + e + '>') : ''
  366. return name + email + url
  367. }
  368. function parsePerson (person) {
  369. if (typeof person !== 'string') {
  370. return person
  371. }
  372. var name = person.match(/^([^(<]+)/)
  373. var url = person.match(/\(([^)]+)\)/)
  374. var email = person.match(/<([^>]+)>/)
  375. var obj = {}
  376. if (name && name[0].trim()) {
  377. obj.name = name[0].trim()
  378. }
  379. if (email) {
  380. obj.email = email[1]
  381. }
  382. if (url) {
  383. obj.url = url[1]
  384. }
  385. return obj
  386. }
  387. function addOptionalDepsToDeps (data, warn) {
  388. var o = data.optionalDependencies
  389. if (!o) {
  390. return
  391. }
  392. var d = data.dependencies || {}
  393. Object.keys(o).forEach(function (k) {
  394. d[k] = o[k]
  395. })
  396. data.dependencies = d
  397. }
  398. function depObjectify (deps, type, warn) {
  399. if (!deps) {
  400. return {}
  401. }
  402. if (typeof deps === 'string') {
  403. deps = deps.trim().split(/[\n\r\s\t ,]+/)
  404. }
  405. if (!Array.isArray(deps)) {
  406. return deps
  407. }
  408. warn('deprecatedArrayDependencies', type)
  409. var o = {}
  410. deps.filter(function (d) {
  411. return typeof d === 'string'
  412. }).forEach(function (d) {
  413. d = d.trim().split(/(:?[@\s><=])/)
  414. var dn = d.shift()
  415. var dv = d.join('')
  416. dv = dv.trim()
  417. dv = dv.replace(/^@/, '')
  418. o[dn] = dv
  419. })
  420. return o
  421. }
  422. function objectifyDeps (data, warn) {
  423. depTypes.forEach(function (type) {
  424. if (!data[type]) {
  425. return
  426. }
  427. data[type] = depObjectify(data[type], type, warn)
  428. })
  429. }
  430. function bugsTypos (bugs, warn) {
  431. if (!bugs) {
  432. return
  433. }
  434. Object.keys(bugs).forEach(function (k) {
  435. if (typos.bugs[k]) {
  436. warn('typo', k, typos.bugs[k], 'bugs')
  437. bugs[typos.bugs[k]] = bugs[k]
  438. delete bugs[k]
  439. }
  440. })
  441. }