@snowplow/snowtype 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -31,5 +31,5 @@ ${K("h2","Debug")}
|
|
|
31
31
|
${g.default.magenta(n)}
|
|
32
32
|
${g.default.cyanBright(o)}
|
|
33
33
|
`}var Lt={...H},J=`./${lt}`;function $t(t){return t.length!==0}function Ut(t){return Array.isArray(Lt[t])}function Dt(t,e){let n=Lt[t];return Array.isArray(n)?!!n.map(r=>r.value).includes(e):!1}function Nt(t){let e={organizationId:t.organizationId,tracker:t.tracker,language:t.language,outpath:t.outpath};return Object.assign({igluCentralSchemas:[],dataStructures:[],eventSpecificationIds:[],dataProductIds:[]},e)}function B(t){let e=(0,C.resolve)((0,I.cwd)(),J);return(0,y.writeFileSync)(e,JSON.stringify(t,null,2))}function U(t){let e=(0,y.statSync)(t,{throwIfNoEntry:!1});return e===void 0?!1:e.isFile()}function W(t){return(0,y.readFileSync)(t,{encoding:"utf-8"})}function q(){let t=(0,C.resolve)((0,I.cwd)(),J);return JSON.parse(W(t))}function Rt(t){return JSON.parse(W(t))}function E(){let t=(0,C.resolve)((0,I.cwd)(),J);if(U(t)===!1)throw new G;let e=JSON.parse(W(t));return P(e)}function Ft(){let t=(0,C.resolve)((0,I.cwd)(),J);if(U(t)===!0){let e=W(t);try{throw P(JSON.parse(e)),new O}catch(n){if(n instanceof O)throw n}}}function Gt(t){return{javascript:".js",typescript:".ts",kotlin:".kt",swift:".swift",go:".go"}[t]||""}function zt(t,e){let n={eventSpecifications:{}};t.forEach(r=>{n.eventSpecifications[r.eventSpecification.id]=r.version});let o=(0,C.resolve)((0,I.cwd)(),e,M);try{(0,y.writeFileSync)(o,JSON.stringify(n,null,2))}catch{throw new j}}function _t(t,e,n){let o=Object.entries(t);o.length>0&&n.success("Available Event Specification Updates"),o.forEach(([r,i])=>{let s=`${r}: version ${i.previous}`,a=`${r}: version ${i.next}`;n.diff(s,a)}),e.length>0&&n.success("Available Data Structures Updates"),e.forEach(r=>{let i=`${r.previous}`,s=`${r.next}`;n.diff(i,s)})}function jt(t){let e=t.optsWithGlobals();return{...e,apiKey:e.apiKey||process.env.SNOWPLOW_CONSOLE_API_KEY}}function D(t){return!!(Array.isArray(t)&&!t.length)}function Vt(t){return t.type==="input"}function Mt(t){return t.type==="password"}function tt(t){return t.type==="select"}function Ht(){return{withVerbosity:e=>({success:o=>{console.log(Et(o))},warn:o=>{console.log(kt(o))},log:o=>{console.log(At(o))},diff:(o,r)=>{console.log(Tt(o,r))},debug:o=>{e===!0&&console.log(bt(JSON.stringify(o)))},error:o=>{console.log(Ot(o.message)),e===!0&&console.log(o)}})}}function m(t){return(e,n)=>{let o=jt(n),r=Ht(),i=Pt(),s=r.withVerbosity(o.verbose),a=o.apiKey;return t(a,s,i,e).then(()=>Kt(i)).catch(p=>{Se(p,s,i)})}}function Se(t,e,n){throw Kt(n),e.error(t),t}function Kt(t){t.isSpinning()===!0&&t.stop(!0)}var N=[{type:"password",message:"Enter your Snowplow organization ID:",mask:!0,validate:$t},{type:"select",message:"Select the Snowplow Tracker to use:",choices:[{value:"@snowplow/javascript-tracker",name:"JavaScript Tracker"},{value:"@snowplow/browser-tracker",name:"Browser Tracker"},{value:"@snowplow/node-tracker",name:"Node Tracker"},{value:"snowplow-android-tracker",name:"Android Tracker"},{value:"snowplow-ios-tracker",name:"iOS Tracker"},{value:"snowplow-golang-tracker",name:"Golang Tracker"}]},{type:"select",message:"Select the language to use:"},{type:"input",message:"Enter the directory for Snowtype output:",default:"./snowtype"}],ye={organizationId:"",tracker:"",language:"",outpath:""};function R(t,e,n,o=""){if(o)return Promise.resolve(o);if(Object.prototype.hasOwnProperty.call(e,t)&&e[t]!=null){let r=e[t];if(typeof r=="string")return Promise.resolve(r);throw new w("Invalid default options.")}if(Vt(n))return(0,k.input)(n);if(Mt(n))return(0,k.password)(n);if(tt(n))return(0,k.select)(n);throw new w("Unknown Prompt inquired.")}function ve(t,e){return tt(e)&&(e.choices=t),e}async function xe(t,e){let n={...ye};e.debug("Checking for existing configuration file.."),Ft();let o=await R("organizationId",t,N[0]);n.organizationId=o;let r=await R("tracker",t,N[1]);if(!Ut(r))throw new T("tracker",r);n.tracker=r;let i=H[r];if(!Array.isArray(i)||i.length===0)throw new w("Missing language entry.");let s;if(i.length===1){let p=i[0];if(typeof p>"u")throw new w("Missing language.");s=await R("language",t,N[2],p.value)}else s=await R("language",t,ve(i,N[2]));if(!Dt(n.tracker,s))throw new T("language",s);n.language=s;let a=await R("outpath",t,N[3]);return n.outpath=a,e.debug("Assempling initialization options.."),n}async function Pe(t,e,n,o){let r=await xe(o,e);n.setSpinnerTitle("Initialising..."),n.isSpinning()===!1&&n.start(),e.debug("Creating configuration with options: ".concat(JSON.stringify(r)));let i=Nt(r);if(P(i),!await new l({apiKey:t,organizationId:i.organizationId}).authenticate())throw new x;e.success("Authentication succeeded"),B(i),e.success("Configuration created")}var et=m(Pe);var te=require("process"),ee=require("path");var Z=require("fs"),nt=require("path");var Jt="snowtype.config.json";function Ie(t){(0,Z.writeFileSync)((0,nt.resolve)(process.cwd(),`./${Jt}`),JSON.stringify(t,null,2))}function Bt(t,e){try{let n=(0,Z.readFileSync)((0,nt.resolve)(process.cwd(),`./${Jt}`),{encoding:"utf-8"}),o=JSON.parse(n);return o[t].some(i=>i===e)||o[t].push(e),Ie(o),!0}catch(n){return console.error(n),!1}}async function Wt(t,e){return await Promise.all(e.map(async o=>{let{version:r}=L(o),{deployments:i}=await t.fetchSchemaHash({schema:o}),s=i.reduce((a,p)=>new Date(p.ts)>new Date(a.ts)?p:a,i[0]);return s.version===r?{previous:o,next:o}:{previous:o,next:ft(o,{version:s.version})}}))}async function qt(t,e,n){let{eventSpecifications:o}=n,r={};return await Promise.all(e.map(async i=>{let s=o[i];if(!s)return i;let a=(await t.fetchEventSpecification({eventSpecificationId:i})).data[0];return(a.version||0)>s&&(r[i]={previous:s,next:a.version}),i})),r}var Zt=require("process"),ot=require("path"),Xt=require("fs"),Yt=require("mkdirp"),Qt=require("@snowplow/snowtype-core");function Ce(t){t.debug("Checking for existing configuration.."),E(),t.success("Valid configuration found")}async function F(t,e,n){Ce(e),n.setSpinnerTitle("Generating..."),n.isSpinning()===!1&&n.start();let o=q();D(o.dataStructures)&&D(o.igluCentralSchemas)&&D(o.eventSpecificationIds)&&D(o.dataProductIds)&&e.warn("Data Structures, Schemas and Event Specifications are empty.");let r=new l({apiKey:t,organizationId:o.organizationId});if(!await r.authenticate())throw new x;let a=[...await ht(r,o.dataProductIds),...o.eventSpecificationIds],p=await wt(r,a,e),h=await St(o.igluCentralSchemas),d=await yt(r,o.dataStructures),A={language:o.language,tracker:o.tracker,schemas:[...h,...d],eventSpecifications:p},u=await(0,Qt.generate)(A);e.success("Code generated, writing...");let b=u?.lines.join(`
|
|
34
|
-
`),at=(0,ot.resolve)((0,Zt.cwd)(),o.outpath);Yt.mkdirp.sync(at);let ie=mt+Gt(o.language);(0,Xt.writeFileSync)((0,ot.resolve)(at,ie),b),zt(p,o.outpath)}var rt=m(F);var Ee={yes:!1};function ke(t,e){let n={...Ee,...t};return e.debug("Checking for existing configuration.."),E(),e.success("Valid configuration found"),n}async function Ae(t,e,n,o){let r=ke(o,e);e.debug("Starting update process with options: ".concat(JSON.stringify(r)));let i=q(),s=(0,ee.resolve)((0,te.cwd)(),i.outpath,M),a=new l({apiKey:t,organizationId:i.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let p={};if(!U(s)&&i.eventSpecificationIds.length)throw new V;if(U(s)&&i.eventSpecificationIds.length){let u=Rt(s);p=await qt(a,i.eventSpecificationIds,u)}let d=(await Wt(a,i.dataStructures)).filter(u=>u.previous!==u.next);if((Object.entries(p).length>0||d.length>0)===!0){if(e.success("Available updates found!"),_t(p,d,e),r.yes===!0)return d.length>0&&(d.forEach(u=>{let b=i.dataStructures.indexOf(u.previous);i.dataStructures[b]=u.next}),B(i)),e.debug("Applying the updates.."),F(t,e,n);e.log("No updates applied"),e.log("To apply the available updates run update with --yes flag")}else e.success("No updates found. Everything is up-to-date!");return Promise.resolve(void 0)}var it=m(Ae);var be={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1};function Oe(t,e){let n={...be,...t};if(Object.values(n).every(o=>typeof o=="boolean"||o.length===0))throw new _;return e.debug("Checking for existing configuration.."),E(),e.success("Valid configuration found"),n}async function Te(t,e,n,o){let r=Oe(o,e);return e.debug("Starting patch process with options: ".concat(JSON.stringify(r))),Object.entries(r).forEach(([s,a])=>{s!=="yes"&&Array.isArray(a)&&a.length!==0&&a.forEach(p=>{Bt(s,p)})}),e.success("Configuration patched"),r.yes===!0?F(t,e,n):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var st=m(Te);var ne="0.2.
|
|
34
|
+
`),at=(0,ot.resolve)((0,Zt.cwd)(),o.outpath);Yt.mkdirp.sync(at);let ie=mt+Gt(o.language);(0,Xt.writeFileSync)((0,ot.resolve)(at,ie),b),zt(p,o.outpath)}var rt=m(F);var Ee={yes:!1};function ke(t,e){let n={...Ee,...t};return e.debug("Checking for existing configuration.."),E(),e.success("Valid configuration found"),n}async function Ae(t,e,n,o){let r=ke(o,e);e.debug("Starting update process with options: ".concat(JSON.stringify(r)));let i=q(),s=(0,ee.resolve)((0,te.cwd)(),i.outpath,M),a=new l({apiKey:t,organizationId:i.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let p={};if(!U(s)&&i.eventSpecificationIds.length)throw new V;if(U(s)&&i.eventSpecificationIds.length){let u=Rt(s);p=await qt(a,i.eventSpecificationIds,u)}let d=(await Wt(a,i.dataStructures)).filter(u=>u.previous!==u.next);if((Object.entries(p).length>0||d.length>0)===!0){if(e.success("Available updates found!"),_t(p,d,e),r.yes===!0)return d.length>0&&(d.forEach(u=>{let b=i.dataStructures.indexOf(u.previous);i.dataStructures[b]=u.next}),B(i)),e.debug("Applying the updates.."),F(t,e,n);e.log("No updates applied"),e.log("To apply the available updates run update with --yes flag")}else e.success("No updates found. Everything is up-to-date!");return Promise.resolve(void 0)}var it=m(Ae);var be={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1};function Oe(t,e){let n={...be,...t};if(Object.values(n).every(o=>typeof o=="boolean"||o.length===0))throw new _;return e.debug("Checking for existing configuration.."),E(),e.success("Valid configuration found"),n}async function Te(t,e,n,o){let r=Oe(o,e);return e.debug("Starting patch process with options: ".concat(JSON.stringify(r))),Object.entries(r).forEach(([s,a])=>{s!=="yes"&&Array.isArray(a)&&a.length!==0&&a.forEach(p=>{Bt(s,p)})}),e.success("Configuration patched"),r.yes===!0?F(t,e,n):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var st=m(Te);var ne="0.2.1";async function re(){await import("dotenv/config");let t=new X.Command;t.exitOverride(),t.version(ne,void 0,"Snowtype CLI version number").name("snowtype").description("The Snowtype CLI is a tool which aims to speed up tracking implementations, provide type safety and inline documentation for developers and ultimately reduce the number of erroneous events.").showHelpAfterError().configureHelp({showGlobalOptions:!0}).option("-k, --apiKey <string>","Snowplow Console API key").option("-v, --verbose","Enable verbose logging"),t.command("init").description("Initialize the setup of Snowtype code generation in a project. Creates the `snowtype.config.json` file.").option("-i, --organizationId <string>","organization ID").option("-t, --tracker <string>","tracker to use").option("-l, --language <string>","language to use").option("-o, --outpath <string>","output path").action(et),t.command("update").description("Checks for latest version updates in Data Structures and Event Specifications.").option("-y, --yes","updates all to latest without asking",!1).action(it),t.command("generate").description("Generates tracking code based on configuration on the `snowtype.config.json` file.").action(rt),t.command("patch").description("Adds new Data Structures and Event Specifications in the `snowtype.config.json` file.").option("-p, --dataProductIds [value...]","data product ID").option("-e, --eventSpecificationIds [value...]","tracking eventSpecification ID").option("-d, --dataStructures [value...]","data structure URI").option("-i, --igluCentralSchemas [value...]","iglu central URI").option("-y, --yes","updates all to latest without asking",!1).action(st),t.addHelpText("before",Ct()).addHelpText("after",`
|
|
35
35
|
`),await t.parseAsync(oe.argv).catch(e=>{Le(e)})}function Le(t){if(!(t instanceof X.CommanderError&&["commander.help","commander.version","commander.helpDisplayed"].indexOf(t.code)>-1))throw t}re().then(()=>{}).catch(()=>{process.exitCode=1});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowplow/snowtype",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A code generator CLI for faster and type-safe Snowplow tracking code",
|
|
5
5
|
"author": "Peter Perlepes <peter@snowplowanalytics.com>",
|
|
6
6
|
"homepage": "https://snowplow.io/",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"@commander-js/extra-typings": "^11.1.0",
|
|
15
15
|
"@inquirer/prompts": "^3.3.0",
|
|
16
|
-
"@snowplow/snowtype-core": "^0.2.
|
|
16
|
+
"@snowplow/snowtype-core": "^0.2.1",
|
|
17
17
|
"chalk": "^4.1.2",
|
|
18
18
|
"cli-spinner": "^0.2.10",
|
|
19
19
|
"dotenv": "^16.3.1",
|