Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method for printing found chrome path #255

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions bin/print-chrome-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node
Copy link
Collaborator

@connorjclark connorjclark Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was chmod +x'd, although I don't know if there is a way to confirm that in GH ui.

related, benefits to using shebang vs just setting the bin script to node bin/print-chrome-path.js ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was chmod +x'd, although I don't know if there is a way to confirm that in GH ui

npm/yarn does the chmod on install

related, benefits to using shebang vs just setting the bin script to node bin/print-chrome-path.js ?

unlike script entries, bin entries are just filenames that are symlinked into node_modules/.bin, so need the shebang

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, thx!


/**
* @license Copyright 2021 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

/** @fileoverview Prints the Chrome path that chrome-launcher will use. */

const {getChromePath} = require('../dist/chrome-launcher.js');

const chromePath = getChromePath();
process.stdout.write(chromePath);
process.exit(0);
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
"type-check": "tsc --allowJs --checkJs --noEmit --target es2019 *.js",
"prepublishOnly": "npm run build && npm run test"
},
"bin": {
"print-chrome-path": "bin/print-chrome-path.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this file plz :)

},
"devDependencies": {
"@types/mocha": "^8.0.4",
"@types/sinon": "^9.0.1",
Expand Down
11 changes: 10 additions & 1 deletion src/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ async function launch(opts: Options = {}): Promise<LaunchedChrome> {
return {pid: instance.pid!, port: instance.port!, kill, process: instance.chrome!};
}

/** Returns Chrome installation path that chrome-launcher will launch by default. */
function getChromePath(): string {
const installation = Launcher.getFirstInstallation();
if (!installation) {
throw new ChromeNotInstalledError();
}
return installation;
}

async function killAll(): Promise<Array<Error>> {
let errors = [];
for (const instance of instances) {
Expand Down Expand Up @@ -420,4 +429,4 @@ class Launcher {
};

export default Launcher;
export {Launcher, launch, killAll};
export {Launcher, launch, killAll, getChromePath};
12 changes: 11 additions & 1 deletion test/chrome-launcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
'use strict';

import {Launcher, launch, killAll, Options} from '../src/chrome-launcher';
import {Launcher, launch, killAll, Options, getChromePath} from '../src/chrome-launcher';
import {DEFAULT_FLAGS} from '../src/flags';

import {spy, stub} from 'sinon';
Expand Down Expand Up @@ -217,4 +217,14 @@ describe('Launcher', () => {
const chromeInstance = new Launcher({chromePath: ''});
chromeInstance.launch().catch(() => done());
});

describe('getChromePath', async () => {
it('returns the same path as a full Launcher launch', async () => {
const spawnStub = await launchChromeWithOpts();
const launchedPath = spawnStub.getCall(0).args[0] as string;

const chromePath = getChromePath();
assert.strictEqual(chromePath, launchedPath);
});
});
});