Package Exports
- genversion
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (genversion) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
genversion
So you want yourmodule.version
to follow the version in package.json but are tired of updating it manually every time the version changes? You could import your package.json into the module but you know it is a naughty thing to do! Genversion to the rescue!
Usage is simple. First install via npm.
$ npm install genversion --save-dev
Genversion works by first reading the current version from package.json and then generating a simple CommonJS module file that exports the version:
module.exports = '1.2.3';
Your job is to 1) choose a location for the file, 2) require() the new file into your module, and 3) add genversion as a part of your build or release pipeline. For example, let us choose location 'lib/version.js' and import it into yourmodule:
// At yourmodule/index.js
exports.version = require('./lib/version');
Then, let us integrate genversion into our build task. The target location is given as the first argument. If the file already exists, it is replaced with the new one. But do not worry, genversion will only replace files generated by genversion itself.
"scripts": {
"build": "genversion lib/version.js && other build stuff"
}
After this, your module has a version property that matches with package.json and is updated every time you build the project.
> var yourmodule = require('yourmodule')
> yourmodule.version
'1.2.3'
Great! Having a version property in your module is very convenient for debugging. More than once we have needed to debug a module for a long time just to find that it was a cached old version that caused the error. An inspectable version property would have helped a big time.
Command line API
Directly from $ genversion --help
:
Usage: genversion [options] <target>
Generates a version module at the target filepath.
Options:
-V, --version output the version number
-v, --verbose Output the new version.
-h, --help output usage information
Node API
You can also use genversion within your code:
var gv = require('genversion');
The available properties and functions are listed below.
genversion.check(targetPath, callback)
Check if it is possible to generate the version module into targetPath.
Parameters:
- targetPath: string. An absolute or relative file path. Relative to
process.cwd()
. - callback: function (err, doesExist, isByGenversion). Parameter doesExist is a boolean that is true if a file at targetPath already exists. Parameter isByGenversion is a boolean that is true if the existing file seems like it has been generated by genversion.
Example:
gv.check('lib/version.js', function (err, doesExist, isByGenversion) {
if (err) {
throw err;
}
if (isByGenversion) {
gv.generate(...)
}
...
});
genversion.generate(targetPath, callback)
Read the version from package.json close to targetPath and generate a version module into targetPath.
Parameters:
- targetPath: string. An absolute or relative file path. Relative to
process.cwd()
. - callback: function (err, version). Parameter version is the version string read from package.json. Parameter err is non-null if package.json cannot be found, its version is not a string, or writing the module fails.
Example:
gv.generate('lib/version.js', function (err, version) {
if (err) {
throw err;
}
console.log('Sliding into', version, 'like a sledge.');
});
genversion.version
The version string of genversion module in semantic versioning format.
Projects using genversion
Related projects
For developers
Run test suite:
$ npm run test
To make release, bump the version in package.json and run:
$ npm run release