-
Notifications
You must be signed in to change notification settings - Fork 208
/
publish-extension.js
282 lines (257 loc) · 13.3 KB
/
publish-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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/********************************************************************************
* Copyright (c) 2023 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 ovsx = require("ovsx");
const readVSIXPackage = require("@vscode/vsce/out/zip").readVSIXPackage;
const path = require("path");
const semver = require("semver");
const exec = require("./lib/exec");
const findUp = require("find-up");
const fg = require("fast-glob");
const { createVSIX } = require("@vscode/vsce");
const { cannotPublish } = require("./lib/reportStat");
const { PublicGalleryAPI } = require("@vscode/vsce/out/publicgalleryapi");
const { PublishedExtension } = require("azure-devops-node-api/interfaces/GalleryInterfaces");
const { artifactDirectory, registryHost, defaultPythonVersion } = require("./lib/constants");
const vscodeBuiltinExtensionsNamespace = "vscode";
const isBuiltIn = (id) => id.split(".")[0] === vscodeBuiltinExtensionsNamespace;
const openGalleryApi = new PublicGalleryAPI(`https://${registryHost}/vscode`, "3.0-preview.1");
openGalleryApi.client["_allowRetries"] = true;
openGalleryApi.client["_maxRetries"] = 5;
openGalleryApi.post = (url, data, additionalHeaders) =>
openGalleryApi.client.post(`${openGalleryApi.baseUrl}${url}`, data, additionalHeaders);
(async () => {
/**
* @type {{extension: import('./types').Extension, context: import('./types').PublishContext, extensions: Readonly<import('./types').Extensions>}}
*/
const { extension, context, extensions } = JSON.parse(process.argv[2]);
console.log(`\nProcessing extension: ${JSON.stringify({ extension, context }, undefined, 2)}`);
try {
const { id } = extension;
const [namespace] = id.split(".");
let packagePath = context.repo;
if (packagePath && extension.location) {
packagePath = path.join(packagePath, extension.location);
}
/** @type {import('ovsx').PublishOptions} */
let options;
if (context.file) {
options = { extensionFile: context.file, targets: [context.target] };
} else if (context.repo && context.ref) {
console.log(`${id}: preparing from ${context.repo}...`);
const [publisher, name] = extension.id.split(".");
process.env.EXTENSION_ID = extension.id;
process.env.EXTENSION_PUBLISHER = publisher;
process.env.EXTENSION_NAME = name;
process.env.VERSION = context.version;
process.env.MS_VERSION = context.msVersion;
process.env.OVSX_VERSION = context.ovsxVersion;
await exec(`git checkout ${context.ref}`, { cwd: context.repo });
try {
const nvmFile = await findUp(".nvmrc", { cwd: path.join(context.repo, extension.location ?? ".") });
if (nvmFile) {
// If the project has a preferred Node version, use it
await exec("source ~/.nvm/nvm.sh && nvm install", {
cwd: path.join(context.repo, extension.location ?? "."),
quiet: true,
});
}
if (extension.pythonVersion) {
console.debug("Installing appropriate Python version...");
await exec(
`pyenv install -s ${extension.pythonVersion} && pyenv global ${extension.pythonVersion}`,
{ cwd: path.join(context.repo, extension.location ?? "."), quiet: false },
);
}
} catch {}
if (extension.custom) {
try {
for (const command of extension.custom) {
await exec(command, { cwd: context.repo });
}
options = {
extensionFile: path.join(
context.repo,
extension.location ?? ".",
extension.extensionFile ?? "extension.vsix",
),
};
if (context.target) {
console.info(`Looking for a ${context.target} vsix package in ${context.repo}...`);
const vsixFiles = await fg(path.join(`*-${context.target}-*.vsix`), {
cwd: context.repo,
onlyFiles: true,
});
if (vsixFiles.length > 0) {
console.info(
`Found ${vsixFiles.length} ${context.target} vsix package(s) in ${context.repo}: ${vsixFiles.join(", ")}`,
);
options = {
extensionFile: path.join(context.repo, vsixFiles[0]),
targets: [context.target],
};
} else {
throw new Error(
`After running the custom commands, no .vsix file was found for ${extension.id}@${context.target}`,
);
}
}
} catch (e) {
throw e;
}
} else {
const yarn = await new Promise((resolve) => {
fs.access(path.join(context.repo, "yarn.lock"), (error) => resolve(!error));
});
try {
await exec(`${yarn ? "yarn" : "npm"} install`, { cwd: packagePath });
} catch (e) {
const pck = JSON.parse(await fs.promises.readFile(path.join(packagePath, "package.json"), "utf-8"));
// try to auto migrate from vscode: https://code.visualstudio.com/api/working-with-extensions/testing-extension#migrating-from-vscode
if (pck.scripts?.postinstall === "node ./node_modules/vscode/bin/install") {
delete pck.scripts["postinstall"];
pck.devDependencies = pck.devDependencies || {};
delete pck.devDependencies["vscode"];
pck.devDependencies["@types/vscode"] = pck.engines["vscode"];
const content = JSON.stringify(pck, undefined, 2).replace(
/node \.\/node_modules\/vscode\/bin\/compile/g,
"tsc",
);
await fs.promises.writeFile(path.join(packagePath, "package.json"), content, "utf-8");
await exec(`${yarn ? "yarn" : "npm"} install`, { cwd: packagePath });
} else {
throw e;
}
}
if (extension.prepublish) {
await exec(extension.prepublish, { cwd: context.repo });
}
if (extension.extensionFile) {
options = { extensionFile: path.join(context.repo, extension.extensionFile) };
} else {
options = { extensionFile: path.join(context.repo, "extension.vsix") };
if (yarn) {
options.yarn = true;
}
// answer y to all questions https://github.com/microsoft/vscode-vsce/blob/7182692b0f257dc10e7fc643269511549ca0c1db/src/util.ts#L12
const vsceTests = process.env["VSCE_TESTS"];
process.env["VSCE_TESTS"] = "1";
try {
await createVSIX({
cwd: packagePath,
packagePath: options.extensionFile,
baseContentUrl: options.baseContentUrl,
baseImagesUrl: options.baseImagesUrl,
useYarn: options.yarn,
target: context.target,
});
} finally {
process.env["VSCE_TESTS"] = vsceTests;
}
}
console.log(`${id}: prepared from ${context.repo}`);
}
}
// Check if the requested version is greater than the one on Open VSX.
const { xmlManifest, manifest } = options.extensionFile && (await readVSIXPackage(options.extensionFile));
context.version = xmlManifest?.PackageManifest?.Metadata[0]?.Identity[0]["$"]?.Version || manifest?.version;
if (!context.version) {
throw new Error(`${extension.id}: version is not resolved`);
}
if (context.ovsxVersion) {
if (semver.gt(context.ovsxVersion, context.version)) {
throw new Error(
`extensions.json is out-of-date: Open VSX version ${context.ovsxVersion} is already greater than specified version ${context.version}`,
);
}
if (semver.eq(context.ovsxVersion, context.version) && process.env.FORCE !== "true") {
console.log(`[SKIPPED] Requested version ${context.version} is already published on Open VSX`);
return;
}
}
// TODO(ak) check license is open-source
if (
!xmlManifest?.PackageManifest?.Metadata[0]?.License?.[0] &&
!manifest.license &&
!(packagePath && (await ovsx.isLicenseOk(packagePath, manifest)))
) {
throw new Error(`${extension.id}: license is missing`);
}
const { extensionDependencies } = manifest;
if (extensionDependencies) {
const extensionDependenciesNotBuiltin = extensionDependencies.filter((id) => !isBuiltIn(id));
const unpublishableDependencies = extensionDependenciesNotBuiltin.filter((dependency) =>
cannotPublish.includes(dependency),
);
if (unpublishableDependencies?.length > 0) {
throw new Error(
`${id} is dependent on ${unpublishableDependencies.join(", ")}, which ${unpublishableDependencies.length === 1 ? "has" : "have"} to be published to Open VSX first by ${unpublishableDependencies.length === 1 ? "its author because of its license" : "their authors because of their licenses"}.`,
);
}
const dependenciesNotOnOpenVsx = [];
for (const dependency of extensionDependenciesNotBuiltin) {
if (process.env.SKIP_PUBLISH && Object.keys(extensions).find((key) => key === dependency)) {
continue;
}
/** @type {[PromiseSettledResult<PublishedExtension | undefined>]} */
const [ovsxExtension] = await Promise.allSettled([openGalleryApi.getExtension(dependency)]);
if (ovsxExtension.status === "fulfilled" && !ovsxExtension.value) {
dependenciesNotOnOpenVsx.push(dependency);
}
}
if (dependenciesNotOnOpenVsx.length > 0) {
throw new Error(
`${id} is dependent on ${dependenciesNotOnOpenVsx.join(", ")}, which ${dependenciesNotOnOpenVsx.length === 1 ? "has" : "have"} to be published to Open VSX first`,
);
}
}
if (options.extensionFile && process.env.EXTENSIONS) {
console.info(`Copying file to ${artifactDirectory}`);
const outputFile = `${extension.id}${context.target ? `@${context.target}` : ""}.vsix`;
fs.copyFileSync(options.extensionFile, path.join("/tmp/artifacts/", outputFile));
}
if (process.env.SKIP_PUBLISH === "true") {
return;
}
console.log(`Attempting to publish ${id} to Open VSX`);
// Create a public Open VSX namespace if needed.
try {
await ovsx.createNamespace({ name: namespace });
} catch (error) {
console.log(`Creating Open VSX namespace failed -- assuming that it already exists`);
console.log(error);
}
console.info(`Publishing extension as ${options.targets ? options.targets.join(", ") : "universal"}`);
if (process.env.OVSX_PAT) {
await ovsx.publish(options);
console.log(`Published ${id} to https://${registryHost}/extension/${id.split(".")[0]}/${id.split(".")[1]}`);
} else {
console.error(
"The OVSX_PAT environment variable was not provided, which means the extension cannot be published. Provide it or set SKIP_PUBLISH to true to avoid seeing this.",
);
process.exitCode = 1;
}
} catch (error) {
if (error && String(error).indexOf("is already published.") !== -1) {
console.log(`Could not process extension -- assuming that it already exists`);
console.log(error);
} else {
console.error(`[FAIL] Could not process extension: ${JSON.stringify({ extension, context }, null, 2)}`);
console.error(error);
process.exitCode = 1;
}
} finally {
// Clean up
if (extension.pythonVersion) {
await exec(`pyenv global ${defaultPythonVersion}`);
}
}
})();