generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial setup and release notes fetch
- Loading branch information
1 parent
a69880a
commit d258d91
Showing
8 changed files
with
616 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"commandDescription": "display CLI release notes on the command line", | ||
"versionFlagDescription": "display release notes for this version", | ||
"pjsonConfigLoadFailure": "unable to load config from package.json, see plugin-info readme for instructions" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright (c) 2020, 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 | ||
*/ | ||
|
||
// Needed this to ensure the "helpers" were decalred before read in examples | ||
/* eslint-disable @typescript-eslint/member-ordering */ | ||
|
||
import axios from 'axios'; | ||
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'; | ||
|
||
// Initialize Messages with the current plugin directory | ||
Messages.importMessagesDirectory(__dirname); | ||
|
||
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core, | ||
// or any library that is sfdxusing the messages framework can also be loaded this way. | ||
const messages = Messages.loadMessages('@salesforce/plugin-info', 'display'); | ||
|
||
export default class Display extends SfdxCommand { | ||
private static helpers = ['stable', 'stable-rc', 'latest', 'latest-rc', 'rc']; | ||
|
||
public static description = messages.getMessage('commandDescription'); | ||
|
||
public static aliases = ['whatsnew']; | ||
|
||
public static examples = [ | ||
`$ sfdx info:releasenotes:display | ||
display release notes for currently installed CLI version | ||
`, | ||
`$ sfdx info:releasenotes:display --version "1.2.3" | ||
display release notes for CLI version 1.2.3 | ||
`, | ||
`$ sfdx info:releasenotes:display --version "stable-rc" | ||
can be called with tag "helpers", available options are: ${Display.helpers.join(', ')} | ||
`, | ||
]; | ||
|
||
protected static flagsConfig = { | ||
version: flags.string({ | ||
char: 'v', | ||
description: messages.getMessage('versionFlagDescription'), | ||
}), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
if (new Env().getBoolean('PLUGIN_INFO_HIDE_RELEASE_NOTES')) { | ||
this.logger.trace('release notes disabled via env var: PLUGIN_INFO_HIDE_RELEASE_NOTES_ENV'); | ||
this.logger.trace('exiting'); | ||
|
||
return; | ||
} | ||
|
||
const installedVersion = this.config.pjson.version; | ||
|
||
let infoConfig: InfoConfig; | ||
|
||
try { | ||
// this.config.root should be cross platform, it is set here: | ||
// https://github.com/salesforcecli/sfvm/blob/2211d7b7b34cb21f6b738dc31ca27ef2e46de1cb/src/api/installation.ts#L111 | ||
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}`); | ||
|
||
return; | ||
} | ||
|
||
const { distTagUrl, releaseNotesPath, releaseNotesFilename } = infoConfig.releasenotes; | ||
|
||
let version = (this.flags.version as string) || installedVersion; | ||
|
||
if (Display.helpers.includes(version)) { | ||
try { | ||
const { data } = await axios.get(distTagUrl); | ||
|
||
version = version.includes('rc') ? data['latest-rc'] : data['latest']; | ||
} 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.`); | ||
|
||
return; | ||
} | ||
} | ||
|
||
let releaseNotes; | ||
|
||
try { | ||
releaseNotes = await getReleaseNotes(releaseNotesPath, releaseNotesFilename, version); | ||
} catch (err) { | ||
const msg = getString(err, 'message'); | ||
|
||
this.ux.warn(`Release notes GET request failed with message:\n${msg}`); | ||
|
||
return; | ||
} | ||
|
||
this.ux.log(releaseNotes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 { join } from 'path'; | ||
import { readJson } from 'fs-extra'; | ||
import { PJSON } from '@oclif/config'; | ||
import { get } from '@salesforce/ts-types'; | ||
|
||
interface PjsonWithInfo extends PJSON { | ||
oclif: PJSON['oclif'] & { | ||
info: InfoConfig; | ||
}; | ||
} | ||
|
||
export interface InfoConfig { | ||
releasenotes: { | ||
distTagUrl: string; | ||
releaseNotesPath: string; | ||
releaseNotesFilename: string; | ||
}; | ||
} | ||
|
||
/* sfdx example to add to cli pjson.oclif | ||
location with npm install: | ||
~/.nvm/versions/node/v14.17.5/lib/node_modules/sfdx-cli/package.json | ||
Add to oclif object | ||
"info": { | ||
"releasenotes": { | ||
"distTagUrl": "https://registry.npmjs.org/-/package/sfdx-cli/dist-tags", | ||
"releaseNotesPath": "https://raw.githubusercontent.com/forcedotcom/cli/main/releasenotes/sfdx", | ||
"releaseNotesFilename": "README.md" | ||
} | ||
} | ||
*/ | ||
|
||
export async function getInfoConfig(root: string): Promise<InfoConfig> { | ||
const fullPath = join(root, 'package.json'); | ||
|
||
const json = (await readJson(fullPath)) as PjsonWithInfo; | ||
|
||
const info = get(json, 'oclif.info') as InfoConfig; | ||
|
||
if (!info) throw new Error('getInfoConfig() failed to find pjson.oclif.info config'); | ||
|
||
return info; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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 axios, { AxiosRequestConfig, AxiosResponse } from 'axios'; | ||
import { major } from 'semver'; | ||
|
||
export async function getReleaseNotes(base: string, filename: string, version: string): Promise<AxiosResponse> { | ||
const majorVersion = major(version); | ||
|
||
const options: AxiosRequestConfig = { | ||
timeout: 5000, | ||
validateStatus: () => true, | ||
}; | ||
|
||
const getPromises = [ | ||
axios.get<AxiosResponse>(`${base}/v${majorVersion}.md`, options), | ||
axios.get<AxiosResponse>(`${base}/${filename}`, options), | ||
]; | ||
|
||
const [versioned, readme] = await Promise.all(getPromises); | ||
|
||
const { data } = versioned.status === 200 ? versioned : readme; | ||
|
||
// check readme status too | ||
|
||
return data; | ||
} |
Oops, something went wrong.