UNPKG

@stories-js/cli

Version:

Stories CLI to manage list of stories in UI

4 lines (3 loc) 4.41 kB
#!/usr/bin/env node import{writeFileSync as t}from"fs";import r from"chokidar";import e from"yargs";import{hideBin as o}from"yargs/helpers";import{parse as n,format as i,sep as a,resolve as s,dirname as u,relative as p,posix as c}from"path";import l from"fast-glob";var f,m=process.cwd(),d=["**/*.stories.tsx","**/*.stories.ts","**/*.stories.jsx","**/*.stories.js"],h=e(o(process.argv)).command("scan <patterns...>","Search and save list of stories in file",function(t){return t.option("output",{describe:"The relative path to file to save output.",default:"./stories-list.js",type:"string"}).option("from",{describe:"The path to search from & generate imports",default:m,type:"string"}).option("dryRun",{describe:"Set this to avoid writing any files.",default:!1,type:"boolean"}).option("quiet",{describe:"Hide all logs",default:!1,type:"boolean"}).option("_",{describe:"Search patterns",default:d,type:"string"})}).example("$0 **/*.stories.tsx","Search by pattern").example("$0 --from ./src","Search in folder and all subfolders").example("$0 --output ./stories-list.js","Save output stories list in file").example("$0 --dryRun","Run but do not save output").example("$0 --quiet","Suppress logs messages").version("version","0.2.0").argv,v={dryRun:h.dryRun||!1,quiet:h.quiet||!1,from:h.from||m,output:h.output||"./stories-list.js",search:(f=h._,f.length>0?f:d)};function g(){return g=Object.assign||function(t){for(var r=1;r<arguments.length;r++){var e=arguments[r];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t},g.apply(this,arguments)}var y=function(){return y=Object.assign||function(t){for(var r,e=1,o=arguments.length;e<o;e++)for(var n in r=arguments[e])Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n]);return t},y.apply(this,arguments)};function P(t){return t.toLowerCase()}var b=[/([a-z0-9])([A-Z])/g,/([A-Z])([A-Z][a-z])/g],j=/[^A-Z0-9]+/gi;function x(t,r,e){return r instanceof RegExp?t.replace(r,e):r.reduce(function(t,r){return t.replace(r,e)},t)}function R(t,r){var e=t.charAt(0),o=t.substr(1).toLowerCase();return r>0&&e>="0"&&e<="9"?"_"+e+o:""+e.toUpperCase()+o}function S(t,r){return 0===r?t.toLowerCase():R(t,r)}function w(t){var r=new Set,e=t.map(function(t,e){var o=n(t),s=function(t,r){return void 0===r&&(r={}),function(t,r){return void 0===r&&(r={}),function(t,r){void 0===r&&(r={});for(var e=r.splitRegexp,o=r.stripRegexp,n=void 0===o?j:o,i=r.transform,a=void 0===i?P:i,s=r.delimiter,u=void 0===s?" ":s,p=x(x(t,void 0===e?b:e,"$1\0$2"),n,"\0"),c=0,l=p.length;"\0"===p.charAt(c);)c++;for(;"\0"===p.charAt(l-1);)l--;return p.slice(c,l).split("\0").map(a).join(u)}(t,y({delimiter:"",transform:R},r))}(t,y({transform:S},r))}(o.name);r.has(s)?r.add(s+"_"+e):r.add(s);var u=i(g({},o,{base:void 0,ext:void 0}));return u.startsWith(".")||(u="."+a+u),{name:s,originalPath:t,importPath:u}});return e}function A(t){return t.split(a).join(c.sep)}var O=function(r){try{return q(r),Promise.resolve(function(t){var r=t.rootPath,e=t.patterns,o=t.outputPath,n=t.ignorePaths;try{var i=s(r);return Promise.resolve(l(e,{absolute:!0,caseSensitiveMatch:!1,ignore:n,cwd:i})).then(function(t){var e=t.map(A),n=s(r,o),a=u(n),c=e.map(function(t){return p(a,t)});return{paths:e,searchFrom:i,outputFilePath:n,importPaths:c}})}catch(t){return Promise.reject(t)}}({rootPath:v.from,patterns:v.search,outputPath:v.output,ignorePaths:$})).then(function(r){var e=r.outputFilePath,o=r.importPaths;q("Saving to: "+e);var n,i=["/* NOTE: This is an autogenerated file; please do not edit. */\n",(n=w(o)).map(function(t){return"import * as "+t.name+" from '"+t.importPath+"'"}).join(";\n"),"","const modules = [",n.map(function(t){return" "+t.name+","}).join("\n"),"];\n","export default modules;"].join("\n");v.dryRun||(t(e,i,"utf8"),q("Saved.")),q(""),(v.quiet||v.dryRun)&&console.log(i)})}catch(t){return Promise.reject(t)}},$=["**/node_modules/**"],q=v.dryRun?function(){}:console.log,C=function(){q("\nLooking stories in "+v.from),q("Search patterns are "+v.search+"\n"),v.dryRun&&q("Dry run."),r.watch(v.search,{cwd:v.from,usePolling:!0,interval:2e3,binaryInterval:2e3}).on("error",function(t){return q("Watcher error: "+t)}).on("add",function(t){return Promise.resolve(O("Added "+t))}).on("change",function(t){return Promise.resolve(O("Change "+t))}).on("unlink",function(t){return Promise.resolve(O("Removed "+t))})};export{C as scan}; //# sourceMappingURL=cli.esm.js.map