UNPKG

6 kBJavaScriptView Raw
1const fs = require('fs');
2const path = require('path');
3const dotenv = require('dotenv');
4const SlateConfig = require('@shopify/slate-config');
5const config = new SlateConfig(require('./slate-env.schema'));
6
7const SLATE_ENV_VARS = [
8 config.get('env.keys.name'),
9 config.get('env.keys.store'),
10 config.get('env.keys.password'),
11 config.get('env.keys.themeId'),
12 config.get('env.keys.ignoreFiles'),
13 config.get('env.keys.userEmail'),
14];
15
16const DEFAULT_ENV_VARS = [
17 config.get('env.keys.store'),
18 config.get('env.keys.password'),
19 config.get('env.keys.themeId'),
20 config.get('env.keys.ignoreFiles'),
21];
22
23// Creates a new env file with optional name and values
24function create({values, name, root} = {}) {
25 const envName = _getFileName(name);
26 const envPath = path.resolve(
27 root || config.get('env.rootDirectory'),
28 envName,
29 );
30 const envContents = _getFileContents(values);
31
32 fs.writeFileSync(envPath, envContents);
33}
34
35// Return the default env file name, with optional name appended
36function _getFileName(name) {
37 if (typeof name === 'undefined' || name.trim() === '') {
38 return config.get('env.basename');
39 }
40
41 return `${config.get('env.basename')}.${name}`;
42}
43
44// Return default list of env variables with their assigned value, if any.
45function _getFileContents(values) {
46 const env = getDefaultSlateEnv();
47
48 for (const key in values) {
49 if (values.hasOwnProperty(key) && env.hasOwnProperty(key)) {
50 env[key] = values[key];
51 }
52 }
53
54 return Object.entries(env)
55 .map((keyValues) => {
56 return `${keyValues.join('=')}\r\n`;
57 })
58 .join('\r\n\r\n');
59}
60
61// Reads an .env file and assigns their values to environment variables
62function assign(name) {
63 const envFileName = _getFileName(name);
64 const envPath = path.resolve(config.get('env.rootDirectory'), envFileName);
65 const result = dotenv.config({path: envPath});
66
67 if (typeof name !== 'undefined' && result.error) {
68 throw result.error;
69 }
70
71 _setEnvName(name);
72}
73
74function _setEnvName(name) {
75 let envName = name;
76 const envFileName = _getFileName(name);
77 const envPath = path.resolve(config.get('env.rootDirectory'), envFileName);
78
79 if (typeof name === 'undefined') {
80 if (fs.existsSync(envPath)) {
81 envName = config.get('env.defaultEnvName');
82 } else {
83 envName = config.get('env.externalEnvName');
84 }
85 }
86
87 process.env[config.get('env.keys.name')] = envName;
88}
89
90// Checks if Slate env variables are the required value types and format
91function validate() {
92 const errors = [].concat(
93 _validateStore(),
94 _validatePassword(),
95 _validateThemeId(),
96 );
97
98 return {
99 errors,
100 isValid: errors.length === 0,
101 };
102}
103
104function _validateStore() {
105 const errors = [];
106 const store = getStoreValue();
107
108 if (store.length === 0) {
109 errors.push(new Error(`${config.get('env.keys.store')} must not be empty`));
110 } else if (
111 store.indexOf('.myshopify.com') < 1 &&
112 store.indexOf('.myshopify.io') < 1
113 ) {
114 errors.push(
115 new Error(
116 `${config.get('env.keys.store')} must be a valid .myshopify.com URL`,
117 ),
118 );
119 } else if (store.slice(-1) === '/') {
120 errors.push(
121 new Error(
122 `${config.get('env.keys.store')} must not end with a trailing slash`,
123 ),
124 );
125 }
126
127 return errors;
128}
129
130function _validatePassword() {
131 const errors = [];
132 const password = getPasswordValue();
133
134 if (password.length === 0) {
135 errors.push(
136 new Error(`${config.get('env.keys.password')} must not be empty`),
137 );
138 } else if (!/^\w+$/.test(password)) {
139 errors.push(
140 new Error(
141 `${config.get(
142 'env.keys.password',
143 )} can only contain numbers and letters`,
144 ),
145 );
146 }
147
148 return errors;
149}
150
151function _validateThemeId() {
152 const errors = [];
153 const themeId = getThemeIdValue();
154
155 if (themeId.length === 0) {
156 errors.push(
157 new Error(`${config.get('env.keys.themeId')} must not be empty`),
158 );
159 } else if (themeId !== 'live' && !/^\d+$/.test(themeId)) {
160 errors.push(
161 new Error(
162 `${config.get(
163 'env.keys.themeId',
164 )} can be set to 'live' or a valid theme ID containing only numbers`,
165 ),
166 );
167 }
168
169 return errors;
170}
171
172// Clears the values of environment variables used by Slate
173function clear() {
174 SLATE_ENV_VARS.forEach((key) => (process.env[key] = ''));
175}
176
177// Get the values of Slate's required environment variables
178function getSlateEnv() {
179 const env = {};
180
181 SLATE_ENV_VARS.forEach((key) => {
182 env[key] = process.env[key];
183 });
184
185 return env;
186}
187
188// Returns the Slate's required environment variables with empty values
189function getEmptySlateEnv() {
190 const env = {};
191
192 SLATE_ENV_VARS.forEach((key) => {
193 env[key] = '';
194 });
195
196 return env;
197}
198
199function getDefaultSlateEnv() {
200 const env = {};
201
202 DEFAULT_ENV_VARS.forEach((key) => {
203 env[key] = '';
204 });
205
206 return env;
207}
208
209function getEnvNameValue() {
210 return process.env[config.get('env.keys.name')];
211}
212
213// Returns the configurable environment varible that reference the store URL
214function getStoreValue() {
215 const value = process.env[config.get('env.keys.store')];
216 return typeof value === 'undefined' ? '' : value;
217}
218
219function getPasswordValue() {
220 const value = process.env[config.get('env.keys.password')];
221 return typeof value === 'undefined' ? '' : value;
222}
223
224function getThemeIdValue() {
225 const value = process.env[config.get('env.keys.themeId')];
226 return typeof value === 'undefined' ? '' : value;
227}
228
229function getIgnoreFilesValue() {
230 const value = process.env[config.get('env.keys.ignoreFiles')];
231 return typeof value === 'undefined' ? '' : value;
232}
233
234function getUserEmail() {
235 const value = process.env[config.get('env.keys.userEmail')];
236 return typeof value === 'undefined' ? '' : value;
237}
238
239module.exports = {
240 create,
241 assign,
242 validate,
243 clear,
244 getSlateEnv,
245 getDefaultSlateEnv,
246 getEmptySlateEnv,
247 getEnvNameValue,
248 getStoreValue,
249 getPasswordValue,
250 getThemeIdValue,
251 getIgnoreFilesValue,
252 getUserEmail,
253};