-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathplugin-github-resolver.ts
148 lines (125 loc) · 5.66 KB
/
plugin-github-resolver.ts
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
// *****************************************************************************
// Copyright (C) 2018 Red Hat, Inc. and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// *****************************************************************************
import { injectable } from '@theia/core/shared/inversify';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as request from 'request';
import { PluginDeployerResolver, PluginDeployerResolverContext } from '../../common';
/**
* Resolver that handle the github: protocol
* github:<org>/<repo>/<filename>@latest
* github:<org>/<repo>/<filename>@<version>
*/
@injectable()
export class GithubPluginDeployerResolver implements PluginDeployerResolver {
private static PREFIX = 'github:';
private static GITHUB_ENDPOINT = 'https://github.com/';
private unpackedFolder: string;
constructor() {
this.unpackedFolder = path.resolve(os.tmpdir(), 'github-remote');
if (!fs.existsSync(this.unpackedFolder)) {
fs.mkdirSync(this.unpackedFolder);
}
}
/**
* Grab the remote file specified by Github URL
*/
async resolve(pluginResolverContext: PluginDeployerResolverContext): Promise<void> {
// download the file
return new Promise<void>((resolve, reject) => {
// extract data
const extracted = /^github:(.*)\/(.*)\/(.*)$/gm.exec(pluginResolverContext.getOriginId());
if (!extracted || extracted === null || extracted.length !== 4) {
reject(new Error('Invalid extension' + pluginResolverContext.getOriginId()));
return;
}
const orgName = extracted[1];
const repoName = extracted[2];
const file = extracted[3];
// get version if any
const splitFile = file.split('@');
let version;
let filename: string;
if (splitFile.length === 1) {
filename = file;
version = 'latest';
} else {
filename = splitFile[0];
version = splitFile[1];
}
// latest version, need to get the redirect
const url = GithubPluginDeployerResolver.GITHUB_ENDPOINT + orgName + '/' + repoName + '/releases/latest';
// disable redirect to grab the release
const options = {
followRedirect: false
};
// if latest, resolve first the real version
if (version === 'latest') {
request.get(url, options).on('response', response => {
// should have a redirect
if (response.statusCode === 302) {
const redirectLocation = response.headers.location;
if (!redirectLocation) {
reject(new Error('Invalid github link with latest not being found'));
return;
}
// parse redirect link
const taggedValueArray = /^https:\/\/.*tag\/(.*)/gm.exec(redirectLocation);
if (!taggedValueArray || taggedValueArray.length !== 2) {
reject(new Error('The redirect link for latest is invalid ' + redirectLocation));
return;
}
// grab version of tag
this.grabGithubFile(pluginResolverContext, orgName, repoName, filename, taggedValueArray[1], resolve, reject);
}
});
} else {
this.grabGithubFile(pluginResolverContext, orgName, repoName, filename, version, resolve, reject);
}
});
}
/*
* Grab the github file specified by the plugin's ID
*/
protected grabGithubFile(pluginResolverContext: PluginDeployerResolverContext, orgName: string, repoName: string, filename: string, version: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
resolve: (value?: void | PromiseLike<void>) => void, reject: (reason?: any) => void): void {
const unpackedPath = path.resolve(this.unpackedFolder, path.basename(version + filename));
const finish = () => {
pluginResolverContext.addPlugin(pluginResolverContext.getOriginId(), unpackedPath);
resolve();
};
// use of cache. If file is already there use it directly
if (fs.existsSync(unpackedPath)) {
finish();
return;
}
const dest = fs.createWriteStream(unpackedPath);
dest.addListener('finish', finish);
const url = GithubPluginDeployerResolver.GITHUB_ENDPOINT + orgName + '/' + repoName + '/releases/download/' + version + '/' + filename;
request.get(url)
.on('error', err => {
reject(err);
}).pipe(dest);
}
/**
* Handle only the plugins that starts with github:
*/
accept(pluginId: string): boolean {
return pluginId.startsWith(GithubPluginDeployerResolver.PREFIX);
}
}