-
Notifications
You must be signed in to change notification settings - Fork 193
/
publish.ts
213 lines (175 loc) · 6.19 KB
/
publish.ts
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
import * as fs from 'fs';
import { ExtensionQueryFlags, PublishedExtension } from 'azure-devops-node-api/interfaces/GalleryInterfaces';
import { pack, readManifest, IPackage } from './package';
import * as tmp from 'tmp';
import { getPublisher } from './store';
import { getGalleryAPI, read, getPublishedUrl, log } from './util';
import { Manifest } from './manifest';
import * as denodeify from 'denodeify';
import * as yauzl from 'yauzl';
import * as semver from 'semver';
import * as cp from 'child_process';
const exec = denodeify<string, { cwd?: string; env?: any; }, { stdout: string; stderr: string; }>(cp.exec as any, (err, stdout, stderr) => [err, { stdout, stderr }]);
const tmpName = denodeify<string>(tmp.tmpName);
function readManifestFromPackage(packagePath: string): Promise<Manifest> {
return new Promise<Manifest>((c, e) => {
yauzl.open(packagePath, (err, zipfile) => {
if (err) {
return e(err);
}
const onEnd = () => e(new Error('Manifest not found'));
zipfile.once('end', onEnd);
zipfile.on('entry', entry => {
if (!/^extension\/package\.json$/i.test(entry.fileName)) {
return;
}
zipfile.removeListener('end', onEnd);
zipfile.openReadStream(entry, (err, stream) => {
if (err) {
return e(err);
}
const buffers = [];
stream.on('data', buffer => buffers.push(buffer));
stream.once('error', e);
stream.once('end', () => {
try {
c(JSON.parse(Buffer.concat(buffers).toString('utf8')));
} catch (err) {
e(err);
}
});
});
});
});
});
}
async function _publish(packagePath: string, pat: string, manifest: Manifest): Promise<void> {
const api = await getGalleryAPI(pat);
const packageStream = fs.createReadStream(packagePath);
const name = `${manifest.publisher}.${manifest.name}`;
const fullName = `${name}@${manifest.version}`;
console.log(`Publishing ${fullName}...`);
return api.getExtension(null, manifest.publisher, manifest.name, null, ExtensionQueryFlags.IncludeVersions)
.catch<PublishedExtension>(err => err.statusCode === 404 ? null : Promise.reject(err))
.then(extension => {
if (extension && extension.versions.some(v => v.version === manifest.version)) {
return Promise.reject(`${fullName} already exists. Version number cannot be the same.`);
}
var promise = extension
? api.updateExtension(undefined, packageStream, manifest.publisher, manifest.name)
: api.createExtension(undefined, packageStream);
return promise
.catch(err => Promise.reject(err.statusCode === 409 ? `${fullName} already exists.` : err))
.then(() => log.done(`Published ${fullName}\nYour extension will live at ${getPublishedUrl(name)} (might take a few minutes for it to show up).`));
})
.catch(err => {
const message = err && err.message || '';
if (/Invalid Resource/.test(message)) {
err.message = `${err.message}\n\nYou're likely using an expired Personal Access Token, please get a new PAT.\nMore info: https://aka.ms/vscodepat`;
}
return Promise.reject(err);
});
}
export interface IPublishOptions {
packagePath?: string;
version?: string;
commitMessage?: string;
cwd?: string;
pat?: string;
baseContentUrl?: string;
baseImagesUrl?: string;
useYarn?: boolean;
noVerify?: boolean;
ignoreFile?: string;
}
async function versionBump(cwd: string = process.cwd(), version?: string, commitMessage?: string): Promise<void> {
if (!version) {
return Promise.resolve(null);
}
const manifest = await readManifest(cwd);
if (manifest.version === version) {
return null;
}
switch (version) {
case 'major':
case 'minor':
case 'patch':
break;
case 'premajor':
case 'preminor':
case 'prepatch':
case 'prerelease':
case 'from-git':
return Promise.reject(`Not supported: ${version}`);
default:
if (!semver.valid(version)) {
return Promise.reject(`Invalid version ${version}`);
}
}
let command = `npm version ${version}`;
if (commitMessage) {
command = `${command} -m "${commitMessage}"`;
}
try {
// call `npm version` to do our dirty work
const { stdout, stderr } = await exec(command, { cwd });
process.stdout.write(stdout);
process.stderr.write(stderr);
return null;
} catch (err) {
throw err.message;
}
}
export function publish(options: IPublishOptions = {}): Promise<any> {
let promise: Promise<IPackage>;
if (options.packagePath) {
if (options.version) {
return Promise.reject(`Not supported: packagePath and version.`);
}
promise = readManifestFromPackage(options.packagePath)
.then(manifest => ({ manifest, packagePath: options.packagePath }));
} else {
const cwd = options.cwd;
const baseContentUrl = options.baseContentUrl;
const baseImagesUrl = options.baseImagesUrl;
const useYarn = options.useYarn;
const ignoreFile = options.ignoreFile;
promise = versionBump(options.cwd, options.version, options.commitMessage)
.then(() => tmpName())
.then(packagePath => pack({ packagePath, cwd, baseContentUrl, baseImagesUrl, useYarn, ignoreFile }));
}
return promise.then(({ manifest, packagePath }) => {
if (!options.noVerify && manifest.enableProposedApi) {
throw new Error('Extensions using proposed API (enableProposedApi: true) can\'t be published to the Marketplace');
}
const patPromise = options.pat
? Promise.resolve(options.pat)
: getPublisher(manifest.publisher).then(p => p.pat);
return patPromise.then(pat => _publish(packagePath, pat, manifest));
});
}
export interface IUnpublishOptions extends IPublishOptions {
id?: string;
force?: boolean;
}
export async function unpublish(options: IUnpublishOptions = {}): Promise<any> {
let publisher: string, name: string;
if (options.id) {
[publisher, name] = options.id.split('.');
} else {
const manifest = await readManifest(options.cwd);
publisher = manifest.publisher;
name = manifest.name;
}
const fullName = `${publisher}.${name}`;
if (!options.force) {
const answer = await read(`This will FOREVER delete '${fullName}'! Are you sure? [y/N] `);
if (!/^y$/i.test(answer)) {
throw new Error('Aborted');
}
}
const pat = options.pat || (await getPublisher(publisher).then(p => p.pat));
const api = await getGalleryAPI(pat);
await api.deleteExtension(publisher, name);
log.done(`Deleted extension: ${fullName}!`);
}