Skip to content

Commit 82080db

Browse files
authored
Merge pull request #40 from anc95/feat-default-param
feat: add default support
2 parents 4989da5 + 039e430 commit 82080db

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ inquirer.prompt({
2222

2323
### Options
2424
Takes `type`, `name`, `message`, [`filter`, `validate`, `transformer`, `default`, `pageSize`, `onlyShowDir`, `onlyShowValid`, `hideChildrenOfValid`, `root`, `hideRoot`, `multiple`] properties.
25+
2526
The extra options that this plugin provides are:
2627
- `onlyShowDir`: (Boolean) if true, will only show directory. Default: false.
2728
- `root`: (String) it is the root of file tree. Default: process.cwd().
@@ -30,6 +31,8 @@ The extra options that this plugin provides are:
3031
- `transformer`: (Function) a hook function to transform the display of directory or file name.
3132
- `multiple`: (Boolean) if true, will enable to select multiple files. Default: false.
3233

34+
When `multiple` is enabled, `default` should be `string[]` type, otherwise it's `string` type.
35+
3336
### Example
3437
```
3538
const inquirer = require('inquirer')

example/custom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ inquirer
1010
{
1111
type: 'file-tree-selection',
1212
name: 'file',
13+
default: '/Users/anchao/code/inquirer-file-tree-selection/example/multiple.js',
1314
message: 'choose a file',
1415
transformer: (input) => {
1516
const name = input.split(path.sep).pop();

example/default.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const inquirer = require('inquirer')
2+
const inquirerFileTreeSelection = require('../index')
3+
const path = require('path');
4+
const chalk = require('chalk');
5+
6+
inquirer.registerPrompt('file-tree-selection', inquirerFileTreeSelection)
7+
8+
inquirer
9+
.prompt([
10+
{
11+
type: 'file-tree-selection',
12+
name: 'file',
13+
default: __filename,
14+
message: 'choose a file',
15+
transformer: (input) => {
16+
const name = input.split(path.sep).pop();
17+
if (name[0] == ".") {
18+
return chalk.grey(name);
19+
}
20+
return name;
21+
}
22+
},
23+
{
24+
type: 'file-tree-selection',
25+
name: 'files',
26+
default: [__dirname],
27+
multiple: true,
28+
message: 'choose mutiple file',
29+
transformer: (input) => {
30+
const name = input.split(path.sep).pop();
31+
if (name[0] == ".") {
32+
return chalk.grey(name);
33+
}
34+
return name;
35+
}
36+
}
37+
])
38+
.then(answers => {
39+
console.log(JSON.stringify(answers))
40+
});

index.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,16 @@ class FileTreeSelectionPrompt extends Base {
4545
onlyShowDir: false,
4646
multiple: false,
4747
states: false,
48-
selectedList: false,
4948
},
50-
...this.opt
49+
...this.opt,
50+
selectedList: this.opt.selectedList || this.opt.default,
5151
}
5252

5353
// Make sure no default is set (so it won't be printed)
54-
this.opt.default = null;
55-
if (this.opt.selectedList) {
56-
this.selectedList = this.opt.selectedList
54+
// this.opt.default = null;
55+
this.selectedList = this.opt.selectedList;
56+
if (this.selectedList) {
57+
!Array.isArray(this.selectedList) && (this.selectedList = [this.selectedList])
5758
} else {
5859
if (this.opt.states) {
5960
this.selectedList = {};
@@ -117,11 +118,12 @@ class FileTreeSelectionPrompt extends Base {
117118
rootNode.open = true;
118119
if (this.opt.hideRoot) {
119120
this.fileTree.children = rootNode.children;
120-
this.active = this.fileTree.children[0];
121+
this.active = this.active || this.fileTree.children[0];
121122
} else {
122-
this.active = rootNode.children[0];
123+
this.active = this.active || rootNode.children[0];
123124
}
124125
this.render();
126+
this.prepareChildren(this.active);
125127
}
126128

127129
return this;
@@ -251,7 +253,41 @@ class FileTreeSelectionPrompt extends Base {
251253
await addValidity(node);
252254
}
253255

254-
!node._rootNode && this.render();
256+
// When it's single selection and has default value, we should expand to the default file.
257+
if (this.firstRender && this.opt.default && !this.opt.multiple) {
258+
const defaultPath = this.opt.default;
259+
const exists = fs.existsSync(defaultPath);
260+
261+
if (exists) {
262+
const founded = node.children.find(item => {
263+
if (item.path === defaultPath) {
264+
return true;
265+
}
266+
267+
if (defaultPath.includes(`${item.path}${path.sep}`)) {
268+
return true;
269+
}
270+
});
271+
272+
if (founded) {
273+
if (founded.path === defaultPath) {
274+
this.active = founded;
275+
276+
let parent = founded.parent;
277+
278+
while (parent && !parent._rootNode) {
279+
parent.open = true;
280+
parent = parent.parent;
281+
}
282+
}
283+
else {
284+
return await this.prepareChildren(founded);
285+
}
286+
}
287+
}
288+
}
289+
290+
!this.firstRender && this.render();
255291
}
256292

257293
/**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "inquirer-file-tree-selection-prompt",
3-
"version": "1.0.14",
3+
"version": "1.0.15",
44
"repository": "https://github.com/anc95/inquirer-file-tree-selection",
55
"description": "inquerer file tree selection prompt",
66
"main": "index.js",

0 commit comments

Comments
 (0)