UNPKG

nopcli

Version:

nopCommerce CLI is a development tool for build plugins for nopCommerce Solutions.

3 lines (2 loc) 11.8 kB
#! /usr/bin/env node import e from"yargs";import{getInstalledPath as t}from"get-installed-path";import i from"inquirer";import n from"fs";import a from"shelljs";import r from"cli-progress";import s from"validatorjs";class l{static getPath(e,i){return new Promise((e=>{t("nopcli",i).then((t=>{e(void 0!==t?t:".")})).catch((()=>{e(".")}))}))}static getGroupAlias(){return{alias:"group",type:"string",default:"Widgets",describe:"support Widgets, Payments, DiscountRules, Shipping and Misc"}}static getVersionAlias(){return{alias:"version",type:"number",default:460,describe:'Only support ["4.20", "4.30", "4.40", "4.50", "4.60"]'}}static getPluginAlias(){return{alias:"plugin",type:"string"}}static getClearAlias(){return{alias:"clear",type:"boolean",default:!1}}static getBuildAlias(){return{alias:"build",type:"boolean",default:!1}}static getInitAlias(){return{alias:"init",type:"boolean",default:!1}}static getDescriptionConfigCommand(){return"config plugin -[g] -[p] -[v] -[c] -[b]"}static getDescriptionNewCommand(){return"create plugin -[g] -[p] -[v] -[c] -[b]"}static getDescriptionBuildCommand(){return"build plugin -[g] -[p]"}static getDescriptionInitCommand(){return"init plugin -[g] -[p] -[v] -[b]"}static getDescriptionDemand(){return"please choose a valid command"}static getCloneNopDefaultCommand(){return"git clone https://github.com/nopSolutions/nopCommerce.git ./ --branch release-4.50.2 --depth 1"}static getGitNopCommercePath(){return"./.git"}static getDotNetAlias(){return{alias:"dotnet",type:"boolean",default:!1}}static getGitAlias(){return{alias:"git",type:"boolean",default:!1}}static getClientSettingFileName(){return".nopcli"}static getConfigPluginQuestions(){return[{type:"list",name:"group",message:"What plugin group do you want to have by default?",choices:["DiscountRoles","Misc","Payments","Shipping","Widgets",new i.Separator,"Ask for help",{name:"Contact support",disabled:"Unavailable at this time"},"Leave"]},{type:"list",name:"version",message:"What version of nopCommerce do you need?",choices:["4.20","4.30","4.40","4.50","4.60"],filter:e=>e.replace(".","")},{type:"input",name:"name",message:"What name do you want to use by default?",filter:e=>e.toLowerCase(),validate(e){const t=e.match(/[a-zA-Z]+/g);return!(""===e||!t)||"String- Please enter a default name for your plugins"}},{type:"input",name:"author",message:"What author do you want to use by default?",default:"NopCli team",validate(e){const t=e.match(/[a-zA-Z]+/g);return!(""===e||!t)||"String- Please enter a default author for your plugins"}},{type:"confirm",name:"toBeAutoClear",message:"Do you want to remove your plugins if they exist when trying to create?",default:!1},{type:"confirm",name:"toBeAutoBuild",message:"Do you want to build after creating the plugins?",default:!1},{type:"confirm",name:"toBeIniCore",message:"do you want to install dotnet core?",default:!1},{type:"confirm",name:"toBeIniNop",message:"do you want to clone nopcommerce?",default:!1},{type:"confirm",name:"toBeIniGit",message:"do you want to init git?",default:!1}]}static getSetting(e){let t=this;if(n.existsSync(`${e}/${t.getClientSettingFileName()}`))e=`${e}/${t.getClientSettingFileName()}`;else{if(!n.existsSync(`${e}/src/${t.getClientSettingFileName()}`))return{};e=`${e}/src/${t.getClientSettingFileName()}`}let i=n.readFileSync(e);return JSON.parse(i)}static Prepare(e,t){let i=this.getSetting(".");return t.p=void 0===t.p?i.name:t.p,t.g=void 0===t.g?i.group:t.g,t.v=void 0===t.v?i.version:t.v,t.a=void 0===t.a?i.author:t.a,t.c=void 0===t.c?i.toBeAutoClear:t.c,t.b=void 0===t.b?i.toBeAutoBuild:t.b,t.i=void 0===t.i?i.toBeIniNop:t.i,t.git=void 0===t.git?i.toBeIniGit:t.git,t}}class o{static printHandler(e,t,i){let n=e,r=t;n?void 0===n.code?a.echo(`${n}\n`):a.echo(`${n.code}: ${n.message}\n`):void 0===r.message?a.echo(`${r}\n`):a.echo(`${r.message}\n`),i&&a.exit(!1)}}let g;g={"000":{message:"Sorry, this script requires git",code:"000"},"001":{message:'The plugin: "{{nopCli}}" already exists!',code:"001"},"001-1":{message:'The plugin: "{{nopCli}}" not exists!',code:"001-1"},"001-2":{message:'The plugin: "{{nopCli}}" can\'t clear!',code:"001-1"},"001-3":{message:'The plugin: "{{nopCli}}" can\'t build!',code:"001-1"},"002":{message:'The plugin: "{{nopCli}}" was created successfully!',code:"002"},"003":{message:'There was an error while building the "{{nopCli}}", please check and try again!',code:"003"},"004":{message:"Plugin build was successful!",code:"004"},"005":{message:"There was an error while getting the path value, please check and try again!",code:"005"},"006":{message:'Creating the plugin: "{{nopCli}}", Please Wait...',code:"006"},"007":{message:"Setting Plugins was successful!",code:"007"},"008":{message:"Init was successful!",code:"008"},"009":{message:"Init not successful!",code:"009"},"010":{message:"Command was successful!",code:"010"}};class u{static waitProgress(e=r.Presets.shades_classic,t=0,i=100,n=10){let s=this,l=new r.Bar({format:"progress [{bar}] {percentage}%"},e);return l.start(i,0),new Promise((e=>{const i=setInterval((()=>{t++,l.update(t),t>=l.getTotal()&&(clearInterval(i),l.stop(),a.echo(""),e(s))}),n)}))}static waitInfinityProgress(e,t=!0,i=!0,n=r.Presets.rect,s=0,l=100,o=500){a.config.silent=t;let g=new r.Bar({clearOnComplete:i,format:"progress [{bar}]"},r.Presets.rect);g.start(l,s),e(g);const u=setInterval((function(){s++,g.update(s),s===l&&(l*=2,g.setTotal(l)),s>=g.getTotal()&&clearInterval(u)}),o)}static SetCompleted(e,t){e.setTotal(100),e.update(100),e.stop(),t()}}const c=new class{ReplacePluginName(e,t){return e.replace("{{nopCli}}",this.getOutPluginName(t))}getSrcPluginName(e){return`Nop.Plugin.${e.g}.NopCliGeneric`}getOutPluginName(e){return`Nop.Plugin.${e.g}.${e.p}`}getFullSrcPlugin(e){return`${this.getSrcSolutionPath()}/Plugins/${this.getOutPluginName(e)}`}getSrcSolutionPath(){return n.existsSync("Plugins")?".":"src"}validateVersion(e){let t=this.getSrcSolutionPath();return n.existsSync(`${t}/Libraries/Nop.Services/Plugins/Samples/uploadedItems.json`)&&n.readFile(`${t}/Libraries/Nop.Services/Plugins/Samples/uploadedItems.json`,"utf8",((t,i)=>{if(t)throw t;let n=JSON.parse(i.replace(new RegExp("//(.*)","g"),""));e=n[3].SupportedVersion})),e}getSrcVersion(e){return this.validateVersion(e.v)}getOutProjectPathPluginName(e){return`${this.getFullSrcPlugin(e)}/${this.getOutPluginName(e)}.csproj`}existOutProject(e){let t=this;return new Promise(((i,r)=>{if(void 0===e.g&&void 0===e.p)i(!1);else{let s=t.getOutProjectPathPluginName(e);void 0===s?(a.echo(g["005"]),r(!1)):i(n.existsSync(s))}}))}copyFiles(e,t){let i=this;return new Promise((async r=>{let s=i.getFullSrcPlugin(t),l=i.getSrcPluginName(t),o=i.getOutPluginName(t);a.mkdir("-p",`${s}`),a.cp("-R",`${e}/src/nopCommerce-${i.getSrcVersion(t)}/${l}/*`,s),a.mv(`${s}/${l}.csproj`,`${s}/${o}.csproj`),n.readFile(`${e}/src/assets/images/logos/logo.png`,(function(e,t){e?(console.log(e),r(!1)):n.writeFileSync(`${s}/logo.png`,t,"base64")})),r(!0)}))}replaceContentFiles(e){let t=this;return new Promise((i=>{let r=t.getFullSrcPlugin(e),s=a.find(`${r}`);if(s.length>0)for(const t of s)n.lstat(t,((i,n)=>{if(n.isFile()){let i=t.replace("NopCliGeneric",e.p);a.sed("-i",/NopCliGeneric/g,e.p,t),i!==t&&a.mv(`${t}`,`${i}`)}}));i(s.length>0)}))}addSolution(e){let t=this;return new Promise(((i,r)=>{n.existsSync(`${t.getSrcSolutionPath()}/NopCommerce.sln`)?(a.echo(""),o.printHandler(t.ReplacePluginName(g["006"].message,e)),u.waitProgress().then((()=>{a.cd(t.getSrcSolutionPath()),a.exec(`dotnet sln add ./Plugins/${t.getOutPluginName(e)}`),i(t.ReplacePluginName(g["002"].message,e))}))):r(t.ReplacePluginName(g["003"].message,e))}))}clearPlugin(e){let t=this;return new Promise((i=>{let n=e.c;n&&a.rm("-r",t.getFullSrcPlugin(e)),i(n)}))}createProject(e,t){let i=this;return new Promise(((n,a)=>{i.copyFiles(t,e).then((t=>{t?i.replaceContentFiles(e).then((t=>{t?i.addSolution(e).then((e=>{n(e)})):a(i.ReplacePluginName(g["001"].message,e))})):a(i.ReplacePluginName(g["001"].message,e))}))}))}clone(e){return new Promise((t=>{e.git?u.waitInfinityProgress((e=>{a.exec(l.getCloneNopDefaultCommand(),(function(i,n,r){u.SetCompleted(e,(()=>{a.rm("-r",l.getGitNopCommercePath()),a.exec("git init && git add *.*"),t(g["008"].message)}))}))})):t(g["008"].message)}))}Build(e){let t=this;return new Promise((i=>{e.b?(a.cd(t.getFullSrcPlugin(e)),a.exec(`dotnet build ${t.getOutPluginName(e)}.csproj`),i(t.ReplacePluginName(g["004"].message,e))):i(!1)}))}Init(e){let t=this;return new Promise(((i,n)=>{t.clone(e).then((e=>{i(e)})).catch((e=>{n(e)}))}))}};const p=new class{CreateSetting(e,t,i){n.writeFile(l.getClientSettingFileName(),JSON.stringify(e,null," "),{encoding:"utf8",flag:"w"},(e=>{e?t(e):i(g["007"])}))}async Init(){let e=this;return new Promise((async(t,n)=>{i.prompt(l.getConfigPluginQuestions()).then((i=>{u.waitInfinityProgress((a=>{u.SetCompleted(a,(()=>{e.CreateSetting(i,t,n)}))}))}))}))}};const m=new class{getResponse(e){let t=e.errors.all();return{valid:e.passes(),hasErrors:t.length>0,errors:t}}validateCreatePlugin(e){let t=this;return new Promise((i=>{e.exitPlugin=n.existsSync(c.getOutProjectPathPluginName(e));let a=new s(e,{g:"required",p:"required",c:"required|boolean|hasClearPlugin",b:"required|boolean|hasBuildPlugin",exitPlugin:"required|boolean|exitPlugin"});s.register("exitPlugin",(function(t){return!t||e.c}),c.ReplacePluginName(g["001"].message,e)),s.register("hasClearPlugin",(function(t){let i=e.exitPlugin&&t;return i||!i}),c.ReplacePluginName(g["001-2"].message,e)),s.register("hasBuildPlugin",(function(t){let i=e.exitPlugin&&t;return i||!i}),c.ReplacePluginName(g["001-3"].message,e)),i(t.getResponse(a))}))}validateBuildPlugin(e){let t=this;return new Promise((i=>{e.exitPlugin=n.existsSync(c.getOutProjectPathPluginName(e));let a=new s(e,{g:"required",p:"required",exitPlugin:"required|boolean|hasBuildPlugin"});s.register("hasBuildPlugin",(function(t){return e.exitPlugin&&t}),c.ReplacePluginName(g["001-3"].message,e)),i(t.getResponse(a))}))}validateClonePlugin(e){let t=this;return new Promise((i=>{e.exitGit=null!==a.which("git");let n=new s(e,{exitGit:"boolean|hasGit"});s.register("hasGit",(function(e){return e}),c.ReplacePluginName(g["000"].message,e)),i(t.getResponse(n))}))}};class d{static create(e){a.config.silent=!1,l.getPath().then((t=>{let i=l.Prepare(t,e.argv);return m.validateCreatePlugin(i,t).then((e=>{!1===e.valid?o.printHandler(e.errors,null,!0):c.clearPlugin(i,t).then((()=>{c.createProject(i,t).then((e=>{c.Build(i,t).then((()=>{o.printHandler(null,e,!0)})).catch((e=>{o.printHandler(e,null)}))}))}))}))}))}static build(e){l.getPath().then((t=>{let i=l.Prepare(t,e.argv);return m.validateBuildPlugin(i).then((e=>{!1===e.valid?o.printHandler(e.errors,null,!0):c.Build(i,t).then((e=>{o.printHandler(null,e,!0)})).catch((e=>{o.printHandler(e,null)}))}))}))}static init(e){l.getPath().then((t=>{let i=l.Prepare(t,e.argv);return m.validateClonePlugin(i).then((e=>{!1===e.valid?o.printHandler(e.errors,null,!0):c.clone(i).then((e=>{o.printHandler(null,e,!0)})).catch((e=>{o.printHandler(e,null)}))}))}))}static config(e){return l.getPath().then((t=>{p.Init(l.Prepare(t,e.argv)).then((e=>{o.printHandler(null,e,!0)})).catch((e=>{o.printHandler(e,null,!0)}))}))}}e.usage("$0 command").option("g",l.getGroupAlias()).option("p",l.getPluginAlias()).option("v",l.getVersionAlias()).option("c",l.getClearAlias()).option("b",l.getBuildAlias()).option("i",l.getInitAlias()).option("d",l.getDotNetAlias()).option("git",l.getGitAlias()).command("config",l.getDescriptionConfigCommand(),(e=>d.config(e))).command("new",l.getDescriptionNewCommand(),(e=>d.create(e))).command("build",l.getDescriptionBuildCommand(),(e=>d.build(e))).command("init",l.getDescriptionInitCommand(),(e=>d.init(e))).demand(1,l.getDescriptionDemand()).showHelpOnFail(!0).help("h").alias("h","help").argv;