Skip to content

Commit

Permalink
Merge pull request #40 from anc95/feat-default-param
Browse files Browse the repository at this point in the history
feat: add default support
  • Loading branch information
anc95 authored Jan 17, 2022
2 parents 4989da5 + 039e430 commit 82080db
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ inquirer.prompt({

### Options
Takes `type`, `name`, `message`, [`filter`, `validate`, `transformer`, `default`, `pageSize`, `onlyShowDir`, `onlyShowValid`, `hideChildrenOfValid`, `root`, `hideRoot`, `multiple`] properties.

The extra options that this plugin provides are:
- `onlyShowDir`: (Boolean) if true, will only show directory. Default: false.
- `root`: (String) it is the root of file tree. Default: process.cwd().
Expand All @@ -30,6 +31,8 @@ The extra options that this plugin provides are:
- `transformer`: (Function) a hook function to transform the display of directory or file name.
- `multiple`: (Boolean) if true, will enable to select multiple files. Default: false.

When `multiple` is enabled, `default` should be `string[]` type, otherwise it's `string` type.

### Example
```
const inquirer = require('inquirer')
Expand Down
1 change: 1 addition & 0 deletions example/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ inquirer
{
type: 'file-tree-selection',
name: 'file',
default: '/Users/anchao/code/inquirer-file-tree-selection/example/multiple.js',
message: 'choose a file',
transformer: (input) => {
const name = input.split(path.sep).pop();
Expand Down
40 changes: 40 additions & 0 deletions example/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const inquirer = require('inquirer')
const inquirerFileTreeSelection = require('../index')
const path = require('path');
const chalk = require('chalk');

inquirer.registerPrompt('file-tree-selection', inquirerFileTreeSelection)

inquirer
.prompt([
{
type: 'file-tree-selection',
name: 'file',
default: __filename,
message: 'choose a file',
transformer: (input) => {
const name = input.split(path.sep).pop();
if (name[0] == ".") {
return chalk.grey(name);
}
return name;
}
},
{
type: 'file-tree-selection',
name: 'files',
default: [__dirname],
multiple: true,
message: 'choose mutiple file',
transformer: (input) => {
const name = input.split(path.sep).pop();
if (name[0] == ".") {
return chalk.grey(name);
}
return name;
}
}
])
.then(answers => {
console.log(JSON.stringify(answers))
});
52 changes: 44 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@ class FileTreeSelectionPrompt extends Base {
onlyShowDir: false,
multiple: false,
states: false,
selectedList: false,
},
...this.opt
...this.opt,
selectedList: this.opt.selectedList || this.opt.default,
}

// Make sure no default is set (so it won't be printed)
this.opt.default = null;
if (this.opt.selectedList) {
this.selectedList = this.opt.selectedList
// this.opt.default = null;
this.selectedList = this.opt.selectedList;
if (this.selectedList) {
!Array.isArray(this.selectedList) && (this.selectedList = [this.selectedList])
} else {
if (this.opt.states) {
this.selectedList = {};
Expand Down Expand Up @@ -117,11 +118,12 @@ class FileTreeSelectionPrompt extends Base {
rootNode.open = true;
if (this.opt.hideRoot) {
this.fileTree.children = rootNode.children;
this.active = this.fileTree.children[0];
this.active = this.active || this.fileTree.children[0];
} else {
this.active = rootNode.children[0];
this.active = this.active || rootNode.children[0];
}
this.render();
this.prepareChildren(this.active);
}

return this;
Expand Down Expand Up @@ -251,7 +253,41 @@ class FileTreeSelectionPrompt extends Base {
await addValidity(node);
}

!node._rootNode && this.render();
// When it's single selection and has default value, we should expand to the default file.
if (this.firstRender && this.opt.default && !this.opt.multiple) {
const defaultPath = this.opt.default;
const exists = fs.existsSync(defaultPath);

if (exists) {
const founded = node.children.find(item => {
if (item.path === defaultPath) {
return true;
}

if (defaultPath.includes(`${item.path}${path.sep}`)) {
return true;
}
});

if (founded) {
if (founded.path === defaultPath) {
this.active = founded;

let parent = founded.parent;

while (parent && !parent._rootNode) {
parent.open = true;
parent = parent.parent;
}
}
else {
return await this.prepareChildren(founded);
}
}
}
}

!this.firstRender && this.render();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "inquirer-file-tree-selection-prompt",
"version": "1.0.14",
"version": "1.0.15",
"repository": "https://github.com/anc95/inquirer-file-tree-selection",
"description": "inquerer file tree selection prompt",
"main": "index.js",
Expand Down

0 comments on commit 82080db

Please sign in to comment.