forked from open-vsx/publish-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-extension.js
209 lines (191 loc) · 7.86 KB
/
add-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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/********************************************************************************
* Copyright (c) 2020 TypeFox 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.
*
* SPDX-License-Identifier: EPL-2.0
********************************************************************************/
// @ts-check
const fs = require('fs');
const minimist = require('minimist');
const ovsx = require('ovsx');
const path = require('path');
const util = require('util');
const semver = require('semver');
const exec = require('./lib/exec');
const gitHubScraper = require('./lib/github-scraper');
const { DH_UNABLE_TO_CHECK_GENERATOR } = require('constants');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
(async () => {
/**
* @type {{
* extensions: {
* id: string,
* repository: string,
* version?: string,
* checkout?: string,
* location?: string,
* prepublish?: string,
* extensionFile?: string,
* download?: string
* }[]
* }}
*/
const { extensions } = JSON.parse(await readFile('./extensions.json', 'utf-8'));
const registry = new ovsx.Registry();
const argv = minimist(process.argv.slice(2));
if (argv._.length !== (!!argv.download ? 0 : 1)) {
console.log(`Usage: node add-extension REPOSITORY [OPTIONS]
OPTIONS:
--checkout=CHECKOUT
--location=LOCATION
--prepublish=PREPUBLISH
--extensionFile=EXTENSION_FILE
Alternative usage: node add-extension --download=VSIX_URL`);
process.exitCode = 1;
process.exit();
}
const repository = (argv._[0] || '').replace(/(\.git)?\/*$/, '');
// If possible, always prefer re-publishing an official VSIX release over trying to re-package ourselves.
if (repository && !argv.download) {
const latestVSIXRelease = await gitHubScraper.findLatestVSIXRelease(repository);
if (latestVSIXRelease) {
// Simulate a 'node add-extension --download=VSIX_URL' CLI call.
argv.download = latestVSIXRelease;
delete argv.checkout;
delete argv.location;
delete argv.prepublish;
delete argv.extensionFile;
}
}
// Handle 'node add-extension --download=VSIX_URL':
if (argv.download) {
try {
await exec('mkdir -p /tmp/vsix');
await exec(`wget -O extension.vsix ${argv.download}`, { cwd: '/tmp/vsix' });
await exec('unzip -q extension.vsix', { cwd: '/tmp/vsix' });
/** @type {{ publisher: string, name: string, version: string }} */
const package = JSON.parse(await readFile('/tmp/vsix/extension/package.json', 'utf-8'));
await ensureNotAlreadyOnOpenVSX(package, registry);
const extension = { id: `${package.publisher}.${package.name}`, download: argv.download, version: package.version };
await addNewExtension(extension, extensions);
} catch (error) {
console.error(`[FAIL] Could not add ${argv.download}!`);
console.error(error);
process.exitCode = -1;
} finally {
await exec('rm -rf /tmp/vsix');
process.exit();
}
}
// Handle 'node add-extension REPOSITORY [OPTIONS]':
const existing = extensions.find(e => e.repository && e.repository.toLowerCase() === repository.toLowerCase() && e.location === argv.location);
if (existing) {
console.log(`[SKIPPED] Repository already in extensions.json: ${JSON.stringify(existing, null, 2)}`);
return;
}
try {
if (!new URL(repository)) {
throw new Error(`Invalid repository URL: ${repository}`);
}
// Clone the repository to determine the extension's latest version.
await exec(`git clone --recurse-submodules ${repository} /tmp/repository`);
if (typeof argv.checkout === 'string') {
// Check out the specified Git branch, tag, or commit.
await exec(`git checkout ${argv.checkout}`, { cwd: '/tmp/repository' });
}
// Locate and parse package.json.
let location = argv.location;
if (!location) {
const { stdout: files } = await exec('ls package.json 2>/dev/null || git ls-files | grep package\\.json', { cwd: '/tmp/repository' });
if (!files.trim()) {
throw new Error(`No package.json found in repository!`);
}
const locations = files.trim().split('\n');
if (locations.length > 1) {
console.warn(`[WARNING] Multiple package.json found in repository, arbitrarily using the first one:\n> ${locations[0]}\n${locations.slice(1).map(l => ' ' + l).join('\n')}`);
}
location = path.dirname(locations[0]);
}
const packagePath = path.join('/tmp/repository', location, 'package.json')
/** @type {{ publisher: string, name: string, version: string }} */
const package = JSON.parse(await readFile(packagePath, 'utf-8'));
if (registry.requiresLicense && !(await ovsx.isLicenseOk(path.dirname(packagePath), package))) {
throw new Error(`License must be present, please ask author of extension to add license (${repository})`)
} else {
ovsx.validateManifest(package)
}
// Check whether the extension is already published on Open VSX.
await ensureNotAlreadyOnOpenVSX(package, registry);
// If --checkout is passed without a value, try to find an appropriate-looking release tag if available.
if (argv.checkout === true) {
// Non-failing grep, source: https://unix.stackexchange.com/a/330662
const { stdout: releaseTags } = await exec(`git tag | { grep ${package.version} || true; }`, { cwd: '/tmp/repository' });
const releaseTag = releaseTags.split('\n')[0].trim();
if (releaseTag) {
argv.checkout = releaseTag;
} else {
delete argv.checkout;
}
}
// Add extension to the list.
const extension = { id: `${package.publisher}.${package.name}`, repository, version: package.version };
if (argv.checkout) {
extension.checkout = argv.checkout;
} else {
// No need to pin a specific version if we're not also using "checkout".
delete extension.version;
}
if (location !== '.') {
extension.location = location;
}
if (argv.prepublish) {
extension.prepublish = argv.prepublish;
}
if (argv.extensionFile) {
extension.extensionFile = argv.extensionFile;
}
await addNewExtension(extension, extensions);
} catch (error) {
console.error(`[FAIL] Could not add ${repository}!`);
console.error(error);
process.exitCode = -1;
} finally {
await exec('rm -rf /tmp/repository');
}
})();
async function ensureNotAlreadyOnOpenVSX(package, registry) {
const id = `${package.publisher}.${package.name}`;
let metadata;
try {
metadata = await registry.getMetadata(package.publisher, package.name);
} catch (error) {
console.warn(`[WARNING] Could not check Open VSX version of ${id}:`);
console.warn(error);
return;
}
if (metadata.error) {
console.warn(`[WARNING] Could not check Open VSX version of ${id}:`);
console.warn(metadata.error);
} else if (metadata.version) {
if (semver.gt(metadata.version, package.version)) {
throw new Error(`Open VSX already has a more recent version of ${id}: ${metadata.version} > ${package.version}`);
}
console.warn(`[WARNING] Open VSX already has ${id} in version ${metadata.version}. Adding ${package.version} here anyway.`);
}
}
async function addNewExtension(extension, extensions) {
extensions.push(extension);
// Sort extensions alphabetically by ID (not case-sensitive).
extensions.sort((a, b) => {
if (b.id.toLowerCase() > a.id.toLowerCase()) return -1;
if (b.id.toLowerCase() < a.id.toLowerCase()) return 1;
return 0;
});
// Save new extensions list.
await writeFile('./extensions.json', JSON.stringify({ extensions }, null, 2) + '\n', 'utf-8');
console.log(`[OK] Succesfully added new extension: ${JSON.stringify(extension, null, 2)}`);
}