Skip to content

Commit

Permalink
feat: added Segment track for CLI scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Sep 11, 2023
1 parent 4b3e0b0 commit c35e0c5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
3 changes: 3 additions & 0 deletions bin/paragon-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const themeCommand = require('../lib/install-theme');
const buildTokensCommand = require('../lib/build-tokens');
const replaceVariablesCommand = require('../lib/replace-variables');
const buildScssCommand = require('../lib/build-scss');
const { sendTrackInfo } = require('../utils');

// command: executor function
const COMMANDS = {
Expand All @@ -17,6 +18,8 @@ const COMMANDS = {
const [command, ...commandArgs] = process.argv.slice(2);
const executor = COMMANDS[command];

sendTrackInfo(command, 'trackCLICommands');

if (!executor) {
// eslint-disable-next-line no-console
console.log(chalk.red.bold('Unknown command. Usage: paragon <command>'));
Expand Down
4 changes: 2 additions & 2 deletions component-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ const path = require('path');
const { COMPONENT_FILES } = require('./constants');
const {
validateComponentName,
sendTrackInfo,
createFile,
addComponentToExports,
addComponentToGit,
} = require('./utils');
const { sendTrackInfo } = require('../utils');

program
.argument('<ComponentName>', 'Component must have a name', validateComponentName)
.action((componentName) => {
// send data to analytics
sendTrackInfo(componentName);
sendTrackInfo(componentName, 'trackGenerateComponent');
const componentDir = path.resolve(__dirname, `../src/${componentName}`);
// create directory for the component files
fs.mkdirSync(componentDir);
Expand Down
20 changes: 0 additions & 20 deletions component-generator/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,6 @@ function validateComponentName(value) {
return value;
}

/**
* Sends request to the Netlify function to inform about generate-component usage.
* @param {string} componentName - component name
*/
function sendTrackInfo(componentName) {
const { BASE_URL, TRACK_ANONYMOUS_ANALYTICS } = process.env;
if (TRACK_ANONYMOUS_ANALYTICS) {
const url = `${BASE_URL}/.netlify/functions/trackGenerateComponent`;
axios.post(url, { componentName })
.then(result => {
// eslint-disable-next-line no-console
console.log(`Track info is successfully sent (status ${result.status})`);
}).catch(error => {
// eslint-disable-next-line no-console
console.log(`Track info request failed (${error})`);
});
}
}

/**
* Creates a file for the component based on the template.
* Note that 'componentName' string is a reserved placeholder,
Expand Down Expand Up @@ -97,7 +78,6 @@ function addComponentToGit(componentName) {
}

exports.validateComponentName = validateComponentName;
exports.sendTrackInfo = sendTrackInfo;
exports.createFile = createFile;
exports.addComponentToExports = addComponentToExports;
exports.addComponentToGit = addComponentToGit;
22 changes: 22 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const axios = require('axios');

Check failure on line 1 in utils.js

View workflow job for this annotation

GitHub Actions / tests

'axios' should be listed in the project's dependencies, not devDependencies

/**
* Sends request to the Netlify function to inform about specified event.
* @param {string} eventName - tracking event name
*/
function sendTrackInfo(eventName, trackFunctionName) {
const { BASE_URL, TRACK_ANONYMOUS_ANALYTICS } = process.env;
if (TRACK_ANONYMOUS_ANALYTICS) {
const url = `${BASE_URL}/.netlify/functions/${trackFunctionName}`;
axios.post(url, { eventName })
.then(result => {
// eslint-disable-next-line no-console
console.log(`Track info is successfully sent (status ${result.status})`);
}).catch(error => {
// eslint-disable-next-line no-console
console.log(`Track info request failed (${error})`);
});
}
}

module.exports = { sendTrackInfo };

0 comments on commit c35e0c5

Please sign in to comment.