update.coffee 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Run this to update the static list of properties stored in the
  2. # completions.json file at the root of this repository.
  3. fs = require 'fs'
  4. request = require 'request'
  5. requestOptions =
  6. url: 'https://api.github.com/repos/atom/atom/releases/latest'
  7. json: true
  8. headers:
  9. 'User-Agent': 'agent'
  10. request requestOptions, (error, response, release) ->
  11. if error?
  12. console.error(error.message)
  13. return process.exit(1)
  14. [apiAsset] = release.assets.filter ({name}) -> name is 'atom-api.json'
  15. unless apiAsset?.browser_download_url
  16. console.error('No atom-api.json asset found in latest release')
  17. return process.exit(1)
  18. apiRequestOptions =
  19. json: true
  20. url: apiAsset.browser_download_url
  21. request apiRequestOptions, (error, response, atomApi) ->
  22. if error?
  23. console.error(error.message)
  24. return process.exit(1)
  25. {classes} = atomApi
  26. publicClasses = {}
  27. for name, {instanceProperties, instanceMethods} of classes
  28. pluckPropertyAttributes = convertPropertyToSuggestion.bind(this, name)
  29. pluckMethodAttributes = convertMethodToSuggestion.bind(this, name)
  30. properties = instanceProperties.filter(isVisible).map(pluckPropertyAttributes).sort(textComparator)
  31. methods = instanceMethods.filter(isVisible).map(pluckMethodAttributes).sort(textComparator)
  32. if properties?.length > 0 or methods.length > 0
  33. publicClasses[name] = properties.concat(methods)
  34. fs.writeFileSync('completions.json', JSON.stringify(publicClasses, null, ' '))
  35. isVisible = ({visibility}) ->
  36. visibility in ['Essential', 'Extended', 'Public']
  37. convertMethodToSuggestion = (className, method) ->
  38. {name, summary, returnValues} = method
  39. args = method['arguments']
  40. snippets = []
  41. if args?.length
  42. for arg, i in args
  43. snippets.push("${#{i+1}:#{arg.name}}")
  44. text = null
  45. snippet = null
  46. if snippets.length
  47. snippet = "#{name}(#{snippets.join(', ')})"
  48. else
  49. text = "#{name}()"
  50. returnValue = returnValues?[0]?.type
  51. description = summary
  52. descriptionMoreURL = getDocLink(className, name)
  53. {name, text, snippet, description, descriptionMoreURL, leftLabel: returnValue, type: 'method'}
  54. convertPropertyToSuggestion = (className, {name, summary}) ->
  55. text = name
  56. returnValue = summary?.match(/\{(\w+)\}/)?[1]
  57. description = summary
  58. descriptionMoreURL = getDocLink(className, name)
  59. {name, text, description, descriptionMoreURL, leftLabel: returnValue, type: 'property'}
  60. getDocLink = (className, instanceName) ->
  61. "https://atom.io/docs/api/latest/#{className}#instance-#{instanceName}"
  62. textComparator = (a, b) ->
  63. return 1 if a.name > b.name
  64. return -1 if a.name < b.name
  65. 0