Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with modifying "package.json" within a custom setter #12

Open
alphanull opened this issue Oct 20, 2014 · 0 comments
Open

Problem with modifying "package.json" within a custom setter #12

alphanull opened this issue Oct 20, 2014 · 0 comments

Comments

@alphanull
Copy link

Hello,

I have the following problem with bumpup:

  • I need to up a build number with each build (no matter what else increases!), but unfortunately this seems not to be supported by bumpup (have to set it manually, but why?)
  • So therefore I wrote a custom setter. This uses another value I defined in package.json called "build"
  • with each build, this value is increased (just a Number) and appended to the bumped Version

So far this works, but fails when trying to save the newly modified value back to disk, using grunt.file.write.

After looking at the bumpup code, this is what seems to happen:

  • bumpup reads the file directly from the "package.json" file
  • and writes the same data back when finished

Of course, any modifications to the package config - done in a setter - are ignored and overwritten! So, I think it would be better if bumpup reads the config from grunt instead from a file, and makes sure it writes the same (current) config back to disk. By this way, setters would have a chance to actually modify something here without being forgotten.

To test my assertion I made the following changes to bumpup:

var meta = grunt.config.get().pkg;
var indentation = grunt.option('indentation') || ' ';
grunt.log.verbose.writeln('Bumping "' + filepath.cyan + '":');

// Update properties with defined setters
Object.keys(setters).forEach(function (key) {

    if (!Object.prototype.hasOwnProperty.call(meta, key)) {
        return;
    }

    var newValue;
    if (o.normalize && norm[key] != null) {
        newValue = norm[key];
    } else {
        norm[key] = newValue = setters[key](meta[key], releaseType, o, buildMeta);
    }

    if (newValue != null) {
        grunt.config.set("pkg." + key, newValue);
        grunt.log.verbose.writeln(grunt.util.repeat(Math.max(16 - key.length, 0), ' ') + key + ' : ' + newValue);
    }

});

// Update config property
if (updatePropsMap[filepath]) {
    grunt.config.set(updatePropsMap[filepath], meta);
}

// Stringify new metafile and save
if (!grunt.file.write(filepath, JSON.stringify(grunt.config.get().pkg, null, indentation) + (o.newlineEof ? "\n" : ''))) {
    grunt.log.warn('Couldn\'t write to "' + filepath + '"');
}

Of course, this is only "quick and dirty", but I think this would be a more flexible solution. An alternative would be an option to bump the build number automatically. Or both ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant