Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Sep 2, 2015
1 parent c943e4e commit 2c12249
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 50 deletions.
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
netrc [![Build Status](https://travis-ci.org/CamShaft/netrc.png?branch=master)](https://travis-ci.org/CamShaft/netrc)
netrc [![Build Status](https://travis-ci.org/camshaft/netrc.png?branch=master)](https://travis-ci.org/camshaft/netrc)
=====

Parse netrc files

Usage
-----

```js
var netrc = require('netrc');

var myNetrc = netrc();

console.log(myNetrc['github.com'])
// { login: 'my-oauth-token',
// password: 'x-oauth-basic' }

myNetrc['github.com'].login = 'my-new-oauth-token';

netrc.save(myNetrc);
```

API
---

### netrc([file])

Loads a `.netrc` file, defaulting to `~/.netrc`

### netrc.parse(string)

Parses netrc formatted string into an object:

```json
{
"machine1.example.com": {
"login": "my-login",
"password": "my-password"
},
"machine2.example.com": {
"login": "my-other-login",
"password": "my-other-password"
}
}
```

### netrc.format(object)

Formats a netrc object into a valid string

### netrc.save(object)

Persists a netrc object to `~/.netrc`

## Tests

```
$ npm test
```
73 changes: 38 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* Module dependencies
*/
var fs = require("fs")
, join = require("path").join;

var fs = require('fs');
var join = require('path').join;

/**
* Read and parse .netrc
Expand All @@ -11,14 +12,15 @@ var fs = require("fs")
* @return {Object}
* @api public
*/

module.exports = exports = function(file) {
var home = getHomePath();

if(!file && !home) return {};
file = file || join(home, ".netrc");

if(!file || !fs.existsSync(file)) return {};
var netrc = fs.readFileSync(file, "UTF-8");
if (!file && !home) return {};
file = file || join(home, '.netrc');

if (!file || !fs.existsSync(file)) return {};
var netrc = fs.readFileSync(file, 'UTF-8');
return exports.parse(netrc);
};

Expand All @@ -29,34 +31,33 @@ module.exports = exports = function(file) {
* @return {Object}
* @api public
*/

exports.parse = function(content) {
// Remove comments
var lines = content.split('\n');
for (var n in lines) {
var i = lines[n].indexOf('#');
if (i > -1) lines[n] = lines[n].substring(0,i);
if (i > -1) lines[n] = lines[n].substring(0, i);
}
content = lines.join('\n');

var tokens = content.split(/[ \t\n\r]+/)
, machines = {}
, m = null
, key = null;
var tokens = content.split(/[ \t\n\r]+/);
var machines = {};
var m = null;
var key = null;

//if first index in array is empty string, strip it off (happens when first line of file is comment. Breaks the parsing)
if (tokens[0] === "") {
tokens.shift();
}
// if first index in array is empty string, strip it off (happens when first line of file is comment. Breaks the parsing)
if (tokens[0] === '') tokens.shift();

for(var i = 0; i < tokens.length; i+=2) {
var key = tokens[i]
, value = tokens[i+1];
for(var i = 0, key, value; i < tokens.length; i+=2) {
key = tokens[i];
value = tokens[i+1];

// Whitespace
if(!key || !value) continue;
if (!key || !value) continue;

// We have a new machine definition
if(key === "machine") {
if (key === 'machine') {
m = {};
machines[value] = m;
}
Expand All @@ -74,20 +75,20 @@ exports.parse = function(content) {
* @param {Object} machines as returned by `netrc.parse`
* @return {String} text of the netrc file
*/

exports.format = function format(machines){
var lines = [],
keys = Object.getOwnPropertyNames(machines).sort();
keys.forEach(function(key){
lines.push('machine ' + key);
var machine = machines[key];
var attrs = Object.getOwnPropertyNames(machine).sort();
attrs.forEach(function(attr){
if(typeof(machine[attr]) === 'string'){
lines.push(' ' + attr + ' ' + machine[attr]);
}
});
var lines = [];
var keys = Object.getOwnPropertyNames(machines).sort();

keys.forEach(function(key){
lines.push('machine ' + key);
var machine = machines[key];
var attrs = Object.getOwnPropertyNames(machine).sort();
attrs.forEach(function(attr){
if (typeof(machine[attr]) === 'string') lines.push(' ' + attr + ' ' + machine[attr]);
});
return lines.join('\n');
});
return lines.join('\n');
};

/**
Expand All @@ -96,10 +97,11 @@ exports.format = function format(machines){
* @param {Object} machines as returned by `netrc.parse`
* @api public
*/

exports.save = function save(machines){
var home = getHomePath();
var destFile = join(home, ".netrc");
var data = exports.format(machines) + "\n";
var destFile = join(home, '.netrc');
var data = exports.format(machines) + '\n';
fs.writeFileSync(destFile, data);
};

Expand All @@ -109,6 +111,7 @@ exports.save = function save(machines){
* @return {String} path to home directory
* @api private
*/

function getHomePath() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}
28 changes: 14 additions & 14 deletions test/netrc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,22 @@ describe("netrc", function() {
});

describe('format', function(){
it('should generate text that parses to the original', function(){
var machines = netrc.parse(valid);
it('should generate text that parses to the original', function(){
var machines = netrc.parse(valid);

var text = netrc.format(machines);
should.exist(text);
text.should.include('machine github.com');
text.should.include('login CamShaft');
text.should.include('password 123');
var text = netrc.format(machines);
should.exist(text);
text.should.include('machine github.com');
text.should.include('login CamShaft');
text.should.include('password 123');

var parsed = netrc.parse(text);
parsed.should.have.property('github.com');
parsed["github.com"].should.have.property("login");
parsed["github.com"].login.should.eql("CamShaft");
parsed["github.com"].should.have.property("password");
parsed["github.com"].password.should.eql("123");
});
var parsed = netrc.parse(text);
parsed.should.have.property('github.com');
parsed["github.com"].should.have.property("login");
parsed["github.com"].login.should.eql("CamShaft");
parsed["github.com"].should.have.property("password");
parsed["github.com"].password.should.eql("123");
});
});

});

0 comments on commit 2c12249

Please sign in to comment.