Skip to content

Commit

Permalink
chore: refactor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iowillhoit committed Nov 3, 2021
1 parent 89fa56b commit 101a5eb
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 230 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
"@oclif/config": "^1",
"@octokit/rest": "^18.12.0",
"@salesforce/command": "^3.1.3",
"@salesforce/core": "^2.25.1",
"@salesforce/kit": "^1.5.17",
"@salesforce/ts-types": "^1.5.20",
"axios": "^0.22.0",
"fs-extra": "^10.0.0",
"got": "^11.8.2",
"marked-terminal": "^4.2.0",
"semver": "^7.3.5",
"sinon-chai": "^3.7.0",
"tslib": "^2"
Expand Down
37 changes: 11 additions & 26 deletions src/commands/info/releasenotes/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
// Needed this to ensure the "helpers" were decalred before read in examples
/* eslint-disable @typescript-eslint/member-ordering */

import got from 'got';
import { Env } from '@salesforce/kit';
import { flags, SfdxCommand } from '@salesforce/command';
import { getString } from '@salesforce/ts-types';
import { Messages } from '@salesforce/core';

import { getInfoConfig, InfoConfig } from '../../../shared/get-info-config';
import { getReleaseNotes } from '../../../shared/get-release-notes';

import { PLUGIN_INFO_GET_TIMEOUT } from '../../../constants';
import { getDistTagVersion } from '../../../shared/get-dist-tag-version';

// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
Expand Down Expand Up @@ -54,47 +52,37 @@ export default class Display extends SfdxCommand {

public async run(): Promise<void> {
if (new Env().getBoolean('PLUGIN_INFO_HIDE_RELEASE_NOTES')) {
// We don't ever want to exit the process for info:releasenotes:display
// In most cases we will log a message, but here we only trace log in case someone using stdout of the update command
this.logger.trace('release notes disabled via env var: PLUGIN_INFO_HIDE_RELEASE_NOTES_ENV');
this.logger.trace('exiting');

return;
}

const warn = (msg: string, err): void => {
this.ux.warn(`${msg}\n${getString(err, 'message')}`);
};

const installedVersion = this.config.pjson.version;

let infoConfig: InfoConfig;

try {
infoConfig = await getInfoConfig(this.config.root);
} catch (err) {
const msg = getString(err, 'message');

this.ux.warn(`Loading plugin-info config from package.json failed with message:\n${msg}`);

warn('Loading plugin-info config from package.json failed with message:', err);
return;
}

const { distTagUrl, releaseNotesPath, releaseNotesFilename } = infoConfig.releasenotes;

let version = (this.flags.version as string) || installedVersion;

if (Display.helpers.includes(version)) {
try {
const options = { timeout: PLUGIN_INFO_GET_TIMEOUT };

type DistTagJson = {
latest: string;
'latest-rc': string;
};

const body = await got(distTagUrl, options).json<DistTagJson>();

version = version.includes('rc') ? body['latest-rc'] : body['latest'];
version = await getDistTagVersion(distTagUrl, version);
} catch (err) {
// TODO: Could fallback up using npm here? That way private cli repos could auth with .npmrc
// -- could use this: https://github.com/salesforcecli/plugin-trust/blob/0393b906a30e8858816625517eda5db69377c178/src/lib/npmCommand.ts
this.ux.warn(`Was not able to look up dist-tags from ${distTagUrl}. Try using a version instead.`);

warn('Getting dist-tag info failed with message:', err);
return;
}
}
Expand All @@ -104,10 +92,7 @@ export default class Display extends SfdxCommand {
try {
releaseNotes = await getReleaseNotes(releaseNotesPath, releaseNotesFilename, version);
} catch (err) {
const msg = getString(err, 'message');

this.ux.warn(`getReleaseNotes() request failed with message:\n${msg}`);

warn('getReleaseNotes() request failed with message:', err);
return;
}

Expand Down
25 changes: 25 additions & 0 deletions src/shared/get-dist-tag-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import got from 'got';
import { PLUGIN_INFO_GET_TIMEOUT } from '../constants';

export type DistTagJson = {
latest: string;
'latest-rc': string;
};

export async function getDistTagVersion(url: string, distTag: string): Promise<string> {
// TODO: Could use npm instead here. That way private cli repos could auth with .npmrc
// -- could utilize this: https://github.com/salesforcecli/plugin-trust/blob/0393b906a30e8858816625517eda5db69377c178/src/lib/npmCommand.ts
const options = { timeout: PLUGIN_INFO_GET_TIMEOUT };

const body = await got(url, options).json<DistTagJson>();

// We are only interested in latest and latest-rc, could update this if other tags are desired
return distTag.includes('rc') ? body['latest-rc'] : body['latest'];
}
2 changes: 1 addition & 1 deletion src/shared/get-info-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface InfoConfig {

/* sfdx example to add to cli pjson.oclif
location with npm install:
example location with npm install:
~/.nvm/versions/node/v14.17.5/lib/node_modules/sfdx-cli/package.json
Add to oclif object
Expand Down
67 changes: 67 additions & 0 deletions test/shared/get-dist-tag-version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import got from 'got';
import { expect, use as chaiUse } from 'chai';
import * as Sinon from 'sinon';
import * as SinonChai from 'sinon-chai';
import { stubMethod } from '@salesforce/ts-sinon';
import { getDistTagVersion, DistTagJson } from '../../src/shared/get-dist-tag-version';

chaiUse(SinonChai);

describe('getReleaseNotes tests', () => {
let sandbox: sinon.SinonSandbox;
let gotStub: sinon.SinonStub;

let url;
let gotResponse: DistTagJson;

beforeEach(() => {
url = 'https://registry.npmjs.org/-/package/sfdx-cli/dist-tags';
gotResponse = {
latest: '1.2.3',
'latest-rc': '1.3.0',
};

sandbox = Sinon.createSandbox();

gotStub = stubMethod(sandbox, got, 'default');
gotStub.returns({
json: () => gotResponse,
});
});

afterEach(() => {
gotStub.restore();
sandbox.restore();
});

it('calls got with correct args', async () => {
await getDistTagVersion(url, 'latest');

expect(gotStub.args[0]).to.deep.equal([url, { timeout: 3000 }]);
});

it('returns rc if version is "latest-rc"', async () => {
const version = await getDistTagVersion(url, 'latest-rc');

expect(version).to.equal('1.3.0');
});

it('returns rc if version includes "rc"', async () => {
const version = await getDistTagVersion(url, 'stable-rc');

expect(version).to.equal('1.3.0');
});

it('returns latest by default', async () => {
const version = await getDistTagVersion(url, 'foobar');

expect(version).to.equal('1.2.3');
});
});
Loading

0 comments on commit 101a5eb

Please sign in to comment.