Skip to content

Commit

Permalink
Replace opn by open lib
Browse files Browse the repository at this point in the history
  • Loading branch information
aswasif007 committed Dec 18, 2023
1 parent e6bdf79 commit 73cafbd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
10 changes: 7 additions & 3 deletions __tests__/commands/phpmyadmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* External dependencies
*/
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import opn from 'opn';

/**
* Internal dependencies
Expand All @@ -23,7 +22,6 @@ const mutationMock = jest.fn( async () => {
} );

jest.mock( '../../src/lib/api' );
jest.mock( 'opn' );
jest.mocked( API ).mockImplementation( () => {
return Promise.resolve( {
mutate: mutationMock,
Expand All @@ -38,6 +36,11 @@ describe( 'commands/PhpMyAdminCommand', () => {
const env = { id: 456, jobs: [] };
const tracker = jest.fn() as CommandTracker;
const cmd = new PhpMyAdminCommand( app, env, tracker );
const openUrl = jest.spyOn( cmd, 'openUrl' );

beforeEach( () => {
openUrl.mockReset();
} );

it( 'should generate a URL by calling the right mutation', async () => {
await cmd.run();
Expand All @@ -50,9 +53,10 @@ describe( 'commands/PhpMyAdminCommand', () => {
},
} );
} );

it( 'should open the generated URL in browser', async () => {
await cmd.run();
expect( opn ).toHaveBeenCalledWith( 'http://test-url.com', { wait: false } );
expect( openUrl ).toHaveBeenCalledWith( 'http://test-url.com' );
} );
} );
} );
10 changes: 6 additions & 4 deletions src/commands/phpmyadmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import chalk from 'chalk';
import { GraphQLFormattedError } from 'graphql';
import gql from 'graphql-tag';
import opn from 'opn';

/**
* Internal dependencies
Expand Down Expand Up @@ -66,6 +65,11 @@ export class PhpMyAdminCommand {
console.log( msg );
}

async openUrl( url: string ) {
const { default: open } = await import( 'open' );
void open( url, { wait: false } );
}

async run( silent = false ) {
this.silent = silent;

Expand All @@ -92,9 +96,7 @@ export class PhpMyAdminCommand {
} );
exit.withError( `Failed to generate PhpMyAdmin URL: ${ error.message }` );
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
void opn( url, { wait: false } );
void this.openUrl( url );
this.log( 'PhpMyAdmin is opened in your default browser.' );
}
}
1 change: 0 additions & 1 deletion src/global.d.ts

This file was deleted.

0 comments on commit 73cafbd

Please sign in to comment.