[RUN ALL RSPEC] First run for Snowplow event dictionary backend entries
Related to #329554 (closed)
This covers all Snowplow events emitted from backend until date. Any missing event will be added on a follow-up pass.
Generator used
/* eslint-disable import/no-commonjs */
/* eslint-disable no-use-before-define */
/* eslint-disable no-restricted-syntax */
const fs = require('fs');
const csv = require('csv-parser');
const SOURCE_FILE_PATH = './events.csv';
const FOLDER_PATH = './event_dictionary_generator_output';
const ARRAY_SEPARATOR = ';';
const baseTemplate = `description:
category:
action:
label_description:
property_description:
value_description:
extra_properties:
identifiers:
product_section:
product_stage:
product_group:
product_category:
milestone:
introduced_by_url:
distributions:
tiers:
`;
createFolderIfNeeded();
iterateRows();
function createFolderIfNeeded() {
if (!fs.existsSync(FOLDER_PATH)) {
fs.mkdirSync(FOLDER_PATH);
}
}
function iterateRows() {
fs.createReadStream(SOURCE_FILE_PATH).pipe(csv()).on('data', createFileFromRow);
}
function createFileFromRow(row) {
let template = String(baseTemplate);
for (const [name, value] of Object.entries(row)) {
const label = `${name}:`;
const isArray = value.includes(ARRAY_SEPARATOR) || ['distributions', 'tiers'].includes(name);
if (isArray) {
const items = value.split(ARRAY_SEPARATOR).join('\n- ');
template = template.replace(label, `${label}\n- ${items}`);
} else {
template = template.replace(label, `${label} ${value}`);
}
}
// Create file
// Custom slug logic attempt
const filename = `${row.category}_${row.action}`
.replace(/:/g, '_')
.replace(/\s+/g, '_')
.replace('API', 'api')
.replace(/[A-Z]/g, (letter, offset) => (offset > 0 ? `_${letter.toLowerCase()}` : letter))
.toLowerCase()
.replace(/_{3,}/, '__');
const path = `${FOLDER_PATH}/${filename}.yml`;
fs.writeFileSync(path, template);
console.info(path, 'created');
}
Edited by Mikołaj Wawrzyniak