Skip to content

Commit

Permalink
feat(exact): checking exact versions works
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 18, 2015
1 parent 806718d commit f432df0
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 5 deletions.
14 changes: 12 additions & 2 deletions bin/exact-semver.js
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);
}
26 changes: 23 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
"main": "index.js",
"version": "0.0.0-semantic-release",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint bin/*.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
"test": "mocha src/*-spec.js",
"watch": "mocha src/*-spec.js --watch",
"lint": "eslint bin/*.js src/*.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"commit": "commit-wizard"
},
"repository": {
"type": "git",
Expand All @@ -28,6 +30,24 @@
"homepage": "https://github.com/bahmutov/exact-semver#readme",
"devDependencies": {
"eslint": "1.9.0",
"lazy-ass": "1.0.0",
"mocha": "2.3.4",
"pre-git": "1.2.11",
"semantic-release": "^4.3.5"
},
"config": {
"pre-git": {
"commit-msg": "validate-commit-msg",
"pre-commit": [],
"pre-push": [],
"post-commit": [],
"post-merge": []
}
},
"czConfig": {
"path": "node_modules/cz-conventional-changelog"
},
"dependencies": {
"check-more-types": "2.1.2"
}
}
37 changes: 37 additions & 0 deletions src/is-strict-semver-spec.js
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'));
});
});
8 changes: 8 additions & 0 deletions src/is-strict-semver.js
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;
32 changes: 32 additions & 0 deletions src/make-exact-spec.js
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);
});
});
17 changes: 17 additions & 0 deletions src/make-exact.js
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;

0 comments on commit f432df0

Please sign in to comment.