diff --git a/src/config.ts b/src/config.ts index a8b42d91..da076886 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,6 +3,7 @@ import * as Lodash from 'lodash' import * as os from 'os' import * as path from 'path' import {format} from 'util' +import {URL} from 'url' import {Command} from './command' import Debug from './debug' @@ -114,6 +115,7 @@ export interface IConfig { scopedEnvVar(key: string): string | undefined scopedEnvVarKey(key: string): string scopedEnvVarTrue(key: string): boolean + s3Url(key: string): string s3Key(type: 'versioned' | 'unversioned', ext: '.tar.gz' | '.tar.xz', options?: IConfig.s3Key.Options): string s3Key(type: keyof PJSON.S3.Templates, options?: IConfig.s3Key.Options): string } @@ -366,6 +368,13 @@ export class Config implements IConfig { const _: typeof Lodash = require('lodash') return _.template(this.pjson.oclif.update.s3.templates[options.platform ? 'target' : 'vanilla'][type])({...this as any, ...options}) } + s3Url(key: string) { + const host = this.pjson.oclif.update.s3.host + if (!host) throw new Error('no s3 host is set') + const url = new URL(host) + url.pathname = path.join(url.pathname, key) + return url.toString() + } protected dir(category: 'cache' | 'data' | 'config'): string { const base = process.env[`XDG_${category.toUpperCase()}_HOME`] diff --git a/test/config.test.ts b/test/config.test.ts index 14eda79d..e0dbf3a9 100644 --- a/test/config.test.ts +++ b/test/config.test.ts @@ -100,4 +100,12 @@ describe('Config', () => { .hasS3Key('versioned', '@oclif/config/channels/beta/oclif-cli-v2.0.0-beta/oclif-cli-v2.0.0-beta.tar.gz', beta) .hasS3Key('versioned', '@oclif/config/channels/beta/oclif-cli-v2.0.0-beta/oclif-cli-v2.0.0-beta-darwin-x64.tar.gz', {...beta, ...target}) }) + + testConfig() + .it('has s3Url', config => { + const orig = config.pjson.oclif.update.s3.host + config.pjson.oclif.update.s3.host = 'https://bar.com/a/' + expect(config.s3Url('/b/c')).to.equal('https://bar.com/a/b/c') + config.pjson.oclif.update.s3.host = orig + }) })