Skip to content

Commit

Permalink
Merge f5cdbb4 into f0e97c6
Browse files Browse the repository at this point in the history
  • Loading branch information
Myhlamaeus committed Dec 29, 2017
2 parents f0e97c6 + f5cdbb4 commit a135bef
Show file tree
Hide file tree
Showing 4 changed files with 2,232 additions and 120 deletions.
48 changes: 34 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
# inquirer-npm-name [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]

Helper function using [inquirer](https://github.com/SBoudrias/Inquirer.js) to validate a value provided in a prompt does not exist as a npm package.
Helper function using [inquirer](https://github.com/SBoudrias/Inquirer.js) to
validate a value provided in a prompt does not exist as a npm package.

If the value is already used as a npm package, then the users will be prompted and asked if they want to choose another one. If so, we'll recurse through the same validation process until we have a name that is unused on the npm registry. This is a helper to catch naming issue in advance, it is not a validation rule as the user can always decide to continue with the same name.
The supplied value must be a valid package name (as per
[validate-npm-package-name]); otherwise, the user will again be prompted to
enter a name.

If the value is already used as a npm package, then the users will be prompted
and asked if they want to choose another one. If so, we'll recurse through the
same validation process until we have a name that is unused on the npm registry.
This is a helper to catch naming issue in advance, it is not a validation rule
as the user can always decide to continue with the same name.

## Install

```sh
$ npm install --save inquirer-npm-name
```


## Usage

```js
var inquirer = require('inquirer');
var askName = require('inquirer-npm-name');

askName({
name: 'name',
message: 'Module Name'
}, inquirer).then(function (answer) {
askName(
{
name: 'name',
message: 'Some Module Name' // Default: 'Module Name'
},
inquirer
).then(function(answer) {
console.log(answer.name);
});

// Equivalent to {name: 'name'}
askName('name', inquirer).then(function(answer) {
console.log(answer.name);
});
```
Expand All @@ -33,11 +49,14 @@ var inquirer = require('inquirer');
var askName = require('inquirer-npm-name');

module.exports = generators.Base.extend({
prompting: function () {
return askName({
name: 'name',
message: 'Module Name'
}, this).then(function (name) {
prompting: function() {
return askName(
{
name: 'name',
message: 'Module Name'
},
this
).then(function(name) {
console.log(name);
});
}
Expand All @@ -46,7 +65,8 @@ module.exports = generators.Base.extend({

`askName` takes 2 parameters:

1. `prompt` an [Inquirer prompt configuration](https://github.com/SBoudrias/Inquirer.js#question).
1. `prompt` an [Inquirer prompt configuration](https://github.com/SBoudrias/Inquirer.js#question)
or just a string to serve as name.
2. `inquirer` or any object with a `obj.prompt()` method.

**Returns:** A `Promise` resolved with the answer object.
Expand All @@ -55,7 +75,7 @@ module.exports = generators.Base.extend({

MIT © [Simon Boudrias](http://twitter.com/vaxilart)


[validate-npm-package-name]: https://npmjs.org/package/validate-npm-package-name
[npm-image]: https://badge.fury.io/js/inquirer-npm-name.svg
[npm-url]: https://npmjs.org/package/inquirer-npm-name
[travis-image]: https://travis-ci.org/SBoudrias/inquirer-npm-name.svg?branch=master
Expand Down
58 changes: 45 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
'use strict';
var npmName = require('npm-name');
var validatePackageName = require('validate-npm-package-name');
var upperCaseFirst = require('upper-case-first');

var defaults = {
message: 'Module Name',
validate: function () {
return true;
}
};

/**
* This function will use the given prompt config and will then check if the value is
* available as a name on npm. If the name is already picked, we'll ask the user to
* confirm or pick another name.
* @param {Object} prompt Inquirer prompt configuration
* @param {inquirer} inquirer Object with a `prompt` method. Usually `inquirer` or a
* yeoman-generator.
* @param {(Object|String)} prompt Inquirer prompt configuration or the name to be
* used in it.
* @param {inquirer} inquirer Object with a `prompt` method. Usually `inquirer`
or a yeoman-generator.
*/
module.exports = function askName(prompt, inquirer) {
var prompts = [prompt, {
type: 'confirm',
name: 'askAgain',
message: 'The name above already exists on npm, choose another?',
default: true,
when: function (answers) {
return npmName(answers[prompt.name]).then(function (available) {
return !available;
});
if (typeof prompt === 'string') {
prompt = {
name: prompt
};
}

var prompts = [
Object.assign({}, defaults, prompt, {
validate: function (val) {
var packageNameValidity = validatePackageName(val);

if (packageNameValidity.validForNewPackages) {
var validate = prompt.validate || defaults.validate;

return validate.apply(this, arguments);
}

return upperCaseFirst(packageNameValidity.errors[0]) ||
'The provided value is not a valid npm package name';
}
}),
{
type: 'confirm',
name: 'askAgain',
message: 'The name above already exists on npm, choose another?',
default: true,
when: function (answers) {
return npmName(answers[prompt.name]).then(function (available) {
return !available;
});
}
}
}];
];

return inquirer.prompt(prompts).then(function (props) {
if (props.askAgain) {
Expand Down
Loading

0 comments on commit a135bef

Please sign in to comment.