UNPKG

file-prompt

Version:

An interactive prompt for selecting files from a directory.

1 lines 4.04 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)}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 _chalk=require("chalk"),_chalk2=_interopRequireDefault(_chalk),_dispatcher=require("../streams/dispatcher"),_dispatcher2=_interopRequireDefault(_dispatcher),_dispatch_transform=require("../streams/dispatch_transform"),_dispatch_transform2=_interopRequireDefault(_dispatch_transform),_menu_transform=require("../streams/menu_transform"),_menu_transform2=_interopRequireDefault(_menu_transform),_minimatchAll=require("minimatch-all"),_minimatchAll2=_interopRequireDefault(_minimatchAll),_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),_child_process=require("child_process"),ChangedPage=function(e){function t(e){_classCallCheck(this,t);var r=_possibleConstructorReturn(this,Object.getPrototypeOf(t).call(this,e));return r.question="Add files",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:this.getFiles(this.getGlob())}}},{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(),r=(0,_child_process.execSync)("git diff --name-only"),n=r.toString().split("\n");return Array.isArray(e)||(e=[e]),n.length?n.map(function(e){return _path2["default"].resolve(e)}).filter(function(r){return(0,_minimatchAll2["default"])(r,e)&&r.indexOf(t)>-1}):[]}},{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 0===this.menu.options().length?(this.props.stdout.write(_chalk2["default"].bold.red("No files have been changed since last git commit.\n")),this.navigate("index")):this.prompt.beckon(this.question).pipe(this.pipeline).pipe(new _dispatcher2["default"](this.route))}},{key:"workflow",value:function(){return{query:new _queries_transform2["default"],menu:new _menu_transform2["default"]({menu:this.menu}),dispatch:new _dispatch_transform2["default"]({store:this.props.store})}}},{key:"renderMenu",value:function(){return this.menu.setOptions(this.createOptionsFrom(this.state.files)),this.menu.render()}},{key:"renderPrompt",value:function(){return this.showPrompt}}]),t}(_page2["default"]);exports["default"]=ChangedPage;