UI for Zipcoin Blue

fixer.js 11KB

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