diff --git a/src/bin/postinstall.js b/src/bin/postinstall.js index 65154ab4..daf52818 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 == 'true'; + // Calculate the SHA-256 hash of the installation directory const installationDirectory = path.join(process.cwd(), '../..'); const hash = crypto.createHash('sha256'); @@ -19,11 +22,11 @@ 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.'); + // logging the error message + console.log(err); } -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 +37,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.'); +} 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" ], }; diff --git a/src/templates/jest-global-setup.js b/src/templates/jest-global-setup.js index b69a1984..1d6c5b13 100644 --- a/src/templates/jest-global-setup.js +++ b/src/templates/jest-global-setup.js @@ -1,11 +1,5 @@ module.exports = async () => { - - // function that sets up Mongo to be used in tests - // global.setUpDb = () => {}; - - // 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) => {}; + // Check if the developer has opted out of telemetry and set the environment variable accordingly + const optOutTelemetry = process.env.PYTHAGORA_TELEMETRY_ENABLED === 'false'; + console.log(`Telemetry is ${optOutTelemetry ? 'disabled' : 'enabled'}`); };