/
publish-extensions.js
96 lines (86 loc) · 3.7 KB
/
publish-extensions.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
/********************************************************************************
* 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 ovsx = require('ovsx');
const path = require('path');
const util = require('util');
const semver = require('semver');
const exec = require('./lib/exec');
const readFile = util.promisify(fs.readFile);
(async () => {
/** @type {{ extensions: { id: string, repository: string, version?: string, checkout?: string, location?: string, prepublish?: string }[] }} */
const { extensions } = JSON.parse(await readFile('./extensions.json', 'utf-8'));
const registry = new ovsx.Registry();
// Also install extensions' devDependencies when using `npm install` or `yarn install`.
process.env.NODE_ENV = 'development';
for (const extension of extensions) {
console.log(`\nProcessing extension: ${JSON.stringify(extension, null, 2)}`);
try {
const { id, repository } = extension;
if (!new URL(repository)) {
throw new Error(`Invalid repository URL: ${repository}`);
}
console.log(`Checking Open VSX version of ${id}`);
let ovsxVersion;
const [namespace, name] = id.split('.');
const metadata = await registry.getMetadata(namespace, name);
if (metadata.error) {
console.error(metadata.error);
} else {
console.log(`Found version: ${metadata.version}`);
ovsxVersion = metadata.version;
}
// Check if the requested version is greater than the one on Open VSX.
if (ovsxVersion && extension.version) {
if (semver.gt(ovsxVersion, extension.version)) {
throw new Error(`extensions.json is out-of-date: Open VSX version ${ovsxVersion} is already greater than specified version ${extension.version}`);
}
if (semver.eq(ovsxVersion, extension.version)) {
console.log(`[SKIPPED] Requested version ${extension.version} is already published on Open VSX`);
continue;
}
}
console.log(`Attempting to publish ${id} to Open VSX`);
// Clone and set up the repository.
await exec(`git clone --recurse-submodules ${repository} /tmp/repository`);
if (extension.checkout) {
await exec(`git checkout ${extension.checkout}`, { cwd: '/tmp/repository' });
}
let yarn = await new Promise(resolve => {
fs.access(path.join('/tmp/repository', 'yarn.lock'), error => resolve(!error));
});
await exec(`${yarn ? 'yarn' : 'npm'} install`, { cwd: '/tmp/repository' });
if (extension.prepublish) {
await exec(extension.prepublish, { cwd: '/tmp/repository' })
}
// 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);
}
// Publish the extension.
const options = { packagePath: path.join('/tmp/repository', extension.location || '.') };
if (yarn) {
options.yarn = true;
}
await ovsx.publish(options);
console.log(`[OK] Successfully published ${id} to Open VSX!`)
} catch (error) {
console.error(`[FAIL] Could not process extension: ${JSON.stringify(extension, null, 2)}`);
console.error(error);
process.exitCode = -1;
} finally {
await exec('rm -rf /tmp/repository');
}
}
})();