-
Notifications
You must be signed in to change notification settings - Fork 3
/
extension.js
152 lines (120 loc) · 3.87 KB
/
extension.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict'
const vscode = require('vscode')
const RecentLibraries = require('./RecentLibraries')
const Cache = require('vscode-cache')
const search = require('./lib/search')
const statusMessage = require('./lib/statusMessage')
const showSearchInput = require('./lib/showSearchInput')
const showLibraryPicker = require('./lib/showLibraryPicker')
const getLibrary = require('./lib/getLibrary')
const showLibraryVersionPicker = require('./lib/showLibraryVersionPicker')
const showFilePicker = require('./lib/showFilePicker')
const showActionPicker = require('./lib/showActionPicker')
const settings = require('./settings')
let activate = context => {
// Save the context for use in other modules
settings.context = context
// Recent Libraries interface
let recentLibraries = new RecentLibraries(context, vscode.workspace)
// Cache interfaces
let searchCache = new Cache(context, 'search')
let libraryCache = new Cache(context, 'library')
vscode.commands.registerCommand('cdnjs.search', async () => {
// Get a search term
let term = await showSearchInput()
if (!term) {
return
}
// Perform the search on the API
let results = await search(term)
if (results.length === false) {
return
}
// Pick a library from the search results
let library = await showLibraryPicker(results)
if (library === false) {
return
}
// Fetch the library information from the API
library = await getLibrary(library.name)
// Pick a version from the library versions
let asset = await showLibraryVersionPicker(library)
if (!asset) {
return
}
// Add the library version to the list of recent libraries
recentLibraries.add(asset)
// Pick the file
let file = await showFilePicker(asset)
if (!file) {
return
}
let chosenFile = {
library: library.name,
version: asset.version,
file: file,
sri: asset.sri[file]
}
// Pick the action to take on the file
showActionPicker(chosenFile)
})
vscode.commands.registerCommand('cdnjs.recentLibraries', async () => {
// No Recent Libraries found
if (recentLibraries.get().length < 1) {
// Offer search instead
let value = await vscode.window.showInformationMessage('cdnjs: No Recent Libraries. Do you want to search instead?', 'Yes', 'No')
if (value === 'Yes') {
vscode.commands.executeCommand('cdnjs.search')
}
return
}
let chosen = {}
// Build array of recent libraries
let items = []
for (let library of recentLibraries.get()) {
items.push({
label: library.libraryName + '/' + library.version,
asset: library
})
}
// Clear recent libraries command
items.push({
label: 'Clear recent libraries list',
clear: true
})
// Show quick pick of recent libraries
let value = await vscode.window.showQuickPick(items, {
placeHolder: 'Choose a recent library'
})
// No recent library was chosen
if (typeof (value) === 'undefined') {
console.error(`No library was chosen`)
return
}
// Clear recent libraries list
if (value.clear === true) {
recentLibraries.clear()
return true
}
let asset = value.asset
// Set the chosen file's library and version'
chosen.library = asset.libraryName
chosen.version = asset.version
recentLibraries.add(asset)
// Pick the file from the library version
let file = await showFilePicker(asset)
chosen.file = file
chosen.sri = asset.sri[file]
showActionPicker(chosen)
})
vscode.commands.registerCommand('cdnjs.clearCache', () => {
// Clear the search cache
searchCache.flush()
// Clear the library cache
libraryCache.flush()
statusMessage('Cache has been cleared')
})
}
exports.activate = activate
let deactivate = () => { }
exports.deactivate = deactivate