diff --git a/src/main/ParseArgs.test.js b/src/main/ParseArgs.test.js index 9a5e763b..8dc7918d 100644 --- a/src/main/ParseArgs.test.js +++ b/src/main/ParseArgs.test.js @@ -1,12 +1,28 @@ // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +const dummyVersion = '1.2.3-test'; + +jest.mock('electron', () => ({ + app: { + getVersion: () => dummyVersion, + }, +})); + import parse from 'main/ParseArgs'; describe('main/ParseArgs', () => { it('should remove arguments following a deeplink', () => { - const args = parse(['mattermost', '--disableDevMode', 'mattermost://server-1.com', '--version']); + const args = parse(['mattermost', '--disableDevMode', 'mattermost://server-1.com']); expect(args.disableDevMode).toBe(true); - expect(args.version).toBeUndefined(); + }); + + it('should show version and exit when specified', async () => { + jest.spyOn(process.stdout, 'write').mockImplementation(() => {}); + const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {}); + const logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + parse(['mattermost', '--version', 'mattermost://server-1.com']); + expect(exitSpy).toHaveBeenCalledWith(0); + expect(logSpy).toHaveBeenCalledWith(dummyVersion); }); }); diff --git a/src/main/ParseArgs.ts b/src/main/ParseArgs.ts index 8602f23b..f579b4a0 100644 --- a/src/main/ParseArgs.ts +++ b/src/main/ParseArgs.ts @@ -1,6 +1,7 @@ // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. +import {app} from 'electron'; import {Args} from 'types/args'; import yargs from 'yargs'; @@ -24,12 +25,32 @@ function triageArgs(args: string[]) { return args; } +// Note that yargs is able to exit the node process when handling +// certain flags, like version or help. +// https://github.com/yargs/yargs/blob/main/docs/api.md#exitprocessenable function parseArgs(args: string[]) { return yargs. - alias('dataDir', 'd').string('dataDir').describe('dataDir', 'Set the path to where user data is stored.'). - alias('disableDevMode', 'p').boolean('disableDevMode').describe('disableDevMode', 'Disable development mode. Allows for testing as if it was Production.'). - alias('version', 'v').boolean('version').describe('version', 'Prints the application version.'). - alias('fullscreen', 'f').boolean('fullscreen').describe('fullscreen', 'Opens the application in fullscreen mode.'). + alias('dataDir', 'd'). + string('dataDir'). + describe('dataDir', 'Set the path to where user data is stored.'). + + alias('disableDevMode', 'p'). + boolean('disableDevMode'). + describe('disableDevMode', 'Disable development mode. Allows for testing as if it was Production.'). + + alias('version', 'v'). + boolean('version'). + describe('version', 'Prints the application version.'). + + alias('fullscreen', 'f'). + boolean('fullscreen'). + describe('fullscreen', 'Opens the application in fullscreen mode.'). + + // Typically, yargs is capable of acquiring the app's version + // through package.json. However, for us this is + // unsuccessful, perhaps due to a complication during the + // build. As such, we provide the version manually. + version(app.getVersion()). help('help'). parse(args); } diff --git a/src/main/app/initialize.test.js b/src/main/app/initialize.test.js index b513f671..d83b3737 100644 --- a/src/main/app/initialize.test.js +++ b/src/main/app/initialize.test.js @@ -170,16 +170,6 @@ describe('main/app/initialize', () => { await initialize(); expect(app.setPath).toHaveBeenCalledWith('userData', '/basedir/some/dir'); }); - - it('should show version and exit when specified', async () => { - jest.spyOn(process.stdout, 'write').mockImplementation(() => {}); - const exitSpy = jest.spyOn(process, 'exit').mockImplementation(() => {}); - parseArgs.mockReturnValue({ - version: true, - }); - await initialize(); - expect(exitSpy).toHaveBeenCalledWith(0); - }); }); describe('initializeConfig', () => { diff --git a/src/main/app/initialize.ts b/src/main/app/initialize.ts index a4da8eb8..80f21d10 100644 --- a/src/main/app/initialize.ts +++ b/src/main/app/initialize.ts @@ -138,12 +138,6 @@ export async function initialize() { function initializeArgs() { global.args = parseArgs(process.argv.slice(1)); - // output the application version via cli when requested (-v or --version) - if (global.args.version) { - process.stdout.write(`v.${app.getVersion()}\n`); - process.exit(0); // eslint-disable-line no-process-exit - } - global.isDev = isDev && !global.args.disableDevMode; // this doesn't seem to be right and isn't used as the single source of truth if (global.args.dataDir) {