yargs, the command line parsing library, was unable to read the app's version automatically from package.json fix by passing the app's version as electron sees it
This commit is contained in:
parent
4ebaebfd17
commit
8dd7d3dcc5
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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', () => {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue