Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: michaellzc/better-opn
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v3.0.0
Choose a base ref
...
head repository: michaellzc/better-opn
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v3.0.1
Choose a head ref
  • 4 commits
  • 8 files changed
  • 4 contributors

Commits on Apr 2, 2021

  1. Copy the full SHA
    115a8ec View commit details

Commits on May 30, 2021

  1. build(deps): bump browserslist from 4.11.1 to 4.16.6 (#24)

    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored May 30, 2021
    Copy the full SHA
    d82a523 View commit details

Commits on Nov 5, 2021

  1. Copy the full SHA
    8b31033 View commit details
  2. chore(release): 3.0.1

    michaellzc committed Nov 5, 2021
    Copy the full SHA
    32ff5c5 View commit details
Showing with 1,116 additions and 69 deletions.
  1. +1 −1 .github/workflows/lint.yml
  2. +39 −0 .github/workflows/test.yml
  3. +12 −2 package.json
  4. +2 −2 src/index.js
  5. +1 −1 test/host-only.js
  6. +0 −3 test/open.js
  7. +134 −0 tests/open.js
  8. +927 −60 yarn.lock
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -30,4 +30,4 @@ jobs:
run: yarn --no-progress --non-interactive --no-lockfile

- name: Lint
run: yarn xo src --node-version ">=10.0.0"
run: yarn xo --node-version ">=10.0.0"
39 changes: 39 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
integration-test:
name: integration test on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: ['macos-latest']
steps:
- uses: actions/checkout@v1

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"

- uses: actions/cache@v1
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install
run: yarn --no-progress --non-interactive --no-lockfile

- name: Test on macOS
if: matrix.os == 'macos-latest'
run: yarn test
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "better-opn",
"version": "3.0.0",
"version": "3.0.1",
"description": "A better opn. Reuse the same tab on Chrome for 👨‍💻.",
"main": "dist/index.js",
"repository": {
@@ -36,10 +36,17 @@
"node"
]
},
"ava": {
"files": [
"tests/**/*",
"!test/host-only.js"
]
},
"scripts": {
"build": "babel src -d dist",
"_lint": "--node-version is a work around since we use v8 as target in babel anyway",
"lint": "xo src/ --fix --node-version \">=10.0.0\""
"lint": "xo --fix --node-version \">=10.0.0\"",
"test": "ava"
},
"dependencies": {
"open": "^8.0.4"
@@ -48,6 +55,9 @@
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"ava": "^3.6.0",
"lodash.countby": "^4.6.0",
"puppeteer-core": "^8.0.0",
"xo": "^0.28.3"
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -70,11 +70,11 @@ const startBrowserProcess = (browser, url, opts = {}, args = []) => {
// on OSX Chromium-based browser with AppleScript
execSync('ps cax | grep "' + chromiumBrowser + '"');
execSync(
`osascript ../openChrome.applescript "${encodeURI(url)}" ${
`osascript ../openChrome.applescript "${encodeURI(url)}" "${
process.env.OPEN_MATCH_HOST_ONLY === 'true'
? encodeURI(normalizeURLToMatch(url))
: encodeURI(url)
} "${chromiumBrowser}"`,
}" "${chromiumBrowser}"`,
{
cwd: __dirname,
stdio: 'ignore',
2 changes: 1 addition & 1 deletion test/host-only.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const opn = require('../src/index');
const opn = require('../src');

// Run test/open.js first, then run this to ensure tab is reused
process.env.OPEN_MATCH_HOST_ONLY = 'true';
3 changes: 0 additions & 3 deletions test/open.js

This file was deleted.

134 changes: 134 additions & 0 deletions tests/open.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const test = require('ava');
const countBy = require('lodash.countby');
const puppeteer = require('puppeteer-core');
const open = require('../src');

const browserName = 'Google Chrome';
const openUrl = 'https://www.google.com/';
const openSecondUrl = 'https://stackoverflow.com/';
let chromeExecutablePath;
if (process.platform === 'darwin') {
chromeExecutablePath = `/Applications/${browserName}.app/Contents/MacOS/${browserName}`;
} else if (process.platform === 'linux') {
// https://github.com/mujo-code/puppeteer-headful#usage
chromeExecutablePath = process.env.PUPPETEER_EXEC_PATH;
} else if (process.platform === 'win32') {
chromeExecutablePath = 'Chrome';
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

test.serial('the same tab is reused in browser on macOS', async t => {
if (process.platform === 'darwin') {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

// Open url with better-opn twice
await open(openUrl);
await open(openUrl);

// Workaround since new pages are not avaliable immediately
// https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698
await sleep(5000);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);
// Expect only one page is opened
t.is(openPagesCounter[openUrl], 1);

// Close browser
await browser.close();
} else {
// Skip for non-macOS environments
t.pass();
}
});

test.serial(
'two tabs are opened when opening two different urls in browser on macOS',
async t => {
if (process.platform === 'darwin') {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

// Open url with better-opn twice
await open(openUrl);
await open(openSecondUrl);

// Workaround since new pages are not avaliable immediately
// https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698
await sleep(5000);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);
// Expect only one of each page is opened
t.is(openPagesCounter[openUrl], 1);
t.is(openPagesCounter[openSecondUrl], 1);

// Close browser
await browser.close();
} else {
// Skip for non-macOS environments
t.pass();
}
}
);

test.serial('open url in browser', async t => {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

await open(openUrl);

// Workaround since new pages are not avaliable immediately
// https://github.com/puppeteer/puppeteer/issues/1992#issuecomment-444857698
await sleep(5000);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);

// Expect page is opened
t.is(openPagesCounter[openUrl], 1);

await browser.close();
});

test.serial(
'should not open browser when process.env.BROWSER is none',
async t => {
const browser = await puppeteer.launch({
headless: false,
executablePath: chromeExecutablePath,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
process.env.BROWSER = 'none';

// Open url
await open(openUrl);

// Get open pages/tabs
const openPages = (await browser.pages()).map(each => each.url());
const openPagesCounter = countBy(openPages);

// Expect no page is opened
t.is(openPagesCounter[openUrl], undefined);

// Clean up
process.env.BROWSER = browserName;
await browser.close();
}
);
Loading