UNPKG

fe-js-utils

Version:

前端代码质量检测js脚本工具

3 lines (2 loc) 16.3 kB
#!/usr/bin/env node import t from"node:fs";import e from"node:path";import r from"fs";import n from"path";function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function i(t,e,r,n,o,i,a){try{var c=t[i](a),s=c.value}catch(t){return void r(t)}c.done?e(s):Promise.resolve(s).then(n,o)}function a(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function c(t){i(a,n,o,c,s,"next",t)}function s(t){i(a,n,o,c,s,"throw",t)}c(void 0)}))}}function c(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=l(t))||e){r&&(t=r);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,a=!0,c=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return a=t.done,t},e:function(t){c=!0,i=t},f:function(){try{a||null==r.return||r.return()}finally{if(c)throw i}}}}function s(){s=function(){return e};var t,e={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(t,e,r){t[e]=r.value},i="function"==typeof Symbol?Symbol:{},a=i.iterator||"@@iterator",c=i.asyncIterator||"@@asyncIterator",u=i.toStringTag||"@@toStringTag";function l(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{l({},"")}catch(t){l=function(t,e,r){return t[e]=r}}function f(t,e,r,n){var i=e&&e.prototype instanceof m?e:m,a=Object.create(i.prototype),c=new A(n||[]);return o(a,"_invoke",{value:E(t,r,c)}),a}function h(t,e,r){try{return{type:"normal",arg:t.call(e,r)}}catch(t){return{type:"throw",arg:t}}}e.wrap=f;var p="suspendedStart",v="suspendedYield",d="executing",y="completed",g={};function m(){}function x(){}function b(){}var w={};l(w,a,(function(){return this}));var j=Object.getPrototypeOf,F=j&&j(j(C([])));F&&F!==r&&n.call(F,a)&&(w=F);var k=b.prototype=m.prototype=Object.create(w);function S(t){["next","throw","return"].forEach((function(e){l(t,e,(function(t){return this._invoke(e,t)}))}))}function L(t,e){function r(o,i,a,c){var s=h(t[o],t,i);if("throw"!==s.type){var u=s.arg,l=u.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,a,c)}),(function(t){r("throw",t,a,c)})):e.resolve(l).then((function(t){u.value=t,a(u)}),(function(t){return r("throw",t,a,c)}))}c(s.arg)}var i;o(this,"_invoke",{value:function(t,n){function o(){return new e((function(e,o){r(t,n,e,o)}))}return i=i?i.then(o,o):o()}})}function E(e,r,n){var o=p;return function(i,a){if(o===d)throw Error("Generator is already running");if(o===y){if("throw"===i)throw a;return{value:t,done:!0}}for(n.method=i,n.arg=a;;){var c=n.delegate;if(c){var s=_(c,n);if(s){if(s===g)continue;return s}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===p)throw o=y,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var u=h(e,r,n);if("normal"===u.type){if(o=n.done?y:v,u.arg===g)continue;return{value:u.arg,done:n.done}}"throw"===u.type&&(o=y,n.method="throw",n.arg=u.arg)}}}function _(e,r){var n=r.method,o=e.iterator[n];if(o===t)return r.delegate=null,"throw"===n&&e.iterator.return&&(r.method="return",r.arg=t,_(e,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),g;var i=h(o,e.iterator,r.arg);if("throw"===i.type)return r.method="throw",r.arg=i.arg,r.delegate=null,g;var a=i.arg;return a?a.done?(r[e.resultName]=a.value,r.next=e.nextLoc,"return"!==r.method&&(r.method="next",r.arg=t),r.delegate=null,g):a:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,g)}function D(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function O(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function A(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(D,this),this.reset(!0)}function C(e){if(e||""===e){var r=e[a];if(r)return r.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,i=function r(){for(;++o<e.length;)if(n.call(e,o))return r.value=e[o],r.done=!1,r;return r.value=t,r.done=!0,r};return i.next=i}}throw new TypeError(typeof e+" is not iterable")}return x.prototype=b,o(k,"constructor",{value:b,configurable:!0}),o(b,"constructor",{value:x,configurable:!0}),x.displayName=l(b,u,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===x||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,l(t,u,"GeneratorFunction")),t.prototype=Object.create(k),t},e.awrap=function(t){return{__await:t}},S(L.prototype),l(L.prototype,c,(function(){return this})),e.AsyncIterator=L,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new L(f(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(t){return t.done?t.value:a.next()}))},S(k),l(k,u,"Generator"),l(k,a,(function(){return this})),l(k,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},e.values=C,A.prototype={constructor:A,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(O),!e)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var r=this;function o(n,o){return c.type="throw",c.arg=e,r.next=n,o&&(r.method="next",r.arg=t),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var a=this.tryEntries[i],c=a.completion;if("root"===a.tryLoc)return o("end");if(a.tryLoc<=this.prev){var s=n.call(a,"catchLoc"),u=n.call(a,"finallyLoc");if(s&&u){if(this.prev<a.catchLoc)return o(a.catchLoc,!0);if(this.prev<a.finallyLoc)return o(a.finallyLoc)}else if(s){if(this.prev<a.catchLoc)return o(a.catchLoc,!0)}else{if(!u)throw Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return o(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=t,a.arg=e,i?(this.method="next",this.next=i.finallyLoc,g):this.complete(a)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),g},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),O(r),g}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if("throw"===n.type){var o=n.arg;O(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,r,n){return this.delegate={iterator:C(e),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=t),g}},e}function u(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,o,i,a,c=[],s=!0,u=!1;try{if(i=(r=r.call(t)).next,0===e);else for(;!(s=(n=i.call(r)).done)&&(c.push(n.value),c.length!==e);s=!0);}catch(t){u=!0,o=t}finally{try{if(!s&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(u)throw o}}return c}}(t,e)||l(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(t,e){if(t){if("string"==typeof t)return o(t,e);var r={}.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(t,e):void 0}}var f,h,p={targetDir:"./src",outputFile:e.join(process.cwd(),"check-any-results.js"),skipSet:[],extFiles:[".ts",".vue"]},v=e.join(process.cwd(),"fe-js-utils.config.js"),d=await import(v).catch((function(){return{}})),y=(null==d||null===(f=d.default)||void 0===f?void 0:f.checkAny)||{},g={targetDir:y.targetDir||p.targetDir,outputFile:y.outputFile||p.outputFile,skipSet:y.skipSet||p.skipSet,extFiles:y.extFiles||p.extFiles},m=g.targetDir,x=g.outputFile,b=g.skipSet,w=g.extFiles;function j(t){return F.apply(this,arguments)}function F(){return(F=a(s().mark((function r(n){var o,i,a,u,l;return s().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return o=[],r.next=3,t.promises.readdir(n);case 3:i=r.sent,a=c(i),r.prev=5,l=s().mark((function r(){var i,a;return s().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return i=u.value,a=e.join(n,i),r.next=4,t.promises.stat(a);case 4:if(!r.sent.isDirectory()){r.next=13;break}return r.t0=o,r.next=9,j(a);case 9:r.t1=r.sent,o=r.t0.concat.call(r.t0,r.t1),r.next=14;break;case 13:w.some((function(t){return a.endsWith(t)}))&&o.push(a);case 14:case"end":return r.stop()}}),r)})),a.s();case 8:if((u=a.n()).done){r.next=12;break}return r.delegateYield(l(),"t0",10);case 10:r.next=8;break;case 12:r.next=17;break;case 14:r.prev=14,r.t1=r.catch(5),a.e(r.t1);case 17:return r.prev=17,a.f(),r.finish(17);case 20:return r.abrupt("return",o);case 21:case"end":return r.stop()}}),r,null,[[5,14,17,20]])})))).apply(this,arguments)}function k(t){var e=t.indexOf("//");return-1!==e?t.substring(0,e).trim():t.trim()}function S(t){return L.apply(this,arguments)}function L(){return(L=a(s().mark((function e(r){var n,o,i,a,u,l,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.promises.readFile(r,"utf-8");case 2:n=e.sent,o=n.split("\n"),i=/(:\s*any\b)|(as\s+any\b)|<\s*any\b[^>]*>/,a=[],u=c(o),e.prev=7,f=s().mark((function t(){var e,r,n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(e=l.value,!((r=e.trim()).startsWith("//")||r.startsWith("/*")||r.endsWith("*/"))){t.next=4;break}return t.abrupt("return",0);case 4:if(n=k(r)){t.next=7;break}return t.abrupt("return",0);case 7:if(!b.some((function(t){return n.includes(t)}))){t.next=9;break}return t.abrupt("return",0);case 9:i.test(n)&&a.push(n);case 10:case"end":return t.stop()}}),t)})),u.s();case 10:if((l=u.n()).done){e.next=17;break}return e.delegateYield(f(),"t0",12);case 12:if(0!==e.t0){e.next=15;break}return e.abrupt("continue",15);case 15:e.next=10;break;case 17:e.next=22;break;case 19:e.prev=19,e.t1=e.catch(7),u.e(e.t1);case 22:return e.prev=22,u.f(),e.finish(22);case 25:return e.abrupt("return",a);case 26:case"end":return e.stop()}}),e,null,[[7,19,22,25]])})))).apply(this,arguments)}function E(){return(E=a(s().mark((function e(){var r,n,o,i,a,u,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,j(m);case 2:r=e.sent,n=[],o=c(r),e.prev=5,o.s();case 7:if((i=o.n()).done){e.next=15;break}return a=i.value,e.next=11,S(a);case 11:(u=e.sent).length&&(n.push("// ".concat(a)),n.push(JSON.stringify(u)+";"));case 13:e.next=7;break;case 15:e.next=20;break;case 17:e.prev=17,e.t0=e.catch(5),o.e(e.t0);case 20:return e.prev=20,o.f(),e.finish(20);case 23:return l=n.join("\n"),e.next=26,t.promises.writeFile(x,l,"utf-8");case 26:console.log("检测结果已输出至 ".concat(x));case 27:case"end":return e.stop()}}),e,null,[[5,17,20,23]])})))).apply(this,arguments)}var _,D={targetDir:"./src",extFiles:[".js",".ts",".vue"]},O=n.join(process.cwd(),"fe-js-utils.config.js"),A=await import(O).catch((function(){return{}})),C=(null==A||null===(h=A.default)||void 0===h?void 0:h.cleanComments)||{},P={targetDir:C.targetDir||D.targetDir,extFiles:C.extFiles||D.extFiles},T=P.targetDir,N=P.extFiles,G=/\/\*\*[\s\S]*?\*\//g;function I(t){try{var e=r.readFileSync(t,"utf8"),n=e.replace(G,Y);e!==n&&(r.writeFileSync(t,n),console.log("Processed: ".concat(t)))}catch(e){console.error("Error processing ".concat(t,": ").concat(e.message))}}function W(t){var e,o=c(r.readdirSync(t));try{for(o.s();!(e=o.n()).done;){var i=e.value,a=n.join(t,i);r.statSync(a).isDirectory()?W(a):N.includes(n.extname(a).toLowerCase())&&I(a)}}catch(t){o.e(t)}finally{o.f()}}function Y(t){return t.split("\n").filter((function(t){var e=t.replace(/^\s*\*\s*/,"");if(e.startsWith("@param")){var r=e.match(/^@param\s+(?:{([^}]+)}\s+)?(\S+)(?:\s+(.*))?$/);return r&&(r[3]||"").trim().length>0}if(e.startsWith("@returns")){var n=e.slice(8).trim().match(/^\s*(?:{([^}]*)}\s*)?(\S.*)?$/);return n&&n[2]&&n[2].trim().length>0}return!0})).join("\n")}var $={targetDir:"./src",outputFile:n.join(process.cwd(),"extract-chinese-results.js"),extFiles:[".js",".jsx",".ts",".tsx",".vue",".html"],excludeDirs:["node_modules","dist","build","locales"],excludeFiles:["extract-chinese-results.js","extract-chinese.js"],verbose:!0},R=n.join(process.cwd(),"fe-js-utils.config.js"),U=await import(R).catch((function(){return{}})),J=(null==U||null===(_=U.default)||void 0===_?void 0:_.extractChinese)||{},M={targetDir:J.targetDir||$.targetDir,outputFile:J.outputFile||$.outputFile,extFiles:J.extFiles||$.extFiles,excludeDirs:J.excludeDirs||$.excludeDirs,excludeFiles:J.excludeFiles||$.excludeFiles,verbose:"boolean"==typeof J.verbose?J.verbose:$.verbose},q=M.targetDir,z=M.outputFile,B=M.extFiles,H=M.excludeDirs,K=M.excludeFiles,Q=M.verbose;function V(t){try{for(var e,n=function(t){return t.replace(/\/\*[\s\S]*?\*\//g,"").replace(/\/\/.*/gm,"").replace(/<!--[\s\S]*?-->/g,"")}(r.readFileSync(t,"utf8")),o=function(t){var e=[];return{newContent:t.replace(/('(\\'|[^'])*'|"(\\"|[^"])*"|`(\\`|[^`])*`)/g,(function(t){return e.push(t),"__STR_".concat(e.length-1,"__")})),strings:e}}(n),i=function(t){return t.replace(/console\.(log|info|warn|error|debug)\([\s\S]*?\);?/g,"")}(function(t,e){return t.replace(/__STR_(\d+)__/g,(function(t,r){return e[r]}))}(o.newContent,o.strings)),a=new Set,c=/([^\s"'`<>]*[\u4e00-\u9fa5]+[^\s"'`<>]*)/g;null!==(e=c.exec(i));){var s=e[0];/[\u4e00-\u9fa5]/.test(s)&&a.add(s)}return Array.from(a)}catch(e){return Q&&console.error("处理文件出错: ".concat(t),e.message),[]}}function X(t){var e,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},i=c(r.readdirSync(t,{withFileTypes:!0}));try{for(i.s();!(e=i.n()).done;){var a=e.value,s=n.join(t,a.name);if(a.isDirectory())H.includes(a.name)?Q&&console.log("跳过目录: ".concat(s)):X(s,o);else if(B.includes(n.extname(s)))if(K.includes(a.name))Q&&console.log("跳过文件: ".concat(s));else{var u=V(s);u.length>0&&(o[s]=u,Q&&console.log("已处理: ".concat(s," (").concat(u.length,"条)")))}}}catch(t){i.e(t)}finally{i.f()}return o}var Z=process.argv.slice(2),tt="可用命令: checkAny, cleanComments, extractChinese";0===Z.length&&(console.error("请提供一个有效的命令。".concat(tt)),process.exit(1));var et=Z[0];switch(et){case"checkAny":!function(){E.apply(this,arguments)}();break;case"cleanComments":r.existsSync(T)||(console.log("Usage: node clean-comments.js <directory>"),console.log("Example: node clean-comments.js ./src"),process.exit(1)),console.log("Processing directory: ".concat(T)),W(T),console.log("Cleanup completed");break;case"extractChinese":!function(){Q&&console.time("处理耗时");var t=X(q);!function(t){for(var e="",n=0,o=Object.entries(t);n<o.length;n++){var i=u(o[n],2),a=i[0],c=i[1];e+="// ".concat(a,"\n"),e+="[".concat(c.map((function(t){return"'".concat(t.replace(/'/g,"\\'"),"'")})).join(", "),"];\n\n")}r.writeFileSync(z,e)}(t),Q&&console.timeEnd("处理耗时"),console.log("生成完成,共处理 ".concat(Object.keys(t).length," 个文件")),console.log("结果已输出至 ".concat(z))}();break;default:console.error("未知命令: ".concat(et)),console.error(tt),process.exit(1)}