Security News
The Unpaid Backbone of Open Source: Solo Maintainers Face Increasing Security Demands
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
@korabo/genversion-plus
Advanced tools
A command line utility to read version from package.json and attach it into your module as a property
So you want yourmodule.version
to follow the version in package.json but are tired of updating it manually every time the version changes? On server side, you could just require('package.json').version
but on client side however, that would expose the versions of your dependencies and possibly other sensitive data too, so it is usually a naughty thing to do! How to import only the version? Genversion to the rescue!
Usage is simple:
$ cd yourmodule
$ npm install -g genversion
$ genversion lib/version.js
Voilà! The new lib/version.js:
module.exports = '1.2.3'
Use flags to match your coding style. $ genversion --es6 --semi lib/version.js
creates:
export const version = '1.2.3';
By default, genversion reads the version from the package.json
nearest to the target version.js
. In case your project contains multiple package.json
files along the target path you can specify the one with --source <path>
parameter.
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 string. For safety, the version file begins with a signature that tells genversion that the file can be overwritten.
// generated by genversion
module.exports = '1.2.3'
Now, your job is to 1) choose a path for the version 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 the path 'lib/version.js' and require it in yourmodule/index.js:
...
exports.version = require('./lib/version')
...
If you use --es6
flag:
...
import { version } from './lib/version'
export const version
...
Then, let us integrate genversion into your build task.
"scripts": {
"build": "genversion lib/version.js && other build stuff"
}
The target path is given as the first argument. If the file already exists and has been previously created by genversion, it is replaced with the new one.
Finished! Now 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 painstakingly debug a module, just to find out that it was a cached old version that caused the error. An inspectable version property would have helped a big time.
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
-s, --semi use semicolons in generated code
-e, --es6 use es6 syntax in generated code
-p, --source <path> search for package.json along a custom path
-h, --help output usage information
You can also use genversion within your code:
var gv = require('genversion');
The available properties and functions are listed below.
Check if it is possible to generate the version module into targetPath
.
Parameters:
process.cwd()
.Example:
gv.check('lib/version.js', function (err, doesExist, isByGenversion) {
if (err) {
throw err;
}
if (isByGenversion) {
gv.generate(...)
}
...
});
Read the version property from the nearest package.json
along the targetPath
and then generate a version module file at targetPath
. A custom path to package.json
can be specified with opts.source
.
Parameters:
process.cwd()
.targetPath
.false
.false
.package.json
. Parameter err is non-null if package.json
cannot be found, its version is not a string, or writing the module fails.Examples:
gv.generate('lib/version.js', function (err, version) {
if (err) {
throw err;
}
console.log('Sliding into', version, 'like a sledge.');
});
gv.generate('src/v.js', { useSemicolon: true }, function (err) {
if (err) { throw err }
console.log('Generated version file with a semicolon.')
})
The version string of genversion module in semantic versioning format. Generated with genversion itself, of course.
Do you use genversion in your project? We are happy to mention it in the list. Just hit us with an issue or a pull request.
Run test suite:
$ npm run test
To make release, bump the version in package.json
and run:
$ npm run release
FAQs
A command line utility to read version from package.json and attach it into your module as a property
We found that @korabo/genversion-plus demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.
Security News
A developer is accusing Tencent of violating the GPL by modifying a Python utility and changing its license to BSD, highlighting the importance of copyleft compliance.