UNPKG

8.61 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22exports.unpublish = exports.publish = void 0;
23const fs = __importStar(require("fs"));
24const util_1 = require("util");
25const semver = __importStar(require("semver"));
26const GalleryInterfaces_1 = require("azure-devops-node-api/interfaces/GalleryInterfaces");
27const package_1 = require("./package");
28const tmp = __importStar(require("tmp"));
29const store_1 = require("./store");
30const util_2 = require("./util");
31const zip_1 = require("./zip");
32const validation_1 = require("./validation");
33const tmpName = (0, util_1.promisify)(tmp.tmpName);
34async function publish(options = {}) {
35 if (options.packagePath) {
36 if (options.version) {
37 throw new Error(`Both options not supported simultaneously: 'packagePath' and 'version'.`);
38 }
39 else if (options.targets) {
40 throw new Error(`Both options not supported simultaneously: 'packagePath' and 'target'. Use 'vsce package --target <target>' to first create a platform specific package, then use 'vsce publish --packagePath <path>' to publish it.`);
41 }
42 for (const packagePath of options.packagePath) {
43 const vsix = await (0, zip_1.readVSIXPackage)(packagePath);
44 let target;
45 try {
46 target = vsix.xmlManifest.PackageManifest.Metadata[0].Identity[0].$.TargetPlatform ?? undefined;
47 }
48 catch (err) {
49 throw new Error(`Invalid extension VSIX manifest. ${err}`);
50 }
51 if (options.preRelease) {
52 let isPreReleasePackage = false;
53 try {
54 isPreReleasePackage = !!vsix.xmlManifest.PackageManifest.Metadata[0].Properties[0].Property.some(p => p.$.Id === 'Microsoft.VisualStudio.Code.PreRelease');
55 }
56 catch (err) {
57 throw new Error(`Invalid extension VSIX manifest. ${err}`);
58 }
59 if (!isPreReleasePackage) {
60 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.`);
61 }
62 }
63 await _publish(packagePath, vsix.manifest, { ...options, target });
64 }
65 }
66 else {
67 const cwd = options.cwd || process.cwd();
68 const manifest = await (0, package_1.readManifest)(cwd);
69 (0, util_2.patchOptionsWithManifest)(options, manifest);
70 await (0, package_1.prepublish)(cwd, manifest, options.useYarn);
71 await (0, package_1.versionBump)(options);
72 if (options.targets) {
73 for (const target of options.targets) {
74 const packagePath = await tmpName();
75 const packageResult = await (0, package_1.pack)({ ...options, target, packagePath });
76 await _publish(packagePath, packageResult.manifest, { ...options, target });
77 }
78 }
79 else {
80 const packagePath = await tmpName();
81 const packageResult = await (0, package_1.pack)({ ...options, packagePath });
82 await _publish(packagePath, packageResult.manifest, options);
83 }
84 }
85}
86exports.publish = publish;
87async function _publish(packagePath, manifest, options) {
88 (0, validation_1.validatePublisher)(manifest.publisher);
89 if (!options.noVerify && manifest.enableProposedApi) {
90 throw new Error("Extensions using proposed API (enableProposedApi: true) can't be published to the Marketplace");
91 }
92 if (!options.noVerify && manifest.enabledApiProposals) {
93 throw new Error("Extensions using proposed API (enabledApiProposals: [...]) can't be published to the Marketplace");
94 }
95 if (semver.prerelease(manifest.version)) {
96 throw new Error(`The VS Marketplace doesn't support prerelease versions: '${manifest.version}'`);
97 }
98 const pat = options.pat ?? (await (0, store_1.getPublisher)(manifest.publisher)).pat;
99 const api = await (0, util_2.getGalleryAPI)(pat);
100 const packageStream = fs.createReadStream(packagePath);
101 const name = `${manifest.publisher}.${manifest.name}`;
102 const description = options.target
103 ? `${name} (${options.target}) v${manifest.version}`
104 : `${name} v${manifest.version}`;
105 util_2.log.info(`Publishing '${description}'...`);
106 let extension = null;
107 try {
108 try {
109 extension = await api.getExtension(null, manifest.publisher, manifest.name, undefined, GalleryInterfaces_1.ExtensionQueryFlags.IncludeVersions);
110 }
111 catch (err) {
112 if (err.statusCode !== 404) {
113 throw err;
114 }
115 }
116 if (extension && extension.versions) {
117 const sameVersion = extension.versions.filter(v => v.version === manifest.version);
118 if (sameVersion.length > 0) {
119 if (options.skipDuplicate) {
120 util_2.log.done(`Version ${manifest.version} is already published. Skipping publish.`);
121 return;
122 }
123 if (sameVersion.some(v => v.targetPlatform === options.target)) {
124 throw new Error(`${description} already exists.`);
125 }
126 }
127 try {
128 await api.updateExtension(undefined, packageStream, manifest.publisher, manifest.name);
129 }
130 catch (err) {
131 if (err.statusCode === 409) {
132 if (options.skipDuplicate) {
133 util_2.log.done(`Version ${manifest.version} is already published. Skipping publish.`);
134 return;
135 }
136 else {
137 throw new Error(`${description} already exists.`);
138 }
139 }
140 else {
141 throw err;
142 }
143 }
144 }
145 else {
146 await api.createExtension(undefined, packageStream);
147 }
148 }
149 catch (err) {
150 const message = (err && err.message) || '';
151 if (/Personal Access Token used has expired/.test(message)) {
152 err.message = `${err.message}\n\nYou're using an expired Personal Access Token, please get a new PAT.\nMore info: https://aka.ms/vscodepat`;
153 }
154 else if (/Invalid Resource/.test(message)) {
155 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`;
156 }
157 throw err;
158 }
159 util_2.log.info(`Extension URL (might take a few minutes): ${(0, util_2.getPublishedUrl)(name)}`);
160 util_2.log.info(`Hub URL: ${(0, util_2.getHubUrl)(manifest.publisher, manifest.name)}`);
161 util_2.log.done(`Published ${description}.`);
162}
163async function unpublish(options = {}) {
164 let publisher, name;
165 if (options.id) {
166 [publisher, name] = options.id.split('.');
167 }
168 else {
169 const manifest = await (0, package_1.readManifest)(options.cwd);
170 publisher = manifest.publisher;
171 name = manifest.name;
172 }
173 const fullName = `${publisher}.${name}`;
174 if (!options.force) {
175 const answer = await (0, util_2.read)(`This will delete ALL published versions! Please type '${fullName}' to confirm: `);
176 if (answer !== fullName) {
177 throw new Error('Aborted');
178 }
179 }
180 const pat = options.pat ?? (await (0, store_1.getPublisher)(publisher)).pat;
181 const api = await (0, util_2.getGalleryAPI)(pat);
182 await api.deleteExtension(publisher, name);
183 util_2.log.done(`Deleted extension: ${fullName}!`);
184}
185exports.unpublish = unpublish;
186//# sourceMappingURL=publish.js.map
\No newline at end of file