This repository has been archived by the owner on May 2, 2023. It is now read-only.
forked from atom/autocomplete-atom-api
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.coffee
84 lines (65 loc) · 2.6 KB
/
update.coffee
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
# Run this to update the static list of properties stored in the
# completions.json file at the root of this repository.
fs = require 'fs'
request = require 'request'
requestOptions =
url: 'https://api.github.com/repos/atom/atom/releases/latest'
json: true
headers:
'User-Agent': 'agent'
request requestOptions, (error, response, release) ->
if error?
console.error(error.message)
return process.exit(1)
[apiAsset] = release.assets.filter ({name}) -> name is 'atom-api.json'
unless apiAsset?.browser_download_url
console.error('No atom-api.json asset found in latest release')
return process.exit(1)
apiRequestOptions =
json: true
url: apiAsset.browser_download_url
request apiRequestOptions, (error, response, atomApi) ->
if error?
console.error(error.message)
return process.exit(1)
{classes} = atomApi
publicClasses = {}
for name, {instanceProperties, instanceMethods} of classes
pluckPropertyAttributes = convertPropertyToSuggestion.bind(this, name)
pluckMethodAttributes = convertMethodToSuggestion.bind(this, name)
properties = instanceProperties.filter(isVisible).map(pluckPropertyAttributes).sort(textComparator)
methods = instanceMethods.filter(isVisible).map(pluckMethodAttributes).sort(textComparator)
if properties?.length > 0 or methods.length > 0
publicClasses[name] = properties.concat(methods)
fs.writeFileSync('completions.json', JSON.stringify(publicClasses, null, ' '))
isVisible = ({visibility}) ->
visibility in ['Essential', 'Extended', 'Public']
convertMethodToSuggestion = (className, method) ->
{name, summary, returnValues} = method
args = method['arguments']
snippets = []
if args?.length
for arg, i in args
snippets.push("${#{i+1}:#{arg.name}}")
text = null
snippet = null
if snippets.length
snippet = "#{name}(#{snippets.join(', ')})"
else
text = "#{name}()"
returnValue = returnValues?[0]?.type
description = summary
descriptionMoreURL = getDocLink(className, name)
{name, text, snippet, description, descriptionMoreURL, leftLabel: returnValue, type: 'method'}
convertPropertyToSuggestion = (className, {name, summary}) ->
text = name
returnValue = summary?.match(/\{(\w+)\}/)?[1]
description = summary
descriptionMoreURL = getDocLink(className, name)
{name, text, description, descriptionMoreURL, leftLabel: returnValue, type: 'property'}
getDocLink = (className, instanceName) ->
"https://atom.io/docs/api/latest/#{className}#instance-#{instanceName}"
textComparator = (a, b) ->
return 1 if a.name > b.name
return -1 if a.name < b.name
0