file-prompt
Version:
An interactive prompt for selecting files from a directory.
1 lines • 4.46 kB
JavaScript
;function _interopRequireDefault(e){return e&&e.__esModule?e:{"default":e}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _possibleConstructorReturn(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function _inherits(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var _createClass=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}();Object.defineProperty(exports,"__esModule",{value:!0});var _globAll=require("glob-all"),_globAll2=_interopRequireDefault(_globAll),_dispatcher=require("../streams/dispatcher"),_dispatcher2=_interopRequireDefault(_dispatcher),_dispatch_transform=require("../streams/dispatch_transform"),_dispatch_transform2=_interopRequireDefault(_dispatch_transform),_generic_transform=require("../streams/generic_transform"),_generic_transform2=_interopRequireDefault(_generic_transform),_menu_transform=require("../streams/menu_transform"),_menu_transform2=_interopRequireDefault(_menu_transform),_page=require("../page"),_page2=_interopRequireDefault(_page),_path=require("path"),_path2=_interopRequireDefault(_path),_prompt=require("../prompt"),_prompt2=_interopRequireDefault(_prompt),_queries_transform=require("../streams/queries_transform"),_queries_transform2=_interopRequireDefault(_queries_transform),_vertical_menu=require("../vertical_menu"),_vertical_menu2=_interopRequireDefault(_vertical_menu),GlobPage=function(e){function t(e){_classCallCheck(this,t);var r=_possibleConstructorReturn(this,Object.getPrototypeOf(t).call(this,e));return r.menu=new _vertical_menu2["default"]({canUnselect:!0,acceptsMany:!0,stdin:r.props.stdin,stdout:r.props.stdout,app:r.props.app}),r.prompt=new _prompt2["default"]({stdin:r.props.stdin,stdout:r.props.stdout}),r.pipeline=r.createPipeline(),r}return _inherits(t,e),_createClass(t,[{key:"getInitialState",value:function(){return{files:[],filter:null}}},{key:"componentShouldUpdate",value:function(){return!1}},{key:"createOptionsFrom",value:function(e){var t=this.select("files"),r=this.getBasedir();return e.map(function(e,n){return{id:n+1,label:_path2["default"].relative(r,e),name:e,value:e,isSelected:t.indexOf(e)>-1}})||[]}},{key:"getFiles",value:function(e){var t=this.getBasedir();return _globAll2["default"].sync(_path2["default"].join(t,e),{cwd:process.cwd()})}},{key:"processGlob",value:function(e,t){var r=void 0,n=[],i=t.type,a=t.creator;return"string"!==i||"prompt"!==a?void e.push(t):(r=t.data)?(n=this.getFiles(r),n.length?void this.setState({filter:r,files:n}):void e.pushError('No files matched the glob string "'+r+'"')):(e.destroy(),t.type="navigate",t.data={value:"blank"},void e.push(t))}},{key:"question",value:function(){var e=this.getBasedir();return this.state.files.length?"Add files":(e=_path2["default"].relative(_path2["default"].resolve(this.select("config.base"),".."),e),"Enter glob from "+e)}},{key:"route",value:function(e,t){switch(t.type){case"navigate":switch(t.data){case"blank":e.end(),this.navigate("index");break;case"all":this.navigate("index")}break;case"done":this.reprompt();break;case"error":this.displayError(t.data)}}},{key:"showPrompt",value:function(){return this.prompt.beckon(this.question()).pipe(this.pipeline).pipe(new _dispatcher2["default"](this.route))}},{key:"workflow",value:function(){return this.state.files.length?{query:new _queries_transform2["default"],menu:new _menu_transform2["default"]({menu:this.menu}),dispatch:new _dispatch_transform2["default"]({store:this.props.store})}:{glob:new _generic_transform2["default"](this.processGlob.bind(this)),dispatch:new _dispatch_transform2["default"]({store:this.props.store})}}},{key:"renderMenu",value:function(){return this.state.files.length?(this.menu.setOptions(this.createOptionsFrom(this.state.files)),this.menu.render()):""}},{key:"renderPrompt",value:function(){return this.showPrompt}}]),t}(_page2["default"]);exports["default"]=GlobPage;