1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const dotenv = require('dotenv');
|
4 | const SlateConfig = require('@shopify/slate-config');
|
5 | const config = new SlateConfig(require('./slate-env.schema'));
|
6 |
|
7 | const 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 |
|
16 | const 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 |
|
24 | function 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 |
|
36 | function _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 |
|
45 | function _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 |
|
62 | function 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 |
|
74 | function _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 |
|
91 | function 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 |
|
104 | function _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 |
|
130 | function _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 |
|
151 | function _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 |
|
173 | function clear() {
|
174 | SLATE_ENV_VARS.forEach((key) => (process.env[key] = ''));
|
175 | }
|
176 |
|
177 |
|
178 | function 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 |
|
189 | function getEmptySlateEnv() {
|
190 | const env = {};
|
191 |
|
192 | SLATE_ENV_VARS.forEach((key) => {
|
193 | env[key] = '';
|
194 | });
|
195 |
|
196 | return env;
|
197 | }
|
198 |
|
199 | function getDefaultSlateEnv() {
|
200 | const env = {};
|
201 |
|
202 | DEFAULT_ENV_VARS.forEach((key) => {
|
203 | env[key] = '';
|
204 | });
|
205 |
|
206 | return env;
|
207 | }
|
208 |
|
209 | function getEnvNameValue() {
|
210 | return process.env[config.get('env.keys.name')];
|
211 | }
|
212 |
|
213 |
|
214 | function getStoreValue() {
|
215 | const value = process.env[config.get('env.keys.store')];
|
216 | return typeof value === 'undefined' ? '' : value;
|
217 | }
|
218 |
|
219 | function getPasswordValue() {
|
220 | const value = process.env[config.get('env.keys.password')];
|
221 | return typeof value === 'undefined' ? '' : value;
|
222 | }
|
223 |
|
224 | function getThemeIdValue() {
|
225 | const value = process.env[config.get('env.keys.themeId')];
|
226 | return typeof value === 'undefined' ? '' : value;
|
227 | }
|
228 |
|
229 | function getIgnoreFilesValue() {
|
230 | const value = process.env[config.get('env.keys.ignoreFiles')];
|
231 | return typeof value === 'undefined' ? '' : value;
|
232 | }
|
233 |
|
234 | function getUserEmail() {
|
235 | const value = process.env[config.get('env.keys.userEmail')];
|
236 | return typeof value === 'undefined' ? '' : value;
|
237 | }
|
238 |
|
239 | module.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 | };
|