parse-git-config
Advanced tools
Comparing version 1.1.1 to 2.0.0
163
index.js
/*! | ||
* parse-git-config <https://github.com/jonschlinkert/parse-git-config> | ||
* | ||
* Copyright (c) 2015 Jon Schlinkert. | ||
* Licensed under the MIT license. | ||
* Copyright (c) 2015-2018, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -10,8 +10,10 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var exists = require('fs-exists-sync'); | ||
var extend = require('extend-shallow'); | ||
var configPath = require('git-config-path'); | ||
var ini = require('ini'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const util = require('util'); | ||
const ini = require('ini'); | ||
const configPath = require('git-config-path'); | ||
const expand = require('expand-tilde'); | ||
const read = util.promisify(fs.readFile); | ||
const stat = util.promisify(fs.stat); | ||
@@ -29,3 +31,3 @@ /** | ||
* @param {Object|String|Function} `options` Options with `cwd` or `path`, the cwd to use, or the callback function. | ||
* @param {Function} `cb` callback function if the first argument is options or cwd. | ||
* @param {Function} `callback` callback function if the first argument is options or cwd. | ||
* @return {Object} | ||
@@ -35,25 +37,50 @@ * @api public | ||
function parse(options, cb) { | ||
function parse(options, callback) { | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = {}; | ||
callback = options; | ||
options = null; | ||
} | ||
if (typeof cb !== 'function') { | ||
throw new TypeError('parse-git-config async expects a callback function.'); | ||
if (typeof callback !== 'function') { | ||
return parse.promise(options); | ||
} | ||
options = options || {}; | ||
var filepath = parse.resolve(options); | ||
return parse.promise(options) | ||
.then(config => callback(null, config)) | ||
.catch(callback); | ||
} | ||
fs.stat(filepath, function(err, stat) { | ||
if (err) return cb(err); | ||
/** | ||
* Parse the given | ||
* | ||
* ```js | ||
* parse.promise({ path: '/path/to/.gitconfig' }) | ||
* .then(config => console.log(config)); | ||
* ``` | ||
* | ||
* @name .sync | ||
* @param {Object|String} `options` Options with `cwd` or `path`, or the cwd to use. | ||
* @return {Object} | ||
* @api public | ||
*/ | ||
fs.readFile(filepath, 'utf8', function(err, str) { | ||
if (err) return cb(err); | ||
var parsed = ini.parse(str); | ||
cb(null, parsed); | ||
parse.promise = function(options) { | ||
const opts = Object.assign({}, options); | ||
const filepath = parse.resolveConfigPath(opts); | ||
if (!filepath) { | ||
return Promise.resolve(null); | ||
} | ||
return stat(filepath) | ||
.then(() => { | ||
return read(filepath, 'utf8'); | ||
}) | ||
.then(str => { | ||
if (opts.include === true) { | ||
str = injectInclude(str, path.resolve(path.dirname(filepath))); | ||
} | ||
return parseIni(str, opts); | ||
}); | ||
}); | ||
} | ||
}; | ||
@@ -65,3 +92,3 @@ /** | ||
* ```js | ||
* var config = parse.sync(); | ||
* const config = parse.sync(); | ||
* ``` | ||
@@ -75,9 +102,15 @@ * | ||
parse.sync = function parseSync(options) { | ||
options = options || {}; | ||
var filepath = parse.resolve(options); | ||
parse.sync = function(options) { | ||
const opts = Object.assign({}, options); | ||
const filepath = parse.resolveConfigPath(opts); | ||
if (filepath && fs.existsSync(filepath)) { | ||
const input = fs.readFileSync(filepath, 'utf8'); | ||
if (filepath && exists(filepath)) { | ||
var str = fs.readFileSync(filepath, 'utf8'); | ||
return ini.parse(str); | ||
if (opts.include === true) { | ||
const cwd = path.resolve(path.dirname(filepath)); | ||
const str = injectInclude(input, cwd); | ||
return parseIni(str, opts); | ||
} | ||
return parseIni(input, opts); | ||
} | ||
@@ -91,8 +124,8 @@ return {}; | ||
parse.resolve = function resolve(options) { | ||
parse.resolveConfigPath = function(options) { | ||
if (typeof options === 'string') { | ||
options = { type: options }; | ||
} | ||
var opts = extend({cwd: process.cwd()}, options); | ||
var fp = opts.path || configPath(opts.type); | ||
const opts = Object.assign({cwd: process.cwd()}, options); | ||
const fp = opts.path ? expand(opts.path) : configPath(opts.type); | ||
return fp ? path.resolve(opts.cwd, fp) : null; | ||
@@ -102,10 +135,17 @@ }; | ||
/** | ||
* Deprecated: use `.resolveConfigPath` instead | ||
*/ | ||
parse.resolve = function(options) { | ||
return parse.resolveConfigPath(options); | ||
}; | ||
/** | ||
* Returns an object with only the properties that had ini-style keys | ||
* converted to objects (example below). | ||
* converted to objects. | ||
* | ||
* ```js | ||
* var config = parse.sync(); | ||
* var obj = parse.keys(config); | ||
* const config = parse.sync({ path: '/path/to/.gitconfig' }); | ||
* const obj = parse.expandKeys(config); | ||
* ``` | ||
* @name .keys | ||
* @param {Object} `config` The parsed git config object. | ||
@@ -116,14 +156,45 @@ * @return {Object} | ||
parse.keys = function parseKeys(config) { | ||
var res = {}; | ||
for (var key in config) { | ||
var m = /(\S+) "(.*)"/.exec(key); | ||
parse.expandKeys = function(config) { | ||
for (const key of Object.keys(config)) { | ||
const m = /(\S+) "(.*)"/.exec(key); | ||
if (!m) continue; | ||
var prop = m[1]; | ||
res[prop] = res[prop] || {}; | ||
res[prop][m[2]] = config[key]; | ||
const prop = m[1]; | ||
config[prop] = config[prop] || {}; | ||
config[prop][m[2]] = config[key]; | ||
delete config[key]; | ||
} | ||
return res; | ||
return config; | ||
}; | ||
function parseIni(str, options) { | ||
const opts = Object.assign({}, options); | ||
const config = ini.parse(str); | ||
if (opts.expandKeys === true) { | ||
return parse.expandKeys(config); | ||
} | ||
return config; | ||
} | ||
function injectInclude(input, cwd) { | ||
const lines = input.split('\n').filter(function(line) { | ||
return line.trim() !== ''; | ||
}); | ||
const len = lines.length; | ||
const res = []; | ||
for (let i = 0; i < len; i++) { | ||
const line = lines[i]; | ||
if (line.indexOf('[include]') === 0) { | ||
const filepath = lines[i + 1].replace(/^\s*path\s*=\s*/, ''); | ||
const fp = path.resolve(cwd, expand(filepath)); | ||
res.push(fs.readFileSync(fp)); | ||
} else { | ||
res.push(line); | ||
} | ||
} | ||
return res.join('\n'); | ||
} | ||
/** | ||
@@ -130,0 +201,0 @@ * Expose `parse` |
{ | ||
"name": "parse-git-config", | ||
"description": "Parse `.git/config` into a JavaScript object. sync or async.", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/jonschlinkert/parse-git-config", | ||
"author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"contributors": [ | ||
"Jason Denizac <jason@denizac.org> (https://jden.us)", | ||
"Jon Schlinkert <jon.schlinkert@sellside.com> (http://twitter.com/jonschlinkert)", | ||
"Sam Holmes <sam@samholmes.net> (https://samholmes.net)" | ||
"Jon Schlinkert (http://twitter.com/jonschlinkert)", | ||
"Sam Holmes (https://samholmes.net)", | ||
"(https://github.com/js-n)" | ||
], | ||
@@ -22,3 +22,3 @@ "repository": "jonschlinkert/parse-git-config", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=8" | ||
}, | ||
@@ -29,11 +29,10 @@ "scripts": { | ||
"dependencies": { | ||
"extend-shallow": "^2.0.1", | ||
"fs-exists-sync": "^0.1.0", | ||
"expand-tilde": "^2.0.2", | ||
"git-config-path": "^1.0.1", | ||
"ini": "^1.3.4" | ||
"ini": "^1.3.5" | ||
}, | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.11", | ||
"gulp-format-md": "^1.0.0", | ||
"homedir-polyfill": "^1.0.1", | ||
"mocha": "^3.2.0" | ||
"mocha": "^3.5.3" | ||
}, | ||
@@ -65,5 +64,2 @@ "keywords": [ | ||
}, | ||
"reflinks": [ | ||
"verb" | ||
], | ||
"lint": { | ||
@@ -70,0 +66,0 @@ "reflinks": true |
128
README.md
@@ -1,5 +0,7 @@ | ||
# parse-git-config [![NPM version](https://img.shields.io/npm/v/parse-git-config.svg?style=flat)](https://www.npmjs.com/package/parse-git-config) [![NPM monthly downloads](https://img.shields.io/npm/dm/parse-git-config.svg?style=flat)](https://npmjs.org/package/parse-git-config) [![NPM total downloads](https://img.shields.io/npm/dt/parse-git-config.svg?style=flat)](https://npmjs.org/package/parse-git-config) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/parse-git-config.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/parse-git-config) | ||
# parse-git-config [![NPM version](https://img.shields.io/npm/v/parse-git-config.svg?style=flat)](https://www.npmjs.com/package/parse-git-config) [![NPM monthly downloads](https://img.shields.io/npm/dm/parse-git-config.svg?style=flat)](https://npmjs.org/package/parse-git-config) [![NPM total downloads](https://img.shields.io/npm/dt/parse-git-config.svg?style=flat)](https://npmjs.org/package/parse-git-config) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/parse-git-config.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/parse-git-config) | ||
> Parse `.git/config` into a JavaScript object. sync or async. | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
## Install | ||
@@ -16,6 +18,6 @@ | ||
```js | ||
var parse = require('parse-git-config'); | ||
const parse = require('parse-git-config'); | ||
// sync | ||
var config = parse.sync(); | ||
const config = parse.sync(); | ||
@@ -59,6 +61,12 @@ // or async | ||
### [parse](index.js#L33) | ||
### [parse](index.js#L35) | ||
Asynchronously parse a `.git/config` file. If only the callback is passed, the `.git/config` file relative to `process.cwd()` is used. | ||
**Params** | ||
* `options` **{Object|String|Function}**: Options with `cwd` or `path`, the cwd to use, or the callback function. | ||
* `callback` **{Function}**: callback function if the first argument is options or cwd. | ||
* `returns` **{Object}** | ||
**Example** | ||
@@ -73,18 +81,22 @@ | ||
### [.sync](index.js#L64) | ||
Parse the given | ||
**Params** | ||
* `options` **{Object|String|Function}**: Options with `cwd` or `path`, the cwd to use, or the callback function. | ||
* `cb` **{Function}**: callback function if the first argument is options or cwd. | ||
* `options` **{Object|String}**: Options with `cwd` or `path`, or the cwd to use. | ||
* `returns` **{Object}** | ||
### [.sync](index.js#L71) | ||
Synchronously parse a `.git/config` file. If no arguments are passed, the `.git/config` file relative to `process.cwd()` is used. | ||
**Example** | ||
```js | ||
var config = parse.sync(); | ||
parse.promise({ path: '/path/to/.gitconfig' }) | ||
.then(config => console.log(config)); | ||
``` | ||
### [.sync](index.js#L98) | ||
Synchronously parse a `.git/config` file. If no arguments are passed, the `.git/config` file relative to `process.cwd()` is used. | ||
**Params** | ||
@@ -95,13 +107,12 @@ | ||
### [.keys](index.js#L109) | ||
Returns an object with only the properties that had ini-style keys converted to objects (example below). | ||
**Example** | ||
```js | ||
var config = parse.sync(); | ||
var obj = parse.keys(config); | ||
const config = parse.sync(); | ||
``` | ||
### [.expandKeys](index.js#L149) | ||
Returns an object with only the properties that had ini-style keys converted to objects. | ||
**Params** | ||
@@ -112,2 +123,9 @@ | ||
**Example** | ||
```js | ||
const config = parse.sync({ path: '/path/to/.gitconfig' }); | ||
const obj = parse.expandKeys(config); | ||
``` | ||
### .keys examples | ||
@@ -120,4 +138,4 @@ | ||
```js | ||
var parse = require('parse-git-config'); | ||
var config = { | ||
const parse = require('parse-git-config'); | ||
const config = { | ||
'foo "bar"': { doStuff: true }, | ||
@@ -144,4 +162,4 @@ 'foo "baz"': { doStuff: true } | ||
```js | ||
var parse = require('parse-git-config'); | ||
var config = { | ||
const parse = require('parse-git-config'); | ||
const config = { | ||
'remote "origin"': { | ||
@@ -191,41 +209,52 @@ url: 'https://github.com/jonschlinkert/normalize-pkg.git', | ||
### Related projects | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
* [git-user-name](https://www.npmjs.com/package/git-user-name): Get a user's name from git config at the project or global scope, depending on… [more](https://github.com/jonschlinkert/git-user-name) | [homepage](https://github.com/jonschlinkert/git-user-name "Get a user's name from git config at the project or global scope, depending on what git uses in the current context.") | ||
* [git-username](https://www.npmjs.com/package/git-username): Get the username from a git remote origin URL. | [homepage](https://github.com/jonschlinkert/git-username "Get the username from a git remote origin URL.") | ||
* [parse-author](https://www.npmjs.com/package/parse-author): Parse a string into an object with `name`, `email` and `url` properties following npm conventions… [more](https://github.com/jonschlinkert/parse-author) | [homepage](https://github.com/jonschlinkert/parse-author "Parse a string into an object with `name`, `email` and `url` properties following npm conventions. Useful for the `authors` property in package.json or for parsing an AUTHORS file into an array of authors objects.") | ||
* [parse-authors](https://www.npmjs.com/package/parse-authors): Parse a string into an array of objects with `name`, `email` and `url` properties following… [more](https://github.com/jonschlinkert/parse-authors) | [homepage](https://github.com/jonschlinkert/parse-authors "Parse a string into an array of objects with `name`, `email` and `url` properties following npm conventions. Useful for the `authors` property in package.json or for parsing an AUTHORS file into an array of authors objects.") | ||
* [parse-github-url](https://www.npmjs.com/package/parse-github-url): Parse a github URL into an object. | [homepage](https://github.com/jonschlinkert/parse-github-url "Parse a github URL into an object.") | ||
* [parse-gitignore](https://www.npmjs.com/package/parse-gitignore): Parse a gitignore file into an array of patterns. Comments and empty lines are stripped. | [homepage](https://github.com/jonschlinkert/parse-gitignore "Parse a gitignore file into an array of patterns. Comments and empty lines are stripped.") | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
### Contributing | ||
</details> | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
### Contributors | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
| **Commits** | **Contributor**<br/> | | ||
| --- | --- | | ||
| 48 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 1 | [sam3d](https://github.com/sam3d) | | ||
| 1 | [jsdnxx](https://github.com/jsdnxx) | | ||
```sh | ||
$ npm install && npm test | ||
``` | ||
### Building docs | ||
</details> | ||
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
To generate the readme and API documentation with [verb](https://github.com/verbose/verb): | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verb verb-generate-readme && verb | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
### Running tests | ||
</details> | ||
Install dev dependencies: | ||
### Related projects | ||
```sh | ||
$ npm install -d && npm test | ||
``` | ||
You might also be interested in these projects: | ||
* [git-user-name](https://www.npmjs.com/package/git-user-name): Get a user's name from git config at the project or global scope, depending on… [more](https://github.com/jonschlinkert/git-user-name) | [homepage](https://github.com/jonschlinkert/git-user-name "Get a user's name from git config at the project or global scope, depending on what git uses in the current context.") | ||
* [git-username](https://www.npmjs.com/package/git-username): Get the username from a git remote origin URL. | [homepage](https://github.com/jonschlinkert/git-username "Get the username from a git remote origin URL.") | ||
* [parse-author](https://www.npmjs.com/package/parse-author): Parse an author, contributor, maintainer or other 'person' string into an object with name, email… [more](https://github.com/jonschlinkert/parse-author) | [homepage](https://github.com/jonschlinkert/parse-author "Parse an author, contributor, maintainer or other 'person' string into an object with name, email and url properties following npm conventions.") | ||
* [parse-authors](https://www.npmjs.com/package/parse-authors): Parse a string into an array of objects with `name`, `email` and `url` properties following… [more](https://github.com/jonschlinkert/parse-authors) | [homepage](https://github.com/jonschlinkert/parse-authors "Parse a string into an array of objects with `name`, `email` and `url` properties following npm conventions. Useful for the `authors` property in package.json or for parsing an AUTHORS file into an array of authors objects.") | ||
* [parse-github-url](https://www.npmjs.com/package/parse-github-url): Parse a github URL into an object. | [homepage](https://github.com/jonschlinkert/parse-github-url "Parse a github URL into an object.") | ||
* [parse-gitignore](https://www.npmjs.com/package/parse-gitignore): Parse a gitignore file into an array of patterns. Comments and empty lines are stripped. | [homepage](https://github.com/jonschlinkert/parse-gitignore "Parse a gitignore file into an array of patterns. Comments and empty lines are stripped.") | ||
### Contributors | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 51 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 1 | [sam3d](https://github.com/sam3d) | | ||
| 1 | [js-n](https://github.com/js-n) | | ||
### Author | ||
@@ -235,12 +264,13 @@ | ||
* [linkedin/in/jonschlinkert](https://linkedin.com/in/jonschlinkert) | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [twitter/jonschlinkert](https://twitter.com/jonschlinkert) | ||
### License | ||
Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](https://github.com/jonschlinkert/parse-git-config/blob/master/LICENSE). | ||
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on December 14, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on February 14, 2018._ |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
15220
3
169
267
1
+ Addedexpand-tilde@^2.0.2
+ Addedexpand-tilde@2.0.2(transitive)
- Removedextend-shallow@^2.0.1
- Removedfs-exists-sync@^0.1.0
Updatedini@^1.3.5