UNPKG

cross-tools

Version:

Cross-platform fundamental tools for developers

1 lines 3.38 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const tslib_1=require("tslib"),command_1=require("@oclif/command"),myPrompts_1=require("../util/myPrompts"),colors_1=tslib_1.__importDefault(require("colors")),prompts_1=tslib_1.__importDefault(require("prompts")),shellExecAsync_1=tslib_1.__importDefault(require("../util/shellExecAsync")),cli_table_1=tslib_1.__importDefault(require("cli-table")),axios_1=tslib_1.__importDefault(require("axios")),asyncLoader_1=tslib_1.__importDefault(require("../util/asyncLoader")),choices=[{title:"Get npm packages info",value:"get-npm-packages-info",description:"If multiple, seprate them by space"},{title:"npm single uninstall",value:"npm-single-uninstall",description:"Uninstall a single package"},{title:"npm bulk uninstall",value:"npm-bulk-uninstall",description:"Uninstall by bulk selection"}];class Utility extends command_1.Command{async run(){const{operation:e}=await myPrompts_1.askFuzzy(choices,{});switch(e){case"npm-single-uninstall":await this.npmSingleUninstall();break;case"npm-bulk-uninstall":await this.npmBulkUninstall();break;case"get-npm-packages-info":await this.getPackagesInfo()}}async npmSingleUninstall(){const e=await shellExecAsync_1.default("npm list --depth=0 --json",{silent:!0},{loadingMsg:"Analyzing directory..."}),{dependencies:t}=JSON.parse(e);if(!t)return console.log(colors_1.default.red("package.json not found in the drectory"));const s=Object.keys(t).map(e=>Object.assign({name:e},t[e])),{operation:a}=await myPrompts_1.askFuzzy(s.map(e=>({title:e.name,value:e.name,description:e.version})),{message:"Select the packages you want to uninstall."});await shellExecAsync_1.default("npm un "+a,{silent:!0},{loadingMsg:"Uninstalling "+a}),console.log(colors_1.default.green(colors_1.default.red(a)+" uninstalled"))}async npmBulkUninstall(){const e=await shellExecAsync_1.default("npm list --depth=0 --json",{silent:!0},{loadingMsg:"Analyzing directory..."}),{dependencies:t}=JSON.parse(e);if(!t)return console.log(colors_1.default.red("package.json not found in the drectory"));const s=Object.keys(t).map(e=>Object.assign({name:e},t[e])),{selectedPackages:a}=await prompts_1.default({type:"autocompleteMultiselect",name:"selectedPackages",message:"Select the packages you want to uninstall.",choices:s.map(e=>({title:e.name,value:e.name,description:e.version})),min:1},{onCancel:()=>process.exit()});for(const e of a)await shellExecAsync_1.default("npm un "+e,{silent:!0},{loadingMsg:"Uninstalling "+e}),console.log(colors_1.default.green(colors_1.default.red(e)+" uninstalled"))}async getPackagesInfo(){const{selectedPackages:e}=await prompts_1.default({type:"list",name:"selectedPackages",message:"Type the packages name",separator:" ",validate:e=>!!e.split(" ").filter(e=>e).length||"At least one name required."},{onCancel:()=>process.exit()}),t=e.filter(e=>e),s=await asyncLoader_1.default(async()=>{const e=new cli_table_1.default({head:["Name","Total Versions","Latest Version"].map(e=>colors_1.default.bold(colors_1.default.cyan(e)))});for(const s of t)await axios_1.default.get("https://registry.npmjs.org/"+s).then(t=>{const{versions:a}=t.data;e.push([s,Object.keys(a).length,Object.keys(a).reverse()[0]])}).catch(t=>{const a=colors_1.default.red("N/A");e.push([s,a,a])});return Promise.resolve(e)},"Checking npm database");console.log(s.toString())}}exports.default=Utility;