file-prompt
Advanced tools
Comparing version 0.0.6 to 0.0.7
{ | ||
"name": "file-prompt", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "An interactive prompt for selecting files from a directory.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -18,3 +18,3 @@ # File Prompt [![Travis][build-badge]][build] [![npm package][npm-badge]][npm] | ||
fileprompt({ base: process.cwd(), filter: '*.js' }) | ||
fileprompt({ base: process.cwd(), glob: '*.js' }) | ||
.then((files) => { | ||
@@ -34,3 +34,3 @@ // Array of absolute path files | ||
fileprompt({ base: process.cwd(), filter: '*.js' }) | ||
fileprompt({ base: process.cwd(), glob: '*.js' }) | ||
.then((files) => { | ||
@@ -47,3 +47,3 @@ // Array of absolute path files | ||
* config.__base__ - Absolute path to a base directory to look for files in | ||
* config.__filter__ - Glob string to filter files from the basedirectory | ||
* config.__glob__ - Glob string to filter files from the basedirectory | ||
@@ -50,0 +50,0 @@ ## Input |
@@ -75,3 +75,3 @@ import Component from './component'; | ||
files: [], | ||
filter: this.props.filter, | ||
glob: this.props.glob, | ||
currentPage: { | ||
@@ -78,0 +78,0 @@ name: 'index', |
@@ -0,1 +1,2 @@ | ||
import colors from 'chalk'; | ||
import Component from './component'; | ||
@@ -22,2 +23,10 @@ import { navigate } from './actions'; | ||
/** | ||
* Get Default Props | ||
* Returns default properties for this component instance | ||
* | ||
* @method | ||
* @public | ||
* @returns {object} Default component properties | ||
*/ | ||
getDefaultProps () { | ||
@@ -58,2 +67,14 @@ return { | ||
/** | ||
* Display Error | ||
* Displays the error message to the user if there is one | ||
* | ||
* @method | ||
* @param {Error} e - Error object | ||
*/ | ||
displayError (e) { | ||
if (e.message === 'no_match') return; | ||
process.stderr.write(colors.bold.red(e.stack || e.message) + '\n'); | ||
} | ||
/** | ||
* Get Basedir | ||
@@ -71,2 +92,14 @@ * Returns the basedir from props or what is in the app state's config | ||
/** | ||
* Get Glob | ||
* Returns the glob filter prop or reads from global store | ||
* | ||
* @method | ||
* @public | ||
* @returns {string} The found glob string | ||
*/ | ||
getGlob () { | ||
return this.props.glob || this.select('glob'); | ||
} | ||
/** | ||
* Navigate | ||
@@ -73,0 +106,0 @@ * Navigates to another page |
@@ -57,6 +57,4 @@ import VerticalMenu from '../vertical_menu'; | ||
getInitialState () { | ||
let filter = this.props.filter || this.select('filter'); | ||
return { | ||
files: this.getFiles(filter), | ||
files: this.getFiles(this.getGlob()), | ||
menu: new VerticalMenu({ | ||
@@ -184,3 +182,4 @@ canUnselect: true, | ||
}) | ||
.catch(() => { | ||
.catch((e) => { | ||
this.displayError(e); | ||
reprompt(); | ||
@@ -187,0 +186,0 @@ }); |
@@ -8,2 +8,3 @@ import colors from 'chalk'; | ||
import { addFile, removeFile } from '../actions'; | ||
import { Minimatch } from 'minimatch'; | ||
@@ -77,36 +78,55 @@ /** | ||
* | ||
* @param {string} glob - Globstr to test the page against | ||
* @param {string} [dir] - Directory to look through | ||
* @returns {array} Array of menu options | ||
*/ | ||
getFiles (dir) { | ||
getFiles (glob, dir) { | ||
let configBasedir = this.select('config.base'), | ||
isBaseDir = dir === configBasedir, | ||
selectedFiles = this.select('files'), | ||
files = []; | ||
files = [], | ||
directories = [], | ||
mm = new Minimatch(glob); | ||
files = fs.readdirSync(dir); | ||
files = files.map((file, i) => { | ||
let filepath = path.join(dir, file), | ||
label = path.relative(dir, filepath), | ||
stats = fs.statSync(filepath), | ||
isDirectory = stats.isDirectory(); | ||
files = files | ||
// Map to full filepath | ||
.map((file) => path.join(dir, file)) | ||
if (isDirectory) { | ||
label = label + '/'; | ||
} | ||
// First filter files against our glob | ||
.filter((filepath) => { | ||
let stats = fs.statSync(filepath); | ||
return { | ||
id: isBaseDir ? i + 1 : i + 2, | ||
name: label, | ||
value: filepath, | ||
isSelected: selectedFiles.indexOf(filepath) > -1, | ||
label: isDirectory ? colors.bold(label) : label | ||
}; | ||
}); | ||
// If we have a directory store it and move on as we are good | ||
if (stats.isDirectory()) { | ||
directories.push(filepath); | ||
return true; | ||
} | ||
return mm.match(filepath); | ||
}) | ||
// Second filter files against | ||
.map((filepath, i) => { | ||
let label = path.relative(dir, filepath), | ||
isDirectory = directories.indexOf(filepath) > -1; | ||
// if file was a directory add a slash to the label | ||
if (isDirectory) label += '/'; | ||
return { | ||
id: isBaseDir ? i + 1 : i + 2, | ||
name: label, | ||
value: filepath, | ||
isSelected: selectedFiles.indexOf(filepath) > -1, | ||
// Make dir labels bold bold | ||
label: isDirectory ? colors.bold(label) : label | ||
}; | ||
}); | ||
// If we nested in the baedir add an option to go back | ||
if (dir !== configBasedir) { | ||
files.unshift({ | ||
id: 1, | ||
name: path.basename(path.resolve(dir, '..')), | ||
name: '..', | ||
value: path.resolve(dir, '..'), | ||
@@ -174,3 +194,4 @@ label: colors.bold('..') | ||
}) | ||
.catch(() => { | ||
.catch((e) => { | ||
this.displayError(e); | ||
reprompt(); | ||
@@ -206,3 +227,3 @@ }); | ||
if (selectedDir) { | ||
if (selectedDir && selections.length === 1) { | ||
this.navigate('directories', { base: selectedDir }); | ||
@@ -229,3 +250,3 @@ return true; | ||
renderMenu () { | ||
this.state.menu.setOptions(this.getFiles(this.getBasedir())); | ||
this.state.menu.setOptions(this.getFiles(this.getGlob(), this.getBasedir())); | ||
return this.state.menu.render(); | ||
@@ -232,0 +253,0 @@ } |
@@ -60,3 +60,3 @@ import glob from 'glob'; | ||
Object.assign(data, { | ||
filter: '**/*.js' | ||
glob: '**/*.js' | ||
}); | ||
@@ -177,3 +177,4 @@ | ||
}) | ||
.catch(() => { | ||
.catch((e) => { | ||
this.displayError(e); | ||
reprompt(); | ||
@@ -197,3 +198,3 @@ }); | ||
renderMenu () { | ||
this.state.menu.setOptions(this.getFiles(this.props.filter)); | ||
this.state.menu.setOptions(this.getFiles(this.getGlob())); | ||
return this.state.menu.render(); | ||
@@ -200,0 +201,0 @@ } |
@@ -189,3 +189,4 @@ import colors from 'chalk'; | ||
}) | ||
.catch(() => { | ||
.catch((e) => { | ||
this.displayError(e); | ||
reprompt(); | ||
@@ -192,0 +193,0 @@ }); |
@@ -129,4 +129,4 @@ import colors from 'chalk'; | ||
.catch((e) => { | ||
this.displayError(e); | ||
reprompt(); | ||
throw e; | ||
}); | ||
@@ -133,0 +133,0 @@ } |
@@ -75,4 +75,4 @@ /* eslint camelcase: 0 */ | ||
/** | ||
* Filter | ||
* Updates the current filter for finding a specific type of file | ||
* Glob | ||
* Updates the current glob filter for finding a specific type of file | ||
* | ||
@@ -83,6 +83,6 @@ * @param {string} state - The filter to be used | ||
*/ | ||
function filter (state = '**/*.js', action) { | ||
function glob (state = '**/*.js', action) { | ||
switch (action.type) { | ||
case SET_FILTER: | ||
return action.filter; | ||
return action.glob; | ||
@@ -94,4 +94,4 @@ default: | ||
const reducers = combineReducers({ config, currentPage, files, filter }); | ||
const reducers = combineReducers({ config, currentPage, files, glob }); | ||
export default reducers; |
@@ -15,3 +15,3 @@ import path from 'path'; | ||
}, | ||
filter: '**/*.js' | ||
glob: '**/*.js' | ||
}; | ||
@@ -18,0 +18,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
377687
7915