From c35e0c5f957d94000b8afdc703ed32727b107a64 Mon Sep 17 00:00:00 2001 From: PKulkoRaccoonGang Date: Mon, 11 Sep 2023 18:12:33 +0300 Subject: [PATCH] feat: added Segment track for CLI scripts --- bin/paragon-scripts.js | 3 +++ component-generator/index.js | 4 ++-- component-generator/utils.js | 20 -------------------- utils.js | 22 ++++++++++++++++++++++ 4 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 utils.js diff --git a/bin/paragon-scripts.js b/bin/paragon-scripts.js index 8410f3cc5b6..2b0f275af84 100755 --- a/bin/paragon-scripts.js +++ b/bin/paragon-scripts.js @@ -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 = { @@ -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 ')); diff --git a/component-generator/index.js b/component-generator/index.js index f27712a22e3..99db7abb9d8 100644 --- a/component-generator/index.js +++ b/component-generator/index.js @@ -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('', '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); diff --git a/component-generator/utils.js b/component-generator/utils.js index 5d4b4c2585e..c06bbd6cedd 100644 --- a/component-generator/utils.js +++ b/component-generator/utils.js @@ -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, @@ -97,7 +78,6 @@ function addComponentToGit(componentName) { } exports.validateComponentName = validateComponentName; -exports.sendTrackInfo = sendTrackInfo; exports.createFile = createFile; exports.addComponentToExports = addComponentToExports; exports.addComponentToGit = addComponentToGit; diff --git a/utils.js b/utils.js new file mode 100644 index 00000000000..e36e96b2dd1 --- /dev/null +++ b/utils.js @@ -0,0 +1,22 @@ +const axios = require('axios'); + +/** + * 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 };