npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

git-credential-node

v1.1.0

Published

Thin wrapper around `git credential` command

Downloads

7,498

Readme

git-credential-node

Thin wrapper around git credential command Allow you to use Git credential API from node.

Travis Build Status NPM module NPM downloads

Installation

npm install --save git-credential-node

How it works

This is a thin wrapper around the git credential command.

The library use the GIT_TERMINAL_PROMPT environment variable to avoid asking the user credential on stdin.

The option is avalable starting with git 2.3, so you must have this version of git intalled on your system (or a newer one).

See detail on the option here

Usage

The library provides the following functions that can be used to interact with a locally installed Git credential tool. All functions with names ending in sync are synchronous, and either return the result of operation or throws. All other functions are asynchronous and could be used in one of two ways:

  • a callback could be provided as last argument which is called with the error or the results of the operation as arguments, following the standard node semantic.
  • a promise is returned that is either resolved with results, or rejected with errors.

fill(url, callback)

Retrieves any stored credentials for the provided target server.

  • url: A String parameter indicating the URL of the target server, e.g. https://github.com
  • callback: Called when the call to the Git credential tool finishes.

The provided callback is called with two parameters:

  • err: An error object in case the call failed.
  • result: { username, password }, containing the stored credentials. If there aren't any stored credentials for the requested target server, result will be null.

When there aren't any stored credentials for the requested target server, the Git credential helper will not ask for credentials, it will simply provide an empty result object to the callback.

Example:

import { fill } from 'git-credential-node';
fill('http://foo/bar.git', (err, data) => {
  if (err) {
    return console.log(err.message);
  }
  if (!data) {
    console.log('credentials not stored!');
  }
  console.dir(data);
});

approve(options, callback)

Stores the provided credentials for the provided target server. The following parameters are expected:

  • options: { username, password, url } An object containing username and password properties with the credentials to be stored, and url indicating the URL of the target server.
  • callback: Called when the call to the Git credential tool finishes.

The provided callback is called with one parameters:

  • err: An error object in case the call failed. If it is null, it means the call has succedeed.

Example:

import { approve } from 'git-credential-node';
const opts = {
  username: 'user',
  password: 'pass',
  url: 'http://foo/bar.git'
};

approve(opts, err => {
  if (err) {
    return console.log(err.message);
  }

  console.log('credentials stored!');
});

reject(url, callback)

Removes any stored credentials for the provided target server. The following parameters are expected:

  • url: A String parameter indicating the URL of the target server, e.g. https://github.com
  • callback: Called when the call to the Git credential tool finishes.

The provided callback is called with one parameters:

  • err: An error object in case the call failed. If it is null, it means the call has succedeed.

Example:

import { reject } from 'git-credential-node';
reject('http://foo/bar.git', err => {
  if (err) {
    return console.log(err.message);
  }

  console.log('credentials removed!');
});

Promises usage

Example of use of the async functions with promise.

All functions resolve or reject the returned promise with the same arguments passed to the callback. We use es2016 proposed async function in these example for clarity, but this is obviously not required. Any other comment specified in the callback section apply also to the promise usage.

fill(url)

Example:

import { fill } from 'git-credential-node';

async function example() {
  try {

    const data = await fill('http://foo/bar.git');

    if (!data) {
      console.log('credentials not stored!');
    }
    return data;

  } catch (err) {
    console.log(err.message);
  }
}

approve(options)

Example:

import { approve } from 'git-credential-node';

async function example() {
  const opts = {
    username: 'user',
    password: 'pass',
    url: 'http://foo/bar.git'
  };

  try {
    await approve(opts);

  } catch (err) {
    return console.log(err.message);
  }

  console.log('credentials stored!');
});

reject(url, callback)

Example:

import { reject } from 'git-credential-node';

async function example() {
  try {
    await reject('http://foo/bar.git');

  } catch (err) {
    return console.log(err.message);
  }

  console.log('credentials removed!');
});

Sync usage

Example of use of the sync functions.

All functions hasve a synchronous counterpart, with name ending in Sync. These can be particulary useful to use within bin scripts.

Any other comment specified in the callback section apply also to the sync functions.

fillSync(url)

Example:

import { fillSync } from 'git-credential-node';

function example() {
  try {

    const data = fillSync('http://foo/bar.git');

    if (!data) {
      console.log('credentials not stored!');
    }
    return data;

  } catch (err) {
    console.log(err.message);
  }
}

Sync(options)

Example:

import { Sync } from 'git-credential-node';

function example() {
  const opts = {
    username: 'user',
    password: 'pass',
    url: 'http://foo/bar.git'
  };

  try {
    Sync(opts);

  } catch (err) {
    return console.log(err.message);
  }

  console.log('credentials stored!');
});

rejectSync(url, callback)

Example:

import { rejectSync } from 'git-credential-node';

function example() {
  try {
    rejectSync('http://foo/bar.git');

  } catch (err) {
    return console.log(err.message);
  }

  console.log('credentials removed!');
});

Credits

I took inspiration and documentation from git-credential-helper by nwinkler

License

MIT - © 2015 Andrea Parodi