@shopify/slate-env
Advanced tools
Comparing version 1.0.0-beta.7 to 1.0.0-beta.8
@@ -1,2 +0,1 @@ | ||
/* eslint-disable no-process-env */ | ||
const fs = require('fs'); | ||
@@ -6,14 +5,19 @@ const path = require('path'); | ||
const dotenv = require('dotenv'); | ||
const SlateConfig = require('@shopify/slate-config'); | ||
const slateEnv = require('../index'); | ||
const config = require('../slate-env.config'); | ||
const config = new SlateConfig(require('../slate-env.schema')); | ||
const envPath = path.resolve(config.envRootDir, config.envDefaultFileName); | ||
const envPath = path.resolve( | ||
config.get('env.rootDirectory'), | ||
config.get('env.basename'), | ||
); | ||
const TEST_ENV = { | ||
[config.envNameVar]: 'production', | ||
[config.envStoreVar]: 'test-shop.myshopify.com', | ||
[config.envPasswordVar]: '123456789', | ||
[config.envThemeIdVar]: '987654321', | ||
[config.envIgnoreFilesVar]: 'config/settings_data.json', | ||
[config.envUserEmail]: 'test@email.com', | ||
[config.get('env.keys.name')]: 'production', | ||
[config.get('env.keys.store')]: 'test-shop.myshopify.com', | ||
[config.get('env.keys.password')]: '123456789', | ||
[config.get('env.keys.themeId')]: '987654321', | ||
[config.get('env.keys.ignoreFiles')]: 'config/settings_data.json', | ||
[config.get('env.keys.userEmail')]: 'test@email.com', | ||
}; | ||
@@ -72,6 +76,6 @@ | ||
const emptyTestVars = { | ||
[config.envStoreVar]: '', | ||
[config.envPasswordVar]: '', | ||
[config.envThemeIdVar]: '', | ||
[config.envIgnoreFilesVar]: '', | ||
[config.get('env.keys.store')]: '', | ||
[config.get('env.keys.password')]: '', | ||
[config.get('env.keys.themeId')]: '', | ||
[config.get('env.keys.ignoreFiles')]: '', | ||
}; | ||
@@ -105,6 +109,6 @@ | ||
expect(envParsed).toEqual({ | ||
[config.envStoreVar]: '', | ||
[config.envPasswordVar]: '', | ||
[config.envThemeIdVar]: '', | ||
[config.envIgnoreFilesVar]: '', | ||
[config.get('env.keys.store')]: '', | ||
[config.get('env.keys.password')]: '', | ||
[config.get('env.keys.themeId')]: '', | ||
[config.get('env.keys.ignoreFiles')]: '', | ||
}); | ||
@@ -117,3 +121,3 @@ }); | ||
env[config.envStoreVar] = store; | ||
env[config.get('env.keys.store')] = store; | ||
slateEnv.create({values: env}); | ||
@@ -123,3 +127,3 @@ | ||
expect(envParsed).toHaveProperty(config.envStoreVar, store); | ||
expect(envParsed).toHaveProperty(config.get('env.keys.store'), store); | ||
}); | ||
@@ -133,3 +137,3 @@ | ||
env[config.envStoreVar] = store; | ||
env[config.get('env.keys.store')] = store; | ||
env[invalidKey] = invalidValue; | ||
@@ -140,3 +144,3 @@ slateEnv.create({values: env}); | ||
expect(envParsed).toHaveProperty(config.envStoreVar, store); | ||
expect(envParsed).toHaveProperty(config.get('env.keys.store'), store); | ||
expect(envParsed).not.toHaveProperty(invalidKey, invalidValue); | ||
@@ -178,4 +182,4 @@ }); | ||
slateEnv.assign(); | ||
expect(process.env[config.envStoreVar]).toBe( | ||
TEST_ENV[config.envStoreVar], | ||
expect(process.env[config.get('env.keys.store')]).toBe( | ||
TEST_ENV[config.get('env.keys.store')], | ||
); | ||
@@ -186,4 +190,4 @@ }); | ||
slateEnv.assign('production'); | ||
expect(process.env[config.envStoreVar]).toBe( | ||
TEST_ENV[config.envStoreVar], | ||
expect(process.env[config.get('env.keys.store')]).toBe( | ||
TEST_ENV[config.get('env.keys.store')], | ||
); | ||
@@ -194,6 +198,6 @@ }); | ||
const store = 'other-value.myshopify.com'; | ||
process.env[config.envStoreVar] = store; | ||
process.env[config.get('env.keys.store')] = store; | ||
slateEnv.assign(); | ||
expect(process.env[config.envStoreVar]).toBe(store); | ||
expect(process.env[config.get('env.keys.store')]).toBe(store); | ||
}); | ||
@@ -219,14 +223,18 @@ | ||
describe('if a env name is not specified', () => { | ||
test(`returns the name '${ | ||
config.envDefaultEnvName | ||
}' if the default env file is present`, () => { | ||
test(`returns the name '${config.get( | ||
'env.defaultEnvName', | ||
)}' if the default env file is present`, () => { | ||
slateEnv.create(TEST_ENV); | ||
slateEnv.assign(); | ||
expect(slateEnv.getEnvNameValue()).toBe(config.envDefaultEnvName); | ||
expect(slateEnv.getEnvNameValue()).toBe( | ||
config.get('env.defaultEnvName'), | ||
); | ||
}); | ||
test(`returns the name '${ | ||
config.envExternalEnvName | ||
}' if the default env file is not present`, () => { | ||
test(`returns the name '${config.get( | ||
'env.externalEnvName', | ||
)}' if the default env file is not present`, () => { | ||
slateEnv.assign(); | ||
expect(slateEnv.getEnvNameValue()).toBe(config.envExternalEnvName); | ||
expect(slateEnv.getEnvNameValue()).toBe( | ||
config.get('env.externalEnvName'), | ||
); | ||
}); | ||
@@ -238,4 +246,7 @@ }); | ||
test('returns the value of the environment variable that references the store URL', () => { | ||
process.env[config.envStoreVar] = TEST_ENV[config.envStoreVar]; | ||
expect(slateEnv.getStoreValue()).toBe(TEST_ENV[config.envStoreVar]); | ||
process.env[config.get('env.keys.store')] = | ||
TEST_ENV[config.get('env.keys.store')]; | ||
expect(slateEnv.getStoreValue()).toBe( | ||
TEST_ENV[config.get('env.keys.store')], | ||
); | ||
}); | ||
@@ -250,4 +261,7 @@ | ||
test('returns the value of the environment variable that references the store API password', () => { | ||
process.env[config.envPasswordVar] = TEST_ENV[config.envPasswordVar]; | ||
expect(slateEnv.getPasswordValue()).toBe(TEST_ENV[config.envPasswordVar]); | ||
process.env[config.get('env.keys.password')] = | ||
TEST_ENV[config.get('env.keys.password')]; | ||
expect(slateEnv.getPasswordValue()).toBe( | ||
TEST_ENV[config.get('env.keys.password')], | ||
); | ||
}); | ||
@@ -262,4 +276,7 @@ | ||
test('returns the value of the environment variable that references the store theme ID', () => { | ||
process.env[config.envThemeIdVar] = TEST_ENV[config.envThemeIdVar]; | ||
expect(slateEnv.getThemeIdValue()).toBe(TEST_ENV[config.envThemeIdVar]); | ||
process.env[config.get('env.keys.themeId')] = | ||
TEST_ENV[config.get('env.keys.themeId')]; | ||
expect(slateEnv.getThemeIdValue()).toBe( | ||
TEST_ENV[config.get('env.keys.themeId')], | ||
); | ||
}); | ||
@@ -274,6 +291,6 @@ | ||
test('returns the value of the environment variable that references a list of files to ignore', () => { | ||
process.env[config.envIgnoreFilesVar] = | ||
TEST_ENV[config.envIgnoreFilesVar]; | ||
process.env[config.get('env.keys.ignoreFiles')] = | ||
TEST_ENV[config.get('env.keys.ignoreFiles')]; | ||
expect(slateEnv.getIgnoreFilesValue()).toBe( | ||
TEST_ENV[config.envIgnoreFilesVar], | ||
TEST_ENV[config.get('env.keys.ignoreFiles')], | ||
); | ||
@@ -308,3 +325,3 @@ }); | ||
Object.assign({}, TEST_ENV, { | ||
[config.envStoreVar]: '', | ||
[config.get('env.keys.store')]: '', | ||
}), | ||
@@ -321,3 +338,3 @@ ); | ||
Object.assign({}, TEST_ENV, { | ||
[config.envStoreVar]: value, | ||
[config.get('env.keys.store')]: value, | ||
}), | ||
@@ -334,3 +351,3 @@ ); | ||
Object.assign({}, TEST_ENV, { | ||
[config.envPasswordVar]: '', | ||
[config.get('env.keys.password')]: '', | ||
}), | ||
@@ -345,3 +362,3 @@ ); | ||
Object.assign({}, TEST_ENV, { | ||
[config.envPasswordVar]: '8h1j-dnjn8', | ||
[config.get('env.keys.password')]: '8h1j-dnjn8', | ||
}), | ||
@@ -356,3 +373,3 @@ ); | ||
Object.assign({}, TEST_ENV, { | ||
[config.envThemeIdVar]: '', | ||
[config.get('env.keys.themeId')]: '', | ||
}), | ||
@@ -367,3 +384,3 @@ ); | ||
Object.assign({}, TEST_ENV, { | ||
[config.envThemeIdVar]: 'ds7dsh8d', | ||
[config.get('env.keys.themeId')]: 'ds7dsh8d', | ||
}), | ||
@@ -370,0 +387,0 @@ ); |
98
index.js
@@ -1,22 +0,21 @@ | ||
/* eslint-disable no-process-env */ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const dotenv = require('dotenv'); | ||
const config = require('./slate-env.config'); | ||
const SlateConfig = require('@shopify/slate-config'); | ||
const config = new SlateConfig(require('./slate-env.schema')); | ||
const SLATE_ENV_VARS = [ | ||
config.envNameVar, | ||
config.envStoreVar, | ||
config.envPasswordVar, | ||
config.envThemeIdVar, | ||
config.envIgnoreFilesVar, | ||
config.envUserEmail, | ||
config.get('env.keys.name'), | ||
config.get('env.keys.store'), | ||
config.get('env.keys.password'), | ||
config.get('env.keys.themeId'), | ||
config.get('env.keys.ignoreFiles'), | ||
config.get('env.keys.userEmail'), | ||
]; | ||
const DEFAULT_ENV_VARS = [ | ||
config.envStoreVar, | ||
config.envPasswordVar, | ||
config.envThemeIdVar, | ||
config.envIgnoreFilesVar, | ||
config.get('env.keys.store'), | ||
config.get('env.keys.password'), | ||
config.get('env.keys.themeId'), | ||
config.get('env.keys.ignoreFiles'), | ||
]; | ||
@@ -27,3 +26,6 @@ | ||
const envName = _getFileName(name); | ||
const envPath = path.resolve(root || config.envRootDir, envName); | ||
const envPath = path.resolve( | ||
root || config.get('env.rootDirectory'), | ||
envName, | ||
); | ||
const envContents = _getFileContents(values); | ||
@@ -37,6 +39,6 @@ | ||
if (typeof name === 'undefined' || name.trim() === '') { | ||
return config.envDefaultFileName; | ||
return config.get('env.basename'); | ||
} | ||
return `${config.envDefaultFileName}.${name}`; | ||
return `${config.get('env.basename')}.${name}`; | ||
} | ||
@@ -56,19 +58,3 @@ | ||
.map((keyValues) => { | ||
const envVar = keyValues[0]; | ||
// Search through config for the key which has a value of the env variable | ||
// e.g. find the key which has the value of 'SLATE_STORE' | ||
for (const key in config) { | ||
if (config.hasOwnProperty(key) && config[key] === envVar) { | ||
// Once we find the key, we can search the config.__schema for the | ||
// schema item. We need to schema item so we can print the description | ||
// comment. | ||
const schemaItem = config.__schema.items.find( | ||
(item) => item.id === key, | ||
); | ||
return `# ${schemaItem.description || ''} \r\n${keyValues.join('=')}`; | ||
} | ||
} | ||
return true; | ||
return `${keyValues.join('=')}\r\n`; | ||
}) | ||
@@ -81,3 +67,3 @@ .join('\r\n\r\n'); | ||
const envFileName = _getFileName(name); | ||
const envPath = path.resolve(config.envRootDir, envFileName); | ||
const envPath = path.resolve(config.get('env.rootDirectory'), envFileName); | ||
const result = dotenv.config({path: envPath}); | ||
@@ -95,13 +81,13 @@ | ||
const envFileName = _getFileName(name); | ||
const envPath = path.resolve(config.envRootDir, envFileName); | ||
const envPath = path.resolve(config.get('env.rootDirectory'), envFileName); | ||
if (typeof name === 'undefined') { | ||
if (fs.existsSync(envPath)) { | ||
envName = config.envDefaultEnvName; | ||
envName = config.get('env.defaultEnvName'); | ||
} else { | ||
envName = config.envExternalEnvName; | ||
envName = config.get('env.externalEnvName'); | ||
} | ||
} | ||
process.env[config.envNameVar] = envName; | ||
process.env[config.get('env.keys.name')] = envName; | ||
} | ||
@@ -128,3 +114,3 @@ | ||
if (store.length === 0) { | ||
errors.push(new Error(`${config.envStoreVar} must not be empty`)); | ||
errors.push(new Error(`${config.get('env.keys.store')} must not be empty`)); | ||
} else if ( | ||
@@ -135,3 +121,5 @@ store.indexOf('.myshopify.com') < 1 && | ||
errors.push( | ||
new Error(`${config.envStoreVar} must be a valid .myshopify.com URL`), | ||
new Error( | ||
`${config.get('env.keys.store')} must be a valid .myshopify.com URL`, | ||
), | ||
); | ||
@@ -148,7 +136,11 @@ } | ||
if (password.length === 0) { | ||
errors.push(new Error(`${config.envPasswordVar} must not be empty`)); | ||
errors.push( | ||
new Error(`${config.get('env.keys.password')} must not be empty`), | ||
); | ||
} else if (!/^\w+$/.test(password)) { | ||
errors.push( | ||
new Error( | ||
`${config.envPasswordVar} can only contain numbers and letters`, | ||
`${config.get( | ||
'env.keys.password', | ||
)} can only contain numbers and letters`, | ||
), | ||
@@ -166,9 +158,11 @@ ); | ||
if (themeId.length === 0) { | ||
errors.push(new Error(`${config.envThemeIdVar} must not be empty`)); | ||
errors.push( | ||
new Error(`${config.get('env.keys.themeId')} must not be empty`), | ||
); | ||
} else if (themeId !== 'live' && !/^\d+$/.test(themeId)) { | ||
errors.push( | ||
new Error( | ||
`${ | ||
config.envThemeIdVar | ||
} can be set to 'live' or a valid theme ID containing only numbers`, | ||
`${config.get( | ||
'env.keys.themeId', | ||
)} can be set to 'live' or a valid theme ID containing only numbers`, | ||
), | ||
@@ -219,3 +213,3 @@ ); | ||
function getEnvNameValue() { | ||
return process.env[config.envNameVar]; | ||
return process.env[config.get('env.keys.name')]; | ||
} | ||
@@ -225,3 +219,3 @@ | ||
function getStoreValue() { | ||
const value = process.env[config.envStoreVar]; | ||
const value = process.env[config.get('env.keys.store')]; | ||
return typeof value === 'undefined' ? '' : value; | ||
@@ -231,3 +225,3 @@ } | ||
function getPasswordValue() { | ||
const value = process.env[config.envPasswordVar]; | ||
const value = process.env[config.get('env.keys.password')]; | ||
return typeof value === 'undefined' ? '' : value; | ||
@@ -237,3 +231,3 @@ } | ||
function getThemeIdValue() { | ||
const value = process.env[config.envThemeIdVar]; | ||
const value = process.env[config.get('env.keys.themeId')]; | ||
return typeof value === 'undefined' ? '' : value; | ||
@@ -243,3 +237,3 @@ } | ||
function getIgnoreFilesValue() { | ||
const value = process.env[config.envIgnoreFilesVar]; | ||
const value = process.env[config.get('env.keys.ignoreFiles')]; | ||
return typeof value === 'undefined' ? '' : value; | ||
@@ -249,3 +243,3 @@ } | ||
function getUserEmail() { | ||
const value = process.env[config.envUserEmail]; | ||
const value = process.env[config.get('env.keys.userEmail')]; | ||
return typeof value === 'undefined' ? '' : value; | ||
@@ -252,0 +246,0 @@ } |
{ | ||
"name": "@shopify/slate-env", | ||
"version": "1.0.0-beta.7", | ||
"version": "1.0.0-beta.8", | ||
"description": "Manages the environment variables which are needed by Slate to interact with Shopify servers.", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"@shopify/slate-config": "1.0.0-beta.7", | ||
"@shopify/slate-config": "1.0.0-beta.8", | ||
"dotenv": "^4.0.0" | ||
@@ -17,0 +17,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
22397
554
+ Added@shopify/slate-config@1.0.0-beta.8(transitive)
- Removed@shopify/slate-config@1.0.0-beta.7(transitive)