From 1640ec693c3464eb4566404159161b05de15f502 Mon Sep 17 00:00:00 2001 From: Shawn Allen Date: Fri, 15 Mar 2019 14:58:26 -0700 Subject: [PATCH] fix: use local now, resolves #10 --- src/__tests__/now.js | 6 ++++-- src/now.js | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/__tests__/now.js b/src/__tests__/now.js index 00bc90a..ea48ba2 100644 --- a/src/__tests__/now.js +++ b/src/__tests__/now.js @@ -2,6 +2,8 @@ const execa = require('execa') const mockedEnv = require('mocked-env') const now = require('../now') +const {NOW_BIN} = now + jest.mock('execa') execa.mockImplementation(() => Promise.resolve({stderr: '', stdout: ''})) @@ -17,13 +19,13 @@ describe('now()', () => { it('calls `npx now --token=$NOW_TOKEN` with no additional args', () => { mockEnv({NOW_TOKEN: 'xyz'}) now() - expect(execa).lastCalledWith('npx', ['now', '--token=xyz'], {stderr: 'inherit'}) + expect(execa).lastCalledWith(NOW_BIN, ['--token=xyz'], {stderr: 'inherit'}) }) it('calls with additional arguments passed as an array', () => { mockEnv({NOW_TOKEN: 'abc'}) now(['foo', 'bar']) - expect(execa).lastCalledWith('npx', ['now', '--token=abc', 'foo', 'bar'], {stderr: 'inherit'}) + expect(execa).lastCalledWith(NOW_BIN, ['--token=abc', 'foo', 'bar'], {stderr: 'inherit'}) }) function mockEnv(env) { diff --git a/src/now.js b/src/now.js index 70914fc..2056581 100644 --- a/src/now.js +++ b/src/now.js @@ -1,10 +1,15 @@ const execa = require('execa') +const {bin: {now: NOW_BIN_PATH}} = require('now/package.json') +const NOW_BIN = require.resolve(`now/${NOW_BIN_PATH}`) + module.exports = function now(args = []) { const {NOW_TOKEN} = process.env if (!NOW_TOKEN) { throw new Error(`The NOW_TOKEN env var is required`) } - const nowArgs = ['now', `--token=${NOW_TOKEN}`, ...args] - return execa('npx', nowArgs, {stderr: 'inherit'}).then(res => res.stdout) + const nowArgs = [`--token=${NOW_TOKEN}`, ...args] + return execa(NOW_BIN, nowArgs, {stderr: 'inherit'}).then(res => res.stdout) } + +Object.assign(module.exports, {NOW_BIN})