From 7d15f8adb5e402b3aecc30d6e6f3bfde9a4dbd39 Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:21:36 +0530 Subject: [PATCH 1/6] Update jest-global-setup.js --- src/templates/jest-global-setup.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/templates/jest-global-setup.js b/src/templates/jest-global-setup.js index b69a1984..a2cc70d7 100644 --- a/src/templates/jest-global-setup.js +++ b/src/templates/jest-global-setup.js @@ -1,11 +1,10 @@ module.exports = async () => { + // Check if the developer has opted out of telemetry and set the environment variable accordingly + const optOutTelemetry = process.env.PYTHAGORA_TELEMETRY_ENABLED === 'false'; - // function that sets up Mongo to be used in tests - // global.setUpDb = () => {}; + if (optOutTelemetry) { + process.env.PYTHAGORA_TELEMETRY_ENABLED = 'false'; + } - // function that cleans up the database after tests are done - // global.cleanUpDb = () => {}; - - // function that returns a Mongo collection so that tests can query the database - // global.getMongoCollection = (collection) => {}; + console.log(`Telemetry is ${optOutTelemetry ? 'disabled' : 'enabled'}`); }; From 92a1c128f181dc2744647c8d7c7023499d25116f Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:22:17 +0530 Subject: [PATCH 2/6] Update jest-config.js --- src/templates/jest-config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/templates/jest-config.js b/src/templates/jest-config.js index d20156c5..301ae1d4 100644 --- a/src/templates/jest-config.js +++ b/src/templates/jest-config.js @@ -1,7 +1,7 @@ module.exports = { testEnvironment: "node", - globalSetup: './pythagora_tests/exported_tests/global-setup.js', + globalSetup: './src/templates/jest-global-setup.js', roots: [ - "/pythagora_tests/exported_tests" + "/pythagora_tests/unit/src" ], }; From 1cefbff1819f67df78979a52020a04a21fd2e1e2 Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Fri, 20 Oct 2023 09:23:32 +0530 Subject: [PATCH 3/6] Update postinstall.js --- src/bin/postinstall.js | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/src/bin/postinstall.js b/src/bin/postinstall.js index 65154ab4..38f65e1d 100644 --- a/src/bin/postinstall.js +++ b/src/bin/postinstall.js @@ -3,12 +3,15 @@ const fs = require('fs'); const crypto = require('crypto'); const path = require('path'); const { v4: uuidv4 } = require('uuid'); -const {PYTHAGORA_METADATA_DIR, CONFIG_FILENAME, PYTHAGORA_API_SERVER} = require("@pythagora.io/js-code-processing").common; +const { PYTHAGORA_METADATA_DIR, CONFIG_FILENAME, PYTHAGORA_API_SERVER } = require("@pythagora.io/js-code-processing").common; const packageJson = require('../../package.json'); const pythagoraVersion = packageJson.version; const configPath = path.join(process.cwd(), '../..', PYTHAGORA_METADATA_DIR, CONFIG_FILENAME); +// Check if telemetry is enabled using an environment variable +const telemetryEnabled = process.env.PYTHAGORA_TELEMETRY_ENABLED !== 'false'; + // Calculate the SHA-256 hash of the installation directory const installationDirectory = path.join(process.cwd(), '../..'); const hash = crypto.createHash('sha256'); @@ -19,11 +22,10 @@ let config; try { config = JSON.parse(fs.readFileSync(configPath)); } catch (err) { - // Config file doesn't exist or is not valid JSON + console.error('Config file does not exist or is not valid JSON. Creating a new one.'); + config = {}; } -if (!config) config = {}; - // If there's no userId, generate one and save it to the config file if (!config.userId) { config.userId = uuidv4(); @@ -34,18 +36,22 @@ if (!config.userId) { fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); } -// Send the request to the telemetry endpoint -const telemetryData = { - userId: config.userId, - pathId, - event: 'install', - pythagoraVersion -}; +// Send telemetry data only if telemetry is enabled +if (telemetryEnabled) { + const telemetryData = { + userId: config.userId, + pathId, + event: 'install', + pythagoraVersion + }; -axios.post(`${PYTHAGORA_API_SERVER}/telemetry`, telemetryData) - .then((res) => { - console.log('Telemetry data sent successfully'); - }) - .catch((err) => { - console.error(`Failed to send telemetry data: ${err.message}`); - }); + axios.post(`${PYTHAGORA_API_SERVER}/telemetry`, telemetryData) + .then((res) => { + console.log('Telemetry data sent successfully'); + }) + .catch((err) => { + console.error(`Failed to send telemetry data: ${err.message}`); + }); +} else { + console.log('Telemetry is disabled. Data will not be sent.'); +} From cbe70494ebe0d8edfb23cb514c955bcd14acc0e0 Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:08:24 +0530 Subject: [PATCH 4/6] Update postinstall.js --- src/bin/postinstall.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/postinstall.js b/src/bin/postinstall.js index 38f65e1d..8790f201 100644 --- a/src/bin/postinstall.js +++ b/src/bin/postinstall.js @@ -10,7 +10,7 @@ const pythagoraVersion = packageJson.version; const configPath = path.join(process.cwd(), '../..', PYTHAGORA_METADATA_DIR, CONFIG_FILENAME); // Check if telemetry is enabled using an environment variable -const telemetryEnabled = process.env.PYTHAGORA_TELEMETRY_ENABLED !== 'false'; +const telemetryEnabled = process.env.PYTHAGORA_TELEMETRY_ENABLED == 'true'; // Calculate the SHA-256 hash of the installation directory const installationDirectory = path.join(process.cwd(), '../..'); From 5f6da14c932ad501b08d2de110d194d1251b58fd Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:10:26 +0530 Subject: [PATCH 5/6] logging the error message in postinstall.js --- src/bin/postinstall.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/bin/postinstall.js b/src/bin/postinstall.js index 8790f201..daf52818 100644 --- a/src/bin/postinstall.js +++ b/src/bin/postinstall.js @@ -23,7 +23,8 @@ try { config = JSON.parse(fs.readFileSync(configPath)); } catch (err) { console.error('Config file does not exist or is not valid JSON. Creating a new one.'); - config = {}; + // logging the error message + console.log(err); } // If there's no userId, generate one and save it to the config file From eee35d4cc0263372c94ac9578729b3559bf99a5d Mon Sep 17 00:00:00 2001 From: Adarsh Jha <132337675+adarsh-jha-dev@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:23:35 +0530 Subject: [PATCH 6/6] Remove unnecessary if block in jest-global-setup.js --- src/templates/jest-global-setup.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/templates/jest-global-setup.js b/src/templates/jest-global-setup.js index a2cc70d7..1d6c5b13 100644 --- a/src/templates/jest-global-setup.js +++ b/src/templates/jest-global-setup.js @@ -1,10 +1,5 @@ module.exports = async () => { // Check if the developer has opted out of telemetry and set the environment variable accordingly const optOutTelemetry = process.env.PYTHAGORA_TELEMETRY_ENABLED === 'false'; - - if (optOutTelemetry) { - process.env.PYTHAGORA_TELEMETRY_ENABLED = 'false'; - } - console.log(`Telemetry is ${optOutTelemetry ? 'disabled' : 'enabled'}`); };