UNPKG

@randspot/pa-cli

Version:

Playable Ads CLI

1 lines 12.7 kB
(()=>{var e={368:(e,t,s)=>{const o=s(231),i=s(423);var n=s(514);class r{folderPath="";useFullPathKey="";constructor(e,t){this.folderPath=e,this.useFullPathKey=t}convert(){return this.processFolder(this.folderPath,{})}processFolder(e,t){return o.readdirSync(e).forEach((s=>{const n=i.join(e,s),r=o.lstatSync(n);if(r.isDirectory())this.processFolder(n,t);else if(r.isFile()){let e=n;this.useFullPathKey||(e=i.relative(this.folderPath,e));const s=this.getBase64Header(n);t[e.replace("\\","/")]=s+o.readFileSync(n,{encoding:"base64"})}})),t}getBase64Header(e){const t=i.extname(e);return`data:${n.contentType(t)};base64,`}static getNextChunkLen(e,t,s,o){if(e.length<=t)return-1;let i=0,n=o;const r=Object.keys(e);for(let o=t;o<r.length;o++){if(n+=JSON.stringify({[r[o]]:e[r[o]]}).length,n>s)return i;i++}return i}static getChunk(e,t,s){const o={},i=Object.keys(e);for(let n=t;n<t+s;n++)o[i[n]]=e[i[n]];return o}static saveChunkedAssets(e,t,s,i,n=!1){let l=0,c=0;const a=Object.keys(t).length,p=[];for(;;){const d=s.replace("{{i}}",c);let h=r.getNextChunkLen(t,l,1024*i,d.length);(h<0||0==h&&n)&&(console.error("Something wrong with chunks. Try to increase chunk size."),process.exit(1)),0==h&&(h=1,console.warn("Asset cannot fit into chunk size."));const g=r.getChunk(t,l,h),u=`${e}${c}.js`;if(o.writeFileSync(u,d+JSON.stringify(g)),p.push(u),c++,l+=h,l>=a)break}return p}}e.exports=r},403:(e,t,s)=>{const o=s(231),i=s(423),n=s(955),r=s(368),l=s(844);e.exports=class{packageFolder="";assetsFolder="";chunkSize=2040;strictChunkSize=!1;constructor(e,t,s=2040,o=!1){this.packageFolder=e,this.assetsFolder=t,this.chunkSize=s,this.strictChunkSize=o}createPackage(){const e=this.createInlineAssets(),t=i.join(this.packageFolder,"index.html");let s=o.readFileSync(t,{encoding:"utf8"});const r=n.parse(s),l=r.getElementsByTagName("head")[0],c=l.getElementsByTagName("script");for(let e of c)l.removeChild(e);const a=this.createAssetsScripts(e,l);for(let e of a)l.insertAdjacentHTML("beforeend",e);for(let e of c)l.appendChild(e);o.writeFileSync(t,r.toString(),{encoding:"utf8",flag:"w"}),this.saveToZip(i.join(this.packageFolder,"package.zip"))}createInlineAssets(){let e=[];e=Array.isArray(this.assetsFolder)?this.assetsFolder:[this.assetsFolder];let t={};for(let s of e){const e=new r(s,!1).convert();t=Object.assign({},t,e)}return r.saveChunkedAssets(`${this.packageFolder}/inlineAssets`,t,"const inlineAssets{{i}} = ",this.chunkSize,this.strictChunkSize)}createAssetsScripts(e,t){const s=[];let o="let inlineAssets = {}; ";for(let t of e){const e=i.basename(t);i.basename(t,".js"),s.push(`<script src="${e}"><\/script>`),o+=`inlineAssets = Object.assign(inlineAssets, ${i.basename(t,".js")}); `}return s.push(`<script>${o}<\/script>`),s}saveToZip(e){o.existsSync(e)&&o.unlinkSync(e);var t=new l,s=o.readdirSync(this.packageFolder);for(let e of s)t.addLocalFile(i.join(this.packageFolder,e));o.writeFileSync(e,t.toBuffer(),{flag:"w"})}}},661:(e,t,s)=>{const o=s(231),i=s(423),{execSync:n}=s(81);e.exports=class{projectName="playableAdProject";gameEngine="phaser";constructor(e,t="phaser"){this.projectName=e,this.gameEngine=t}generate(){const e=process.cwd(),t=i.join(e,this.projectName);try{console.log(`Creating folder ${this.projectName}`);let e=this.createProjectDir(t,this.projectName);if(0!=e)return e;let s=`git clone --depth 1 git@bitbucket.org:_Tibor/pa-boilerplate.git ${t}`;"babylon"==this.gameEngine?s=`git clone --depth 1 git@bitbucket.org:_Tibor/pa-boilerplate-3d.git ${t}`:"pixi"==this.gameEngine&&(s=`git clone --depth 1 git@bitbucket.org:_Tibor/pa-boilerplate-pixi.git ${t}`),console.log("Downloading files..."),n(s),process.chdir(t),this.replaceInFile(i.join(t,"package.json"),"{{project-name}}",this.projectName),this.replaceInFile(i.join(t,"src","index.html"),"{{project-name}}",this.projectName),this.replaceInFile(i.join(t,"src","index-google.html"),"{{project-name}}",this.projectName),this.replaceInFile(i.join(t,"src","index-tiktok.html"),"{{project-name}}",this.projectName),console.log("Installing dependencies..."),n("npm install"),console.log("Removing useless files"),n("npx rimraf ./.git"),console.log("Done")}catch(e){console.log(e)}return 0}createProjectDir(e,t){try{return o.mkdirSync(e),0}catch(e){return"EEXIST"===e.code?console.log(`The project ${t} already exist in the current directory, please give it another name.`):console.log(error),1}}replaceInFile(e,t,s){let i=o.readFileSync(e,{encoding:"utf8"});i=i.replace(t,s),o.writeFileSync(e,i,{encoding:"utf8",flag:"w"})}}},613:(e,t,s)=>{const o=s(231),i=s(423);var n=s(955);e.exports=class{indexFilePath="";jsFilePath="";dapiFilePath="";outFile="";constructor(e,t,s,o){this.indexFilePath=e,this.jsFilePath=t,this.dapiFilePath=s,this.outFile=o,this.outFile||(this.outFile=e)}merge(){let e=o.readFileSync(this.indexFilePath,{encoding:"utf8"}),t=o.readFileSync(this.jsFilePath,{encoding:"utf8"}),s="";this.dapiFilePath&&(s=o.readFileSync(this.dapiFilePath,{encoding:"utf8"})+" ");const i=n.parse(e),r=this.findScriptElem(i);return r?(r.removeAttribute("src"),r.hasAttribute("defer")&&r.removeAttribute("defer"),r.textContent=s+t,o.writeFileSync(this.outFile,i.toString(),{encoding:"utf8",flag:"w"}),!0):(console.warn("Cannot find the right script elem"),!1)}findScriptElem(e){let t=null;const s=i.basename(this.jsFilePath);return e.getElementsByTagName("script").forEach((e=>{e.hasAttribute("src")&&e.getAttribute("src").endsWith(s)&&(t=e)})),t}}},150:(e,t,s)=>{const o=s(231);s(423);var i=s(955);e.exports=class{indexFilePath="";jsonFilePath="";constructor(e,t){this.indexFilePath=e,this.jsonFilePath=t}write(){let e=o.readFileSync(this.indexFilePath,{encoding:"utf8"}),t=o.readFileSync(this.jsonFilePath,{encoding:"utf8"});try{const s=JSON.parse(t),n=s?.general?.iOSUrl??"",r=s?.general?.AndroidUrl??"";n||console.log("Cannot find general->iOSUrl"),r||console.log("Cannot find general->AndroidUrl");const l=i.parse(e),c=l.getElementsByTagName("head")[0],a=`<script>var IOS_URL="${n}"; var ANDROID_URL="${r}"<\/script>`;c.insertAdjacentHTML("afterbegin",a),o.writeFileSync(this.indexFilePath,l.toString(),{encoding:"utf8",flag:"w"})}catch(e){console.log(e)}return!0}}},844:e=>{"use strict";e.exports=require("adm-zip")},231:e=>{"use strict";e.exports=require("fs")},514:e=>{"use strict";e.exports=require("mime-types")},566:e=>{"use strict";e.exports=require("minimist")},955:e=>{"use strict";e.exports=require("node-html-parser")},423:e=>{"use strict";e.exports=require("path")},81:e=>{"use strict";e.exports=require("child_process")},147:e=>{"use strict";e.exports=JSON.parse('{"name":"@randspot/pa-cli","version":"1.0.36","description":"Playable Ads CLI","author":{"name":"Randspot s.r.o.","email":"support@randspot.com","url":"https://randspot.com"},"license":"ISC","keywords":["playable ads","playable","ads"],"main":"./dist/index.js","types":"./dist/index.d.ts","files":["bin","dist"],"bin":{"playable-ads":"./bin/cli.js"},"scripts":{"build":"webpack --mode=production","publish-to-npmjs":"npm publish --access public","publish":"npm-run-all -s build publish-to-npmjs"},"dependencies":{"adm-zip":"^0.5.9","fs":"^0.0.1-security","mime-types":"^2.1.34","minimist":"^1.2.5","node-html-parser":"^5.1.0","path":"^0.12.7"},"devDependencies":{"clean-webpack-plugin":"^4.0.0","npm-run-all":"^4.1.5","ts-loader":"^9.4.2","ts-node":"^10.9.1","typescript":"^4.9.4","webpack":"^5.75.0","webpack-cli":"^5.0.1","webpack-node-externals":"^3.0.0"}}')}},t={};function s(o){var i=t[o];if(void 0!==i)return i.exports;var n=t[o]={exports:{}};return e[o](n,n.exports,s),n.exports}s.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return s.d(t,{a:t}),t},s.d=(e,t)=>{for(var o in t)s.o(t,o)&&!s.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},s.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),(()=>{"use strict";var e,t,o=s(231),i=s(423),n=s(147);const r=s(661),l=(s(661),s(368)),c=s(613),a=s(150),p=s(403);var d=s(566)(process.argv.slice(2));function h(){console.log("Playable Ads CLI"),console.log(`v. ${n.version}`),console.log("- Boilerplate a new playable-ad project use: new <project-name>"),console.log(" --engine <game engine> (optional, default is phaser, allowed values: phaser, babylon, pixi)"),console.log(),console.log("- Convert assets to base64 use: assets"),console.log(" --folder <assets-folder-path> (required)"),console.log(" --o <output-file-path> (required)"),console.log(" --prefix <prefix> (optional)"),console.log(" --chunkSize <max-size-in-KB> (optional)"),console.log(" useFullPathKey (optional)"),console.log(" empty (option) - to create a file with content 'prefix {}'"),console.log(),console.log("- Merge script code into index use: mergeJs2Index"),console.log(" --index <index-file-path> (required)"),console.log(" --js <js-file-path> (required --js or --folder)"),console.log(" --folder <path-to-folder-with-main.js-and-index.html> (required --js or --folder)"),console.log(" --o <output-file-path> (required)"),console.log(" --dapi <path-to-dapi-file> (optional)"),console.log(),console.log("- Write store urls to index.html from def json: storeUrlsToIndex"),console.log(" --index <index-file-path> (required)"),console.log(" --json <json-file-path> (required)"),console.log(),console.log("- Create facebook package from webpack output: fbPackage"),console.log(" --folder <folder-patch-with-index-and-main-js> (required)"),console.log(" --assets <folder-with-assets-to-merge> (required)"),console.log(" --chunkSize (optional) - maximum size of asset file"),console.log(" --strictChunkSize (optional) - if true, the process wil fail in case when any asset cannot fit into chunkSize"),console.log(),console.log("- Copy file to output dir use: copy"),console.log(" --i <file-to-copy> (required)"),console.log(" --o <destination-file> (required) - if the dest folder dow not exists, will create it. If the destination file exists, it will overwrite."),console.log()}if(0!=d._.length&&"-h"!=d._[0]||(h(),process.exit(1)),"new"==d._[0]){d._.length<2&&(console.log("You have to provide a name to your playable-ad app."),process.exit(1));const t=null!==(e=d.engine)&&void 0!==e?e:"phaser";["phaser","babylon","pixi"].includes(t)||(console.log("You have to provide a valid engine (allowed engines: phaser, babylon or pixi)."),process.exit(1));const s=new r(d._[1],t).generate();process.exit(s)}else if("assets"==d._[0]){const e=d.folder,s=d.o,i=d._.includes("useFullPathKey"),n=null!==(t=d.prefix)&&void 0!==t?t:"";let r=parseInt(d.chunkSize);d._.includes("empty")&&(s||(console.error("Missing argument o (output file)."),process.exit(1)),o.writeFileSync(s,n+JSON.stringify({})),process.exit(0)),d.chunkSize?(isNaN(r)||r<100)&&(console.error("chunkSize must be higher then 100."),process.exit(1)):r=-1,e||(console.error("Missing folder input. Use --folder <assets-folder-path>"),process.exit(1));let c=[];c=Array.isArray(e)?e:[e];let a={};for(let e of c){const t=new l(e,i).convert();a=Object.assign({},a,t)}s?r>0?l.saveChunkedAssets(s,a,n,r):o.writeFileSync(s,n+JSON.stringify(a)):Object.keys(a).forEach((e=>{console.log(e),console.log(a[e]),console.log()})),console.log("conversion finished"),process.exit(0)}else if("mergeJs2Index"==d._[0]){let e=d.index,t=d.js;const s=d.folder,o=d.o,n=d.dapi;s||e&&t||(console.error("Missing some inputs. Use --index <index-file-path> --js <js-file-path> or --folder <path-to-folder-with-main.js-and-index.html>"),process.exit(1)),e&&t||(e=i.join(s,"index.html"),t=i.join(s,"main.js")),new c(e,t,n,o).merge()?console.log("merge finished"):console.log("ended with error"),process.exit(0)}else if("storeUrlsToIndex"==d._[0]){let e=d.index,t=d.json;e&&t||(console.error("Missing some inputs. Use --index <index-file-path> --json <js-file-path>"),process.exit(1)),new a(e,t).write()?console.log("Finished"):console.log("Ended with error"),process.exit(0)}else if("fbPackage"==d._[0]){const e=d.folder,t=d.assets;let s=d.chunkSize;e||(console.error("Missing folder input."),process.exit(1)),t||(console.error("Missing assets input."),process.exit(1)),d.chunkSize?(isNaN(s)||s<100)&&(console.error("chunkSize must be higher then 100."),process.exit(1)):s=2040,new p(e,t,s,"true"==d.strictChunkSize).createPackage(),console.log("FB package created"),process.exit(0)}else if("copy"==d._[0]){const e=d.i,t=d.o;e&&t||(console.error("Missing input params."),process.exit(1)),o.existsSync(e)||(console.error("Input file does not exists."),process.exit(1));const s=t.split(i.sep);let n="";for(let e=0;e<s.length-1;e++)n=i.join(n,s[e]),o.existsSync(n)||o.mkdirSync(n);o.existsSync(t)&&(console.log("Rewriting existing output file."),o.rmSync(t)),o.copyFileSync(e,t),console.log("Copy done"),process.exit(0)}else h(),process.exit(1)})()})();