all files / lib/ package-json.js

100% Statements 19/19
100% Branches 2/2
100% Functions 7/7
100% Lines 19/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44                                                 
'use strict';
 
var
  path = require('path'),
  fs = require('fs'),
  getRoot = require('find-root')
  ;
 
 
function getFilePath(startingPath) {
  startingPath = startingPath || process.cwd();
  return path.join(getRoot(startingPath), 'package.json');
}
 
function readFile(startingPath) {
  return JSON.parse(fs.readFileSync(getFilePath(startingPath), 'utf8'));
}
 
function writeFile(json, startingPath) {
  var string = JSON.stringify(json, null, '  ') + '\n';
 
  fs.writeFileSync(getFilePath(startingPath), string, 'utf8');
}
 
 
module.exports.getPath = function(/* optional */ startingPath) {
  return getFilePath(startingPath);
};
 
module.exports.getVersion = function(/* optional */ startingPath) {
  return readFile(startingPath).version;
};
 
module.exports.setVersion = function(version, /* optional */ startingPath) {
  var pkg = readFile(startingPath);
 
  pkg.version = version;
  writeFile(pkg, startingPath);
};
 
module.exports.getContent = function() {
  return readFile();
};