UNPKG

valence-connect

Version:

Connect to Valence for requests coming from applications running in the Valence Portal

1 lines 4.16 kB
const prompt=require("prompt"),shell=require("shelljs"),urlParse=require("url-parse"),ip=require("ip"),jsonfile=require("jsonfile"),isWindows="win32"===process.platform,promptSchema={properties:{autoCreate:{description:'Automatically create application skeleton?\n"This is recommended only for clean installs"',pattern:/^(?:Y|N|y|n)$/,message:"Y or N",default:"Y"}}};let npmInfo=process.env.npm_config_argv;prompt.message="",prompt.delimiter="",prompt.colors=!1,shell.cd(__dirname),shell.cd("../../..");const directoryContents=shell.ls()||[];let continueSetup=!0;if(npmInfo&&"object"==typeof(npmInfo=JSON.parse(npmInfo))&&npmInfo.original&&Array.isArray(npmInfo.original)&&"update"===npmInfo.original[0]&&(continueSetup=!1),continueSetup&&(directoryContents.length<3?shell.echo("valence-connect: Quick Setup\nThe setup will create an application skeleton using valence-connect in your directory.\n"):(shell.echo("valence-connect: Since this is not a clean install please refer to the package Readme for setup instructions."),continueSetup=!1),continueSetup)){const e=__dirname.indexOf("node_modules");if(-1!==e){const n=__dirname.substring(0,e)+"package.json";jsonfile.readFile(n,function(e,t){if(e)console.error("Unable to access package.json : ",e);else{const e={start:"node ./bin/www.js","start:production":"NODE_ENV=production node ./bin/www.js",test:"node ./tests/services.js"};isWindows&&Object.assign(e,{"start:production":"set NODE_ENV=production & node ./bin/www.js","start:production_ps":"powershell ./production.ps1"}),t.main&&delete t.main,t.scripts?Object.assign(t.scripts,e):Object.assign(t,{scripts:e}),jsonfile.writeFile(n,t,{replace:!0,spaces:2},function(e){e&&console.error("Unable to add npm start scripts : ",e)})}})}else console.error("Unable to find node_modules directory, `npm init` is required before installing valence-connect."),continueSetup=!1;continueSetup&&(prompt.start(),prompt.get(promptSchema,function(e,n){if(e)shell.echo(e.message);else if(n.autoCreate=n.autoCreate.toUpperCase(),"Y"===n.autoCreate){const e={properties:{url:{description:'Valence Connect Web Service URL\n"Specify URL Located in Valence Portal Admin>Web Services":',message:"Must be a valid URL that contains the key parameter.",required:!0,conform:function(e){const n=urlParse(e,!0),t=n.query;return!(!t||!t.key||!n.protocol&&1!==Object.keys(t).length)}},port:{description:"What port number will this node server listen on?:",pattern:new RegExp("^\\d[0-9]{1,5}","g"),message:"Must be a valid port number",required:!0},ibmi:{description:"Running Valence for IBM i?",pattern:/^(?:Y|N|y|n)$/,message:"Y or N",default:"Y"},logging:{description:'Turn logging on?\n"This captures transaction statistics/information"',pattern:/^(?:Y|N|y|n)$/,message:"Y or N",default:"N"}}};prompt.start(),prompt.get(e,function(e,n){if(e)shell.echo(e.message);else{-1===n.url.indexOf("http://")&&-1===n.url.indexOf("https://")&&(n.url="http://"+n.url);const e=urlParse(n.url,!0),t=e.host,s=e.protocol+"//"+t;n.ibmi=n.ibmi.toUpperCase(),n.logging=n.logging.toUpperCase(),shell.cd(__dirname),shell.cp("-R","template/*","../../.."),shell.cd("../../.."),isWindows||shell.rm("-r","production.ps1"),shell.ls("valence.json").forEach(function(e){shell.sed("-i",new RegExp("VCONNECTURL","ig"),n.url,e),shell.sed("-i",new RegExp("VIBMI","ig"),"Y"===n.ibmi?"true":"false",e),shell.sed("-i",new RegExp("VLOGGING","ig"),"Y"===n.logging?"true":"false",e)}),shell.mv("gitignore",".gitignore"),shell.cd("bin"),shell.ls("*.js").forEach(function(e){shell.sed("-i",new RegExp("VPORT","ig"),n.port,e)}),shell.cd("../public/apps/customers"),shell.ls("*.html").forEach(function(e){shell.sed("-i",new RegExp("VBASEURL","ig"),s,e)}),shell.cd("../portalInteractions"),shell.ls("*.html").forEach(function(e){shell.sed("-i",new RegExp("VBASEURL","ig"),s,e)}),shell.cd("../../../routes"),shell.ls("*.js").forEach(function(e){shell.sed("-i",new RegExp("VBASEURL","ig"),s,e)}),shell.echo("\nCompleted...\n"),shell.echo("\nPerform a quick test by creating a new app in Valence Admin with the below URL\nhttp://"+ip.address()+":"+n.port+"/apps/customers/index.html\n")}})}else shell.echo("\nCompleted...")}))}