Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unregister and login commands (rebased) #1719

Merged
merged 4 commits into from
Mar 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = function (grunt) {
simplemocha: {
options: {
reporter: 'spec',
timeout: '10000'
timeout: '15000'
},
full: {
src: ['test/test.js']
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ module.exports = {
install: commandFactory('./install'),
link: commandFactory('./link'),
list: commandFactory('./list'),
login: commandFactory('./login'),
lookup: commandFactory('./lookup'),
prune: commandFactory('./prune'),
register: commandFactory('./register'),
search: commandFactory('./search'),
update: commandFactory('./update'),
uninstall: commandFactory('./uninstall'),
unregister: commandFactory('./unregister'),
version: commandFactory('./version')
};
123 changes: 123 additions & 0 deletions lib/commands/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
var Configstore = require('configstore');
var GitHub = require('github');
var Q = require('q');

var createError = require('../util/createError');
var defaultConfig = require('../config');

function login(logger, options, config) {
var configstore = new Configstore('bower-github');

config = defaultConfig(config);

var promise;

options = options || {};

if (options.token) {
promise = Q.resolve({ token: options.token });
} else {
// This command requires interactive to be enabled
if (!config.interactive) {
logger.emit('error', createError('Login requires an interactive shell', 'ENOINT', {
details: 'Note that you can manually force an interactive shell with --config.interactive'
}));

return;
}

var questions = [
{
'name': 'username',
'message': 'Username',
'type': 'input',
'default': configstore.get('username')
},
{
'name': 'password',
'message': 'Password',
'type': 'password'
}
];

var github = new GitHub({
version: '3.0.0'
});

promise = Q.nfcall(logger.prompt.bind(logger), questions)
.then(function (answers) {
configstore.set('username', answers.username);

github.authenticate({
type: 'basic',
username: answers.username,
password: answers.password
});

return Q.ninvoke(github.authorization, 'create', {
scopes: ['user', 'repo'],
note: 'Bower command line client (' + (new Date()).toISOString() + ')'
});
});
}

return promise.then(function (result) {
configstore.set('accessToken', result.token);

return result;
}, function (error) {
var message;

try {
message = JSON.parse(error.message).message;
} catch (e) {
message = 'Authorization failed';
}

var questions = [
{
'name': 'otpcode',
'message': 'Two-Factor Auth Code',
'type': 'input'
}
];

if (message === 'Must specify two-factor authentication OTP code.') {
return Q.nfcall(logger.prompt.bind(logger), questions)
.then(function (answers) {
return Q.ninvoke(github.authorization, 'create', {
scopes: ['user', 'repo'],
note: 'Bower command line client (' + (new Date()).toISOString() + ')',
headers: {
'X-GitHub-OTP': answers.otpcode
}
});
})
.then(function (result) {
configstore.set('accessToken', result.token);

return result;
}, function () {
logger.emit('error', createError(message, 'EAUTH'));
});
} else {
logger.emit('error', createError(message, 'EAUTH'));
}
});
}

// -------------------

login.readOptions = function (argv) {
var cli = require('../util/cli');

var options = cli.readOptions({
token: { type: String, shorthand: 't' },
}, argv);

delete options.argv;

return [options];
};

module.exports = login;
85 changes: 85 additions & 0 deletions lib/commands/unregister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
var chalk = require('chalk');
var Q = require('q');

var defaultConfig = require('../config');
var PackageRepository = require('../core/PackageRepository');
var Tracker = require('../util/analytics').Tracker;
var createError = require('../util/createError');

function unregister(logger, name, config) {

if (!name) {
return;
}

var repository;
var registryClient;
var tracker;
var force;

config = defaultConfig(config);
force = config.force;
tracker = new Tracker(config);

// Bypass any cache
config.offline = false;
config.force = true;

// Trim name
name = name.trim();

repository = new PackageRepository(config, logger);

tracker.track('unregister');

if (!config.accessToken) {
return logger.emit('error',
createError('Use "bower login" with collaborator credentials', 'EFORBIDDEN')
);
}

return Q.resolve()
.then(function () {
// If non interactive or user forced, bypass confirmation
if (!config.interactive || force) {
return true;
}

return Q.nfcall(logger.prompt.bind(logger), {
type: 'confirm',
message: 'You are about to remove component "' + chalk.cyan.underline(name) + '" from the bower registry (' + chalk.cyan.underline(config.registry.register) + '). It is generally considered bad behavior to remove versions of a library that others are depending on. Are you really sure?',
default: false
});
})
.then(function (result) {
// If user response was negative, abort
if (!result) {
return;
}

registryClient = repository.getRegistryClient();

logger.action('unregister', name, { name: name });

return Q.nfcall(registryClient.unregister.bind(registryClient), name);
})
.then(function (result) {
tracker.track('unregistered');
logger.info('Package unregistered', name);

return result;
});
}

// -------------------

unregister.readOptions = function (argv) {
var cli = require('../util/cli');

var options = cli.readOptions(argv);
var name = options.argv.remain[1];

return [name];
};

module.exports = unregister;
4 changes: 4 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var tty = require('tty');
var object = require('mout').object;
var bowerConfig = require('bower-config');
var Configstore = require('configstore');

var cachedConfigs = {};

Expand All @@ -18,6 +19,9 @@ function readCachedConfig(cwd) {
}

var config = cachedConfigs[cwd] = bowerConfig.read(cwd);
var configstore = new Configstore('bower-github').all;

object.mixIn(config, configstore);

// Delete the json attribute because it is no longer supported
// and conflicts with --json
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
"cardinal": "0.4.4",
"chalk": "^1.0.0",
"chmodr": "0.1.0",
"configstore": "^0.3.2",
"decompress-zip": "^0.1.0",
"fstream": "^1.0.3",
"fstream-ignore": "^1.0.2",
"github": "^0.2.3",
"glob": "^4.3.2",
"graceful-fs": "^3.0.5",
"handlebars": "^2.0.0",
Expand Down
19 changes: 19 additions & 0 deletions templates/json/help-login.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"command": "login",
"description": "Authenticate with GitHub and store credentials to be used later.",
"usage": [
"login [<options>]"
],
"options": [
{
"shorthand": "-h",
"flag": "--help",
"description": "Show this help message"
},
{
"shorthand": "-t",
"flag": "--token",
"description": "Pass an existing GitHub auth token rather than prompting for username and password."
}
]
}
14 changes: 14 additions & 0 deletions templates/json/help-unregister.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"command": "unregister",
"description": "Unregisters a package.",
"usage": [
"unregister <name> [<options>]"
],
"options": [
{
"shorthand": "-h",
"flag": "--help",
"description": "Show this help message"
}
]
}
2 changes: 2 additions & 0 deletions templates/json/help.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
"install": "Install a package locally",
"link": "Symlink a package folder",
"list": "List local packages - and possible updates",
"login": "Authenticate with GitHub and store credentials",
"lookup": "Look up a package URL by name",
"prune": "Removes local extraneous packages",
"register": "Register a package",
"search": "Search for a package by name",
"update": "Update a local package",
"uninstall": "Remove a local package",
"unregister": "Remove a package from the registry",
"version": "Bump a package version"
},
"options": [
Expand Down