diff --git a/src/main.ts b/src/main.ts index ee3359fb..f7b9a1c6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -316,7 +316,7 @@ module.exports = function (argv: string[]): void { 'Personal Access Token (defaults to VSCE_PAT environment variable)', process.env['VSCE_PAT'] ) - .action((name, { pat }) => main(verifyPat(pat, name))); + .action((publisherName, { pat }) => main(verifyPat({ publisherName, pat }))); program .command('show ') diff --git a/src/store.ts b/src/store.ts index 10eef95f..08661014 100644 --- a/src/store.ts +++ b/src/store.ts @@ -39,7 +39,7 @@ export class FileStore implements IStore { return this.publishers.length; } - private constructor(readonly path: string, private publishers: IPublisher[]) {} + private constructor(readonly path: string, private publishers: IPublisher[]) { } private async save(): Promise { await fs.promises.writeFile(this.path, JSON.stringify({ publishers: this.publishers }), { mode: '0600' }); @@ -92,7 +92,7 @@ export class KeytarStore implements IStore { private readonly keytar: typeof import('keytar'), private readonly serviceName: string, private publishers: IPublisher[] - ) {} + ) { } get(name: string): IPublisher { return this.publishers.filter(p => p.name === name)[0]; @@ -113,20 +113,14 @@ export class KeytarStore implements IStore { } } -export async function verifyPat(pat: string, publisherName?: string): Promise { - if (!pat) { - throw new Error('The Personal Access Token is mandatory.'); - } +export interface IVerifyPatOptions { + readonly publisherName?: string; + readonly pat?: string; +} - if (!publisherName) { - try { - publisherName = (await readManifest()).publisher; - } catch (error) { - throw new Error( - `Can not read the publisher's name. Either supply it as an argument or run vsce from the extension folder. Additional information:\n\n${error}` - ); - } - } +export async function verifyPat(options: IVerifyPatOptions): Promise { + const publisherName = options.publisherName ?? (await readManifest()).publisher; + const pat = options.pat ?? (await getPublisher(publisherName)).pat; try { // If the caller of the `getRoleAssignments` API has any of the roles @@ -145,7 +139,7 @@ async function requestPAT(publisherName: string): Promise { console.log(`${getMarketplaceUrl()}/manage/publishers/`); const pat = await read(`Personal Access Token for publisher '${publisherName}':`, { silent: true, replace: '*' }); - await verifyPat(pat, publisherName); + await verifyPat({ publisherName, pat }); return pat; }