UNPKG

file-prompt

Version:

An interactive prompt for selecting files from a directory.

1 lines 3.62 kB
"use strict";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)}function readDir(e,t){var r={};return Array.isArray(t)||(t=[t]),_fs2["default"].readdirSync(e).filter(function(e){return(0,_minimatchAll2["default"])(e,t)}).forEach(function(t){var n=_path2["default"].basename(t,"_page.js");r[n]=require(_path2["default"].resolve(__dirname,e,t))["default"]}),r}function selectCurrentPage(e){return e.getState().currentPage}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 _component=require("./component"),_component2=_interopRequireDefault(_component),_fs=require("fs"),_fs2=_interopRequireDefault(_fs),_minimatchAll=require("minimatch-all"),_minimatchAll2=_interopRequireDefault(_minimatchAll),_path=require("path"),_path2=_interopRequireDefault(_path),_reducers=require("./reducers"),_reducers2=_interopRequireDefault(_reducers),_redux=require("redux"),_actions=require("./actions"),App=function(e){function t(e){_classCallCheck(this,t);var r=_possibleConstructorReturn(this,Object.getPrototypeOf(t).call(this,e));return r.store=(0,_redux.createStore)(_reducers2["default"],{config:{base:r.props.base},files:[],glob:r.props.glob,currentPage:{name:"index",props:{glob:r.props.glob},isNavigating:!1}}),r}return _inherits(t,e),_createClass(t,[{key:"getDefaultProps",value:function(){return{base:process.cwd(),filter:"**/*.js",stdin:process.stdin,stdout:process.stdout}}},{key:"getInitialState",value:function(){return{pageName:null,pageProps:null}}},{key:"componentWillMount",value:function(){var e=this,t=selectCurrentPage(this.store);this.once("complete",function(){e.componentWillUnmount()},this),this.once("error",function(){e.componentWillUnmount()},this),this.setState({pageName:t.name,pageProps:t.props})}},{key:"componentDidMount",value:function(){var e=this;this.unsubscribe=this.store.subscribe(function(){var t=selectCurrentPage(e.store);t.isNavigating&&(e.setState({pageName:t.name,pageProps:t.props}),_component2["default"].display(e),e.store.dispatch((0,_actions.navigateComplete)()))})}},{key:"componentWillUnmount",value:function(){this.off(),this.unsubscribe()}},{key:"renderPage",value:function(){var e={app:this,store:this.store,stdin:this.props.stdin,stdout:this.props.stdout},r=void 0;if(!t.PAGES.hasOwnProperty(this.state.pageName))throw new Error("App: Page does not exist “"+this.state.pageName+"”.");return this.state.pageProps&&Object.assign(e,this.state.pageProps),r=new t.PAGES[this.state.pageName](e),"index"===this.state.pageName&&r.once("complete",this.emit.bind(this,"complete")),r.render()}},{key:"render",value:function(){return this.renderPage()}}]),t}(_component2["default"]);App.PAGES=readDir(_path2["default"].join(__dirname,"pages"),"*_page.js"),exports["default"]=App;