forked from EclipseFdn/publish-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-extension.js
executable file
·195 lines (174 loc) · 7.2 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
/********************************************************************************
* Copyright (c) 2022 Gitpod 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
********************************************************************************/
//
// Usage:
// node add-extension.js ext.id https://github.com/my-org/repo
// Optional extra arguments [see: extensions-schema.json]:
// node add-extension.js abusaidm.html-snippets2 https://github.com/my-org/repo --location 'packages/xy'
//
// @ts-check
const fs = require("fs");
const minimist = require("minimist");
const util = require("util");
const exec = require("./lib/exec");
const extensionsSchema = require("./extensions-schema.json");
const fetch = require("node-fetch");
const { getPublicGalleryAPI } = require("@vscode/vsce/out/util");
const parseXmlManifest = require("@vscode/vsce/out/xml").parseXmlManifest;
const { ExtensionQueryFlags, PublishedExtension } = require("azure-devops-node-api/interfaces/GalleryInterfaces");
const flags = [
ExtensionQueryFlags.IncludeMetadata,
ExtensionQueryFlags.IncludeAssetUri,
ExtensionQueryFlags.IncludeFiles,
ExtensionQueryFlags.IncludeLatestVersionOnly,
];
const msGalleryApi = getPublicGalleryAPI();
msGalleryApi.client["_allowRetries"] = true;
msGalleryApi.client["_maxRetries"] = 5;
const getRepositoryFromMarketplace = async (/** @type {string} */ id) => {
/** @type {[PromiseSettledResult<PublishedExtension | undefined>]} */
let [msExtension] = await Promise.allSettled([msGalleryApi.getExtension(id, flags)]);
if (msExtension.status === "fulfilled") {
const vsixManifest =
msExtension.value?.versions &&
msExtension.value?.versions[0].files?.find(
(file) => file.assetType === "Microsoft.VisualStudio.Services.VsixManifest",
)?.source;
const response = await fetch(vsixManifest);
const data = await parseXmlManifest(await response.text());
const url = new URL(
data.PackageManifest.Metadata[0].Properties[0].Property.find(
(property) => property.$.Id === "Microsoft.VisualStudio.Services.Links.Source",
).$.Value,
);
if (url.host === "github.com") {
return url.toString().replace(".git", "");
}
return url.toString();
}
};
(async () => {
// Parse args
const argv = minimist(process.argv.slice(2)); // without executable & script path
// Check positional args
if (argv._.length === 0) {
console.error("Need two positional arguments: ext-id, repo-url or a Microsoft Marketplace URL");
process.exit(1);
}
let [extID, repoURL] = argv._;
try {
const urlObject = new URL(extID);
if (urlObject.host === "marketplace.visualstudio.com") {
const id = urlObject.searchParams.get("itemName");
if (!id) {
console.error(`Couldn\'t get the extension ID from ${extID}`);
process.exit(1);
} else {
extID = id;
const url = await getRepositoryFromMarketplace(id);
if (!url) {
console.error(`Couldn\'t get the repository URL for ${extID}`);
process.exit(1);
} else {
repoURL = url;
}
}
}
} catch {
} finally {
if (argv._.length < 2 && !repoURL) {
console.error(
"Need two positional arguments: ext-id, repo-url, since the provided argument is not a Marketplace URL",
);
process.exit(1);
}
}
const extDefinition = {
repository: repoURL,
};
// Validate extra args
delete argv._; // delete positional arguments
for (const arg of Object.keys(argv)) {
const propDef = extensionsSchema.additionalProperties.properties[arg];
// console.debug(`arg=${arg}:`, argv[arg], propDef)
if (!propDef) {
console.error(`argument '${arg}' not found in ./extensions-schema.json`);
process.exit(1);
}
// parse & validate value
if (propDef.type === "string") {
extDefinition[arg] = String(argv[arg]); // minimist might've assumed a different type (e.g. number)
} else if (propDef.type === "number") {
if (typeof argv[arg] !== "number") {
console.error(
`argument '${arg}' should be type '${propDef.type}' but yours seems to be '${typeof argv[arg]}'`,
);
process.exit(1);
}
extDefinition[arg] = argv[arg]; // numbers are parsed by minimist already
} else {
console.error(
`argument '${arg}' is of type '${propDef.type}' which is not implemented by this script, sorry`,
);
process.exit(1);
}
}
console.info("Adding extension:", util.inspect(extDefinition, { colors: true, compact: false }));
// Read current file
const extensions = Object.entries(
JSON.parse(await fs.promises.readFile("./extensions.json", { encoding: "utf8" })),
);
// Sort extensions (most are, but not always)
extensions.sort(([k1], [k2]) => k1.localeCompare(k2));
const originalList = JSON.stringify(Object.fromEntries(extensions), undefined, 2);
// Find position & insert extension
for (let i = 0; i < extensions.length; i++) {
const [currentID] = extensions[i];
// console.debug(i, currentID)
const diff = currentID.localeCompare(extID, undefined, { sensitivity: "base" });
if (diff === 0) {
console.error("Extension already defined:", currentID);
process.exit(1);
}
if (diff > 0) {
extensions.splice(i, 0, [extID, extDefinition]);
break;
}
}
// Persist changes
await fs.promises.writeFile(
"./extensions.json",
JSON.stringify(Object.fromEntries(extensions), undefined, 2) + "\n", // add newline at EOF
{ encoding: "utf8" },
);
console.info(`Successfully added ${extID}`);
if (process.env.TEST_EXTENSION === "false") {
console.info("Skipping tests, TEST_EXTENSION was provided.");
process.exit(0);
}
console.info(`Trying to build ${extID} for the first time`);
process.env.EXTENSIONS = extID;
process.env.FORCE = "true";
process.env.SKIP_PUBLISH = "true";
const out = await exec("node publish-extensions", { quiet: true, ghtoken: true });
if (out && out.stderr.includes("[FAIL] Could not process extension:")) {
console.error(
`There was an error while trying to build ${extID}. Reverting back to the previous state of extensions.json.`,
);
await fs.promises.writeFile(
"./extensions.json",
originalList + "\n", // add newline at EOF
{ encoding: "utf8" },
);
} else {
console.info("Built extension successfully");
console.info(`Feel free to use the message below for your commit:\r\nAdded \`${extID}\``);
}
})();