forked from forcedotcom/salesforcedx-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: change status bar color when in production org setting
Create a setting to allow the status bar to change color when the default org is a production one fix forcedotcom#5517
- Loading branch information
1 parent
8164e8d
commit 165d499
Showing
10 changed files
with
246 additions
and
11 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
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 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 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 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
70 changes: 70 additions & 0 deletions
70
packages/salesforcedx-vscode-core/src/settings/colorWarningWhenProdOrg.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,70 @@ | ||
/* | ||
* Copyright (c) 2024, 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 * as vscode from 'vscode'; | ||
import { WorkspaceContext } from '../context'; | ||
import { getDefaultUsernameOrAlias } from '../context/workspaceOrgType'; | ||
import { OrgAuthInfo } from '../util'; | ||
import { SfdxCoreSettings } from './sfdxCoreSettings'; | ||
|
||
/** | ||
* Change the color of the status bar when the default org is a production org | ||
* @returns {Promise<boolean>} - returns true if the color was changed | ||
*/ | ||
export const colorWhenProductionOrg = async () => { | ||
const baseColorStatusBar = new vscode.ThemeColor('statusBar.background'); | ||
|
||
const colorWHenProductionOrgHandler = async () => { | ||
const usernameOrAlias = await getDefaultUsernameOrAlias(); | ||
const settings = SfdxCoreSettings.getInstance(); | ||
|
||
const activated = settings.getColorWarningWhenProductionOrg(); | ||
const colorForProdOrg = settings.getColorWarningWhenProductionOrgColor(); | ||
|
||
if (!usernameOrAlias || !activated) { | ||
return false; | ||
} | ||
const isProdOrg = await OrgAuthInfo.isAProductionOrg( | ||
await OrgAuthInfo.getUsername(usernameOrAlias) | ||
); | ||
const colorCustomizations = { | ||
'statusBar.background': isProdOrg ? colorForProdOrg : baseColorStatusBar | ||
}; | ||
|
||
// Save the configuration to the global settings file | ||
await vscode.workspace | ||
.getConfiguration() | ||
.update( | ||
'workbench.colorCustomizations', | ||
colorCustomizations, | ||
vscode.ConfigurationTarget.Global | ||
); | ||
return true; | ||
}; | ||
|
||
WorkspaceContext.getInstance().onOrgChange(() => | ||
colorWHenProductionOrgHandler() | ||
); | ||
|
||
/** | ||
* Change the color of the status bar when the window state changes, avoiding the status bar color on others vscode windows | ||
*/ | ||
vscode.window.onDidChangeWindowState(async e => { | ||
if (e.focused) { | ||
await colorWHenProductionOrgHandler(); | ||
} else { | ||
await vscode.workspace | ||
.getConfiguration() | ||
.update( | ||
'workbench.colorCustomizations', | ||
{}, | ||
vscode.ConfigurationTarget.Global | ||
); | ||
} | ||
}); | ||
return await colorWHenProductionOrgHandler(); | ||
}; |
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 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
108 changes: 108 additions & 0 deletions
108
packages/salesforcedx-vscode-core/test/jest/settings/colorWarningWhenProdOrg.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,108 @@ | ||
/* | ||
* Copyright (c) 2024, 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 * as vscode from 'vscode'; | ||
import { WorkspaceContext } from '../../../src/context'; | ||
import { getDefaultUsernameOrAlias } from '../../../src/context/workspaceOrgType'; | ||
import { colorWhenProductionOrg } from '../../../src/settings/colorWarningWhenProdOrg'; | ||
import { SfdxCoreSettings } from '../../../src/settings/sfdxCoreSettings'; | ||
import { OrgAuthInfo } from '../../../src/util'; | ||
|
||
// Mocking getDefaultUsernameOrAlias function | ||
jest.mock('../../../src/context/workspaceOrgType', () => ({ | ||
getDefaultUsernameOrAlias: jest.fn() | ||
})); | ||
|
||
// Mocking OrgAuthInfo class | ||
jest.mock('../../../src/util', () => ({ | ||
OrgAuthInfo: { | ||
getUsername: jest.fn(), | ||
isAProductionOrg: jest.fn() | ||
} | ||
})); | ||
|
||
// Mocking SfdxCoreSettings class | ||
jest.mock('../../../src/settings/sfdxCoreSettings', () => ({ | ||
SfdxCoreSettings: { | ||
getInstance: jest.fn() | ||
} | ||
})); | ||
|
||
const mockWorkspaceContextInstance = { | ||
onOrgChange: jest.fn() | ||
}; | ||
|
||
// Mock the WorkspaceContext class | ||
jest.mock('../../../src/context', () => ({ | ||
WorkspaceContext: jest.fn().mockImplementation(() => ({ | ||
getInstance: jest.fn().mockReturnValue(mockWorkspaceContextInstance) | ||
})) | ||
})); | ||
|
||
describe('colorWhenProductionOrg', () => { | ||
let mockConfiguration: any; | ||
let mockOnOrgChange: any; | ||
|
||
beforeEach(() => { | ||
mockConfiguration = { | ||
update: jest.fn() | ||
}; | ||
|
||
mockOnOrgChange = jest.fn(); | ||
(SfdxCoreSettings.getInstance as jest.Mock).mockReturnValue({ | ||
getColorWarningWhenProductionOrg: jest.fn(() => true), | ||
getColorWarningWhenProductionOrgColor: jest.fn() | ||
}); | ||
|
||
WorkspaceContext.getInstance = jest.fn().mockReturnValue({ | ||
onOrgChange: mockOnOrgChange | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should update status bar color when production org is detected', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.getUsername as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.isAProductionOrg as jest.Mock).mockResolvedValue(true); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
|
||
expect(updated).toBe(true); | ||
}); | ||
|
||
it('should not update status bar color when no username or alias is found', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue(null); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
|
||
expect(updated).toBe(false); | ||
}); | ||
|
||
it('should not update status bar color when color warning for production org is not activated', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.getUsername as jest.Mock).mockResolvedValue('testUsername'); | ||
(SfdxCoreSettings.getInstance as jest.Mock).mockReturnValue({ | ||
getColorWarningWhenProductionOrg: jest.fn(() => false), | ||
getColorWarningWhenProductionOrgColor: jest.fn(() => undefined) | ||
}); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
expect(updated).toBe(false); | ||
}); | ||
|
||
it('should not update status bar color when org is not a production org', async () => { | ||
(getDefaultUsernameOrAlias as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.getUsername as jest.Mock).mockResolvedValue('testUsername'); | ||
(OrgAuthInfo.isAProductionOrg as jest.Mock).mockResolvedValue(false); | ||
|
||
const updated = await colorWhenProductionOrg(); | ||
|
||
expect(updated).toBe(true); | ||
}); | ||
}); |
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