folder-validator
Version:
A powerful tool to validate and maintain consistent folder structures across your projects
5 lines • 2.05 kB
JavaScript
import{a as c,b as f}from"../chunk-FPPRYSIY.js";import d from"fs";import a from"path";import s from"chalk";import g from"chokidar";var u=["folder.config.json","folder.config.js"];async function w(r){for(let t of u){let e=a.join(r,t);if(d.existsSync(e))return e}return null}async function h(r){var t;try{if(a.extname(r)===".json"){let o=await d.promises.readFile(r,"utf-8"),n=JSON.parse(o),i=n.folders||n;return c(Array.isArray(i)?i:[])}else{let n=await import(a.resolve(r)),i=((t=n.default)==null?void 0:t.folders)||n.default||n.folders||n;return c(Array.isArray(i)?i:[])}}catch(e){console.error(s.red(`Error loading config: ${e}`)),process.exit(1)}}async function l(r,t){try{let e=await h(t),o=await f(r,e,{strict:!0,ignoreCase:!0});return o.isValid?console.log(s.green("[Success]")+" - All folders match the expected structure"):(console.log(s.red("[Error]")+" - Folder structure validation failed"),o.errors.forEach(n=>{console.log(s.red("[Error]")+` ${n}`)})),o.warnings.length>0&&o.warnings.forEach(n=>{console.log(s.yellow("[Warning]")+` ${n}`)}),o.isValid}catch(e){return console.error(s.red(`
\u274C Error: ${e}
`)),!1}}async function p(r,t){console.log(s.blue("info")+" - Watching for folder structure changes. Press Ctrl+C to stop"),g.watch(r,{ignored:[/(^|[\/\\])\../,"node_modules","dist"],persistent:!0,ignoreInitial:!1,depth:5,awaitWriteFinish:!0}).on("addDir",async o=>{console.log(s.blue("[Info]")+` - New folder detected: ${o}`),await l(r,t)}).on("unlinkDir",async o=>{console.log(s.blue("[Info]")+` - Folder removed: ${o}`),await l(r,t)})}async function m(){let r=process.argv.slice(2),t=r.includes("--watch")||r.includes("-w"),e=process.cwd(),o=await w(e);if(o||(console.error(s.red("[Error]")+" - No configuration file found. Create one of: folder.config.ts, folder.config.js, or folder.config.json"),process.exit(1)),t)await p(e,o);else{let n=await l(e,o);process.exit(n?0:1)}}m().catch(r=>{console.error(s.red("[Error]")+` - Fatal error occurred: ${r}`),process.exit(1)});
//# sourceMappingURL=cli.js.map