-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4208 from theJiawen/feat/rush-custom-tips
feat(rush-lib): setup Rush Custom Tips
- Loading branch information
Showing
20 changed files
with
513 additions
and
134 deletions.
There are no files selected for viewing
247 changes: 118 additions & 129 deletions
247
build-tests/install-test-workspace/workspace/common/pnpm-lock.yaml
Large diffs are not rendered by default.
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
10 changes: 10 additions & 0 deletions
10
common/changes/@microsoft/rush/feat-rush-custom-tips_2023-06-15-18-23.json
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,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@microsoft/rush", | ||
"comment": "Add a new config file \"custom-tips.json\" for customizing Rush messages (GitHub #4207)", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@microsoft/rush" | ||
} |
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
41 changes: 41 additions & 0 deletions
41
libraries/rush-lib/assets/rush-init/common/config/rush/custom-tips.json
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,41 @@ | ||
/** | ||
* This configuration file allows repo maintainers to configure extra details to be | ||
* printed alongside certain Rush messages. More documentation is available on the | ||
* Rush website: https://rushjs.io | ||
*/ | ||
{ | ||
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/custom-tips.schema.json", | ||
|
||
/** | ||
* If specified, this prefix will be prepended to any the tip messages when they are displayed. | ||
* The default value is an empty string. | ||
*/ | ||
/*[LINE "HYPOTHETICAL"]*/ "defaultMessagePrefix": "⭐️ Contoso monorepo tip: ", | ||
|
||
/** | ||
* Specifies the custom tips to be displayed by Rush. | ||
*/ | ||
"customTips": [ | ||
/*[BEGIN "DEMO"]*/ | ||
{ | ||
/** | ||
* (REQUIRED) An identifier indicating a message that may be printed by Rush. | ||
* If that message is printed, then this custom tip will be shown. | ||
* Consult the Rush documentation for the current list of possible identifiers. | ||
*/ | ||
"tipId": "TIP_RUSH_INCONSISTENT_VERSIONS", | ||
|
||
/** | ||
* (REQUIRED) The message text to be displayed for this tip. | ||
*/ | ||
"message": "For additional troubleshooting information, refer this wiki article:\n\nhttps://intranet.contoso.com/docs/pnpm-mismatch", | ||
|
||
/** | ||
* Overrides the "defaultMessagePrefix" for this tip. | ||
* Specify an empty string to omit the "defaultMessagePrefix" entirely. | ||
*/ | ||
// "messagePrefix": "" | ||
} | ||
/*[END "DEMO"]*/ | ||
] | ||
} |
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,166 @@ | ||
import * as path from 'path'; | ||
import { FileSystem, ITerminal, InternalError, JsonFile, JsonSchema } from '@rushstack/node-core-library'; | ||
|
||
import schemaJson from '../schemas/custom-tips.schema.json'; | ||
|
||
/** | ||
* This interface represents the raw custom-tips.json file which allows repo maintainers | ||
* to configure extra details to be printed alongside certain Rush messages. | ||
* @beta | ||
*/ | ||
export interface ICustomTipsJson { | ||
/** | ||
* If specified, this prefix will be prepended to any the tip messages when they are displayed. | ||
* The default value is an empty string. | ||
*/ | ||
defaultMessagePrefix?: string; | ||
/** | ||
* Specifies the custom tips to be displayed by Rush. | ||
*/ | ||
customTips?: ICustomTipItemJson[]; | ||
} | ||
|
||
/** | ||
* An item from the {@link ICustomTipsJson.customTips} list. | ||
* @beta | ||
*/ | ||
export interface ICustomTipItemJson { | ||
/** | ||
* (REQUIRED) An identifier indicating a message that may be printed by Rush. | ||
* If that message is printed, then this custom tip will be shown. | ||
* Consult the Rush documentation for the current list of possible identifiers. | ||
*/ | ||
tipId: CustomTipId; | ||
|
||
/** | ||
* (REQUIRED) The message text to be displayed for this tip. | ||
*/ | ||
message: string; | ||
|
||
/** | ||
* Overrides the "defaultMessagePrefix" for this tip. | ||
* Specify an empty string to omit the "defaultMessagePrefix" entirely. | ||
*/ | ||
messagePrefix?: string; | ||
} | ||
|
||
/** | ||
* An identifier representing a Rush message that can be customized by | ||
* defining a custom tip in `common/config/rush/custom-tips.json`. | ||
* @remarks | ||
* Custom tip ids always start with the `TIP_` prefix. | ||
* | ||
* @privateRemarks | ||
* Events from the Rush process should with "TIP_RUSH_". | ||
* Events from a PNPM subprocess should start with "TIP_PNPM_". | ||
* | ||
* @beta | ||
*/ | ||
export type CustomTipId = | ||
// The rush.json "ensureConsistentVersions" validation. | ||
| 'TIP_RUSH_INCONSISTENT_VERSIONS' | ||
// In the future, plugins can contribute other strings. | ||
| string; | ||
|
||
/** | ||
* Used to access the `common/config/rush/custom-tips.json` config file, | ||
* which allows repo maintainers to configure extra details to be printed alongside | ||
* certain Rush messages. | ||
* @beta | ||
*/ | ||
export class CustomTipsConfiguration { | ||
private static _jsonSchema: JsonSchema = JsonSchema.fromLoadedObject(schemaJson); | ||
|
||
private readonly _tipMap: Map<CustomTipId, ICustomTipItemJson>; | ||
private readonly _jsonFileName: string; | ||
|
||
/** | ||
* The JSON settings loaded from `custom-tips.json`. | ||
*/ | ||
public readonly configuration: Readonly<ICustomTipsJson>; | ||
|
||
/** | ||
* The list of identifiers that are allowed to be used in the "tipId" field | ||
* of the config file. | ||
*/ | ||
public static readonly supportedTipIds: ReadonlySet<string> = new Set<string>([ | ||
'TIP_RUSH_INCONSISTENT_VERSIONS' | ||
]); | ||
|
||
public constructor(configFilename: string) { | ||
this._jsonFileName = configFilename; | ||
this._tipMap = new Map(); | ||
|
||
if (!FileSystem.exists(this._jsonFileName)) { | ||
this.configuration = {}; | ||
} else { | ||
this.configuration = JsonFile.loadAndValidate(this._jsonFileName, CustomTipsConfiguration._jsonSchema); | ||
|
||
const customTips: ICustomTipItemJson[] | undefined = this.configuration?.customTips; | ||
if (customTips) { | ||
for (const tipItem of customTips) { | ||
if (!CustomTipsConfiguration.supportedTipIds.has(tipItem.tipId)) { | ||
throw new Error( | ||
`The ${path.basename(this._jsonFileName)} configuration` + | ||
` references an unknown ID "${tipItem.tipId}"` | ||
); | ||
} | ||
if (this._tipMap.has(tipItem.tipId)) { | ||
throw new Error( | ||
`The ${path.basename(this._jsonFileName)} configuration` + | ||
` specifies a duplicate definition for "${tipItem.tipId}"` | ||
); | ||
} | ||
this._tipMap.set(tipItem.tipId, tipItem); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private _formatTipMessage(tipId: CustomTipId): string | undefined { | ||
if (!tipId.startsWith('TIP_')) { | ||
throw new InternalError('Identifiers for custom tips must start with the "TIP_" prefix'); | ||
} | ||
|
||
const customTipItem: ICustomTipItemJson | undefined = this._tipMap.get(tipId); | ||
if (!customTipItem) { | ||
return undefined; | ||
} | ||
|
||
const prefix: string | undefined = customTipItem.messagePrefix ?? this.configuration.defaultMessagePrefix; | ||
return `${prefix ?? ''}${customTipItem.message}`; | ||
} | ||
|
||
/** | ||
* If custom-tips.json defines a tip for the specified tipId, | ||
* display the tip on the terminal. | ||
*/ | ||
public showInfoTip(terminal: ITerminal, tipId: CustomTipId): void { | ||
const message: string | undefined = this._formatTipMessage(tipId); | ||
if (message !== undefined) { | ||
terminal.writeLine(message); | ||
} | ||
} | ||
|
||
/** | ||
* If custom-tips.json defines a tip for the specified tipId, | ||
* display the tip on the terminal. | ||
*/ | ||
public showWarningTip(terminal: ITerminal, tipId: CustomTipId): void { | ||
const message: string | undefined = this._formatTipMessage(tipId); | ||
if (message !== undefined) { | ||
terminal.writeWarningLine(message); | ||
} | ||
} | ||
|
||
/** | ||
* If custom-tips.json defines a tip for the specified tipId, | ||
* display the tip on the terminal. | ||
*/ | ||
public showErrorTip(terminal: ITerminal, tipId: CustomTipId): void { | ||
const message: string | undefined = this._formatTipMessage(tipId); | ||
if (message !== undefined) { | ||
terminal.writeErrorLine(message); | ||
} | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
libraries/rush-lib/src/api/test/CustomTipsConfiguration.test.ts
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,16 @@ | ||
import { CustomTipsConfiguration } from '../CustomTipsConfiguration'; | ||
import { RushConfiguration } from '../RushConfiguration'; | ||
|
||
describe(CustomTipsConfiguration.name, () => { | ||
it('loads the config file (custom-tips.json)', () => { | ||
const rushFilename: string = `${__dirname}/repo/rush-npm.json`; | ||
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(rushFilename); | ||
expect(rushConfiguration.customTipsConfiguration.configuration.customTips?.length).toBe(1); | ||
}); | ||
|
||
it('reports an error for duplicate tips', () => { | ||
expect(() => { | ||
new CustomTipsConfiguration(`${__dirname}/jsonFiles/custom-tips.error.json`); | ||
}).toThrowError('TIP_RUSH_INCONSISTENT_VERSIONS'); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
libraries/rush-lib/src/api/test/jsonFiles/custom-tips.error.json
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,14 @@ | ||
{ | ||
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/custom-tips.schema.json", | ||
|
||
"customTips": [ | ||
{ | ||
"tipId": "TIP_RUSH_INCONSISTENT_VERSIONS", | ||
"message": "duplicate 1" | ||
}, | ||
{ | ||
"tipId": "TIP_RUSH_INCONSISTENT_VERSIONS", | ||
"message": "duplicate 2" | ||
} | ||
] | ||
} |
13 changes: 13 additions & 0 deletions
13
libraries/rush-lib/src/api/test/repo/common/config/rush/custom-tips.json
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,13 @@ | ||
{ | ||
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/custom-tips.schema.json", | ||
|
||
// The prefix will be prepended to all the custom message. | ||
"defaultMessagePrefix": "⭐️ [Monorepo Infra team tip]: ", | ||
|
||
"customTips": [ | ||
{ | ||
"tipId": "TIP_RUSH_INCONSISTENT_VERSIONS", | ||
"message": "This is so wrong my friend. Please read this doc for more information: google.com" | ||
} | ||
] | ||
} |
Oops, something went wrong.