UNPKG

8.04 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var chalk=_interopDefault(require("chalk")),process=require("process"),propReplace=_interopDefault(require("commander")),global$1="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};function defaultSetTimout(){throw new Error("setTimeout has not been defined")}function defaultClearTimeout(){throw new Error("clearTimeout has not been defined")}var cachedSetTimeout=defaultSetTimout,cachedClearTimeout=defaultClearTimeout;function runTimeout(o){if(cachedSetTimeout===setTimeout)return setTimeout(o,0);if((cachedSetTimeout===defaultSetTimout||!cachedSetTimeout)&&setTimeout)return cachedSetTimeout=setTimeout,setTimeout(o,0);try{return cachedSetTimeout(o,0)}catch(e){try{return cachedSetTimeout.call(null,o,0)}catch(e){return cachedSetTimeout.call(this,o,0)}}}function runClearTimeout(o){if(cachedClearTimeout===clearTimeout)return clearTimeout(o);if((cachedClearTimeout===defaultClearTimeout||!cachedClearTimeout)&&clearTimeout)return cachedClearTimeout=clearTimeout,clearTimeout(o);try{return cachedClearTimeout(o)}catch(e){try{return cachedClearTimeout.call(null,o)}catch(e){return cachedClearTimeout.call(this,o)}}}"function"==typeof global$1.setTimeout&&(cachedSetTimeout=setTimeout),"function"==typeof global$1.clearTimeout&&(cachedClearTimeout=clearTimeout);var currentQueue,queue=[],draining=!1,queueIndex=-1;function cleanUpNextTick(){draining&&currentQueue&&(draining=!1,currentQueue.length?queue=currentQueue.concat(queue):queueIndex=-1,queue.length&&drainQueue())}function drainQueue(){if(!draining){var e=runTimeout(cleanUpNextTick);draining=!0;for(var o=queue.length;o;){for(currentQueue=queue,queue=[];++queueIndex<o;)currentQueue&&currentQueue[queueIndex].run();queueIndex=-1,o=queue.length}currentQueue=null,draining=!1,runClearTimeout(e)}}function nextTick(e){var o=new Array(arguments.length-1);if(1<arguments.length)for(var t=1;t<arguments.length;t++)o[t-1]=arguments[t];queue.push(new Item(e,o)),1!==queue.length||draining||runTimeout(drainQueue)}function Item(e,o){this.fun=e,this.array=o}Item.prototype.run=function(){this.fun.apply(null,this.array)};var title="browser",platform="browser",browser=!0,env={},argv=[],version="",versions={},release={},config={};function noop(){}var on=noop,addListener=noop,once=noop,off=noop,removeListener=noop,removeAllListeners=noop,emit=noop;function binding(e){throw new Error("process.binding is not supported")}function cwd(){return"/"}function chdir(e){throw new Error("process.chdir is not supported")}function umask(){return 0}var performance=global$1.performance||{},performanceNow=performance.now||performance.mozNow||performance.msNow||performance.oNow||performance.webkitNow||function(){return(new Date).getTime()};function hrtime(e){var o=.001*performanceNow.call(performance),t=Math.floor(o),r=Math.floor(o%1*1e9);return e&&(t-=e[0],(r-=e[1])<0&&(t--,r+=1e9)),[t,r]}var startTime=new Date;function uptime(){return(new Date-startTime)/1e3}var process$1={nextTick:nextTick,title:title,browser:browser,env:env,argv:argv,version:version,versions:versions,on:on,addListener:addListener,once:once,off:off,removeListener:removeListener,removeAllListeners:removeAllListeners,emit:emit,binding:binding,cwd:cwd,chdir:chdir,umask:umask,hrtime:hrtime,platform:platform,release:release,config:config,uptime:uptime},version$1="0.6.2",es6PropTypeJust=/,\s?{\sPropTypes\s}/g,es6PropTypeRight=/,\s?PropTypes\s?}/g,es6PropTypeLeft=/PropTypes\s?,/g,fileEncoding="utf-8",importState="\nimport PropTypes from 'prop-types';",reactProto=/React.PropTypes./g,_require=require("fs"),lstatSync=_require.lstatSync,readdir=_require.readdir,readFile=_require.readFile,stat=_require.stat,writeFile=_require.writeFile,_require2=require("child_process"),exec=_require2.exec,installPackage=function(){console.log("");try{require.resolve("prop-types"),console.log("".concat(chalk.cyan.underline.bold("prop-types")," is already installed in your project"))}catch(e){console.log("Installing prop-types to your project"),exec("npm i --color=always prop-types -S",function(e,o,t){if(e)return console.log("stderr: ".concat(t)),void console.log("");console.log("".concat(chalk.hex("#FF6347").bold("Installation underway"))),console.log(o),console.log("".concat(chalk.cyan.underline.bold("prop-types")," is now installed"))})}},writeFileAsyncEs6=function(i){readFile(i,fileEncoding,function(e,o){if(e)throw e;var t=es6PropTypeJust.test(o)||es6PropTypeLeft.test(o)||es6PropTypeLeft.test(o),r=-1!==o.indexOf(importState);if(t&&!r){var n="string"==typeof o?o.replace(es6PropTypeJust,""):o;(n="string"==typeof(n="string"==typeof(n=(n=n.replace(es6PropTypeLeft,"")).replace(es6PropTypeRight," }"))?n.replace(reactProto,"PropTypes."):n)?[n.slice(0,n.indexOf("';\n")+2),importState,n.slice(n.indexOf("';\n")+2)].join(""):n)&&writeFile(i,n,fileEncoding,function(e){if(e)throw e})}}),console.log("".concat(chalk.magenta.italic(i)," just got ").concat(chalk.green("updated"),"!"))},updateFile=function(e,o){if(/[.]/.exec(o)){if(!/\S+.js[x]?$/.test(o))return}else stat("".concat(o,".js"),function(e){e&&("ENOENT"===e.code&&console.log("".concat(chalk.magenta.italic(o+".js")," doesn't ").concat(chalk.red.inverse("seem to exist in the given path"))),console.log("error : ".concat(e))),o="".concat(o,".js")}),stat("".concat(o,".jsx"),function(e){e&&("ENOENT"===e.code&&console.log("".concat(chalk.magenta.italic(o+".jsx")," doesn't ").concat(chalk.red.inverse("seem to exist in the given path"))),console.log("error : ".concat(e))),o="".concat(o,".jsx")});writeFileAsyncEs6(o)},updateFolder=function t(e,r){console.log(""),readdir(r,function(e,o){e&&console.log("error : ".concat(e)),o.filter(function(e){return lstatSync("".concat(r,"/").concat(e)).isDirectory()}).map(function(e){t("updateFolder","".concat(r,"/").concat(e))}),o.filter(function(e){return!lstatSync("".concat(r,"/").concat(e)).isDirectory()}).forEach(function(e){updateFile("updateFolder","".concat(r,"/").concat(e))}),console.log(""),console.log("folder ".concat(chalk.underline.yellowBright(r)," and js/jsx files inside are now ").concat(chalk.greenBright("ready"),"!")),process.stdout.write(""),process.stdout.write(""),console.log("Your folder and files have been updated.Thank you for using ".concat(chalk.yellowBright("move-prop-types")))})},helpExamples=function(){console.log(""),console.log(" Examples:"),console.log(""),console.log(" $ prop-replace --help for info"),console.log(" $ prop-replace -P ../dir1/dir2/filename.[js|jsx] , This will run replace only on the given file."),console.log(" $ prop-replace -F ../dir1/dir2 , This will run the update for all the files inside the given directory"),console.log(" $ prop-replace -I -F ../dir1/dir2 , This will install prop-types to dependencies and run the update for all the files inside the given directory"),console.log("")},findMatch=function(o,t){var r=0;if(o instanceof Array)return o.filter(function(e){e!==t[0]&&e!==t[1]||(r=o.indexOf(e)+1)}),r?o[r]:""};-1===process.argv.indexOf("--install")&&-1===process.argv.indexOf("-I")&&-1===process.argv.indexOf("--path")&&-1===process.argv.indexOf("-P")&&-1===process.argv.indexOf("--folder")&&-1===process.argv.indexOf("-F")&&process.argv.push("--help");var filePath=findMatch(process.argv,["--path","-P"]),folderPath=findMatch(process.argv,["--folder","-F"]);propReplace.command("move-prop-types").alias("mpt").usage("[options] [file|folder]").version("".concat(version$1)).option("-I, --install","install the latest prop-types package and then continue with rest of the commands",installPackage,{cmd:process$1.argv}).option("-P, --path","input path information of the file to update",updateFile,filePath).option("-F, --folder","input folder info where all the files would be updated",updateFolder,folderPath).on("--help , -H",helpExamples).parse(process.argv),propReplace.cli=!0,exports.mpt=propReplace;