@snowplow/snowtype 0.2.4 → 0.2.5
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
|
@@ -72,5 +72,5 @@ ${Q("h2","Debug")}
|
|
|
72
72
|
`}var Kt={...q},tt=`./${xt}`;function Wt(t){return t.length!==0}function Bt(t){return Array.isArray(Kt[t])}function Jt(t,e){let n=Kt[t];return Array.isArray(n)?!!n.map(r=>r.value).includes(e):!1}function Yt(t){let e={organizationId:t.organizationId,tracker:t.tracker,language:t.language,outpath:t.outpath};return Object.assign({igluCentralSchemas:[],dataStructures:[],eventSpecificationIds:[],dataProductIds:[]},e)}function et(t){let e=(0,T.resolve)((0,A.cwd)(),tt);return(0,E.writeFileSync)(e,JSON.stringify(t,null,2))}function R(t){let e=(0,E.statSync)(t,{throwIfNoEntry:!1});return e===void 0?!1:e.isFile()}function nt(t){return(0,E.readFileSync)(t,{encoding:"utf-8"})}function it(){let t=(0,T.resolve)((0,A.cwd)(),tt);return JSON.parse(nt(t))}function qt(t){return JSON.parse(nt(t))}function O(){let t=(0,T.resolve)((0,A.cwd)(),tt);if(R(t)===!1)throw new H;let e=JSON.parse(nt(t));return C(e)}function Zt(){let t=(0,T.resolve)((0,A.cwd)(),tt);if(R(t)===!0){let e=nt(t);try{throw C(JSON.parse(e)),new $}catch(n){if(n instanceof $)throw n}}}function Xt(t){return{javascript:".js",typescript:".ts",kotlin:".kt",swift:".swift",go:".go"}[t]||""}function Qt(t,e){let n={eventSpecifications:{}};t.forEach(r=>{n.eventSpecifications[r.eventSpecification.id]=r.version});let i=(0,T.resolve)((0,A.cwd)(),e,Y);try{(0,E.writeFileSync)(i,JSON.stringify(n,null,2))}catch{throw new W}}function te(t,e,n){let i=Object.entries(t);i.length>0&&n.success("Available Event Specification Updates"),i.forEach(([r,o])=>{let s=`${r}: version ${o.previous}`,c=`${r}: version ${o.next}`;n.diff(s,c)}),e.length>0&&n.success("Available Data Structures Updates"),e.forEach(r=>{let o=`${r.previous}`,s=`${r.next}`;n.diff(o,s)})}function ee(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 ne(t){return t.type==="input"}function ie(t){return t.type==="password"}function pt(t){return t.type==="select"}function re(){return{withVerbosity:e=>({success:i=>{console.log(Gt(i))},warn:i=>{console.log(zt(i))},log:i=>{console.log(Vt(i))},diff:(i,r)=>{console.log(Mt(i,r))},debug:i=>{e===!0&&console.log(jt(JSON.stringify(i)))},error:i=>{console.log(Ht(i.message)),e===!0&&console.log(i)}})}}function y(t){return(e,n)=>{let i=ee(n),r=re(),o=Dt(),s=r.withVerbosity(i.verbose),c=i.apiKey;return t(c,s,o,e).then(()=>oe(o)).catch(a=>{ze(a,s,o)})}}function ze(t,e,n){throw oe(n),e.error(t),t}function oe(t){t.isSpinning()===!0&&t.stop(!0)}var se=S(require("chalk")),_=[{type:"password",message:"Enter your Snowplow organization ID:",mask:!0,validate:Wt},{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"},{value:"@snowplow/react-native-tracker",name:"React Native Tracker"}]},{type:"select",message:"Select the language to use:"},{type:"input",message:"Enter the directory for Snowtype output:",default:"./snowtype"}],Ve={organizationId:"",tracker:"",language:"",outpath:""};function F(t,e,n,i=""){if(i)return Promise.resolve(i);if(Object.prototype.hasOwnProperty.call(e,t)&&e[t]!=null){let r=e[t];if(typeof r=="string")return Promise.resolve(r);throw new I("Invalid default options.")}if(ne(n))return(0,b.input)(n);if(ie(n))return(0,b.password)(n);if(pt(n))return(0,b.select)(n);throw new I("Unknown Prompt inquired.")}function je(t,e){return pt(e)&&(e.choices=t),e}async function He(t,e){let n={...Ve};e.debug("Checking for existing configuration file.."),Zt();let i=await F("organizationId",t,_[0]);n.organizationId=i;let r=await F("tracker",t,_[1]);if(!Bt(r))throw new U("tracker",r);n.tracker=r;let o=q[r];if(!Array.isArray(o)||o.length===0)throw new I("Missing language entry.");let s;if(o.length===1){let a=o[0];if(typeof a>"u")throw new I("Missing language.");s=await F("language",t,_[2],a.value)}else s=await F("language",t,je(o,_[2]));if(!Jt(n.tracker,s))throw new U("language",s);n.language=s;let c=await F("outpath",t,_[3]);return n.outpath=c,e.debug("Assempling initialization options.."),n}async function Me(t,e,n,i){t||e.warn(`No SNOWPLOW_CONSOLE_API_KEY detected. You might need to set this up before initializing.
|
|
73
73
|
Find out more ${se.default.underline("https://docs.snowplow.io/docs/collecting-data/code-generation/using-the-cli/#authenticating-with-the-console")}
|
|
74
74
|
`);let r=await He(i,e);n.setSpinnerTitle("Initialising..."),n.isSpinning()===!1&&n.start(),e.debug("Creating configuration with options: ".concat(JSON.stringify(r)));let o=Yt(r);if(C(o),!await new v({apiKey:t,organizationId:o.organizationId}).authenticate())throw new k;e.success("Authentication succeeded"),et(o),e.success("Configuration created")}var gt=y(Me);var he=require("process"),Se=require("path");var rt=require("fs"),ut=require("path");var ae="snowtype.config.json";function Ke(t){(0,rt.writeFileSync)((0,ut.resolve)(process.cwd(),`./${ae}`),JSON.stringify(t,null,2))}function ce(t,e){try{let n=(0,rt.readFileSync)((0,ut.resolve)(process.cwd(),`./${ae}`),{encoding:"utf-8"}),i=JSON.parse(n);return i[t].some(o=>o===e)||i[t].push(e),Ke(i),!0}catch(n){return console.error(n),!1}}async function pe(t,e){return await Promise.all(e.map(async i=>{let{version:r}=w(i),{deployments:o}=await t.fetchSchemaHash({schema:i}),s=o.reduce((c,a)=>new Date(a.ts)>new Date(c.ts)?a:c,o[0]);return s.version===r?{previous:i,next:i}:{previous:i,next:vt(i,{version:s.version})}}))}async function ge(t,e,n){let{eventSpecifications:i}=n,r={};return await Promise.all(e.map(async o=>{let s=i[o];if(!s)return o;let c=(await t.fetchEventSpecification({eventSpecificationId:o})).data[0];return(c.version||0)>s&&(r[o]={previous:s,next:c.version}),o})),r}var ue=require("process"),ot=require("path"),fe=require("fs"),de=require("mkdirp"),le=require("@snowplow/snowtype-core");var me=S(require("fs-extra"));var G={instructions:!1};function We(t,e){return e.debug("Checking for existing configuration.."),O(),e.success("Valid configuration found"),{...G,...t}}async function z(t,e,n,i){let r=We(i,e);n.setSpinnerTitle("Generating..."),n.isSpinning()===!1&&n.start();let o=it();D(o.dataStructures)&&D(o.igluCentralSchemas)&&D(o.eventSpecificationIds)&&D(o.dataProductIds)&&e.warn("Data Structures, Schemas and Event Specifications are empty.");let s=new v({apiKey:t,organizationId:o.organizationId});if(!await s.authenticate())throw new k;let g=[...await bt(s,o.dataProductIds),...o.eventSpecificationIds],u=await Lt(s,g,e,r.instructions);yt(u)&&o.tracker==="@snowplow/browser-tracker"&&e.log("You are generating Snowplow Ecommerce events. You would need to install `@snowplow/browser-plugin-snowplow-ecommerce`.");let m=await $t(o.igluCentralSchemas),f=await Ut(s,o.dataStructures),L={language:o.language,tracker:o.tracker,schemas:[...m,...f],eventSpecifications:u},P=await(0,le.generate)(L);e.success("Code generated, writing...");let V=P?.lines.join(`
|
|
75
|
-
`),h=(0,ot.resolve)((0,ue.cwd)(),o.outpath);me.default.removeSync((0,ot.resolve)(h)),de.mkdirp.sync(h);let j=Et+Xt(o.language);(0,fe.writeFileSync)((0,ot.resolve)(h,j),V),Qt(u,o.outpath),P&&r.instructions&&At(u,P.lines,h,j)}var ft=y(z);var Be={yes:!1,...G};function Je(t,e){let n={...Be,...t};return e.debug("Checking for existing configuration.."),O(),e.success("Valid configuration found"),n}async function Ye(t,e,n,i){let r=Je(i,e);e.debug("Starting update process with options: ".concat(JSON.stringify(r)));let o=it(),s=(0,Se.resolve)((0,he.cwd)(),o.outpath,Y),c=new v({apiKey:t,organizationId:o.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let a={};if(!R(s)&&o.eventSpecificationIds.length)throw new B;if(R(s)&&o.eventSpecificationIds.length){let f=qt(s);a=await ge(c,o.eventSpecificationIds,f)}let u=(await pe(c,o.dataStructures)).filter(f=>f.previous!==f.next);if((Object.entries(a).length>0||u.length>0)===!0){if(e.success("Available updates found!"),te(a,u,e),r.yes===!0)return u.length>0&&(u.forEach(f=>{let L=o.dataStructures.indexOf(f.previous);o.dataStructures[L]=f.next}),et(o)),e.debug("Applying the updates.."),z(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 dt=y(Ye);var qe={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1,...G};function Ze(t,e){let n={...qe,...t};if(Object.values(n).every(i=>typeof i=="boolean"||i.length===0))throw new K;return e.debug("Checking for existing configuration.."),O(),e.success("Valid configuration found"),n}async function Xe(t,e,n,i){let r=Ze(i,e);return e.debug("Starting patch process with options: ".concat(JSON.stringify(r))),Object.entries(r).forEach(([s,c])=>{s!=="yes"&&Array.isArray(c)&&c.length!==0&&c.forEach(a=>{ce(s,a)})}),e.success("Configuration patched"),r.yes===!0?z(t,e,n,r):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var lt=y(Xe);var we="0.2.
|
|
75
|
+
`),h=(0,ot.resolve)((0,ue.cwd)(),o.outpath);me.default.removeSync((0,ot.resolve)(h)),de.mkdirp.sync(h);let j=Et+Xt(o.language);(0,fe.writeFileSync)((0,ot.resolve)(h,j),V),Qt(u,o.outpath),P&&r.instructions&&At(u,P.lines,h,j)}var ft=y(z);var Be={yes:!1,...G};function Je(t,e){let n={...Be,...t};return e.debug("Checking for existing configuration.."),O(),e.success("Valid configuration found"),n}async function Ye(t,e,n,i){let r=Je(i,e);e.debug("Starting update process with options: ".concat(JSON.stringify(r)));let o=it(),s=(0,Se.resolve)((0,he.cwd)(),o.outpath,Y),c=new v({apiKey:t,organizationId:o.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let a={};if(!R(s)&&o.eventSpecificationIds.length)throw new B;if(R(s)&&o.eventSpecificationIds.length){let f=qt(s);a=await ge(c,o.eventSpecificationIds,f)}let u=(await pe(c,o.dataStructures)).filter(f=>f.previous!==f.next);if((Object.entries(a).length>0||u.length>0)===!0){if(e.success("Available updates found!"),te(a,u,e),r.yes===!0)return u.length>0&&(u.forEach(f=>{let L=o.dataStructures.indexOf(f.previous);o.dataStructures[L]=f.next}),et(o)),e.debug("Applying the updates.."),z(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 dt=y(Ye);var qe={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1,...G};function Ze(t,e){let n={...qe,...t};if(Object.values(n).every(i=>typeof i=="boolean"||i.length===0))throw new K;return e.debug("Checking for existing configuration.."),O(),e.success("Valid configuration found"),n}async function Xe(t,e,n,i){let r=Ze(i,e);return e.debug("Starting patch process with options: ".concat(JSON.stringify(r))),Object.entries(r).forEach(([s,c])=>{s!=="yes"&&Array.isArray(c)&&c.length!==0&&c.forEach(a=>{ce(s,a)})}),e.success("Configuration patched"),r.yes===!0?z(t,e,n,r):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var lt=y(Xe);var we="0.2.5";async function ye(){await import("dotenv/config");let t=new st.Command;t.exitOverride(),t.version(we,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(gt),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).option("-is, --instructions","generate event specification instructions",!1).action(dt),t.command("generate").option("-is, --instructions","generate event specification instructions",!1).description("Generates tracking code based on configuration on the `snowtype.config.json` file.").action(ft),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).option("-is, --instructions","generate event specification instructions",!1).action(lt),t.addHelpText("before",Ft()).addHelpText("after",`
|
|
76
76
|
`),await t.parseAsync(ve.argv).catch(e=>{Qe(e)})}function Qe(t){if(!(t instanceof st.CommanderError&&["commander.help","commander.version","commander.helpDisplayed"].indexOf(t.code)>-1))throw t}ye().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.5",
|
|
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.5",
|
|
17
17
|
"chalk": "^4.1.2",
|
|
18
18
|
"cli-spinner": "^0.2.10",
|
|
19
19
|
"dotenv": "^16.3.1",
|