Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/jayree/plugin-info into sup…
Browse files Browse the repository at this point in the history
…port-sfdx-plugins
  • Loading branch information
jayree committed Jan 7, 2022
2 parents c1a84ea + 64ff5e3 commit 12215ac
Show file tree
Hide file tree
Showing 11 changed files with 504 additions and 698 deletions.
52 changes: 26 additions & 26 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ workflows:
branches:
only: main
context: CLI_CTC
- release-management/test-nut:
matrix:
parameters:
os:
- linux
- windows
sfdx_version:
- latest
node_version:
- latest
requires:
- release-management/test-package
# - release-management/test-nut:
# matrix:
# parameters:
# os:
# - linux
# - windows
# sfdx_version:
# - latest
# node_version:
# - latest
# requires:
# - release-management/test-package
when: << pipeline.parameters.run-auto-workflows >>
test-ts-update:
triggers:
Expand All @@ -56,20 +56,20 @@ workflows:
- main
jobs:
- release-management/test-ts-update
just-nuts:
when: << pipeline.parameters.run-just-nuts >>
jobs:
- release-management/test-nut:
name: just-nuts-<< matrix.os >>
sfdx_version: << pipeline.parameters.sfdx_version >>
sfdx_executable_path: sfdx
repo_tag: << pipeline.parameters.repo_tag >>
matrix:
parameters:
os:
- linux
- windows
npm_module_name: << pipeline.parameters.npm_module_name >>
# just-nuts:
# when: << pipeline.parameters.run-just-nuts >>
# jobs:
# - release-management/test-nut:
# name: just-nuts-<< matrix.os >>
# sfdx_version: << pipeline.parameters.sfdx_version >>
# sfdx_executable_path: sfdx
# repo_tag: << pipeline.parameters.repo_tag >>
# matrix:
# parameters:
# os:
# - linux
# - windows
# npm_module_name: << pipeline.parameters.npm_module_name >>
dependabot-automerge:
triggers:
- schedule:
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [1.2.0](https://github.com/salesforcecli/plugin-info/compare/v1.1.4...v1.2.0) (2022-01-05)

### Features

- find close version fallback ([f1b2edc](https://github.com/salesforcecli/plugin-info/commit/f1b2edcb26057b1eef10656a8c7fb255f1e4dd08))

### Bug Fixes

- type and typos ([75d46c6](https://github.com/salesforcecli/plugin-info/commit/75d46c66b1b22c74b1d8dfeb56da727c1e51ea51))

### [1.1.4](https://github.com/salesforcecli/plugin-info/compare/v1.1.3...v1.1.4) (2021-12-07)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2021, Salesforce.com, Inc.
Copyright (c) 2022, Salesforce.com, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ EXAMPLES
sfdx info:releasenotes:display --version latest
```

_See code: [src/commands/info/releasenotes/display.ts](https://github.com/salesforcecli/plugin-info/blob/v1.1.3/src/commands/info/releasenotes/display.ts)_
_See code: [src/commands/info/releasenotes/display.ts](https://github.com/salesforcecli/plugin-info/blob/v1.1.4/src/commands/info/releasenotes/display.ts)_

<!-- commandsstop -->
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/plugin-info",
"description": "Plugin for accessing cli info from the command line",
"version": "1.1.4",
"version": "1.2.0",
"author": "Salesforce",
"bugs": "https://github.com/forcedotcom/cli/issues",
"dependencies": {
Expand All @@ -13,15 +13,14 @@
"marked": "^4.0.1",
"marked-terminal": "^4.2.0",
"semver": "^7.3.5",
"sinon-chai": "^3.7.0",
"tslib": "^2"
},
"devDependencies": {
"@oclif/dev-cli": "^1",
"@oclif/plugin-command-snapshot": "^2.2.2",
"@salesforce/cli-plugins-testkit": "^1.4.17",
"@salesforce/dev-config": "^2.1.2",
"@salesforce/dev-scripts": "^1.0.2",
"@salesforce/dev-config": "^3.0.0",
"@salesforce/dev-scripts": "^2.0.0",
"@salesforce/plugin-command-reference": "^1.3.17",
"@salesforce/prettier-config": "^0.0.2",
"@salesforce/ts-sinon": "1.3.21",
Expand All @@ -44,12 +43,13 @@
"eslint-plugin-prettier": "^3.4.1",
"husky": "^7.0.4",
"lint-staged": "^11.2.6",
"mocha": "^8.4.0",
"mocha": "^9.1.3",
"nyc": "^15.1.0",
"prettier": "^2.4.1",
"pretty-quick": "^3.1.0",
"shx": "0.3.3",
"sinon": "^11.1.1",
"sinon-chai": "^3.7.0",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/commands/info/releasenotes/display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class Display extends SfdxCommand {
renderer: new TerminalRenderer({ emoji: false }),
});

this.ux.log(marked.parse(`# Release notes for '${plugin.name}':`));
tokens.unshift(marked.lexer(`# Release notes for '${this.config.bin}':`)[0]);

this.ux.log(marked.parser(tokens));

Expand Down
58 changes: 42 additions & 16 deletions src/shared/parseReleaseNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,54 @@
*/

import { marked } from 'marked';
import * as semver from 'semver';

const parseReleaseNotes = (notes: string, version: string, baseUrl: string): marked.Token[] => {
let found = false;
let closestVersion: string;
let versions: string[];

const parsed = marked.lexer(notes);

// https://stackoverflow.com/a/6969486
const escapeRegExp = (string: string): string => {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
};
let tokens: marked.Token[];

const findVersion = (desiredVersion: string): void => {
versions = [];

tokens = parsed.filter((token) => {
// TODO: Could make header depth (2) a setting in oclif.info.releasenotes
if (token.type === 'heading' && token.depth === 2) {
const coercedVersion = semver.coerce(token.text).version;

// We will use this to find the closest patch if passed version is not found
versions.push(coercedVersion);

const regexp = new RegExp(`\\b${escapeRegExp(version)}\\b`);
if (coercedVersion === desiredVersion) {
found = true;

const tokens = parsed.filter((token) => {
// TODO: Could make header depth (2) a setting in oclif.info.releasenotes
if (token.type === 'heading' && token.depth <= 2) {
if (regexp.exec(token.text)) {
found = true;
return token;
}

found = false;
} else if (found === true) {
return token;
}
});
};

found = false;
} else if (found === true) {
return token;
}
});
findVersion(version);

if (!tokens.length) {
throw new Error(`Didn't find version '${version}'. View release notes online at: ${baseUrl}`);
// If version was not found, try again with the closest patch version
const semverRange = `${semver.major(version)}.${semver.minor(version)}.x`;

closestVersion = semver.maxSatisfying<string>(versions, semverRange);

findVersion(closestVersion);

if (!tokens.length) {
throw new Error(`Didn't find version '${version}'. View release notes online at: ${baseUrl}`);
}
}

const fixRelativeLinks = (token: marked.Token): void => {
Expand All @@ -48,6 +66,14 @@ const parseReleaseNotes = (notes: string, version: string, baseUrl: string): mar

marked.walkTokens(tokens, fixRelativeLinks);

if (closestVersion !== undefined) {
const warning = marked.lexer(
`# ATTENTION: Version ${version} was not found. Showing notes for closest patch version ${closestVersion}.`
)[0];

tokens.unshift(warning);
}

return tokens;
};

Expand Down
8 changes: 4 additions & 4 deletions test/commands/info/releasenotes/display.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('info:releasenotes:display', () => {

await runDisplayCmd([]);

expect(uxLogStub.args[1][0]).to.contain('## Release notes for 3.3.3');
expect(uxLogStub.args[0][0]).to.contain('## Release notes for 3.3.3');
});

it('calls getInfoConfig with config root', async () => {
Expand All @@ -116,7 +116,7 @@ describe('info:releasenotes:display', () => {

await runDisplayCmd([]);

expect(uxLogStub.args[1][0]).to.contain('## 3.3.3 :tada:');
expect(uxLogStub.args[0][0]).to.contain('## 3.3.3 :tada:');
});

it('throws an error if info config lookup fails', async () => {
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('info:releasenotes:display', () => {
it('logs markdown on the command line', async () => {
await runDisplayCmd([]);

expect(uxLogStub.args[1][0]).to.contain('## Release notes for 3.3.3');
expect(uxLogStub.args[0][0]).to.contain('## Release notes for 3.3.3');
});

it('throws an error if parsing fails', async () => {
Expand All @@ -238,7 +238,7 @@ describe('info:releasenotes:display', () => {
it('renders a footer if --hook is set', async () => {
await runDisplayCmd(['--hook']);

expect(uxLogStub.args[2][0]).to.contain('to manually view the current release notes');
expect(uxLogStub.args[1][0]).to.contain('to manually view the current release notes');
});

it('hides footer if env var is set', async () => {
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/notes.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## 63.18.2 (September 17th, 2043)

- testing multiple minors (higher)

## 63.18.1 (September 17th, 2043)

- testing multiple minors (lower)

## 63.17.2 (September 10th, 2043)

- test for finding nearby versions
- `63.17.0` will show results for `63.17.2`
- `63.17.5` will show results for `63.17.2`

## 13.3.1 (Aug 3, 2023)

- test for matching full version (`3.3.1 !== 13.3.1`)
Expand Down
39 changes: 39 additions & 0 deletions test/shared/parseReleaseNotes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,43 @@ describe('parseReleaseNotes tests', () => {
const results = JSON.stringify(tokens, null, ' ');
expect(results).to.include(`${baseUrl}/./test.md`);
});

it('finds a version above what was asked for if not found', () => {
const tokens = parseReleaseNotes(notes, '63.17.0', baseUrl);

const results = JSON.stringify(tokens, null, ' ');

expect(tokens[1].raw).to.include('63.17.2');
expect(results).to.include('- test for finding nearby versions');
});

it('finds a version below what was asked for if not found', () => {
const tokens = parseReleaseNotes(notes, '63.17.5', baseUrl);

const results = JSON.stringify(tokens, null, ' ');

expect(tokens[1].raw).to.include('63.17.2');
expect(results).to.include('- test for finding nearby versions');
});

it('finds highest version if multiple minors exist', () => {
const tokens = parseReleaseNotes(notes, '63.18.0', baseUrl);

const results = JSON.stringify(tokens, null, ' ');

expect(tokens[1].raw).to.include('63.18.2'); // 63.18.1 exists in fixtures/notes
expect(results).to.include('- testing multiple minors (higher)');
});

it('shows warning if a different version is shown', () => {
const tokens = parseReleaseNotes(notes, '63.18.0', baseUrl);

const results = JSON.stringify(tokens, null, ' ');

expect(tokens[0].raw).to.include('63.18.0'); // version asked for
expect(tokens[0].raw).to.include('63.18.2'); // version found
expect(results).to.include(
'ATTENTION: Version 63.18.0 was not found. Showing notes for closest patch version 63.18.2.'
);
});
});
Loading

0 comments on commit 12215ac

Please sign in to comment.