Skip to content

Instantly share code, notes, and snippets.

@MarZab
Last active May 20, 2017
Embed
What would you like to do?
comments for npm package.json with a grunt.js task
/*jslint node: true */
'use strict';
module.exports = function (grunt) {
/*
1. make package.json.js and export the json that will be saved as package.json
2. run grunt package.json, the new file will be written and you get a patch as well
*/
grunt.registerTask('package.json', function () {
var fs = require('fs');
var pnew = require('./package.json.js');
var pcurrent = JSON.parse(fs.readFileSync('./package.json'));
var x = require('xdiff')
var diff = x.diff(pcurrent, pnew);
if (diff) {
console.log('Writing new package.json with changes:');
console.log(diff);
fs.writeFileSync('./package.json', JSON.stringify(pnew, null, ' '));
} else {
console.log('No changes.');
}
});
};
{
"name": "your-project",
"version": "0.0.0",
"private": true,
"dependencies": {},
"devDependencies": {
"xdiff": "^0.2.11",
"grunt": "^0.4.5"
}
}
'use strict';
/*
You can use any kind of valid javascript comments in this file.
- marko
ps: you could also use this script to dynamicaly create a package.js
*/
module.exports = {
"name": "your-project",
"version": "0.0.0",
"private": true,
"dependencies": {
// place your project dependencies here
},
"devDependencies": {
// comments are execial to quickly assert the state of a dependency
"xdiff": "^0.2.11",
// we still need grunt
"grunt": "^0.4.5"
}
};
@MarZab
Copy link
Author

MarZab commented Apr 12, 2016

sorry, missed your comment.
simple, you destribute the script with the package.json prebuild, its just a build script anyway

@tdmalone
Copy link

tdmalone commented Sep 26, 2016

Interesting idea. But what do you do when you install a new package with eg. --save-dev and your package.json is updated directly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment