@snowplow/snowtype 0.1.1 → 0.2.0

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 CHANGED
@@ -1,10 +1,35 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const commands_1 = require("./commands");
4
- (0, commands_1.main)()
5
- .then(() => {
6
- return undefined;
7
- })
8
- .catch(() => {
9
- process.exitCode = 1;
10
- });
1
+ "use strict";var se=Object.create;var ct=Object.defineProperty;var ae=Object.getOwnPropertyDescriptor;var ce=Object.getOwnPropertyNames;var pe=Object.getPrototypeOf,ge=Object.prototype.hasOwnProperty;var ue=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of ce(e))!ge.call(t,r)&&r!==n&&ct(t,r,{get:()=>e[r],enumerable:!(o=ae(e,r))||o.enumerable});return t};var v=(t,e,n)=>(n=t!=null?se(pe(t)):{},ue(e||!t||!t.__esModule?ct(n,"default",{value:t,enumerable:!0}):n,t));var oe=v(require("process")),X=require("@commander-js/extra-typings");var k=require("@inquirer/prompts");var fe={AuthError:"Authentication failed. Check if your API key and organization ID are correct.",ConfigAlreadyExists:"Snowtype config file already exists.",ConfigNotExists:"Snowtype config file does not exist in current directory. Start with running 'init'.",ConfigInvalid:"Snowtype config exists but it is invalid.",InternalError:"An internal error occured.",InvalidOption:"Invalid option provided.",MissingOptions:"Action called with no options.",WriteLockFile:"Failed writing lock file.",MissingLockFile:"Missing lockfile. You might need to run `snowtype generate` first."},de="Please report a bug.",f=class extends Error{code;constructor(e,n=""){let o=fe[e].concat(n);super(o),Error.captureStackTrace(this,this.constructor),this.name=this.constructor.name,this.code=e}},O=class extends f{constructor(){super("ConfigAlreadyExists")}},G=class extends f{constructor(){super("ConfigNotExists")}},z=class extends f{constructor(e){super("ConfigInvalid",e)}},w=class extends f{constructor(e){super("InternalError",e.concat(de))}},T=class extends f{constructor(e,n){let o=`Value ${n} is an invalid ${e}.`;super("InvalidOption",o)}},_=class extends f{constructor(){super("MissingOptions")}},x=class extends f{constructor(){super("AuthError")}},j=class extends f{constructor(){super("WriteLockFile")}},V=class extends f{constructor(){super("MissingLockFile")}};var pt=v(require("got"));async function gt({schema:t}){return(await(0,pt.default)(`https://raw.githubusercontent.com/snowplow/iglu-central/master/schemas/${t.replace("iglu:","")}`)).body}var Q=v(require("got"));var ut=require("js-sha256");function Y(t){let e=ut.sha256.create();return e.update(t),e.hex()}function L(t){let e=t.replace("iglu:",""),[n,o,r,i]=e.split("/");return{vendor:n,name:o,format:r,version:i}}function ft(t,{vendor:e,name:n,format:o,version:r}){let i=t.replace("iglu:",""),[s,a,p,h]=i.split("/");return`iglu:${e||s}/${n||a}/${o||p}/${r||h}`}var l=class{apiKey;organizationId;accessToken;apiHost="https://console.snowplowanalytics.com/api/msc";constructor({apiKey:e,organizationId:n}){this.apiKey=e,this.organizationId=n}async fetchAccessToken(){if(this.accessToken)return this.accessToken;let e=`${this.apiHost}/v1/organizations/${this.organizationId}/credentials/v2/token`,{accessToken:n}=await(0,Q.default)(e,{headers:{"X-API-Key":this.apiKey}}).json();return this.accessToken=n,n}async fetchResource(e){let n=await this.fetchAccessToken();return(0,Q.default)(e,{headers:{Authorization:"Bearer "+n}}).json()}fetchSchemaVersion({schema:e}){let{vendor:n,name:o,format:r,version:i}=L(e),s=Y(`${this.organizationId}-${n}-${o}-${r}`),a=`${this.apiHost}/v1/organizations/${this.organizationId}/data-structures/v1/${s}/versions/${i}`;return this.fetchResource(a)}fetchSchemaHash({schema:e}){let{vendor:n,name:o,format:r}=L(e),i=Y(`${this.organizationId}-${n}-${o}-${r}`),s=`${this.apiHost}/v1/organizations/${this.organizationId}/data-structures/v1/${i}`;return this.fetchResource(s)}async fetchEventSpecification({eventSpecificationId:e}){let n=`${this.apiHost}/v1/organizations/${this.organizationId}/tracking-scenarios/v2/${e}`;return await this.fetchResource(n)}async fetchDataProduct({dataProductId:e}){let n=`${this.apiHost}/v1/organizations/${this.organizationId}/data-products/v1/${e}`;return await this.fetchResource(n)}async authenticate(){try{return await this.fetchAccessToken(),!0}catch{return!1}}};var c=v(require("joi"));var dt=new RegExp("^iglu:[a-zA-Z0-9-_.]+/[a-zA-Z0-9-_]+/[a-zA-Z0-9-_]+/[0-9]+-[0-9]+-[0-9]+$"),le=c.default.object().required().keys({tracker:c.default.string().required().valid("@snowplow/browser-tracker","@snowplow/javascript-tracker","@snowplow/node-tracker","snowplow-android-tracker","snowplow-ios-tracker","snowplow-golang-tracker"),language:c.default.string().required().valid("typescript","kotlin","swift","javascript","go"),outpath:c.default.string().required(),organizationId:c.default.string().when("dataStructures",{is:c.default.array().min(1),then:c.default.required(),otherwise:c.default.optional()}).when("eventSpecificationIds",{is:c.default.array().min(1),then:c.default.required(),otherwise:c.default.optional()}).when("dataProductIds",{is:c.default.array().min(1),then:c.default.required(),otherwise:c.default.optional()}),igluCentralSchemas:c.default.array().optional().items(c.default.string().regex(dt)),dataStructures:c.default.array().optional().items(c.default.string().regex(dt)),eventSpecificationIds:c.default.array().optional().items(c.default.string()),dataProductIds:c.default.array().optional().items(c.default.string())}),P=t=>{let e=le.validate(t,{abortEarly:!1,convert:!1});if(e.error)throw new z(e.error.annotate());return t};var lt="snowtype.config.json",M=".snowtype-lock.json",mt="snowplow";var H={"@snowplow/javascript-tracker":[{value:"javascript",name:"JavaScript"}],"@snowplow/browser-tracker":[{value:"javascript",name:"JavaScript"},{value:"typescript",name:"TypeScript"}],"@snowplow/node-tracker":[{value:"javascript",name:"JavaScript"},{value:"typescript",name:"TypeScript"}],"snowplow-android-tracker":[{value:"kotlin",name:"Kotlin"}],"snowplow-ios-tracker":[{value:"swift",name:"Swift"}],"snowplow-golang-tracker":[{value:"go",name:"Go"}]};var I=require("process"),C=require("path"),y=require("fs");var me=["iglu:com.snowplowanalytics.snowplow/page_ping/jsonschema/1-0-0","iglu:com.snowplowanalytics.snowplow/link_click/jsonschema/1-0-1","iglu:com.snowplowanalytics.snowplow/page_view/jsonschema/1-0-0","iglu:com.snowplowanalytics.mobile/screen_view/jsonschema/1-0-0"];async function ht(t,e){let n=[];for(let r of e)n.push(t.fetchDataProduct({dataProductId:r}));return(await Promise.all(n)).map(({data:r})=>r[0]?.trackingScenarios).flat().map(r=>r?.id).filter(r=>!!r)}async function wt(t,e,n){let o=[];for(let r of e){let i=await t.fetchEventSpecification({eventSpecificationId:r}),s=i.data[0]?.name,a=i.data[0]?.version,p=i.data[0]?.event;if(p?.source&&me.indexOf(p.source)!==-1){n.warn("Currently you cannot generate code for `Standard` events. Skipping...");continue}let h=await t.fetchSchemaVersion({schema:p?.source||""}),d=[],A=[];i.data[0]?.entities?.tracked?.forEach(u=>A.push(u.source)),await Promise.all(A.map(async u=>{let b=await t.fetchSchemaVersion({schema:u});d.push(b)})),o.push({eventSpecification:{name:s||"",id:r},version:a||0,eventSchema:h,entitiesSchemas:d})}return o}async function St(t){return await Promise.all(t.map(async n=>JSON.parse(await gt({schema:n}))))}async function yt(t,e){return await Promise.all(e.map(o=>t.fetchSchemaVersion({schema:o})))}var xt=require("cli-spinner"),g=v(require("chalk"));function Pt(){let t=new xt.Spinner;return t.setSpinnerString(18),t}var he="https://snowplow.io",we="https://docs.snowplow.io/docs/collecting-data/code-generation/",$={SUCCESS:"\u2714",LOG:"\u2139",DEBUG:"\u27A4",ERROR:"\u2717",WARN:"\u26A0"},It={LINK:"#9911BB",OPTION:"#9911DD"};function vt(t){return g.default.hex(It.LINK).bold(t)}function S(t){return g.default.hex(It.OPTION).italic(t)}function K(t,e){switch(t){case"h1":return g.default.bold.underline(e);default:return g.default.bold(e)}}function Ct(){return`
2
+ ${K("h1","CLI Help")}
3
+
4
+ For more information visit the Snowplow website ${vt(he)}
5
+
6
+ For details see the CLI documentation ${vt(we)}
7
+
8
+ ${K("h2","How to get started")}
9
+ 1. Use ${g.default.cyanBright("snowtype init")} to initialize the setup.
10
+ 2. Use ${g.default.cyanBright("snowtype patch")} to add a Data Structure or Event Specification.
11
+ 3. Use ${g.default.cyanBright("snowtype generate")} to generate tracking code.
12
+
13
+ ${K("h2","Available commands")}
14
+ To learn more about each CLI command, use the ${S("--help")} option, for example, ${S("snowtype init --help")}.
15
+
16
+ ${S("snowtype init")}
17
+ Initialize the setup of Snowtype code generation in a project. Creates the \`snowtype.config.json\` file.
18
+
19
+ ${S("snowtype patch")}
20
+ Adds new Data Structures and Event Specifications in the \`snowtype.config.json\` file.
21
+
22
+ ${S("snowtype generate")}
23
+ Generates tracking code based on configuration on the \`snowtype.config.json\` file.
24
+
25
+ ${S("snowtype update")}
26
+ Checks for latest version updates in Data Structures and Event Specifications.
27
+
28
+ ${K("h2","Debug")}
29
+ Use the ${S("-v")} to output verbose logs
30
+ `}function Et(t){let e=`${$.SUCCESS}: `;return g.default.cyanBright(e)+t}function kt(t){let e=`${$.WARN} Warning: `;return g.default.hex("#FFA500")(e)+t}function At(t){let e=`${$.LOG} ${t}`;return g.default.yellowBright(e)}function bt(t){let e=`${$.DEBUG} Debug: `;return g.default.magenta(e)+t}function Ot(t){let e=`${$.ERROR} Error: `;return g.default.redBright(e)+t}function Tt(t,e){let n=`- ${t}`,o=`+ ${e}`;return`
31
+ ${g.default.magenta(n)}
32
+ ${g.default.cyanBright(o)}
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.0";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
+ `),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.1.1",
3
+ "version": "0.2.0",
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.1.1",
16
+ "@snowplow/snowtype-core": "^0.2.0",
17
17
  "chalk": "^4.1.2",
18
18
  "cli-spinner": "^0.2.10",
19
19
  "dotenv": "^16.3.1",
@@ -28,6 +28,7 @@
28
28
  "@types/got": "^9.6.12",
29
29
  "@types/jest": "^29.5.8",
30
30
  "@types/node": "^20.9.0",
31
+ "esbuild": "^0.19.11",
31
32
  "jest": "^29.7.0",
32
33
  "rimraf": "^5.0.5",
33
34
  "ts-jest": "^29.1.1",
@@ -36,12 +37,15 @@
36
37
  "typescript": "5.2.2"
37
38
  },
38
39
  "scripts": {
39
- "prebuild": "node ./scripts/version.js",
40
+ "fix-version": "node ./scripts/version.js",
41
+ "prebuild": "npm run fix-version",
40
42
  "build": "tsc",
41
- "lint": "eslint",
42
- "lint:fix": "eslint --fix",
43
- "prepack": "npm run build",
44
- "test": "jest"
43
+ "lint": "eslint src",
44
+ "lint:fix": "eslint src --fix",
45
+ "format": "prettier --write src",
46
+ "test": "jest --passWithNoTests",
47
+ "prebuild:production": "npm run fix-version && rimraf ./dist",
48
+ "build:production": "esbuild --bundle --packages=external --platform=node --target=es2022 --outdir=dist --minify src/index.ts"
45
49
  },
46
50
  "engines": {
47
51
  "node": ">=18"
@@ -50,8 +54,10 @@
50
54
  "snowplow",
51
55
  "analytics"
52
56
  ],
53
- "types": "dist/index.d.ts",
54
57
  "bin": {
55
58
  "snowtype": "bin/bin"
59
+ },
60
+ "publishConfig": {
61
+ "access": "public"
56
62
  }
57
63
  }
@@ -1,60 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ConsoleAPI = void 0;
7
- const got_1 = __importDefault(require("got"));
8
- const utils_1 = require("../utils");
9
- class ConsoleAPI {
10
- apiKey;
11
- organizationId;
12
- accessToken;
13
- apiHost = "https://console.snowplowanalytics.com/api/msc";
14
- constructor({ apiKey, organizationId }) {
15
- this.apiKey = apiKey;
16
- this.organizationId = organizationId;
17
- }
18
- async fetchAccessToken() {
19
- if (this.accessToken) {
20
- return this.accessToken;
21
- }
22
- const accessTokenUrl = `${this.apiHost}/v1/organizations/${this.organizationId}/credentials/v2/token`;
23
- const { accessToken } = await (0, got_1.default)(accessTokenUrl, {
24
- headers: { "X-API-Key": this.apiKey },
25
- }).json();
26
- this.accessToken = accessToken;
27
- return accessToken;
28
- }
29
- /* TODO allow for adding params in the resourceUrl before reaching fetchResource */
30
- async fetchResource(resourceUrl) {
31
- const token = await this.fetchAccessToken();
32
- return (0, got_1.default)(resourceUrl, {
33
- headers: { Authorization: "Bearer " + token },
34
- }).json();
35
- }
36
- /* Todo, return schema type */
37
- async fetchSchemaVersion({ schema, }) {
38
- const { vendor, name, format, version } = (0, utils_1.splitIgluSchemaUrlParts)(schema);
39
- const schemaSHA256 = (0, utils_1.hashSHA256)(`${this.organizationId}-${vendor}-${name}-${format}`);
40
- const schemaVersionUrl = `${this.apiHost}/v1/organizations/${this.organizationId}/data-structures/v1/${schemaSHA256}/versions/${version}`;
41
- return await this.fetchResource(schemaVersionUrl);
42
- }
43
- async fetchSchemaHash({ schema }) {
44
- const { vendor, name, format } = (0, utils_1.splitIgluSchemaUrlParts)(schema);
45
- const schemaSHA256 = (0, utils_1.hashSHA256)(`${this.organizationId}-${vendor}-${name}-${format}`);
46
- const schemaVersionUrl = `${this.apiHost}/v1/organizations/${this.organizationId}/data-structures/v1/${schemaSHA256}`;
47
- return await this.fetchResource(schemaVersionUrl);
48
- }
49
- async fetchTrackingScenario({ scenarioId }) {
50
- const schemaVersionUrl = `${this.apiHost}/v1/organizations/${this.organizationId}/tracking-scenarios/v1/${scenarioId}`;
51
- return await this.fetchResource(schemaVersionUrl);
52
- }
53
- // TODO - refine all of it
54
- async authenticate() {
55
- return this.fetchAccessToken()
56
- .then((token) => !!token)
57
- .catch(() => false);
58
- }
59
- }
60
- exports.ConsoleAPI = ConsoleAPI;
@@ -1,11 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.fetchIgluCentralSchema = void 0;
7
- const got_1 = __importDefault(require("got"));
8
- async function fetchIgluCentralSchema({ schema, }) {
9
- return (await (0, got_1.default)(`https://raw.githubusercontent.com/snowplow/iglu-central/master/schemas/${schema.replace("iglu:", "")}`)).body;
10
- }
11
- exports.fetchIgluCentralSchema = fetchIgluCentralSchema;
package/dist/api/index.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./fetchIgluCentralSchema"), exports);
18
- __exportStar(require("./ConsoleAPI"), exports);
@@ -1,121 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.checkScenarioUpdates = exports.createStorageJSON = exports.checkDataStructureUpdates = exports.addConfigAttribute = exports.createConfigFile = void 0;
4
- const fs_1 = require("fs");
5
- const path_1 = require("path");
6
- const utils_1 = require("../utils");
7
- const DEFAULT_CONFIG_NAME = 'snowtype.config.json';
8
- const DEFAULT_CONFIG = {
9
- tracker: "@snowplow/browser-tracker",
10
- language: "typescript",
11
- outpath: "./snowtype"
12
- };
13
- const DEFAULT_STORAGE_FILE = '.snowtype-lock.json';
14
- function writeToConfig(config) {
15
- (0, fs_1.writeFileSync)((0, path_1.resolve)(process.cwd(), `./${DEFAULT_CONFIG_NAME}`), JSON.stringify(config, null, 2));
16
- }
17
- function createConfigFile(partialConfig) {
18
- const config = { ...DEFAULT_CONFIG, ...partialConfig };
19
- try {
20
- writeToConfig(config);
21
- return true;
22
- }
23
- catch (error) {
24
- console.error(error);
25
- return false;
26
- }
27
- }
28
- exports.createConfigFile = createConfigFile;
29
- // TODO refactor file stuff
30
- function addConfigAttribute(attribute, value) {
31
- try {
32
- const rawConfig = (0, fs_1.readFileSync)((0, path_1.resolve)(process.cwd(), `./${DEFAULT_CONFIG_NAME}`), { encoding: "utf-8" });
33
- const parsedConfig = JSON.parse(rawConfig);
34
- const valueExists = parsedConfig[attribute].some(entry => entry === value);
35
- if (!valueExists) {
36
- parsedConfig[attribute].push(value);
37
- }
38
- // TODO, run validate after patch and if failed abort ?
39
- writeToConfig(parsedConfig);
40
- return true;
41
- }
42
- catch (error) {
43
- console.error(error);
44
- return false;
45
- }
46
- }
47
- exports.addConfigAttribute = addConfigAttribute;
48
- /**
49
- *
50
- *
51
- * @export
52
- * @param {ConsoleAPI} consoleApi
53
- * @param {string[]} dataStructures Array with Console data structure iglu URLs
54
- * @return {*} {Promise<string[]>}
55
- */
56
- async function checkDataStructureUpdates(consoleApi, dataStructures) {
57
- // Should we store the time we fetched the previous URL ?
58
- // Does newest means by time uploaded or by version, should this be an option ?
59
- const schemaUpdates = await Promise.all(dataStructures.map(async (dataStructureUrl) => {
60
- const { version } = (0, utils_1.splitIgluSchemaUrlParts)(dataStructureUrl);
61
- const { deployments } = (await consoleApi.fetchSchemaHash({ schema: dataStructureUrl }));
62
- // Should we get only PROD ? (PROLLY)
63
- // With one pass, find the latest.
64
- const latestDeployment = deployments.reduce((accum, cur) => {
65
- const currentTs = new Date(cur.ts);
66
- if (currentTs > new Date(accum.ts)) {
67
- return cur;
68
- }
69
- return accum;
70
- }, deployments[0]);
71
- if (latestDeployment.version === version) {
72
- return {
73
- previous: dataStructureUrl,
74
- next: dataStructureUrl
75
- };
76
- }
77
- return {
78
- previous: dataStructureUrl,
79
- next: (0, utils_1.replaceIgluSchemaPart)(dataStructureUrl, { version: latestDeployment.version })
80
- };
81
- }));
82
- return schemaUpdates;
83
- }
84
- exports.checkDataStructureUpdates = checkDataStructureUpdates;
85
- // the storage JSON file. Will it go on the `snowtype` folder or in a `.snowtype` directory ?
86
- function createStorageJSON({ trackingScenarios }) {
87
- try {
88
- const latestGenerationConfig = { trackingScenarios: trackingScenarios.map(ts => ({ [ts.id]: ts.version })) };
89
- (0, fs_1.writeFileSync)((0, path_1.resolve)(process.cwd(), `./${DEFAULT_CONFIG_NAME}/${DEFAULT_STORAGE_FILE}`), JSON.stringify(latestGenerationConfig, null, 2));
90
- return true;
91
- }
92
- catch (error) {
93
- console.error(error);
94
- return false;
95
- }
96
- }
97
- exports.createStorageJSON = createStorageJSON;
98
- // replace latestGenerationConfig type with what we decide. Created from createStorageJSON
99
- // Just check if there is any update ? The generator for now will regenerate all anyway.
100
- async function checkScenarioUpdates(consoleApi, trackingScenarioIds, latestGenerationConfig) {
101
- // trackingScenarios would be [ts.id]: ts.version
102
- const { trackingScenarios } = latestGenerationConfig;
103
- const updates = {};
104
- await Promise.all(trackingScenarioIds.map(async (trackingScenarioId) => {
105
- const existingScenarioGeneratedVersion = trackingScenarios[trackingScenarioId];
106
- // should we check if trackingScenarioId exists in the storage, and if not notify for update ?
107
- if (!existingScenarioGeneratedVersion) {
108
- return trackingScenarioId;
109
- }
110
- const scenario = (await consoleApi.fetchTrackingScenario({ scenarioId: trackingScenarioId })).data[0];
111
- if ((scenario.version || 0) > existingScenarioGeneratedVersion) {
112
- updates[trackingScenarioId] = {
113
- previous: existingScenarioGeneratedVersion,
114
- next: scenario.version
115
- };
116
- }
117
- return trackingScenarioId;
118
- }));
119
- return updates;
120
- }
121
- exports.checkScenarioUpdates = checkScenarioUpdates;
@@ -1,53 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TRACKER_LANGUAGES = exports.DEFAULT_LOCK = exports.DEFAULT_FILENAME = exports.DEFAULT_STORAGE_FILENAME = exports.DEFAULT_CONFIG_NAME = void 0;
4
- exports.DEFAULT_CONFIG_NAME = 'snowtype.config.json';
5
- exports.DEFAULT_STORAGE_FILENAME = '.snowtype-lock.json';
6
- exports.DEFAULT_FILENAME = 'snowplow';
7
- exports.DEFAULT_LOCK = '.snowtype.json';
8
- exports.TRACKER_LANGUAGES = {
9
- '@snowplow/javascript-tracker': [
10
- {
11
- value: 'javascript',
12
- name: 'JavaScript'
13
- }
14
- ],
15
- '@snowplow/browser-tracker': [
16
- {
17
- value: 'javascript',
18
- name: 'JavaScript'
19
- },
20
- {
21
- value: 'typescript',
22
- name: 'TypeScript'
23
- }
24
- ],
25
- '@snowplow/node-tracker': [
26
- {
27
- value: 'javascript',
28
- name: 'JavaScript'
29
- },
30
- {
31
- value: 'typescript',
32
- name: 'TypeScript'
33
- }
34
- ],
35
- 'snowplow-android-tracker': [
36
- {
37
- value: 'kotlin',
38
- name: 'Kotlin'
39
- }
40
- ],
41
- 'snowplow-ios-tracker': [
42
- {
43
- value: 'swift',
44
- name: 'Swift'
45
- }
46
- ],
47
- 'snowplow-golang-tracker': [
48
- {
49
- value: 'go',
50
- name: 'Go'
51
- }
52
- ]
53
- };
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.act = void 0;
4
- const logger_1 = require("../logger");
5
- const utils_1 = require("./utils");
6
- function act(action) {
7
- return (options, cmd) => {
8
- const globalOptions = (0, utils_1.getGlobalOptions)(cmd);
9
- const logging = (0, logger_1.createLogger)();
10
- const spinner = (0, utils_1.createSpinner)();
11
- const logger = logging.withVerbosity(globalOptions.verbose);
12
- const apiKey = globalOptions.apiKey;
13
- return action(apiKey, logger, spinner, options)
14
- .then(() => stopSpinner(spinner))
15
- .catch((err) => {
16
- errHandle(err, logger, spinner);
17
- });
18
- };
19
- }
20
- exports.act = act;
21
- function errHandle(err, logger, spinner) {
22
- stopSpinner(spinner);
23
- logger.error(err);
24
- throw err;
25
- }
26
- function stopSpinner(spinner) {
27
- if (spinner.isSpinning() === true) {
28
- spinner.stop(true);
29
- }
30
- }
@@ -1,59 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateAction = exports.execGenerate = void 0;
4
- const process_1 = require("process");
5
- const path_1 = require("path");
6
- const fs_1 = require("fs");
7
- const mkdirp_1 = require("mkdirp");
8
- const snowtype_core_1 = require("@snowplow/snowtype-core");
9
- const api_1 = require("../../api");
10
- const errors_1 = require("../../errors");
11
- const constants_1 = require("./constants");
12
- const utils_1 = require("./utils");
13
- const dispatch_1 = require("./dispatch");
14
- function checkGenerateOptions(logger) {
15
- logger.debug("Checking for existing configuration..");
16
- (0, utils_1.guardConfigExistsValid)();
17
- logger.success("Valid configuration found");
18
- }
19
- async function execGenerate(apiKey, logger, spinner) {
20
- checkGenerateOptions(logger);
21
- spinner.setSpinnerTitle("Generating...");
22
- if (spinner.isSpinning() === false) {
23
- spinner.start();
24
- }
25
- const config = (0, utils_1.getConfig)();
26
- if ((0, utils_1.isEmpty)(config.dataStructures) &&
27
- (0, utils_1.isEmpty)(config.igluCentralSchemas) &&
28
- (0, utils_1.isEmpty)(config.trackingScenarioIds)) {
29
- logger.warn("Data Structures, Schemas and Tracking Scenarios are empty.");
30
- }
31
- const consoleApi = new api_1.ConsoleAPI({
32
- apiKey,
33
- organizationId: config.organizationId,
34
- });
35
- // we could also throw properly in api
36
- const isAuthenticated = await consoleApi.authenticate();
37
- if (!isAuthenticated) {
38
- throw new errors_1.SnowtypeAuthError();
39
- }
40
- const dataProducts = await (0, utils_1.mkDataProducts)(consoleApi, config.trackingScenarioIds);
41
- const igluSchemas = await (0, utils_1.mkIgluSchemas)(config.igluCentralSchemas);
42
- const dataStructures = await (0, utils_1.mkDataStructures)(consoleApi, config.dataStructures);
43
- const genCfg = {
44
- language: config.language,
45
- tracker: config.tracker,
46
- schemas: [...igluSchemas, ...dataStructures],
47
- dataProducts,
48
- };
49
- const generationOutput = await (0, snowtype_core_1.generate)(genCfg);
50
- logger.success("Code generated, writing...");
51
- const output = generationOutput?.lines.join("\n");
52
- const trueDir = (0, path_1.resolve)((0, process_1.cwd)(), config.outpath);
53
- mkdirp_1.mkdirp.sync(trueDir);
54
- const trueFile = constants_1.DEFAULT_FILENAME + (0, utils_1.getExtension)(config.language);
55
- (0, fs_1.writeFileSync)((0, path_1.resolve)(trueDir, trueFile), output);
56
- (0, utils_1.writeLockFile)(dataProducts, config.outpath);
57
- }
58
- exports.execGenerate = execGenerate;
59
- exports.generateAction = (0, dispatch_1.act)(execGenerate);
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.patchAction = exports.generateAction = exports.updateAction = exports.initAction = void 0;
4
- var init_1 = require("./init");
5
- Object.defineProperty(exports, "initAction", { enumerable: true, get: function () { return init_1.initAction; } });
6
- var update_1 = require("./update");
7
- Object.defineProperty(exports, "updateAction", { enumerable: true, get: function () { return update_1.updateAction; } });
8
- var generate_1 = require("./generate");
9
- Object.defineProperty(exports, "generateAction", { enumerable: true, get: function () { return generate_1.generateAction; } });
10
- var patch_1 = require("./patch");
11
- Object.defineProperty(exports, "patchAction", { enumerable: true, get: function () { return patch_1.patchAction; } });