-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathpublish.ts
220 lines (187 loc) · 7.05 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
214
215
216
217
218
219
220
import * as fs from 'fs';
import { promisify } from 'util';
import * as semver from 'semver';
import { ExtensionQueryFlags, PublishedExtension } from 'azure-devops-node-api/interfaces/GalleryInterfaces';
import { pack, readManifest, versionBump, prepublish } from './package';
import * as tmp from 'tmp';
import { getPublisher } from './store';
import { getGalleryAPI, read, getPublishedUrl, log, getHubUrl, patchOptionsWithManifest } from './util';
import { Manifest } from './manifest';
import { readVSIXPackage } from './zip';
import { validatePublisher } from './validation';
const tmpName = promisify(tmp.tmpName);
export interface IPublishOptions {
readonly packagePath?: string[];
readonly version?: string;
readonly targets?: string[];
readonly commitMessage?: string;
readonly gitTagVersion?: boolean;
readonly updatePackageJson?: boolean;
readonly cwd?: string;
readonly githubBranch?: string;
readonly gitlabBranch?: string;
readonly baseContentUrl?: string;
readonly baseImagesUrl?: string;
readonly useYarn?: boolean;
readonly dependencyEntryPoints?: string[];
readonly ignoreFile?: string;
readonly pat?: string;
readonly noVerify?: boolean;
readonly dependencies?: boolean;
readonly preRelease?: boolean;
readonly allowStarActivation?: boolean;
readonly allowMissingRepository?: boolean;
}
export async function publish(options: IPublishOptions = {}): Promise<any> {
if (options.packagePath) {
if (options.version) {
throw new Error(`Both options not supported simultaneously: 'packagePath' and 'version'.`);
} else if (options.targets) {
throw new Error(`Both options not supported simultaneously: 'packagePath' and 'target'.`);
}
for (const packagePath of options.packagePath) {
const vsix = await readVSIXPackage(packagePath);
let target: string | undefined;
try {
target = vsix.xmlManifest.PackageManifest.Metadata[0].Identity[0].$.TargetPlatform ?? undefined;
} catch (err) {
throw new Error(`Invalid extension VSIX manifest. ${err}`);
}
if (options.preRelease) {
let isPreReleasePackage = false;
try {
isPreReleasePackage = !!vsix.xmlManifest.PackageManifest.Metadata[0].Properties[0].Property.some(
p => p.$.Id === 'Microsoft.VisualStudio.Code.PreRelease'
);
} catch (err) {
throw new Error(`Invalid extension VSIX manifest. ${err}`);
}
if (!isPreReleasePackage) {
throw new Error(
`Cannot use '--pre-release' flag with a package that was not packaged as pre-release. Please package it using the '--pre-release' flag and publish again.`
);
}
}
await _publish(packagePath, vsix.manifest, { ...options, target });
}
} else {
const cwd = options.cwd || process.cwd();
const manifest = await readManifest(cwd);
patchOptionsWithManifest(options, manifest);
await prepublish(cwd, manifest, options.useYarn);
await versionBump(options);
if (options.targets) {
for (const target of options.targets) {
const packagePath = await tmpName();
const packageResult = await pack({ ...options, target, packagePath });
await _publish(packagePath, packageResult.manifest, { ...options, target });
}
} else {
const packagePath = await tmpName();
const packageResult = await pack({ ...options, packagePath });
await _publish(packagePath, packageResult.manifest, options);
}
}
}
export interface IInternalPublishOptions {
readonly target?: string;
readonly pat?: string;
readonly noVerify?: boolean;
}
async function _publish(packagePath: string, manifest: Manifest, options: IInternalPublishOptions) {
validatePublisher(manifest.publisher);
if (!options.noVerify && manifest.enableProposedApi) {
throw new Error("Extensions using proposed API (enableProposedApi: true) can't be published to the Marketplace");
}
if (!options.noVerify && manifest.enabledApiProposals) {
throw new Error("Extensions using proposed API (enabledApiProposals: [...]) can't be published to the Marketplace");
}
if (semver.prerelease(manifest.version)) {
throw new Error(`The VS Marketplace doesn't support prerelease versions: '${manifest.version}'`);
}
const pat = options.pat ?? (await getPublisher(manifest.publisher)).pat;
const api = await getGalleryAPI(pat);
const packageStream = fs.createReadStream(packagePath);
const name = `${manifest.publisher}.${manifest.name}`;
const description = options.target
? `${name} (${options.target}) v${manifest.version}`
: `${name} v${manifest.version}`;
log.info(`Publishing '${description}'...`);
let extension: PublishedExtension | null = null;
try {
try {
extension = await api.getExtension(
null,
manifest.publisher,
manifest.name,
undefined,
ExtensionQueryFlags.IncludeVersions
);
} catch (err: any) {
if (err.statusCode !== 404) {
throw err;
}
}
if (extension && extension.versions) {
const sameVersion = extension.versions.filter(v => v.version === manifest.version);
if (sameVersion.length > 0) {
if (!options.target) {
throw new Error(`${description} already exists.`);
}
if (sameVersion.some(v => !v.targetPlatform)) {
throw new Error(`${name} (no target) v${manifest.version} already exists.`);
}
if (sameVersion.some(v => v.targetPlatform === options.target)) {
throw new Error(`${description} already exists.`);
}
}
try {
await api.updateExtension(undefined, packageStream, manifest.publisher, manifest.name);
} catch (err: any) {
if (err.statusCode === 409) {
throw new Error(`${description} already exists.`);
} else {
throw err;
}
}
} else {
await api.createExtension(undefined, packageStream);
}
} catch (err: any) {
const message = (err && err.message) || '';
if (/Personal Access Token used has expired/.test(message)) {
err.message = `${err.message}\n\nYou're using an expired Personal Access Token, please get a new PAT.\nMore info: https://aka.ms/vscodepat`;
} else 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`;
}
throw err;
}
log.info(`Extension URL (might take a few minutes): ${getPublishedUrl(name)}`);
log.info(`Hub URL: ${getHubUrl(manifest.publisher, manifest.name)}`);
log.done(`Published ${description}.`);
}
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)).pat;
const api = await getGalleryAPI(pat);
await api.deleteExtension(publisher, name);
log.done(`Deleted extension: ${fullName}!`);
}