-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exact): checking exact versions works
- Loading branch information
Showing
6 changed files
with
129 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 |
---|---|---|
@@ -1,15 +1,25 @@ | ||
#!/usr/bin/env node | ||
|
||
var exists = require('fs').existsSync; | ||
var load = require('fs').readFileSync; | ||
var save = require('fs').writeFileSync; | ||
var join = require('path').join; | ||
var pkgFilename = join(process.cwd(), 'package.json'); | ||
|
||
function checkPackageExists() { | ||
function verifyPackageExists() { | ||
if (!exists(pkgFilename)) { | ||
/* eslint no-console:0 */ | ||
console.error('WARNING: cannot find package.json in the current folder'); | ||
process.exit(-1); | ||
} | ||
} | ||
|
||
checkPackageExists(); | ||
verifyPackageExists(); | ||
|
||
var pkg = JSON.parse(load(pkgFilename)); | ||
var makeExact = require('../src/make-exact'); | ||
var needSaving = makeExact(pkg); | ||
if (needSaving) { | ||
save(pkgFilename, JSON.stringify(pkg, null, 2) + '\n'); | ||
console.log('saved exact semver numbers in %s', pkgFilename); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
var la = require('lazy-ass'); | ||
var check = require('check-more-types'); | ||
|
||
describe('is-strict-semver', function () { | ||
var isStrictSemver = require('./is-strict-semver'); | ||
|
||
it('is a function', function () { | ||
la(check.fn(isStrictSemver)); | ||
}); | ||
|
||
it('passes strict versions', function () { | ||
la(isStrictSemver('0.1.0')); | ||
la(isStrictSemver('1.1.0')); | ||
la(isStrictSemver('1.1.20')); | ||
la(isStrictSemver('10.1.0')); | ||
}); | ||
|
||
it('fails fuzzy chars', function () { | ||
la(!isStrictSemver('~0.1.0'), '~'); | ||
la(!isStrictSemver('^0.1.0'), '^'); | ||
}); | ||
|
||
it('fails wildcards', function () { | ||
la(!isStrictSemver('0.1.*'), '* patch'); | ||
la(!isStrictSemver('0.*.0'), '* minor'); | ||
la(!isStrictSemver('*.1.2'), '* major'); | ||
}); | ||
|
||
it('fails on missing numbers', function () { | ||
la(!isStrictSemver('1'), 'major only'); | ||
la(!isStrictSemver('1.0'), 'minor'); | ||
}); | ||
|
||
it('fails on tags', function () { | ||
la(!isStrictSemver('1.0.0-foo')); | ||
}); | ||
}); |
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,8 @@ | ||
function isStrictSemver(version) { | ||
// TODO use check-more-types when available | ||
// https://github.com/kensho/check-more-types/issues/31 | ||
var pattern = /^[0-9]+\.[0-9]+\.[0-9]+$/; | ||
return pattern.test(version); | ||
} | ||
|
||
module.exports = isStrictSemver; |
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,32 @@ | ||
var la = require('lazy-ass'); | ||
var check = require('check-more-types'); | ||
|
||
describe('make exact', function () { | ||
var makeExact = require('./make-exact'); | ||
|
||
it('is a function', function () { | ||
la(check.fn(makeExact)); | ||
}); | ||
|
||
it('returns false if there are no dependencies', function () { | ||
var needSaving = makeExact({}); | ||
la(!needSaving); | ||
}); | ||
|
||
it('returns false if empty dependencies', function () { | ||
var needSaving = makeExact({ | ||
dependencies: {} | ||
}); | ||
la(!needSaving); | ||
}); | ||
|
||
it('returns false is exact semver', function () { | ||
var needSaving = makeExact({ | ||
dependencies: { | ||
foo: '1.0.2', | ||
bar: '2.0.1' | ||
} | ||
}); | ||
la(!needSaving); | ||
}); | ||
}); |
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,17 @@ | ||
var isStrictSemver = require('./is-strict-semver'); | ||
|
||
function makeExact(pkg) { | ||
var needSaving; | ||
if (pkg.dependencies) { | ||
Object.keys(pkg.dependencies).forEach(function (name) { | ||
var version = pkg.dependencies[name]; | ||
if (!isStrictSemver(version)) { | ||
version = toExact(version); | ||
pkg.dependencies[name] = version; | ||
needSaving = true; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = makeExact; |