Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Extracts the version SHA from the manifest URL
* @param jsonManifest JSON object representing the manifest.
* expecting to have a 'url' at root level of format:
* "https://sourcegraph.com/-/static/extension/13212-sourcegraph-verilog.js?cf8s0z1vs41c--sourcegraph-verilog"
* @return string version
Comment on lines +5 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: the number 13212 here seems to be the "version" (release ID), while the string we use (cf8s0z1vs41c) is the timestamp at release. For our purpose, I think these can be used interchangeably.

*/
function getExtensionVersion(jsonManifest) {
const url = new URL(jsonManifest['url']);
// .search.substr(1) to remove the '?'
const version = url.search.substr(1);
// now remove the extension ID (everything after --)
return version.substr(0, version.indexOf('--'));
}

module.exports = { getExtensionVersion }
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require("fs");
const https = require("https");
const common = require("./common");

(async function main() {
const extensionIDs = getExtensionIDs();
Expand Down Expand Up @@ -144,15 +145,21 @@ function createBundlesDirectory(extensions) {
// Clone customer instructions and publish script.
fs.copyFileSync("./to-clone/instructions.md", `${bundlesPath}/README.md`);
fs.copyFileSync("./to-clone/publish.js", `${bundlesPath}/publish.js`);
fs.copyFileSync("./common.js", `${bundlesPath}/common.js`);

// Create extension directories (to be used by publish script).
for (const { extensionID, manifest, bundle } of extensions) {
const extensionFileName = extensionID.replace("/", "-");

// get the version
// assumption: URL is in format:
// "url": "https://sourcegraph.com/-/static/extension/13212-sourcegraph-verilog.js?cf8s0z1vs41c--sourcegraph-verilog"
const version = common.getExtensionVersion(JSON.parse(manifest));

fs.mkdirSync(`${bundlesPath}/${extensionFileName}`);

fs.writeFileSync(
`${bundlesPath}/${extensionFileName}/${extensionFileName}.js`,
`${bundlesPath}/${extensionFileName}/${extensionFileName}.${version}.js`,
bundle,
"utf8"
);
Expand Down
29 changes: 16 additions & 13 deletions to-clone/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs");
const https = require("https");
const http = require("http");
const path = require("path");
const common = require("./common");

const PUBLISHER = process.env.PUBLISHER;
if (!PUBLISHER) {
Expand All @@ -24,7 +25,7 @@ const sgurl = new URL(SRC_ENDPOINT);
})();

function determineExtensionDirectories() {
const fixedFiles = ["package.json", "publish.js", "README.md"];
const fixedFiles = ["package.json", "publish.js", "common.js", "README.md"];

const files = fs.readdirSync(__dirname);
return files.filter((file) => !fixedFiles.includes(file));
Expand Down Expand Up @@ -56,13 +57,15 @@ async function publishExtension(extensionDirectory) {
path.join(fullPathToExtensions, `package.json`),
"utf-8"
);
const jsonManifest = JSON.parse(manifest);
const version = common.getExtensionVersion(jsonManifest);
const bundle = await fs.promises.readFile(
path.join(fullPathToExtensions, `${extensionDirectory}.js`),
path.join(fullPathToExtensions, `${extensionDirectory}.${version}.js`),
"utf-8"
);

// Parse manifest to find extension name.
const { name } = JSON.parse(manifest);
const { name } = jsonManifest;
const extensionID = `${PUBLISHER}/${name}`;

// Make POST request to publish extension to registry.
Expand Down Expand Up @@ -98,7 +101,7 @@ async function publishExtension(extensionDirectory) {
{
hostname: sgurl.hostname,
port: sgurl.port || undefined,
path: "/.api/graphql",
path: "/.api/graphql?PublishExtension",
method: "POST",
headers: {
Authorization: `token ${SRC_ACCESS_TOKEN}`,
Expand All @@ -121,15 +124,15 @@ async function publishExtension(extensionDirectory) {
});

try {
if (typeof JSON.parse(result) === "string") {
// Could be an error message (e.g. "Private mode requires authentication")
console.log(result);
if (typeof result == 'object' && 'errors' in result && result['errors'].length > 0) {
console.log(`Unable to publish ${extensionID} from directory ${extensionDirectory}`);
result['errors'].forEach(err => console.log(err.message));
} else {
console.log(
`Published extension ${extensionID} from directory: ${extensionDirectory}`
);
}
} catch {
// noop
} catch (e) {
console.log(e.stack);
}

console.log(
`Published extension ${extensionID} from directory: ${extensionDirectory}`
);
}