npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

assert-version

v0.0.3

Published

Check if version mentions match the one from package.json

Downloads

8

Readme

assert-version

Check if version mentions match the one from package.json

Install

npm install -g assert-version

How matches work

Version could be extracted with regular expression or, if you use assert-version as Node.js module, with user-defined function.

In case of regular expression, version should present in first capture.

There are two default regular expressions. If your file has .json extension, /"version":\s*"([^"]+)/ expression is used. Otherwise, /\/\*\![\s\S]+(v?\d+\.\d+\.\d+)/ expression is used. I understand that these expressions are not great, they just seem to work for my cases. Feel free to send improvements.

Extracted version should be valid semver version.

Use as Node.js module

assertVersion = require('assert-version');

var error = assertVersion({
    'bower.json': '', // First default regular expression will be used.
    'mylib.js': '', // Second default regular expression will be used.
    'other.js': /version=([^,]+)/,
    'other2.js': function(contents) {
        // `contents` is a file contents, function should return version as
        // string.
        return contents.substring(100500, 100510);
    },
    'other3.js': [matcher1, matcher2] // If you need to check more than one place in file.
});

// `error` will have error message in case of error, `undefined` otherwise.

Optionally, you can pass path to package.json with reference version as second argument of assertVersion(). It is package.json from current working directory by default.

Use as command line tool

Same to previous example (except for function extractor):

assert-version -f bower.json -f mylib.js -f other.js='version=([^,]+)' -f other3.js=matcher1 -f other3.js=matcher2

Example (Gulp task)

Example from https://github.com/hoho/conkitty-route/blob/master/gulpfile.js.

gulp.task('assert-version', function(err) {
    var assertVersion = require('assert-version');

    err(assertVersion({
        'croute.js': '',
        'bower.json': ''
    }));
});

Example (Grunt task)

Example from https://github.com/hoho/concat.js/blob/master/Gruntfile.js.

grunt.registerTask('assert-version', function() {
    var assertVersion = require('assert-version'),
        error;

    error = assertVersion({
        'concat.js': '',
        'bower.json': ''
    });

    if (error) {
        grunt.log.error(error);
        return false;
    }
});