Skip to content

Commit

Permalink
Merged release/v1.1.0 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
KamiKillertO committed Jan 18, 2017
2 parents c0f75c8 + 33aaafa commit ad64430
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 151 deletions.
37 changes: 25 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# inquirer-select-directory

A directory prompt for Inquirer.js [inquirer](https://github.com/SBoudrias/Inquirer.js).
A directory prompt for [Inquirer.js](https://github.com/SBoudrias/Inquirer.js).

This project is a fork of [Inquirer-directory](https://github.com/nicksrandall/inquirer-directory) which does not limit the navigation to the current folder.

<!--[![Issue Count](https://codeclimate.com/github/KamiKillertO/inquirer-select-directory/badges/issue_count.svg)](https://codeclimate.com/github/KamiKillertO/inquirer-select-directory)!-->
![](https://img.shields.io/badge/license-MIT-blue.svg)
[![](https://img.shields.io/badge/release-v1.0.2-blue.svg)](https://github.com/KamiKillertO/inquirer-select-directory/releases/tag/v1.0.2)
[![](https://img.shields.io/badge/release-v1.1.0-blue.svg)](https://github.com/KamiKillertO/inquirer-select-directory/releases/tag/v1.1.0)
[![Build Status](https://travis-ci.org/KamiKillertO/inquirer-select-directory.svg)](https://travis-ci.org/KamiKillertO/inquirer-select-directory)
[![Build status](https://ci.appveyor.com/api/projects/status/fdyk5g3y56381742?svg=true)](https://ci.appveyor.com/project/KamiKillertO/inquirer-select-directory)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/e6a963539c4440b69356649c0048ea30)](https://www.codacy.com/app/kamikillerto/inquirer-select-directory?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=KamiKillertO/inquirer-select-directory&amp;utm_campaign=Badge_Grade)
Expand Down Expand Up @@ -46,13 +46,13 @@ Change `directory` to whatever you might prefer.

### Options

Takes `type`, `name`, `message`, `basePath` properties.
Takes `type`, `name`, `message`, `basePath`, `options` properties.

See [inquirer](https://github.com/SBoudrias/Inquirer.js) readme for meaning of all except **basePath**.

**basePath** is the relative path from your current working directory

#### Example
#### [Example](https://github.com/KamiKillertO/inquirer-select-directory/tree/develop/example/example.js)

```javascript
inquirer.registerPrompt('directory', require('inquirer-select-directory'));
Expand All @@ -61,16 +61,29 @@ inquirer.prompt([{
name: 'from',
message: 'Where you like to put this component?',
basePath: './src'
}], function(answers) {
}]).then(function(answers) {
//etc
});
```
<!--
[![asciicast](https://asciinema.org/a/31651.png)](https://asciinema.org/a/31651)
-->
<!--
See also [example.js](https://github.com/nicksrandall/inquierer-directory/blob/master/example.js) for a working example
-->

#### options.displayHidden

default ***false***

Set this to true if you to display hidden folders

```javascript
inquirer.prompt([{
type: 'directory',
//...
options: {
displayHidden:true
}
}]).then(function(answers) {
//etc
});
```

## License

MIT
MIT
12 changes: 7 additions & 5 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

"use strict";
var inquirer = require("inquirer");
var fs = require('fs');

inquirer.registerPrompt("directory", require("../src/index"));

inquirer.prompt([{
type: "directory",
name: "path",
message: "In what directory would like to perform this action?",
basePath: "./node_modules"
}], function(answers) {
console.log(JSON.stringify(answers, null, " "));
});
message: "In what directory would like to create an new file?",
basePath: "./"
}]).then(function(answers) {
fs.writeFile(answers.path + '/file.txt', 'Whoa! You have created this file');
});
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inquirer-select-directory",
"version": "1.0.2",
"version": "1.1.0",
"description": "A directory prompt for Inquirer.js",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -39,4 +39,4 @@
"mock-fs": "3.12.1",
"sinon": "1.17.2"
}
}
}
21 changes: 16 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ function listRender(choices, pointer) {

/**
* Function for getting list of folders in directory
* @param {String} basePath the path the folder to get a list of containing folders
* @return {Array} array of folder names inside of basePath
* @param {String} basePath the path the folder to get a list of containing folders
* @param {Boolean} [displayHidden=false] set to true if you want to get hidden files
* @return {Array} array of folder names inside of basePath
*/
function getDirectories(basePath) {
function getDirectories(basePath, displayHidden) {
displayHidden = displayHidden || false;
return fs
.readdirSync(basePath)
.filter(function(file) {
Expand All @@ -65,7 +67,10 @@ function getDirectories(basePath) {
return false;
}
var isDir = stats.isDirectory();
var isNotDotFile = path.basename(file).indexOf('.') !== 0;
if (displayHidden) {
return isDir;
}
var isNotDotFile = path.basename(file).indexOf(".") !== 0;
return isDir && isNotDotFile;
} catch (error) {
return false;
Expand All @@ -82,6 +87,7 @@ function Prompt() {
if (!this.opt.basePath) {
this.throwParamError('basePath');
}
this.opt.displayHidden = this.opt.displayHidden || false;
this.currentPath = path.isAbsolute(this.opt.basePath) ? path.resolve(this.opt.basePath) : path.resolve(process.cwd(), this.opt.basePath);
this.root = path.parse(this.currentPath).root;
this.opt.choices = new Choices(this.createChoices(this.currentPath), this.answers);
Expand Down Expand Up @@ -308,7 +314,7 @@ Prompt.prototype.onKeyPress = function(/*e*/) {
* Helper to create new choices based on previous selection.
*/
Prompt.prototype.createChoices = function(basePath) {
var choices = getDirectories(basePath);
var choices = getDirectories(basePath, this.opt.displayHidden);
if (basePath !== this.root) {
choices.unshift(BACK);
}
Expand All @@ -327,3 +333,8 @@ Prompt.prototype.createChoices = function(basePath) {
*/

module.exports = Prompt;


// TODO: Add option displayHidden id:0
// TODO: Add option displayFile id:1
// TODO: Add theming option id:2
Loading

0 comments on commit ad64430

Please sign in to comment.