Skip to content

Commit 1af60cb

Browse files
authored
add method for printing found chrome path (#255)
1 parent bc83630 commit 1af60cb

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

bin/print-chrome-path.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* @license Copyright 2021 Google Inc. All Rights Reserved.
5+
* 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
6+
* 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.
7+
*/
8+
'use strict';
9+
10+
/** @fileoverview Prints the Chrome path that chrome-launcher will use. */
11+
12+
const {getChromePath} = require('../dist/chrome-launcher.js');
13+
14+
const chromePath = getChromePath();
15+
process.stdout.write(chromePath);
16+
process.exit(0);

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"type-check": "tsc --allowJs --checkJs --noEmit --target es2019 *.js",
1414
"prepublishOnly": "npm run build && npm run test"
1515
},
16+
"bin": {
17+
"print-chrome-path": "bin/print-chrome-path.js"
18+
},
1619
"devDependencies": {
1720
"@types/mocha": "^8.0.4",
1821
"@types/sinon": "^9.0.1",

src/chrome-launcher.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ async function launch(opts: Options = {}): Promise<LaunchedChrome> {
8484
return {pid: instance.pid!, port: instance.port!, kill, process: instance.chrome!};
8585
}
8686

87+
/** Returns Chrome installation path that chrome-launcher will launch by default. */
88+
function getChromePath(): string {
89+
const installation = Launcher.getFirstInstallation();
90+
if (!installation) {
91+
throw new ChromeNotInstalledError();
92+
}
93+
return installation;
94+
}
95+
8796
async function killAll(): Promise<Array<Error>> {
8897
let errors = [];
8998
for (const instance of instances) {
@@ -420,4 +429,4 @@ class Launcher {
420429
};
421430

422431
export default Launcher;
423-
export {Launcher, launch, killAll};
432+
export {Launcher, launch, killAll, getChromePath};

test/chrome-launcher-test.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
'use strict';
77

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

1111
import {spy, stub} from 'sinon';
@@ -217,4 +217,14 @@ describe('Launcher', () => {
217217
const chromeInstance = new Launcher({chromePath: ''});
218218
chromeInstance.launch().catch(() => done());
219219
});
220+
221+
describe('getChromePath', async () => {
222+
it('returns the same path as a full Launcher launch', async () => {
223+
const spawnStub = await launchChromeWithOpts();
224+
const launchedPath = spawnStub.getCall(0).args[0] as string;
225+
226+
const chromePath = getChromePath();
227+
assert.strictEqual(chromePath, launchedPath);
228+
});
229+
});
220230
});

0 commit comments

Comments
 (0)