laybot-auto-config 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ReadMe.md ADDED
@@ -0,0 +1,3 @@
1
+ # larryms-auto-config
2
+ 适用于larryms 前后端分离框架,用于监听开发过程中关键目录的配置生成,可实现类似vue3
3
+ 中自动import的功能
@@ -0,0 +1,8 @@
1
+ import e from"fs";import n from"path";import t from"prettier";import i from"proper-lockfile";import o from"chokidar";
2
+ /**
3
+ * @license
4
+ * Author:larry
5
+ * webSite:https://www.laybot.cn
6
+ * larryms/laybot自动配置注册插件
7
+ * laybot自动配置注册插件(支持组件统一驼峰key,推荐kebab-case目录/文件名)
8
+ */const r=e=>e.charAt(0).toUpperCase()+e.slice(1),c=process.env.LAYBOT_LOG_LEVEL||"warn",s="production"===process.env.NODE_ENV;function a(...e){["warn","debug"].includes(c)&&console.warn("[laybot-config-warn]",...e)}function l(...e){"debug"===c&&console.log("[laybot-config-debug]",...e)}function u(e,t,i){const o=n.relative(t,e).split(n.sep),c=n.basename(o.pop(),".js");return(o.map(r).join("")+r(c)+i).replace(/^./,(e=>e.toLowerCase()))}function f(e,n=500,t=3){let i,o=0;return async(...r)=>{clearTimeout(i),i=setTimeout((async()=>{try{await e(...r),o=0}catch(n){o++<t?(l("laybot模块注册重试 ("+o+"/"+t+")"),await new Promise((e=>setTimeout(e,300))),i=setTimeout((()=>e(...r)),0)):function(...e){["error","warn","debug"].includes(c)&&console.error("[laybot-config-error]",...e)}("laybot模块注册失败:",n)}}),n)}}function d(e){return e.map(((e,n)=>(e=e.replace(/-([a-z])/g,((e,n)=>n.toUpperCase())),0===n?e:r(e)))).join("")}function p(t,i){const o={},r=n.join(t,i.dir);if(!e.existsSync(r))return o;const c=t=>{e.readdirSync(t,{withFileTypes:!0}).forEach((e=>{const s=n.join(t,e.name);if(e.isDirectory())return void(i.deep&&c(s));if(!e.isFile())return;if(!i.exts.some((n=>e.name.endsWith(n))))return;const l=n.relative(r,s).replace(/\\/g,"/").replace(/\.[^.]+$/,""),u=function(e){return d(e.split("/").filter(Boolean))}(l);if(o[u]){const e='[laybot-auto-config] biz module key conflict: "'+u+'"\n- '+i.dir+"/"+o[u]+"\n- "+i.dir+"/"+l+"\n请保证业务模块 key 全局唯一(建议调整目录/文件名)。";if(i.strictUnique)throw new Error(e);a(e)}o[u]=l}))};return c(r),o}async function y(o,c,f,y,m,h,g){let w={api:{},store:{},utils:{},mock:{},route:{}};const S={};for(const t of f){w[t]||(w[t]={});const i=r(t),c=n.join(o,t),s=o=>{e.readdirSync(o,{withFileTypes:!0}).forEach((r=>{const a=n.join(o,r.name);if(r.isDirectory())s(a);else if(r.isFile()&&r.name.endsWith(".js")){const o=u(a,c,i),r=n.relative(c,a).replace(/\\/g,"/").replace(/\.js$/,"").replace(/'/g,"\\'");if(w[t][o]=r,y&&a.includes(m)){const n=e.readFileSync(a,"utf8"),t=/mock\.add\(\s*{[^}]*url:\s*['"]([^'"]+)['"]/g;let i;for(;null!==(i=t.exec(n));)S[i[1]]=o}}}))};e.existsSync(c)&&s(c)}const b=p(o,g);let j={};j=function(t,i="components"){const o={},r=n.join(t,i);return e.existsSync(r)?(function t(i){const c=e.readdirSync(i,{withFileTypes:!0});for(const e of c){const c=n.join(i,e.name);if(e.isDirectory())t(c);else if(e.isFile()&&e.name.endsWith(".html")){let e=n.relative(r,c).replace(/\\/g,"/").replace(/\.html$/,""),t=d(e.split("/"));t.endsWith("Index")&&(t=t.slice(0,-5)),o[t]=e}}}(r),o):o}(o,h);const k=Object.assign({},w,{service:b,mockMap:y?S:{},components:j}),x="\n/* laybot-config */\nlarryms.define(function(exports) {\n exports('autoconfig', "+JSON.stringify(k,((e,n)=>"string"==typeof n?n.replace(/\\/g,"/"):n),2)+")\n})";let v;try{v=await t.format(x,{parser:"babel",semi:!1,singleQuote:!0,trailingComma:"none"}),s&&!function(e){try{const n=/exports\(['"]autoconfig['"],\s*\{/.test(e),t=["api","store","utils","mock","route"].every((n=>e.includes('"'+n+'":')));return n&&t}catch(e){return l("laybot组件注册校验异常:",e.message),!1}}(v)&&a("laybot配置结构校验未通过,请检查配置文件")}catch(e){if(s)throw e;v=x}const F=c+".tmp",D=c+".bak";let E="";try{!e.existsSync(c)&&(e.mkdirSync(n.dirname(c),{recursive:!0}),e.writeFileSync(c,"","utf8"));const t=await i.lock(c,{retries:5});try{e.existsSync(c)&&(E=e.readFileSync(c,"utf8")),e.writeFileSync(F,v,"utf8"),E&&e.writeFileSync(D,E,"utf8"),e.renameSync(F,c)}finally{await t()}}catch(n){throw e.existsSync(D)&&(e.writeFileSync(c,E,"utf8"),e.unlinkSync(D)),n}finally{[F,D].forEach((n=>{e.existsSync(n)&&e.unlinkSync(n)}))}}function m({baseDir:t="src",directories:i=[],componentDir:r="components",configureFile:c="autoconfig",enableMockMap:s=!1,mockDirectory:l="mock",biz:u={dir:"service",deep:!0,keyMode:"camelUnique",exts:[".js"],strictUnique:!0}}={}){return{name:"vite:autoconfig",configureServer(d){const p=n.resolve(process.cwd(),t),m=Object.assign({dir:"service",deep:!0,keyMode:"camelUnique",exts:[".js"],strictUnique:!0},u||{}),h=Array.isArray(i)?i:[],g=h.filter((e=>"service"!==e&&e!==m.dir));h.length!==g.length&&a('directories 中检测到 "service" 或 "'+m.dir+'",已自动剔除。请使用 biz.dir 管理业务模块扫描。');const w=n.join(p,...c.split("/"))+".js";y(p,w,g,s,l,r,m).catch((e=>console.error("laybot模块初始化注册失败:",e)));const S=f((async()=>{await y(p,w,g,s,l,r,m),d.ws.send({type:"full-reload"})}),300),b=[];g.forEach((t=>{const i=n.join(p,t);if(e.existsSync(i)){const r=o.watch(i,{ignoreInitial:!0});function c(e){e.endsWith(".js")&&S()}r.on("add",c).on("change",c).on("unlink",c).on("addDir",c),b.push(r)}}));const j=n.join(p,m.dir);if(e.existsSync(j)){const x=o.watch(j,{ignoreInitial:!0});function v(e){m.exts.some((n=>e.endsWith(n)))&&S()}x.on("add",v).on("change",v).on("unlink",v).on("addDir",v),b.push(x)}const k=n.join(p,r);if(e.existsSync(k)){const F=o.watch(k,{ignoreInitial:!0});function D(e){e.endsWith(".html")&&S()}F.on("add",D).on("change",D).on("unlink",D).on("addDir",D),b.push(F)}d.httpServer?.once("close",(()=>{b.forEach((e=>e.close()))}))}}}export{m as default};
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "laybot-auto-config",
3
+ "version": "1.9.0",
4
+ "main": "dist/index.min.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1",
7
+ "build": "rollup -c",
8
+ "prepublishOnly": "npm run build"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "keywords": [],
14
+ "author": "larry",
15
+ "license": "ISC",
16
+ "type": "module",
17
+ "description": "适用于laybot库自动载入模块组件",
18
+ "dependencies": {
19
+ "prettier": "^3.4.2",
20
+ "proper-lockfile": "^4.1.2"
21
+ },
22
+ "devDependencies": {
23
+ "@rollup/plugin-dsv": "^3.0.5",
24
+ "@rollup/plugin-terser": "^0.4.4",
25
+ "@rollup/pluginutils": "^5.1.4",
26
+ "javascript-obfuscator": "^4.1.1",
27
+ "jsdoc": "^4.0.4",
28
+ "rollup": "^4.37.0"
29
+ }
30
+ }