-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to set npm version from source package.json
- Loading branch information
1 parent
c7f1a0e
commit 0a76804
Showing
5 changed files
with
106 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
}, | ||
"preferGlobal": true, | ||
"engines": { | ||
"node": ">= 0.10.0" | ||
"node": ">= 0.10.0", | ||
"npm": ">=3.10.8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters