From eb0ae9bd5429682ea2cd4b70dc6029c54446f32c Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Tue, 22 Sep 2015 11:28:38 +0200 Subject: [PATCH] make publish work --- src/login.ts | 16 ++++++++++++++-- src/publish.ts | 25 ++++++++++++++++++++----- src/util.ts | 8 ++++++-- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/login.ts b/src/login.ts index b197ccc..77450eb 100644 --- a/src/login.ts +++ b/src/login.ts @@ -3,6 +3,7 @@ import * as path from 'path'; import { Promise, nfcall, resolve, reject } from 'q'; import { home } from 'osenv'; import { read } from './util'; +import { WebApi, getBasicHandler } from 'vso-node-api/WebApi'; const credentialsPath = path.join(home(), '.vsce'); @@ -62,7 +63,7 @@ export function getCredentials(options: IGetCredentialsOptions = {}): Promise(answer => /^y$/i.test(answer) ? promptForCredentials() : credentials); + .then(answer => /^y$/i.test(answer) ? null : credentials); }) .then(credentials => { if (credentials || !options.promptIfMissing) { @@ -74,8 +75,19 @@ export function getCredentials(options: IGetCredentialsOptions = {}): Promise { - return getCredentials({ promptIfMissing: true, promptToOverwrite: true }); + return getCredentials({ promptIfMissing: true, promptToOverwrite: true }).then(credentials => { + const authHandler = getBasicHandler('oauth', credentials.pat); + const vsoapi = new WebApi(credentials.account, authHandler); + const api = vsoapi.getQGalleryApi(galleryUrl); + + return api.getPublisher(credentials.publisher).then(publisher => { + console.log(`Authentication successful. Found publisher '${ publisher.displayName }'.`); + return credentials; + }); + }); } export function logout(): Promise { diff --git a/src/publish.ts b/src/publish.ts index 11d915c..1edc69b 100644 --- a/src/publish.ts +++ b/src/publish.ts @@ -1,6 +1,6 @@ import { readFile } from 'fs'; import { WebApi, getBasicHandler } from 'vso-node-api/WebApi'; -import gallery = require('vso-node-api/GalleryApi'); +import { ExtensionQueryFlags, PublishedExtension } from 'vso-node-api/interfaces/GalleryInterfaces'; import { nfcall, Promise, reject, resolve, all } from 'q'; import { pack, IPackageResult } from './package'; import { tmpName } from 'tmp'; @@ -19,11 +19,26 @@ export function publish(cwd = process.cwd()): Promise { .then(packagePath => pack(packagePath, cwd)) .then(result => { const { manifest, packagePath } = result; + const fullName = `${ manifest.name }@${ manifest.version }`; - return nfcall(readFile, packagePath, 'base64') - .then(extensionManifest => { - return api.createExtension({ extensionManifest }); - }); + return nfcall(readFile, packagePath, 'base64').then(extensionManifest => { + console.log(`Publishing ${ fullName }...`); + return api.getExtension(credentials.publisher, manifest.name, null, ExtensionQueryFlags.IncludeVersions) + .catch(err => err.statusCode === 404 ? null : reject(err)) + .then(extension => { + if (extension && extension.versions.some(v => v.version === manifest.version)) { + return reject(`${ fullName } already exists.`); + } + + var promise = extension + ? api.updateExtension({ extensionManifest }, credentials.publisher, manifest.name) + : api.createExtension({ extensionManifest }); + + return promise + .catch(err => reject(err.statusCode === 409 ? `${ fullName } already exists.` : err)) + .then(() => console.log(`Successfully published ${ fullName }!`)); + }); + }); }); }); }; \ No newline at end of file diff --git a/src/util.ts b/src/util.ts index a4862a1..96b58e5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -3,8 +3,12 @@ import { assign } from 'lodash'; import _read = require('read'); export function fatal(message: any, ...args: any[]) { - if (message instanceof Error && /^cancell?ed$/i.test(message.message)) { - return; + if (message instanceof Error) { + if (/^cancell?ed$/i.test(message.message)) { + return; + } + + message = message.message; } console.error('Error:', message, ...args);