cross-tools
Version:
Cross-platform fundamental tools for developers
1 lines • 5.71 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});const tslib_1=require("tslib"),command_1=require("@oclif/command"),prompts_1=tslib_1.__importDefault(require("prompts")),colors_1=tslib_1.__importDefault(require("colors")),dockerode_1=tslib_1.__importDefault(require("dockerode")),asyncLoader_1=tslib_1.__importDefault(require("../../util/asyncLoader")),removeAllVolumes_1=tslib_1.__importDefault(require("./actions/removeAllVolumes")),runAnImage_1=tslib_1.__importDefault(require("./actions/runAnImage")),removeImages_1=tslib_1.__importDefault(require("./actions/removeImages")),viewContainerNetworks_1=tslib_1.__importDefault(require("./actions/viewContainerNetworks")),askOperation=()=>prompts_1.default({type:"autocomplete",name:"operation",message:"Select an operation",choices:[{title:"Select containers",value:"select-containers",description:"List, view & take action"},{title:"Clean",value:"clean",description:"Remove Both running & stopped containers + volumes + untagged images"},{title:"Get my docker info",value:"get-my-docker-info",description:"Details of running docker instance"},{title:"Get network info of containers",value:"view-containers-networks",description:"View network info of containers"},{title:"Boilerplate",value:"boilerplate",description:"Run a pre-configured image"}]},{onCancel:()=>process.exit()}),pickContaniers=e=>prompts_1.default({type:"autocompleteMultiselect",name:"selectedContainers",hint:"green names are running containers",message:"Select the containers you wanna deal with",min:1,choices:e.map(e=>({title:""+colors_1.default["running"===e.State?"green":"red"](e.Names[0]),value:e,message:"test",description:"test desc"}))},{onCancel:()=>process.exit()}),askContainersAction=()=>prompts_1.default({type:"autocomplete",name:"containerAction",message:"Select an action",choices:[{title:"Start",value:"start",description:"Start selected containers. (already running containrs will be skipped)"},{title:"Stop",value:"stop",description:"Stop selected containers. (already stopped containrs will be skipped)"},{title:"Remove",value:"remove",description:"Remove (forced) selected containers"},{title:"Restart",value:"restart",description:"Restart / Start (if stopped) selected containers"}]},{onCancel:()=>process.exit()}),commands={removeAllVolumes:removeAllVolumes_1.default,removeImages:removeImages_1.default,runAnImage:runAnImage_1.default,viewContainerNetworks:viewContainerNetworks_1.default};class DockerTools extends command_1.Command{constructor(){super(...arguments),this.docker=new dockerode_1.default}async run(){if(!await this.getDockerInfo())return console.log(colors_1.default.red("Docker is not running."));Object.values(commands).forEach(e=>e.init(this.docker));const{operation:e}=await prompts_1.default({type:"autocomplete",name:"operation",message:"Select an operation",choices:[{title:"Select containers",value:"select-containers",description:"List, view & take action"},{title:"Clean",value:"clean",description:"Remove Both running & stopped containers + volumes + untagged images"},{title:"Get my docker info",value:"get-my-docker-info",description:"Details of running docker instance"},{title:"Get network info of containers",value:"view-containers-networks",description:"View network info of containers"},{title:"Boilerplate",value:"boilerplate",description:"Run a pre-configured image"}]},{onCancel:()=>process.exit()}),t=await this.docker.listContainers({all:!0});switch(e){case"select-containers":if(!t.length)return console.log(colors_1.default.red("No containers found"));const e=(await pickContaniers(t)).selectedContainers;e||(console.log(colors_1.default.red("No containers selected")),process.exit(1));const{containerAction:o}=await prompts_1.default({type:"autocomplete",name:"containerAction",message:"Select an action",choices:[{title:"Start",value:"start",description:"Start selected containers. (already running containrs will be skipped)"},{title:"Stop",value:"stop",description:"Stop selected containers. (already stopped containrs will be skipped)"},{title:"Remove",value:"remove",description:"Remove (forced) selected containers"},{title:"Restart",value:"restart",description:"Restart / Start (if stopped) selected containers"}]},{onCancel:()=>process.exit()});this.execContainersAction(e,o);break;case"clean":await this.execContainersAction(t,"remove"),await commands.removeAllVolumes.handle(),await commands.removeImages.handle({untagged:!0});break;case"view-containers-networks":await commands.viewContainerNetworks.handle();break;case"boilerplate":await commands.runAnImage.handle();break;case"get-my-docker-info":const n=await this.getDockerInfo();console.log(n)}}async execContainersAction(e,t){for(const o of e){const e=`Performing [${t}] container`,n=colors_1.default.cyan(`✅ action: ${colors_1.default.bold(t)}. container: ${o.Names[0]}`);let a;switch(t){case"start":a=()=>new Promise((e,t)=>this.docker.getContainer(o.Id).start().then(e).catch(t)),await asyncLoader_1.default(a,e);break;case"stop":a=()=>new Promise((e,t)=>this.docker.getContainer(o.Id).stop().then(e).catch(e)),await asyncLoader_1.default(a,e);break;case"remove":a=()=>new Promise((e,t)=>this.docker.getContainer(o.Id).remove({force:!0}).then(e).catch(t)),await asyncLoader_1.default(a,e);break;case"restart":a=()=>new Promise((e,t)=>this.docker.getContainer(o.Id).restart().then(e).catch(t)),await asyncLoader_1.default(a,e);break;default:console.log(colors_1.default.red("container action not supported"))}console.log(n)}console.log(colors_1.default.cyan(`✅ total ${e.length}: container(s) affected (${t}).`))}async getDockerInfo(){return new Promise(e=>{this.docker.info().then(e).catch(()=>e(!1))})}}exports.default=DockerTools;