From 58c75f75daacc4eb742de972ba3358975bc79fb8 Mon Sep 17 00:00:00 2001 From: Denis <76569844+DenisPower1@users.noreply.github.com> Date: Thu, 6 Jan 2022 13:08:35 -0800 Subject: [PATCH] The parser --- src/v1/parser.js | 600 +++++++++++++++++++++++++---------------------- 1 file changed, 315 insertions(+), 285 deletions(-) diff --git a/src/v1/parser.js b/src/v1/parser.js index bcecd48..6f19d3b 100644 --- a/src/v1/parser.js +++ b/src/v1/parser.js @@ -1,285 +1,315 @@ -/** - * - * Parser - * - * MIT LICENSED BY DENIS POWER. - * - * 2022 - * - * - */ - - - const fs=require("fs"); - const path=require("path"); - - let config; - let dir; - - /** - * - * config.json - * - * { - * - * "target":["file1.js", "file2.js"], - * "outputDir":"./browser", - * "comment":false, - * "ext":"" - * - * - * } - * - * - */ - - - -String.prototype.addToStart=function(content){ - - let comm=content+` - -`; - - return comm=comm+this; - - -} - -String.prototype.removeSpaces=function(){ - - - return this.replace(/\s/g,"") ; - - -} - - - function hasConfig(){ - - const bool=fs.existsSync("./config.json"); - - - return bool; - - } - - - function isAValidFile(file){ - - const pattern=/\.js/; - - return pattern.test(path.extname(file)); - - } - - function getConfig(){ - - if(hasConfig()){ - - const configContent=fs.readFileSync("./config.json"); - - - try { - config=JSON.parse(configContent); - - }catch(e){ - - - - throw new Error(`"${configContent}" is not a json format. - The content of config.json must be something like: - - { - - "target":["file1.js","file2.js"......], - outPutDir:"/folder" - - } - - `) - - } - - }else{ - - throw new Error(` - - The config.json file was not found, create one. - - `) - - } - - } - - - function parseConfig(){ - - getConfig(); - - - - const{ - target, - outPutDir, - comment, - ext, - - - }=config; - - const setting={ - comment:(comment!=void 0 && !comment) ? false : true, - ext:(typeof ext=="string") ? ext : null, - outPutDir:(typeof outPutDir=="string") ? outPutDir :"./", - } - - - - if(Array.isArray(target)){ - - /** - * - * ["./file1.js", "./file2.js","./file3.js",...] - * - */ - - if(typeof outPutDir=="string"){ - - dir=outPutDir; - - - }else{ - - dir=__dirname; - - } - - - - - function files(){ - - for(let file of target){ - - if(typeof file=="string" && isAValidFile(file)){ - - compile(file,setting); - - }else{ - - throw new Error(` - - Ops, "${file}" is not a valid javascript file. - - It must be a string and must have the ".js" extension. - - `) - - } - - } - - - } - - files(); - - - } - - - } - - function compile(file,sett){ - - const fileDir=path.dirname(file) - - - const exist=fs.existsSync(file); - - if(exist){ - - if(dir){ - - - try{ - - fs.readdirSync(sett.outPutDir); - - }catch(e){ - - throw new Error(` - - The outPutDir: "${sett.outPutDir}" does not exist. - - `) - - } - - } - - let content=""; - const importStatement=/import(:?\s*){.*}(:?\s*)from(:?\s*)(:? ".*"|'.*'|`.*`)(:?\s*);*/g; - - content+=fs.readFileSync(file); - - if(!importStatement.test(content)){ - - throw new Error(` - - The parser could not find an import statement in ${file} file. - - `) - - } - - - content=content.replace(importStatement,""); - - - console.log(content); - - if(sett.comment){ - content=content.addToStart(`/** - * - * The original file is in:${fileDir.removeSpaces().trim().length==0 ? __dirname : fileDir }/${file} - * - */ - - - `.trim()) - - } - - file=file.replace(".js",""); - file=path.basename(file); - fs.writeFileSync(`${sett.outPutDir ? dir : "."}/${file}.${sett.ext ? sett.ext : "browser"}.js`, content); - - - - }else{ - - throw new Error(` - - The Parser could not find the "${file}" file. - - `) - - } - - - - } - - parseConfig(); - - console.log(` - - Done, check the files in: ${dir} directory. - - `) - - - +/** + * + * Parser + * + * MIT LICENSED BY DENIS POWER. + * + * 2022 + * + * + */ + + + +const fs=require("fs"); + const path=require("path"); + + + + let config; + let dir; + + /** + * + * config.json + * + * { + * + * "target":["file1.js", "file2.js"], + * "outputDir":"./browser", + * "comment":false, + * "ext":"" + * + * + * } + * + * + */ + + + +String.prototype.addToStart=function(content){ + + let comm=content+` + +`; + + return comm=comm+this; + + +} + +function getFileName(file){ + + return path.basename(file); + + +} + +function getExt(file){ + + return path.extname(file); + +} + + function hasConfig(){ + + const bool=fs.existsSync("./config.json"); + + + return bool; + + } + + + function isAValidFile(file){ + + const pattern=/\.js/; + + return pattern.test(getExt(file)); + + } + + function getConfig(){ + + if(hasConfig()){ + + const configContent=fs.readFileSync("./config.json"); + + + try { + config=JSON.parse(configContent); + + }catch(e){ + + + + throw new Error(`"${configContent}" is not a valid json format. + The content of config.json must be something like: + + { + + "target":["file1.js","file2.js"......], + outPutDir:"/folder" + + } + + `) + + } + + }else{ + + throw new Error(` + + The config.json file was not found, create one. + + `) + + } + + } + + + function parseConfig(){ + + getConfig(); + + + + const{ + target, + outPutDir, + comment, + subExt, + + + }=config; + + const setting={ + comment:(comment!=void 0 && !comment) ? false : true, + ext:(typeof subExt=="string" && subExt.trim().length>0) ? ext : null, + outPutDir:(typeof outPutDir=="string") ? outPutDir :"./", + } + + + + if(Array.isArray(target)){ + + /** + * + * ["./file1.js", "./file2.js","./file3.js",...] + * + */ + + if(typeof outPutDir=="string"){ + + dir=outPutDir; + + + }else{ + + dir=__dirname; + + } + + + + + function files(){ + + for(let file of target){ + + if(typeof file=="string" && isAValidFile(file)){ + + compile(file,setting); + + }else{ + + throw new Error(` + + Ops, "${file}" is not a valid javascript file. + + It must be a string and must have the ".js" extension. + + `) + + } + + } + + + } + + files(); + + + }else{ + + throw new Error(` + + "target" must be an array like: + + { + + "target":["file1.js", "file2.js",...] + + } + + `) + + } + + + } + + function compile(file,sett){ + + const fileDir=path.dirname(file).replace(/\./,"") + + + + const exist=fs.existsSync(file); + + if(exist){ + + if(dir){ + + + try{ + + fs.readdirSync(sett.outPutDir); + + }catch(e){ + + throw new Error(` + + The outPutDir: "${sett.outPutDir}" does not exist. + + `) + + } + + } + + let content=""; + const importStatement=/import(:?\s*){.*}(:?\s*)from(:?\s*)(:? ".*"|'.*'|`.*`)(:?\s*);*/g; + + content+=fs.readFileSync(file); + + if(!importStatement.test(content)){ + + throw new Error(` + + The parser could not find an import statement in ${file} file. + + `) + + } + + + content=content.replace(importStatement,""); + + + + + if(sett.comment){ + + + const loc=(`${fileDir.trim().length>0 ? fileDir : __dirname }/${getFileName(file)}`) + content=content.addToStart(`/** + * + * You must use this code in the browser. + * + * The original code is in : ${loc} + * + * To remove this, set comment to false in config.json. + * + */ + + + `.trim()) + + } + + file=getFileName(file); + file=file.replace(/.js/,""); + + fs.writeFileSync(`${sett.outPutDir ? dir : "."}/${file}.${sett.ext ? sett.ext : "browser"}.js`, content); + + + + }else{ + + throw new Error(` + + The Parser could not find the "${file}" file. + + `) + + } + + + + } + + parseConfig(); + + console.log(` + + Done, check the files in: ${dir} directory. + + `) + + + \ No newline at end of file