make publish work

This commit is contained in:
Joao Moreno 2015-09-22 11:28:38 +02:00
parent fafd5a7246
commit eb0ae9bd54
3 changed files with 40 additions and 9 deletions

View file

@ -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<IC
console.log(`Existing credentials found: { account: ${ credentials.account }, publisher: ${ credentials.publisher } }`);
return read('Do you want to overwrite existing credentials? [y/N] ')
.then<ICredentials>(answer => /^y$/i.test(answer) ? promptForCredentials() : credentials);
.then<ICredentials>(answer => /^y$/i.test(answer) ? null : credentials);
})
.then(credentials => {
if (credentials || !options.promptIfMissing) {
@ -74,8 +75,19 @@ export function getCredentials(options: IGetCredentialsOptions = {}): Promise<IC
});
}
const galleryUrl = 'https://app.market.visualstudio.com';
export function login(): Promise<ICredentials> {
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<any> {

View file

@ -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<any> {
.then(packagePath => pack(packagePath, cwd))
.then(result => {
const { manifest, packagePath } = result;
const fullName = `${ manifest.name }@${ manifest.version }`;
return nfcall<string>(readFile, packagePath, 'base64')
.then(extensionManifest => {
return api.createExtension({ extensionManifest });
});
return nfcall<string>(readFile, packagePath, 'base64').then(extensionManifest => {
console.log(`Publishing ${ fullName }...`);
return api.getExtension(credentials.publisher, manifest.name, null, ExtensionQueryFlags.IncludeVersions)
.catch<PublishedExtension>(err => err.statusCode === 404 ? null : reject(err))
.then(extension => {
if (extension && extension.versions.some(v => v.version === manifest.version)) {
return reject<void>(`${ 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 }!`));
});
});
});
});
};

View file

@ -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);