UNPKG

move-prop-types

Version:

"Help quickly replace the prop type checker in older react projects to map to prop-types"

3 lines (2 loc) 4.4 kB
#!/usr/bin/env node import{Command as e}from"commander";import{stdout as o,argv as t}from"process";import r from"chalk";import{readFile as s,writeFile as n,readdir as i,lstatSync as l}from"fs";import{exec as a}from"child_process";import{promisify as p}from"util";const c=/,\s?{\s?PropTypes\s?}/g,d=/,\s?PropTypes\s?}/g,f=/{\s?PropTypes\s?,/g,g=/,\s?PropTypes\s?,/g,y="utf-8",h="\nimport PropTypes from 'prop-types';",m=/React\.PropTypes\./g,u=p(a),$=p(s),x=p(n),w=p(i),v=async(e,o)=>{if(!o)return void console.error("No file path provided");let t=o;if(/[.]/.exec(t)){if(!/\S+\.(jsx?|tsx?)$/.test(t))return void console.log(`Skipping ${t} - not a .js, .jsx, .ts, or .tsx file`)}else{const e=await import("fs"),o=p(e.stat),s=[".js",".jsx",".ts",".tsx"];let n=!1;for(const e of s)try{await o(`${t}${e}`),t=`${t}${e}`,n=!0;break}catch(e){}if(!n)return void console.log(`${r.magenta.italic(t)} doesn't ${r.red.inverse("seem to exist in the given path")} with extensions .js, .jsx, .ts, or .tsx`)}await(async e=>{try{const o=(await $(e,y)).toString(),t=c.test(o)||f.test(o)||g.test(o)||d.test(o),s=-1!==o.indexOf(h);if(!t||s)return;let n=o.replace(c,"");n=n.replace(f,"{"),n=n.replace(g,","),n=n.replace(d," }"),n=n.replace(/import React, \{\s+([^}]+)\s+\}/g,"import React, { $1 }"),n=n.replace(/,\s+,/g,","),n=n.replace(/,\s+}/g," }"),n=n.replace(m,"PropTypes.");const i=/(import.*?['"].*?['"];?\n)/,l=n.match(i);if(l){const e=n.indexOf(l[0])+l[0].length;n=n.slice(0,e)+h+n.slice(e)}else n=`${h}\n${n}`;n&&(await x(e,n,y),console.log(`${r.magenta.italic(e)} just got ${r.green("updated")}!`))}catch(o){const t=o instanceof Error?o.message:String(o);console.error(`Error processing file ${e}:`,t)}})(t)},j=async(e,t)=>{console.log("");try{const e=await w(t),s=e.filter(e=>l(`${t}/${e}`).isDirectory());for(const e of s)await j("updateFolder",`${t}/${e}`);const n=e.filter(e=>!l(`${t}/${e}`).isDirectory()).filter(e=>/\.(jsx?|tsx?)$/.test(e));for(const e of n)await v(0,`${t}/${e}`);console.log(""),console.log(`folder ${r.underline.yellowBright(t)} and js/jsx/ts/tsx files inside are now ${r.greenBright("ready")}!`),o.write(""),o.write(""),console.log(`Your folder and files have been updated. Thank you for using ${r.yellowBright("move-prop-types")}`)}catch(e){const o=e instanceof Error?e.message:String(e);console.error(`Error processing folder ${t}:`,o)}},T=()=>"\n Examples:\n $ move-prop-types --help for info\n $ move-prop-types -P ../dir1/dir2/filename.[js|jsx|ts|tsx] - This will run replace only on the given file.\n $ move-prop-types -F ../dir1/dir2 - This will run the update for all the files inside the given directory\n $ move-prop-types -I -F ../dir1/dir2 - This will install prop-types to dependencies and run the update for all the files inside the given directory\n",P=(e,o)=>{if(!Array.isArray(e))return"";let t=0;return e.filter(r=>{r!==o[0]&&r!==o[1]||(t=e.indexOf(r)+1)}),t&&t<e.length&&e[t]||""},F=()=>{const o=new e;return o.name("move-prop-types").alias("mpt").usage("[options] [file|folder]").version("1.1.2").option("-I, --install","install the latest prop-types package and then continue with rest of the commands").option("-P, --path <path>","input path information of the file to update").option("-F, --folder <folder>","input folder info where all the files would be updated").addHelpText("after",T),o.cli=!0,o};F(),(async(e=t)=>{const o=F(),s=[...e];-1===s.indexOf("--install")&&-1===s.indexOf("-I")&&-1===s.indexOf("--path")&&-1===s.indexOf("-P")&&-1===s.indexOf("--folder")&&-1===s.indexOf("-F")&&s.push("--help");const n=P(s,["--path","-P"]),i=P(s,["--folder","-F"]);o.parse(s);const l=o.opts();l.install&&await(async()=>{console.log("");try{await import("prop-types"),console.log(`${r.cyan.underline.bold("prop-types")} is already installed in your project`)}catch{console.log("Installing prop-types to your project");try{const{stdout:e,stderr:o}=await u("pnpm add prop-types");if(o)return console.log(`stderr: ${o}`),void console.log("");console.log(`${r.hex("#FF6347").bold("Installation underway")}`),console.log(e),console.log(`${r.cyan.underline.bold("prop-types")} is now installed`)}catch(e){const o=e instanceof Error?e.message:String(e);console.error("Error installing prop-types:",o)}}})(),(l.path||n)&&await v(0,l.path||n),(l.folder||i)&&await j("updateFolder",l.folder||i)})().catch(e=>{console.error("CLI Error:",e),process.exit(1)});