@snowplow/snowtype 0.2.8 → 0.2.9

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.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -74,5 +74,5 @@ Find out more ${pe.default.underline("https://docs.snowplow.io/docs/collecting-d
74
74
  `);let o=await Xe(i,e);n.setSpinnerTitle("Initialising..."),n.isSpinning()===!1&&n.start(),e.debug("Creating configuration with options: ".concat(JSON.stringify(o)));let r=Xt(o);if(C(r),!await new I({apiKey:t,organizationId:r.organizationId}).authenticate())throw new A;e.success("Authentication succeeded"),et(r),e.success("Configuration created")}var dt=P(Qe);var Ee=require("process"),ke=require("path");var ot=require("fs"),ft=require("path");var ue="snowtype.config.json";function tn(t){(0,ot.writeFileSync)((0,ft.resolve)(process.cwd(),`./${ue}`),JSON.stringify(t,null,2))}function ge(t,e){try{let n=(0,ot.readFileSync)((0,ft.resolve)(process.cwd(),`./${ue}`),{encoding:"utf-8"}),i=JSON.parse(n);return i[t].some(r=>r===e)||i[t].push(e),tn(i),!0}catch(n){return console.error(n),!1}}async function de(t,e){return await Promise.all(e.map(async i=>{let{version:o}=y(i),{deployments:r}=await t.fetchSchemaHash({schema:i}),s=r.reduce((a,c)=>new Date(c.ts)>new Date(a.ts)?c:a,r[0]);return s.version===o?{previous:i,next:i}:{previous:i,next:It(i,{version:s.version})}}))}async function fe(t,e,n){let{eventSpecifications:i}=n,o={};return await Promise.all(e.map(async r=>{let s=i[r];if(!s)return r;let a=(await t.fetchEventSpecification({eventSpecificationId:r})).data[0];return(a.version||0)>s&&(o[r]={previous:s,next:a.version}),r})),o}var we=require("process"),rt=require("path"),ye=require("fs"),Ie=require("mkdirp"),Pe=require("@snowplow/snowtype-core");var xe=v(require("fs-extra"));var le=require("fs"),me=require("path"),he=v(require("util"));function Se(t,e){(0,le.writeFileSync)((0,me.resolve)(t,"validations.ts"),`import { AnySchema } from "ajv";
75
75
 
76
76
  export const schemasObject: Record<string, AnySchema> = ${he.default.inspect(e,!1,null)}`,"utf-8")}function ve(t){return Object.values(t).some(e=>e.properties?Object.values(e.properties).some(n=>Object.hasOwn(n||{},"pattern")):!1)}var G={instructions:!1,validations:!1};function en(t,e){return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),{...G,...t}}async function j(t,e,n,i){let o=en(i,e);n.setSpinnerTitle("Generating..."),n.isSpinning()===!1&&n.start();let r=it();R(r.dataStructures)&&R(r.igluCentralSchemas)&&R(r.eventSpecificationIds)&&R(r.dataProductIds)&&e.warn("Data Structures, Schemas and Event Specifications are empty.");let s=new I({apiKey:t,organizationId:r.organizationId});if(!await s.authenticate())throw new A;let g=[...await $t(s,r.dataProductIds),...r.eventSpecificationIds],u=await Ut(s,g,e,o.instructions),m=await Rt(s,u);Pt(u)&&r.tracker==="@snowplow/browser-tracker"&&e.log("You are generating Snowplow Ecommerce events. You would need to install `@snowplow/browser-plugin-snowplow-ecommerce`."),o.validations&&e.log("For validations to work properly you would need to install `ajv@8`, `ajv-formats@2` and `ajv-draft-04@1` on your project.");let l=await Nt(r.igluCentralSchemas),h=await Dt(s,r.dataStructures),at={language:r.language,tracker:r.tracker,schemas:[...l,...h],eventSpecifications:m,validations:o.validations},w=await(0,Pe.generate)(at);if(!w)throw new V;e.success("Code generated, writing...");let ct=w.lines.join(`
77
- `),S=(0,rt.resolve)((0,we.cwd)(),r.outpath);xe.default.removeSync((0,rt.resolve)(S)),Ie.mkdirp.sync(S);let z=kt+ee(r.language);(0,ye.writeFileSync)((0,rt.resolve)(S,z),ct),ne(u,r.outpath),o.validations&&w.validationsSchemaObject&&(Se(S,w.validationsSchemaObject),ve(w.validationsSchemaObject)&&e.warn("Some schemas appear to have attributes that can have inconsistent validations between the pipeline and client-side validation. Read more at https://docs.snowplow.io/docs/collecting-data/code-generation/client-side-validation/#divergence-with-pipeline-validation")),o.instructions&&Ot(u,w.lines,S,z)}var lt=P(j);var nn={yes:!1,...G};function on(t,e){let n={...nn,...t};return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function rn(t,e,n,i){let o=on(i,e);e.debug("Starting update process with options: ".concat(JSON.stringify(o)));let r=it(),s=(0,ke.resolve)((0,Ee.cwd)(),r.outpath,Y),a=new I({apiKey:t,organizationId:r.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let c={};if(!D(s)&&r.eventSpecificationIds.length)throw new B;if(D(s)&&r.eventSpecificationIds.length){let l=Qt(s);c=await fe(a,r.eventSpecificationIds,l)}let u=(await de(a,r.dataStructures)).filter(l=>l.previous!==l.next);if((Object.entries(c).length>0||u.length>0)===!0){if(e.success("Available updates found!"),ie(c,u,e),o.yes===!0)return u.length>0&&(u.forEach(l=>{let h=r.dataStructures.indexOf(l.previous);r.dataStructures[h]=l.next}),et(r)),e.debug("Applying the updates.."),j(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 mt=P(rn);var sn={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1,...G};function an(t,e){let n={...sn,...t};if(Object.values(n).every(i=>typeof i=="boolean"||i.length===0))throw new W;return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function cn(t,e,n,i){let o=an(i,e);return e.debug("Starting patch process with options: ".concat(JSON.stringify(o))),Object.entries(o).forEach(([s,a])=>{s!=="yes"&&Array.isArray(a)&&a.length!==0&&a.forEach(c=>{ge(s,c)})}),e.success("Configuration patched"),o.yes===!0?j(t,e,n,o):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var ht=P(cn);var Ae="0.2.8";async function be(){await import("dotenv/config");let t=new st.Command;t.exitOverride(),t.version(Ae,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(dt),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(mt),t.command("generate").option("-is, --instructions","generate event specification instructions",!1).option("-va, --validations","add runtime validation using Ajv",!1).description("Generates tracking code based on configuration on the `snowtype.config.json` file.").action(lt),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(ht),t.addHelpText("before",zt()).addHelpText("after",`
77
+ `),S=(0,rt.resolve)((0,we.cwd)(),r.outpath);xe.default.removeSync((0,rt.resolve)(S)),Ie.mkdirp.sync(S);let z=kt+ee(r.language);(0,ye.writeFileSync)((0,rt.resolve)(S,z),ct),ne(u,r.outpath),o.validations&&w.validationsSchemaObject&&(Se(S,w.validationsSchemaObject),ve(w.validationsSchemaObject)&&e.warn("Some schemas appear to have attributes that can have inconsistent validations between the pipeline and client-side validation. Read more at https://docs.snowplow.io/docs/collecting-data/code-generation/client-side-validation/#divergence-with-pipeline-validation")),o.instructions&&Ot(u,w.lines,S,z)}var lt=P(j);var nn={yes:!1,...G};function on(t,e){let n={...nn,...t};return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function rn(t,e,n,i){let o=on(i,e);e.debug("Starting update process with options: ".concat(JSON.stringify(o)));let r=it(),s=(0,ke.resolve)((0,Ee.cwd)(),r.outpath,Y),a=new I({apiKey:t,organizationId:r.organizationId});n.setSpinnerTitle("Checking for updates..."),n.isSpinning()===!1&&n.start();let c={};if(!D(s)&&r.eventSpecificationIds.length)throw new B;if(D(s)&&r.eventSpecificationIds.length){let l=Qt(s);c=await fe(a,r.eventSpecificationIds,l)}let u=(await de(a,r.dataStructures)).filter(l=>l.previous!==l.next);if((Object.entries(c).length>0||u.length>0)===!0){if(e.success("Available updates found!"),ie(c,u,e),o.yes===!0)return u.length>0&&(u.forEach(l=>{let h=r.dataStructures.indexOf(l.previous);r.dataStructures[h]=l.next}),et(r)),e.debug("Applying the updates.."),j(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 mt=P(rn);var sn={eventSpecificationIds:[],dataStructures:[],igluCentralSchemas:[],dataProductIds:[],yes:!1,...G};function an(t,e){let n={...sn,...t};if(Object.values(n).every(i=>typeof i=="boolean"||i.length===0))throw new W;return e.debug("Checking for existing configuration.."),T(),e.success("Valid configuration found"),n}async function cn(t,e,n,i){let o=an(i,e);return e.debug("Starting patch process with options: ".concat(JSON.stringify(o))),Object.entries(o).forEach(([s,a])=>{s!=="yes"&&Array.isArray(a)&&a.length!==0&&a.forEach(c=>{ge(s,c)})}),e.success("Configuration patched"),o.yes===!0?j(t,e,n,o):(e.log("Code is not generated yet for the patched configuration"),Promise.resolve(void 0))}var ht=P(cn);var Ae="0.2.9";async function be(){await import("dotenv/config");let t=new st.Command;t.exitOverride(),t.version(Ae,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(dt),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(mt),t.command("generate").option("-is, --instructions","generate event specification instructions",!1).option("-va, --validations","add runtime validation using Ajv",!1).description("Generates tracking code based on configuration on the `snowtype.config.json` file.").action(lt),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(ht),t.addHelpText("before",zt()).addHelpText("after",`
78
78
  `),await t.parseAsync(Ce.argv).catch(e=>{pn(e)})}function pn(t){if(!(t instanceof st.CommanderError&&["commander.help","commander.version","commander.helpDisplayed"].indexOf(t.code)>-1))throw t}be().then(()=>{}).catch(()=>{process.exitCode=1});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowplow/snowtype",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
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.8",
16
+ "@snowplow/snowtype-core": "^0.2.9",
17
17
  "chalk": "^4.1.2",
18
18
  "cli-spinner": "^0.2.10",
19
19
  "dotenv": "^16.3.1",