grunt-prompt
grunt-prompt copied to clipboard
Reference Errors: currentVersion and semver
When trying to run the example you have for working with grunt-bump, I get the following errors:
ReferenceError: currentVersion is not defined
and ReferenceError: semver is not defined
Any idea what I'm doing wrong? Thanks!
Add this to the top of your Gruntfile.js
var semver = require('semver');
var currentVersion = require('./package.json').version;
And then, how could we use the values stored by grunt-prompt into grunt-bump ?
I tried this in bump.js
but this is not working, i always get the default values ( strings after || )
module.exports = function (grunt, options) {
return {
options: {
files: grunt.config('bump.files') || ['package.json'],
updateConfigs: [ 'pkg' ],
commit: grunt.config('bump.commit') || true,
commitMessage: 'Release v%VERSION%',
commitFiles: grunt.config('bump.commitFiles') || ['package.json'],
createTag: grunt.config('bump.createTag') || true,
tagName: 'v%VERSION%',
tagMessage: 'Version %VERSION%',
push: grunt.config('bump.push') || false,
pushTo: 'upstream',
gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d',
globalReplace: false,
prereleaseName: grunt.config('bump.preReleaseName') || 'beta',
regExp: false
}
};
};
Thanks a lot
It looks like the docs are a bit out of date, looking through the code it looks like you should be doing this:
bump: {
options: {
files: ['<%= bump.files %>'],
updateConfigs: [],
commit: false,
commitMessage: 'Release Version {%= version %}',
tag: false
}
},
It seems its not working
module.exports = function (grunt, options) {
return {
options: {
files: ['<%= bump.files %>'],
updateConfigs: [ 'pkg' ],
commit: '<%= bump.commit %>',
commitMessage: 'Release v%VERSION%',
commitFiles: ['<%= bump.commitFiles %>'],
createTag: '<%= bump.createTag %>',
tagName: 'v%VERSION%',
tagMessage: 'Version %VERSION%',
push: '<%= bump.push %>',
pushTo: 'upstream',
gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d',
globalReplace: false,
prereleaseName: '<%= bump.preReleaseName %>',
regExp: false
}
};
};
Result
$ grunt prompt:bump
Running "prompt:bump" (prompt) task
? Bump version from 0.0.8 to: Patch: 0.0.9 Backwards-compatible bug fixes.
? What should get the new version: package.json
? Should bump changes and changelog be committed ? Yes
? Which files should be commited: package.json, CHANGELOG.md
? Create a new tag ? No
? Push commited files to repository ? No
Running "bump-only:patch" (bump-only) task
Running "bump:patch:bump-only" (bump) task
>> Version bumped to 0.0.9 (in package.json)
>> pkg's version updated
Running "changelog" task
>> Generating changelog from v0.0.5 to HEAD...
>> Parsed 0 commits.
>> CHANGELOG.md updated
Running "bump::commit-only" (bump) task
Fatal error: Can not create the commit:
error: pathspec 'package.json,CHANGELOG.md' did not match any file(s) known to git.
Execution Time (2015-04-20 13:52:12 UTC)
loading tasks 1.2s ▇▇▇▇ 9%
prompt:bump 12.1s ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇ 86%
changelog 189ms ▇ 1%
bump::commit-only 545ms ▇▇ 4%
Total 14.1s
``
options.files
Type: Array Default value: ['package.json']
Maybe you wanna bump 'component.json' instead? Or maybe both: ['package.json', 'component.json']? Can be either a list of files to bump (an array of files) or a grunt glob (e.g., ['*.json']).
You'll probably have to do some helper function to split it to any array. It looks like bump ants file to be an array.
ok,
thanks a lot,
module.exports = function (grunt, options) {
function toArray($arg){
return $arg !== undefined ? $arg.split(',') : false;
}
return {
options: {
files: toArray(grunt.config('bump.files')) || ['package.json'],
updateConfigs: [ 'pkg' ],
commit: grunt.config('bump.commit') || true,
commitMessage: 'Release v%VERSION%',
commitFiles: toArray(grunt.config('bump.commitFiles')) || ['package.json'],
createTag: grunt.config('bump.createTag') || true,
tagName: 'v%VERSION%',
tagMessage: 'Version %VERSION%',
push: grunt.config('bump.push') || false,
pushTo: 'upstream',
gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d',
globalReplace: false,
prereleaseName: grunt.config('bump.preReleaseName') || 'beta',
regExp: false
}
};
};
argh ... its still not working :( still using default values :(
Ah ok, i'ts just that bump values are wrapped in an 'options' array. In case someone want it, see code below.
bump.js
module.exports = function (grunt, options) {
return {
options: {
files: ["package.json"],
updateConfigs: [ "pkg" ],
commit: true,
commitMessage: "Up v%VERSION%",
commitFiles: ["package.json"],
createTag: true,
tagName: "v%VERSION%",
tagMessage: "Version %VERSION%",
push: false,
pushTo: "upstream",
gitDescribeOptions: "--tags --always --abbrev=1 --dirty=-d",
globalReplace: false,
prereleaseName: "beta",
regExp: false
}
};
};
prompt.js
module.exports = function (grunt, options) {
var semver = require('semver');
return {
bump: {
options: {
questions: [
{
config: 'bump.prompt.increment',
type: 'list',
message: 'Bump version from ' + options.pkg.version + ' to:',
choices: [
{
value: 'build',
name: 'Build: ' + (options.pkg.version + '-?') + ' Unstable, betas, and release candidates.'
},
{
value: 'patch',
name: 'Patch: ' + semver.inc(options.pkg.version, 'patch') + ' Backwards-compatible bug fixes.'
},
{
value: 'minor',
name: 'Minor: ' + semver.inc(options.pkg.version, 'minor') + ' Add functionality in a backwards-compatible manner.'
},
{
value: 'major',
name: 'Major: ' + semver.inc(options.pkg.version, 'major') + ' Incompatible API changes.'
},
{
value: 'custom',
name: 'Custom version number (format: 0.0.0)'
}
]
},
{
config: 'bump.prompt.version',
type: 'input',
message: 'What specific version would you like',
when: function (answers) {
return answers['bump.prompt.increment'] === 'custom';
},
validate: function (value) {
var valid = semver.valid(value);
return valid || 'Must be a valid semver, such as 1.2.3-rc1. See http://semver.org/ for more details.';
}
},
{
config: 'bump.prompt.useDefaults',
type: 'confirm',
message: 'Use default values ([config/mastack | .mastack/src]/grunt/actions/bump.js) ?',
default: false
},
{
config: 'bump.options.prereleaseName',
type: 'input',
message: 'Enter the pre-release name for your version (eg. 1.2.3-beta):',
default: 'beta',
when: function (answers) {
return ( answers['bump.prompt.increment'] === 'build' && answers['bump.prompt.useDefaults'] === false );
},
validate: function (value) {
var valid = value != "";
return valid || 'Please, enter something.';
}
},
{
config: 'bump.options.files',
type: 'checkbox',
message: 'What should get the new version:',
choices: [
{
value: 'package.json',
name: 'package.json' + (!grunt.file.isFile('package.json') ? ' not found, will create one' : ''),
checked: grunt.file.isFile('package.json')
},
{
value: 'bower.json',
name: 'bower.json' + (!grunt.file.isFile('bower.json') ? ' not found, will create one' : ''),
checked: grunt.file.isFile('bower.json')
}
],
when: function (answers) {
return answers['bump.prompt.useDefaults'] === false;
}
},
{
config: 'bump.options.commit',
type: 'confirm',
message: 'Should bump changes and changelog be committed ?',
default: true,
when: function (answers) {
return answers['bump.prompt.useDefaults'] === false;
}
},
{
config: 'bump.options.commitFiles',
type: 'checkbox',
message: 'Which files should be commited:',
when: function (answers) {
return ( answers['bump.options.commit'] === true && answers['bump.prompt.useDefaults'] === false );
},
choices: [
{
value: 'package.json',
name: 'package.json' + (!grunt.file.isFile('package.json') ? ' not found' : ''),
checked: grunt.file.isFile('package.json')
},
{
value: 'bower.json',
name: 'bower.json' + (!grunt.file.isFile('bower.json') ? ' not found' : ''),
checked: grunt.file.isFile('bower.json')
},
{
value: 'CHANGELOG.md',
name: 'CHANGELOG.md' + (!grunt.file.isFile('CHANGELOG.md') ? ' not found' : ''),
checked: grunt.file.isFile('CHANGELOG.md')
}
]
},
{
config: 'bump.options.createTag',
type: 'confirm',
message: 'Create a new tag ?',
default: true,
when: function (answers) {
return answers['bump.prompt.useDefaults'] === false;
}
},
{
config: 'bump.options.push',
type: 'confirm',
message: 'Push commited files to repository ?',
default: false,
when: function (answers) {
return answers['bump.prompt.useDefaults'] === false;
}
}
],
then: function (results) {
console.log(grunt.config('bump'));
if (results['bump.prompt.increment'] === 'custom') {
// Run task with custom number
grunt.task.run([
'bump-only --setversion=' + results['bump.prompt.version'],
'changelog',
'bump-commit'
]);
} else {
grunt.task.run([
'bump-only:' + results['bump.prompt.increment'],
'changelog',
'bump-commit'
]);
}
}
}
}
};
};