Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
Merge pull request #592 from Shopify/add-email-env
Browse files Browse the repository at this point in the history
Add SLATE_USER_EMAIL env variable for slate-analytics
  • Loading branch information
t-kelly authored May 25, 2018
2 parents 5d7a382 + eaf942c commit aaaab3b
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 9 deletions.
20 changes: 20 additions & 0 deletions packages/slate-analytics/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-process-env */

jest.mock('../prompt');
jest.mock('axios');

Expand Down Expand Up @@ -100,6 +102,24 @@ describe('init()', () => {
expect(config.trackingVersion).toBe(packageJson.trackingVersion);
});
});

describe('skips prompting new user for tracking consent', () => {
test('if SLATE_USER_EMAIL environment variable is set to a valid email', async () => {
const analytics = require('../index');
const prompt = require('../prompt');
const mock = require('mock-fs');

process.env.SLATE_USER_EMAIL = '[email protected]';

mock();

await analytics.init();

expect(prompt.forNewConsent).not.toHaveBeenCalled();

delete process.env.SLATE_USER_EMAIL;
});
});
});

describe('event()', () => {
Expand Down
13 changes: 11 additions & 2 deletions packages/slate-analytics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const uuidGenerator = require('uuid/v4');
const clearConsole = require('react-dev-utils/clearConsole');
const rc = require('@shopify/slate-rc');
const {getUserEmail} = require('@shopify/slate-env');
const axios = require('axios');
const prompt = require('./prompt');
const {validateEmail} = require('./utils');
const packageJson = require('./package.json');

async function init() {
Expand All @@ -21,8 +23,15 @@ async function init() {
) {
if (typeof config.tracking === 'undefined') {
// If new user
const answers = await prompt.forNewConsent();
config = Object.assign({}, config, answers, {
let email = getUserEmail();

if (!validateEmail(email)) {
const answer = await prompt.forNewConsent();
email = answer.email;
}

config = Object.assign({}, config, {
email,
tracking: true,
trackingVersion: packageJson.trackingVersion,
});
Expand Down
1 change: 1 addition & 0 deletions packages/slate-analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"homepage": "https://github.com/shopify/slate#readme",
"dependencies": {
"@shopify/slate-env": "^1.0.0-beta.1",
"@shopify/slate-error": "^1.0.0-beta.1",
"@shopify/slate-rc": "^1.0.0-beta.1",
"axios": "^0.18.0",
Expand Down
6 changes: 2 additions & 4 deletions packages/slate-analytics/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ const inquirer = require('inquirer');
const clearConsole = require('react-dev-utils/clearConsole');
const wrap = require('word-wrap');
const chalk = require('chalk');
const {validateEmail} = require('./utils');

const question = {
type: 'input',
name: 'email',
message: 'To continue, please enter your email address:',
validate: (input) => {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(input).toLowerCase())
? true
: 'Email not valid. Please try again.';
return validateEmail(input) || 'Email not valid. Please try again.';
},
};

Expand Down
6 changes: 6 additions & 0 deletions packages/slate-analytics/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
validateEmail(input) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(input).toLowerCase());
},
};
14 changes: 14 additions & 0 deletions packages/slate-env/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const TEST_ENV = {
[config.envPasswordVar]: '123456789',
[config.envThemeIdVar]: '987654321',
[config.envIgnoreFilesVar]: 'config/settings_data.json',
[config.envUserEmail]: '[email protected]',
};

function setVars(vars) {
Expand Down Expand Up @@ -64,6 +65,19 @@ describe('Slate Env', () => {
});
});

describe('getDefaultSlateEnv', () => {
test('returns an object which contains the default variables and values of an env file', () => {
const emptyTestVars = {
[config.envStoreVar]: '',
[config.envPasswordVar]: '',
[config.envThemeIdVar]: '',
[config.envIgnoreFilesVar]: '',
};

expect(slateEnv.getDefaultSlateEnv()).toEqual(emptyTestVars);
});
});

describe('getEmptySlateEnv()', () => {
test('returns object containing all env file variables with empty values', () => {
const emptyTestVars = Object.assign({}, TEST_ENV);
Expand Down
29 changes: 26 additions & 3 deletions packages/slate-env/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const SLATE_ENV_VARS = [
config.envPasswordVar,
config.envThemeIdVar,
config.envIgnoreFilesVar,
config.envUserEmail,
];

const DEFAULT_ENV_VARS = [
config.envStoreVar,
config.envPasswordVar,
config.envThemeIdVar,
config.envIgnoreFilesVar,
];

// Creates a new env file with optional name and values
Expand All @@ -33,9 +41,7 @@ function _getFileName(name) {

// Return default list of env variables with their assigned value, if any.
function _getFileContents(values) {
const env = getEmptySlateEnv();

delete env[config.envNameVar];
const env = getDefaultSlateEnv();

for (const key in values) {
if (values.hasOwnProperty(key) && env.hasOwnProperty(key)) {
Expand Down Expand Up @@ -190,6 +196,16 @@ function getEmptySlateEnv() {
return env;
}

function getDefaultSlateEnv() {
const env = {};

DEFAULT_ENV_VARS.forEach((key) => {
env[key] = '';
});

return env;
}

function getEnvNameValue() {
return process.env[config.envNameVar];
}
Expand All @@ -215,16 +231,23 @@ function getIgnoreFilesValue() {
return typeof value === 'undefined' ? '' : value;
}

function getUserEmail() {
const value = process.env[config.envUserEmail];
return typeof value === 'undefined' ? '' : value;
}

module.exports = {
create,
assign,
validate,
clear,
getSlateEnv,
getDefaultSlateEnv,
getEmptySlateEnv,
getEnvNameValue,
getStoreValue,
getPasswordValue,
getThemeIdValue,
getIgnoreFilesValue,
getUserEmail,
};
6 changes: 6 additions & 0 deletions packages/slate-env/slate-env.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,11 @@ module.exports = slateConfig.generate({
"A list of file patterns to ignore, with each list item separated by ':'",
type: 'string',
},
{
id: 'envUserEmail',
default: 'SLATE_USER_EMAIL',
description: 'The email of the user to register for Slate analytics',
type: 'string',
},
],
});

0 comments on commit aaaab3b

Please sign in to comment.