@snowplow/snowtype 0.2.2 → 0.2.3
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 @@ ${X("h2","Debug")}
|
|
|
72
72
|
`}var Ht={...Y},Q=`./${It}`;function Mt(t){return t.length!==0}function Kt(t){return Array.isArray(Ht[t])}function Bt(t,e){let n=Ht[t];return Array.isArray(n)?!!n.map(r=>r.value).includes(e):!1}function Wt(t){let e={organizationId:t.organizationId,tracker:t.tracker,language:t.language,outpath:t.outpath};return Object.assign({igluCentralSchemas:[],dataStructures:[],eventSpecificationIds:[],dataProductIds:[]},e)}function tt(t){let e=(0,O.resolve)((0,A.cwd)(),Q);return(0,I.writeFileSync)(e,JSON.stringify(t,null,2))}function D(t){let e=(0,I.statSync)(t,{throwIfNoEntry:!1});return e===void 0?!1:e.isFile()}function et(t){return(0,I.readFileSync)(t,{encoding:"utf-8"})}function nt(){let t=(0,O.resolve)((0,A.cwd)(),Q);return JSON.parse(et(t))}function Jt(t){return JSON.parse(et(t))}function T(){let t=(0,O.resolve)((0,A.cwd)(),Q);if(D(t)===!1)throw new j;let e=JSON.parse(et(t));return C(e)}function Yt(){let t=(0,O.resolve)((0,A.cwd)(),Q);if(D(t)===!0){let e=et(t);try{throw C(JSON.parse(e)),new $}catch(n){if(n instanceof $)throw n}}}function qt(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 i=(0,O.resolve)((0,A.cwd)(),e,J);try{(0,I.writeFileSync)(i,JSON.stringify(n,null,2))}catch{throw new K}}function Xt(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 Qt(t){let e=t.optsWithGlobals();return{...e,apiKey:e.apiKey||process.env.SNOWPLOW_CONSOLE_API_KEY}}function _(t){return!!(Array.isArray(t)&&!t.length)}function te(t){return t.type==="input"}function ee(t){return t.type==="password"}function ct(t){return t.type==="select"}function ne(){return{withVerbosity:e=>({success:i=>{console.log(_t(i))},warn:i=>{console.log(Ft(i))},log:i=>{console.log(Gt(i))},diff:(i,r)=>{console.log(jt(i,r))},debug:i=>{e===!0&&console.log(zt(JSON.stringify(i)))},error:i=>{console.log(Vt(i.message)),e===!0&&console.log(i)}})}}function w(t){return(e,n)=>{let i=Qt(n),r=ne(),o=Nt(),s=r.withVerbosity(i.verbose),c=i.apiKey;return t(c,s,o,e).then(()=>ie(o)).catch(a=>{Fe(a,s,o)})}}function Fe(t,e,n){throw ie(n),e.error(t),t}function ie(t){t.isSpinning()===!0&&t.stop(!0)}var re=h(require("chalk")),F=[{type:"password",message:"Enter your Snowplow organization ID:",mask:!0,validate:Mt},{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"}],Ge={organizationId:"",tracker:"",language:"",outpath:""};function G(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 v("Invalid default options.")}if(te(n))return(0,b.input)(n);if(ee(n))return(0,b.password)(n);if(ct(n))return(0,b.select)(n);throw new v("Unknown Prompt inquired.")}function ze(t,e){return ct(e)&&(e.choices=t),e}async function Ve(t,e){let n={...Ge};e.debug("Checking for existing configuration file.."),Yt();let i=await G("organizationId",t,F[0]);n.organizationId=i;let r=await G("tracker",t,F[1]);if(!Kt(r))throw new U("tracker",r);n.tracker=r;let o=Y[r];if(!Array.isArray(o)||o.length===0)throw new v("Missing language entry.");let s;if(o.length===1){let a=o[0];if(typeof a>"u")throw new v("Missing language.");s=await G("language",t,F[2],a.value)}else s=await G("language",t,ze(o,F[2]));if(!Bt(n.tracker,s))throw new U("language",s);n.language=s;let c=await G("outpath",t,F[3]);return n.outpath=c,e.debug("Assempling initialization options.."),n}async function je(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 ${re.default.underline("https://docs.snowplow.io/docs/collecting-data/code-generation/using-the-cli/#authenticating-with-the-console")}
|
|
74
74
|
`);let r=await Ve(i,e);n.setSpinnerTitle("Initialising..."),n.isSpinning()===!1&&n.start(),e.debug("Creating configuration with options: ".concat(JSON.stringify(r)));let o=Wt(r);if(C(o),!await new S({apiKey:t,organizationId:o.organizationId}).authenticate())throw new k;e.success("Authentication succeeded"),tt(o),e.success("Configuration created")}var pt=w(je);var le=require("process"),me=require("path");var it=require("fs"),gt=require("path");var oe="snowtype.config.json";function He(t){(0,it.writeFileSync)((0,gt.resolve)(process.cwd(),`./${oe}`),JSON.stringify(t,null,2))}function se(t,e){try{let n=(0,it.readFileSync)((0,gt.resolve)(process.cwd(),`./${oe}`),{encoding:"utf-8"}),i=JSON.parse(n);return i[t].some(o=>o===e)||i[t].push(e),He(i),!0}catch(n){return console.error(n),!1}}async function ae(t,e){return await Promise.all(e.map(async i=>{let{version:r}=N(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 ce(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 pe=require("process"),rt=require("path"),ge=require("fs"),ue=require("mkdirp"),fe=require("@snowplow/snowtype-core");var de=h(require("fs-extra")),z={instructions:!1};function Me(t,e){return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),{...z,...t}}async function V(t,e,n,i){let r=Me(i,e);n.setSpinnerTitle("Generating..."),n.isSpinning()===!1&&n.start();let o=nt();_(o.dataStructures)&&_(o.igluCentralSchemas)&&_(o.eventSpecificationIds)&&_(o.dataProductIds)&&e.warn("Data Structures, Schemas and Event Specifications are empty.");let s=new S({apiKey:t,organizationId:o.organizationId});if(!await s.authenticate())throw new k;let g=[...await Ot(s,o.dataProductIds),...o.eventSpecificationIds],f=await Tt(s,g,e,r.instructions),d=await bt(o.igluCentralSchemas),l=await Lt(s,o.dataStructures),x={language:o.language,tracker:o.tracker,schemas:[...d,...l],eventSpecifications:f},P=await(0,fe.generate)(x);e.success("Code generated, writing...");let L=P?.lines.join(`
|
|
75
|
-
`),E=(0,rt.resolve)((0,pe.cwd)(),o.outpath);de.default.removeSync((0,rt.resolve)(E)),ue.mkdirp.sync(E);let lt=xt+qt(o.language);(0,ge.writeFileSync)((0,rt.resolve)(E,lt),L),Zt(f,o.outpath),P&&r.instructions&&Ct(f,P.lines,E,lt)}var ut=w(V);var Ke={yes:!1,...z};function Be(t,e){let n={...Ke,...t};return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function We(t,e,n,i){let r=Be(i,e);e.debug("Starting update process with options: ".concat(JSON.stringify(r)));let o=nt(),s=(0,me.resolve)((0,le.cwd)(),o.outpath,J),c=new S({apiKey:t,organizationId:o.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let a={};if(!D(s)&&o.eventSpecificationIds.length)throw new B;if(D(s)&&o.eventSpecificationIds.length){let l=Jt(s);a=await ce(c,o.eventSpecificationIds,l)}let f=(await ae(c,o.dataStructures)).filter(l=>l.previous!==l.next);if((Object.entries(a).length>0||f.length>0)===!0){if(e.success("Available updates found!"),Xt(a,f,e),r.yes===!0)return f.length>0&&(f.forEach(l=>{let x=o.dataStructures.indexOf(l.previous);o.dataStructures[x]=l.next}),tt(o)),e.debug("Applying the updates.."),V(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 ft=w(We);var Je={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1,...z};function Ye(t,e){let n={...Je,...t};if(Object.values(n).every(i=>typeof i=="boolean"||i.length===0))throw new M;return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function qe(t,e,n,i){let r=Ye(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=>{se(s,a)})}),e.success("Configuration patched"),r.yes===!0?V(t,e,n,r):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var dt=w(qe);var he="0.2.
|
|
75
|
+
`),E=(0,rt.resolve)((0,pe.cwd)(),o.outpath);de.default.removeSync((0,rt.resolve)(E)),ue.mkdirp.sync(E);let lt=xt+qt(o.language);(0,ge.writeFileSync)((0,rt.resolve)(E,lt),L),Zt(f,o.outpath),P&&r.instructions&&Ct(f,P.lines,E,lt)}var ut=w(V);var Ke={yes:!1,...z};function Be(t,e){let n={...Ke,...t};return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function We(t,e,n,i){let r=Be(i,e);e.debug("Starting update process with options: ".concat(JSON.stringify(r)));let o=nt(),s=(0,me.resolve)((0,le.cwd)(),o.outpath,J),c=new S({apiKey:t,organizationId:o.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let a={};if(!D(s)&&o.eventSpecificationIds.length)throw new B;if(D(s)&&o.eventSpecificationIds.length){let l=Jt(s);a=await ce(c,o.eventSpecificationIds,l)}let f=(await ae(c,o.dataStructures)).filter(l=>l.previous!==l.next);if((Object.entries(a).length>0||f.length>0)===!0){if(e.success("Available updates found!"),Xt(a,f,e),r.yes===!0)return f.length>0&&(f.forEach(l=>{let x=o.dataStructures.indexOf(l.previous);o.dataStructures[x]=l.next}),tt(o)),e.debug("Applying the updates.."),V(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 ft=w(We);var Je={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1,...z};function Ye(t,e){let n={...Je,...t};if(Object.values(n).every(i=>typeof i=="boolean"||i.length===0))throw new M;return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function qe(t,e,n,i){let r=Ye(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=>{se(s,a)})}),e.success("Configuration patched"),r.yes===!0?V(t,e,n,r):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var dt=w(qe);var he="0.2.3";async function we(){await import("dotenv/config");let t=new ot.Command;t.exitOverride(),t.version(he,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(pt),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(ft),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(ut),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(dt),t.addHelpText("before",Dt()).addHelpText("after",`
|
|
76
76
|
`),await t.parseAsync(Se.argv).catch(e=>{Ze(e)})}function Ze(t){if(!(t instanceof ot.CommanderError&&["commander.help","commander.version","commander.helpDisplayed"].indexOf(t.code)>-1))throw t}we().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.3",
|
|
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.3",
|
|
17
17
|
"chalk": "^4.1.2",
|
|
18
18
|
"cli-spinner": "^0.2.10",
|
|
19
19
|
"dotenv": "^16.3.1",
|