UNPKG

@stories-js/cli

Version:

Stories CLI to manage list of stories in UI

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