Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Commit

Permalink
feat(compatibility): Refactor for compatibility with node v4 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonkberg authored Mar 5, 2017
1 parent a8715a0 commit 14423d6
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ cache: yarn
node_js:
- '7'
- '6'
- '4'
12 changes: 7 additions & 5 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#!/usr/bin/env node

const { spawn } = require('child_process')
const { bin, cwd, args, isYarn, isNPM } = require('./index')
'use strict'

if (!isYarn && !isNPM) {
const spawn = require('child_process').spawn
const nyr = require('./index')

if (!nyr.isYarn && !nyr.isNPM) {
throw new Error('Expected to be launched from either npm or yarn.')
}

if (args.length < 2) {
if (nyr.args.length < 2) {
throw new Error('Expected at least one argument with what script to run.')
}

const child = spawn(bin, args, { cwd, stdio: 'inherit' })
const child = spawn(nyr.bin, nyr.args, { cwd: nyr.cwd, stdio: 'inherit' })

child.on('exit', code => process.exit(code))
15 changes: 6 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const {
env: { npm_execpath: bin, DEBUG },
argv,
cwd,
} = process
'use strict'

const args = argv.slice(2)
const bin = process.env.npm_execpath
const args = process.argv.slice(2)

if (args[0] !== 'run') {
args.unshift('run')
Expand All @@ -13,13 +10,13 @@ if (args[0] !== 'run') {
module.exports = {
bin,
args,
cwd: cwd(),
cwd: process.cwd(),
isYarn: bin.endsWith('yarn.js'),
isNPM: bin.endsWith('npm-cli.js'),
}

if (DEBUG === 'nyr') {
const { inspect } = require('util')
if (process.env.DEBUG === 'nyr') {
const inspect = require('util').inspect
const colors = process.stdout.isTTY
console.log(
'-- nyr DEBUG START --\n%s\n-- nyr DEBUG END --',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"start": "DEBUG=nyr node ./index.js"
},
"engine": {
"node": ">=6"
"node": ">=4"
},
"devDependencies": {
"@simonkberg/eslint-config": "^5.0.0",
Expand Down
10 changes: 2 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
const { spawn } = require('child_process')
const { assign } = Object
const spawn = require('child_process').spawn
const options = {
cwd: process.cwd(),
stdio: 'inherit',
}

const run = name => new Promise((resolve, reject) => {
const args = ['run', `test:${name}`]
const opts = assign({}, options, {
env: assign({}, process.env, {
_: name,
}),
})
const child = spawn(name, args, opts)
const child = spawn(name, args, options)

child.on('error', err => reject(err))
child.on('exit', code =>
Expand Down
12 changes: 6 additions & 6 deletions test/npm.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require('assert')
const { bin, args, cwd, isYarn, isNPM } = require('../index')
const nyr = require('../index')

assert(bin.endsWith('npm-cli.js'), `'bin' is '${bin}'`)
assert(args.indexOf('run') === 0, `'args' is '${args}'`)
assert(cwd === process.cwd(), `'cwd' is '${cwd}'`)
assert(isYarn === false, `'isYarn' is '${isYarn}'`)
assert(isNPM === true, `'isNPM' is '${isNPM}'`)
assert(nyr.bin.endsWith('npm-cli.js'), `'bin' is '${nyr.bin}'`)
assert(nyr.args.indexOf('run') === 0, `'args' is '${nyr.args}'`)
assert(nyr.cwd === process.cwd(), `'cwd' is '${nyr.cwd}'`)
assert(nyr.isYarn === false, `'isYarn' is '${nyr.isYarn}'`)
assert(nyr.isNPM === true, `'isNPM' is '${nyr.isNPM}'`)
12 changes: 6 additions & 6 deletions test/yarn.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require('assert')
const { bin, args, cwd, isYarn, isNPM } = require('../index')
const nyr = require('../index')

assert(bin.endsWith('yarn.js'), `'bin' is '${bin}'`)
assert(args.indexOf('run') === 0, `'args' is '${args}'`)
assert(cwd === process.cwd(), `'cwd' is '${cwd}'`)
assert(isYarn === true, `'isYarn' is '${isYarn}'`)
assert(isNPM === false, `'isNPM' is '${isNPM}'`)
assert(nyr.bin.endsWith('yarn.js'), `'bin' is '${nyr.bin}'`)
assert(nyr.args.indexOf('run') === 0, `'args' is '${nyr.args}'`)
assert(nyr.cwd === process.cwd(), `'cwd' is '${nyr.cwd}'`)
assert(nyr.isYarn === true, `'isYarn' is '${nyr.isYarn}'`)
assert(nyr.isNPM === false, `'isNPM' is '${nyr.isNPM}'`)

0 comments on commit 14423d6

Please sign in to comment.