From ae0880983977007fa358d09ba9040189d9546daa Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Fri, 25 Sep 2015 15:35:44 +0200 Subject: [PATCH] better store format --- src/publish.ts | 45 +++++++++++++++++---------------- src/store.ts | 67 +++++++++++++++++++++++++++++--------------------- 2 files changed, 63 insertions(+), 49 deletions(-) diff --git a/src/publish.ts b/src/publish.ts index 5429081..962a799 100644 --- a/src/publish.ts +++ b/src/publish.ts @@ -3,7 +3,7 @@ import { ExtensionQueryFlags, PublishedExtension } from 'vso-node-api/interfaces import { nfcall, Promise, reject } from 'q'; import { pack } from './package'; import { tmpName } from 'tmp'; -import { get } from './store'; +import { getPublisher } from './store'; import { getGalleryAPI } from './util'; const galleryUrl = 'https://app.market.visualstudio.com'; @@ -14,27 +14,30 @@ export function publish(cwd = process.cwd()): Promise { .then(result => { const { manifest, packagePath } = result; - return get(manifest.publisher).then(getGalleryAPI).then(api => { - return nfcall(readFile, packagePath, 'base64').then(extensionManifest => { - const fullName = `${ manifest.name }@${ manifest.version }`; - console.log(`Publishing ${ fullName }...`); - - return api.getExtension(manifest.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 }, manifest.publisher, manifest.name) - : api.createExtension({ extensionManifest }); - - return promise - .catch(err => reject(err.statusCode === 409 ? `${ fullName } already exists.` : err)) - .then(() => console.log(`Successfully published ${ fullName }!`)); + return getPublisher(manifest.publisher) + .then(p => p.pat) + .then(getGalleryAPI) + .then(api => { + return nfcall(readFile, packagePath, 'base64').then(extensionManifest => { + const fullName = `${ manifest.name }@${ manifest.version }`; + console.log(`Publishing ${ fullName }...`); + + return api.getExtension(manifest.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 }, manifest.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/store.ts b/src/store.ts index fd30612..359f72a 100644 --- a/src/store.ts +++ b/src/store.ts @@ -7,8 +7,13 @@ import { validatePublisher } from './validation'; const storePath = path.join(home(), '.vsce'); +export interface IPublisher { + name: string; + pat: string; +} + export interface IStore { - [publisher: string]: string; + publishers: IPublisher[]; } export interface IGetOptions { @@ -25,6 +30,10 @@ function load(): Promise { } catch (e) { return reject(`Error parsing store: ${ storePath }`); } + }) + .then(store => { + store.publishers = store.publishers || []; + return resolve(store); }); } @@ -33,71 +42,73 @@ function save(store: IStore): Promise { .then(() => store); } -function requestPAT(store: IStore, publisher: string): Promise { - return read(`Personal Access Token for publisher '${ publisher }':`, { silent: true, replace: '*' }) +function requestPAT(store: IStore, publisherName: string): Promise { + return read(`Personal Access Token for publisher '${ publisherName }':`, { silent: true, replace: '*' }) .then(pat => { const api = getGalleryAPI(pat); - return api.getPublisher(publisher).then(p => { + return api.getPublisher(publisherName).then(p => { console.log(`Authentication successful. Found publisher '${ p.displayName }'.`); return pat; }); }) .then(pat => { - store[publisher] = pat; - return save(store).then(() => pat); + const publisher = { name: publisherName, pat }; + store.publishers = [...store.publishers.filter(p => p.name !== publisherName), publisher]; + return save(store).then(() => publisher); }); } -export function get(publisher: string): Promise { - validatePublisher(publisher); +export function getPublisher(publisherName: string): Promise { + validatePublisher(publisherName); return load().then(store => { - if (store[publisher]) { - return resolve(store[publisher]); - } - - return requestPAT(store, publisher); + const publisher = store.publishers.filter(p => p.name === publisherName)[0]; + return publisher ? resolve(publisher) : requestPAT(store, publisherName); }); } -function add(publisher: string): Promise { - validatePublisher(publisher); +function addPublisher(publisherName: string): Promise { + validatePublisher(publisherName); return load() .then(store => { - if (store[publisher]) { - console.log(`Publisher '${ publisher }' is already known`); + const publisher = store.publishers.filter(p => p.name === publisherName)[0]; + + if (publisher) { + console.log(`Publisher '${ publisherName }' is already known`); return read('Do you want to overwrite its PAT? [y/N] ') .then(answer => /^y$/i.test(answer) ? store : reject('Aborted')); } return resolve(store); }) - .then(store => requestPAT(store, publisher)); + .then(store => requestPAT(store, publisherName)); } -function rm(publisher: string): Promise { - validatePublisher(publisher); +function rmPublisher(publisherName: string): Promise { + validatePublisher(publisherName); return load().then(store => { - if (!store[publisher]) { - return reject(`Unknown publisher '${ publisher }'`); + const publisher = store.publishers.filter(p => p.name === publisherName)[0]; + + if (!publisher) { + return reject(`Unknown publisher '${ publisherName }'`); } - delete store[publisher]; + store.publishers = store.publishers.filter(p => p.name !== publisherName); return save(store); }); } -function list(): Promise { - return load().then(store => Object.keys(store)); +function listPublishers(): Promise { + return load().then(store => store.publishers); } export function publisher(action: string, publisher: string): Promise { switch (action) { - case 'add': return add(publisher); - case 'rm': return rm(publisher); - case 'list': default: return list().then(publishers => publishers.forEach(p => console.log(p))); + case 'add': return addPublisher(publisher); + case 'rm': return rmPublisher(publisher); + case 'list': default: return listPublishers().then(publishers => publishers.forEach(p => console.log(p.name))); } } \ No newline at end of file