diff --git a/package.json b/package.json index 18a46a97..8166ff08 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ }, "repository": "bump-sh/cli", "scripts": { + "prepare": "npm run build", "build": "shx rm -rf dist && tsc -b", "clean": "rm -rf dist/ oclif.manifest.json", "lint": "eslint . --ext .ts", diff --git a/src/api/index.ts b/src/api/index.ts index f8ddb546..d7515b36 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -59,10 +59,11 @@ class BumpApi { } // Check https://oclif.io/docs/config for details about Config.IConfig - public constructor(protected config: Config) { + public constructor(protected config?: Config) { const baseURL = `${vars.apiUrl}${vars.apiBasePath}` + const userAgent = config?.userAgent || 'bump-cli' const headers: {Authorization?: string; 'User-Agent': string} = { - 'User-Agent': vars.apiUserAgent(config.userAgent), + 'User-Agent': vars.apiUserAgent(userAgent), } this.client = axios.create({ diff --git a/src/commands/diff.ts b/src/commands/diff.ts index 2c857bfd..e34e2254 100644 --- a/src/commands/diff.ts +++ b/src/commands/diff.ts @@ -105,7 +105,7 @@ export default class Diff extends BaseCommand { ux.action.status = '...diff on Bump.sh in progress' - const diff: DiffResponse | undefined = await new CoreDiff(this.bump).run( + const diff: DiffResponse | undefined = await new CoreDiff(this.config).run( args.file, args.otherFile, documentation, diff --git a/src/core/diff.ts b/src/core/diff.ts index f138eb28..43c3f74a 100644 --- a/src/core/diff.ts +++ b/src/core/diff.ts @@ -1,3 +1,4 @@ +import {Config} from '@oclif/core' import {CLIError} from '@oclif/core/errors' import debug from 'debug' @@ -10,9 +11,17 @@ export class Diff { static readonly TIMEOUT = 120 private _bump!: BumpApi + private _config: Config | undefined - public constructor(bumpClient: BumpApi) { - this._bump = bumpClient + public constructor(config?: Config) { + if (config) { + this._config = config + } + } + + get bumpClient(): BumpApi { + if (!this._bump) this._bump = new BumpApi(this._config!) + return this._bump } get pollingPeriod(): number { @@ -32,7 +41,7 @@ export class Diff { references, } - const response = await this._bump.postDiff(request) + const response = await this.bumpClient.postDiff(request) switch (response.status) { case 201: { @@ -68,7 +77,7 @@ export class Diff { unpublished: true, } - const response = await this._bump.postVersion(request, token) + const response = await this.bumpClient.postVersion(request, token) switch (response.status) { case 201: { @@ -125,6 +134,8 @@ export class Diff { format: string, expires: string | undefined, ): Promise { + if (!this._config) this._config = await Config.load('../../') + let diffVersion: DiffResponse | VersionResponse | undefined if (file2 && (!documentation || !token)) { @@ -157,8 +168,8 @@ export class Diff { opts: {format: string; timeout: number}, ): Promise { const pollingResponse = await (this.isVersion(result) && token - ? this._bump.getVersion(result.id, token) - : this._bump.getDiff(result.id, opts.format)) + ? this.bumpClient.getVersion(result.id, token) + : this.bumpClient.getDiff(result.id, opts.format)) if (opts.timeout <= 0) { throw new CLIError(