Skip to content

Commit

Permalink
Add ability to set npm version from source package.json
Browse files Browse the repository at this point in the history
  • Loading branch information
theworkflow committed Nov 24, 2016
1 parent c7f1a0e commit 0a76804
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 5 deletions.
23 changes: 23 additions & 0 deletions lib/find-npm-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const Fs = require('fs')
const Path = require('path')

const Hoek = require('hoek')

const DEFAULT_NPM_VERSION = 'latest'

module.exports = (options) => {
var pkg

Hoek.assert(options !== undefined, 'options is required')

if (typeof options.npmVersion !== 'undefined') return options.npmVersion

try {
pkg = Fs.readFileSync(Path.resolve(options.input, 'package.json'), 'utf8')
pkg = JSON.parse(pkg)

if (pkg.engines && pkg.engines.npm) return pkg.engines.npm
} catch (err) {}

return DEFAULT_NPM_VERSION
}
3 changes: 2 additions & 1 deletion lib/update-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const Path = require('path')
const Hoek = require('hoek')

const FindNodeVersion = require('./find-node-version')
const FindNpmVersion = require('./find-npm-version')

module.exports = (options, done) => {
var packagePath, packageContents
Expand All @@ -25,7 +26,7 @@ module.exports = (options, done) => {

packageContents.engines = {
node: FindNodeVersion(options),
npm: options.npmVersion || 'latest'
npm: FindNpmVersion(options)
}

packageContents.main = '../../main.js'
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"preferGlobal": true,
"engines": {
"node": ">= 0.10.0"
"node": ">= 0.10.0",
"npm": ">=3.10.8"
}
}
75 changes: 75 additions & 0 deletions test/find-npm-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const Code = require('code')
const Lab = require('lab')
const Fs = require('fs')
const Path = require('path')
const Sinon = require('sinon')

const FindVersion = require('../lib/find-npm-version')

var lab = exports.lab = Lab.script()

var describe = lab.describe
var beforeEach = lab.beforeEach
var afterEach = lab.afterEach
var it = lab.it
var Expect = Code.expect

describe('find-npm-version', () => {
var options

beforeEach((done) => {
options = {
directory: process.env.PWD,
npmVersion: '3.10.8',
input: process.env.PWD
}

done()
})

describe('when options.npmVersion is set', () => {
it('sets npm version from --npm-version', (done) => {
Expect(FindVersion(options)).to.equal(options.npmVersion)
done()
})
})

describe('when setting version from options.input package.json', () => {
it('sets npm version from bundle package.json', (done) => {
var pkg = require(Path.resolve(options.input, 'package.json'))
delete options.npmVersion
Expect(FindVersion(options)).to.equal(pkg.engines.npm)
done()
})
})

describe('when setting version to default', () => {
var fsStub

beforeEach((done) => {
fsStub = Sinon.stub(Fs, 'readFileSync').returns('{}')
done()
})

afterEach((done) => {
fsStub.restore()
done()
})

it('sets npm version to latest', (done) => {
delete options.npmVersion
Expect(FindVersion(options)).to.equal('latest')
done()
})
})

describe('when no options are provided', () => {
it('throws an error', (done) => {
Expect(() => {
FindVersion()
}).to.throw()

done()
})
})
})
7 changes: 4 additions & 3 deletions test/update-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,17 @@ describe('update-package', () => {
})
})

describe('defaults to latest version of npm', () => {
it('sets npm version to latest', (done) => {
describe('uses bundle package.json for npm version', () => {
it('sets npm version to bundle engines npm version', (done) => {
var pkg = require(Path.resolve(options.input, 'package.json'))
delete options.npmVersion
UpdatePackage(options, () => {
var path = Path.resolve('./bundle/programs/server/package.json')
var json = [
'{',
' "engines": {',
` "node": "${options.nodeVersion}",`,
' "npm": "latest"',
` "npm": "${pkg.engines.npm}"`,
' },',
' "main": "../../main.js",',
' "scripts": {',
Expand Down

0 comments on commit 0a76804

Please sign in to comment.