forked from magento/pwa-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-environment.js
136 lines (132 loc) · 5.21 KB
/
validate-environment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function validateEnvironment(env) {
const envalid = require('envalid');
const { str, bool, url } = envalid;
const validation = {
IMAGE_SERVICE_PATH: str({
desc:
'Root path to mount the onboard image optimization service in the DevServer and staging server.',
example: '/img/',
default: '/img/'
}),
IMAGE_CACHE_EXPIRES: str({
desc:
'Lifetime of images in the local cache of resized images. Format is "[length] [unit]", as in "10 minutes" or "1 day".',
example: '5 minutes',
default: '1 hour'
}),
IMAGE_CACHE_DEBUG: bool({
desc: 'Log image cache debug info to the console.',
default: false
}),
IMAGE_CACHE_REDIS_CLIENT: str({
desc:
'To use a Redis instance instead of a local memory cache for persistence between server processes, set this variable to the socket or URL of the Redis instance.',
default: ''
}),
SERVICE_WORKER_FILE_NAME: str({
desc:
'Filename to use when autogenerating a service worker to be served at root.',
example: 'sw.js',
default: 'sw.js'
}),
MAGENTO_BACKEND_MEDIA_PATH_PRODUCT: str({
desc:
'URL path where the PWA expects Magento to serve product media.',
example: '/media/catalog/product',
default: '/media/catalog/product'
}),
MAGENTO_BACKEND_MEDIA_PATH_CATEGORY: str({
desc:
'URL path where the PWA expects Magento to serve category media.',
example: '/media/catalog/category',
default: '/media/catalog/category'
}),
MAGENTO_BUILDPACK_PROVIDE_SECURE_HOST: bool({
desc:
'On first run, create a secure, unique hostname and generate a trusted SSL certificate.',
default: true
}),
MAGENTO_BUILDPACK_SECURE_HOST_AND_UNIQUE_HASH: bool({
desc:
'Add a unique hash based on filesystem location to the unique hostname. No effect if MAGENTO_BUILDPACK_PROVIDE_SECURE_HOST is false.',
default: true
}),
ENABLE_SERVICE_WORKER_DEBUGGING: bool({
desc:
'Use a service worker in developer mode (PWADevServer), which are disabled in dev mode normally to simplify cache. Good for debugging.',
default: false
}),
UPWARD_JS_UPWARD_PATH: str({
desc:
'UPWARD configuration file to use for the PWADevServer and the "stage:venia" sample server.',
default: 'venia-upward.yml'
}),
UPWARD_JS_BIND_LOCAL: bool({
desc:
'Upon launching the staging server, automatically attach to a local port and listen.',
default: true
}),
UPWARD_JS_LOG_URL: bool({
desc:
'Tell the upward-js prod server to log the bound URL to stdout. Useful in testing and discovery scenarios, but may be disabled in production.',
default: true
})
};
if (process.env.NODE_ENV !== 'production') {
validation.MAGENTO_BACKEND_URL = url({
desc: 'Public base URL of of Magento 2.3 instance.',
example: 'https://magento2.vagrant65'
});
const { readFileSync } = require('fs');
const path = require('path');
const chalk = require('chalk');
const dotenv = require('dotenv');
let parsedEnv;
const envPath = path.join(__dirname, '.env');
try {
parsedEnv = dotenv.parse(readFileSync(envPath));
// don't use console.log, which writes to stdout. writing to stdout
// interferes with webpack json output
console.warn(
chalk.green(
`Using environment variables from ${chalk.greenBright(
'.env'
)}`
)
);
if (env.DEBUG || env.NODE_DEBUG) {
console.warn(
'\n ' +
require('util')
.inspect(parsedEnv, {
colors: true,
compact: false
})
.replace(/\s*[\{\}]\s*/gm, '')
.replace(/,\n\s+/gm, '\n ') +
'\n'
);
}
} catch (e) {
if (e.code === 'ENOENT') {
console.warn(
chalk.redBright(
`\nNo .env file in ${__dirname}\n\tYou may need to copy '.env.dist' to '.env' to begin, or create your own '.env' file manually.`
)
);
} else {
console.warn(
chalk.redBright(
`\nCould not retrieve and parse ${envPath}.`,
e
)
);
}
}
}
return envalid.cleanEnv(env, validation);
}
module.exports = validateEnvironment;
if (module === require.main) {
validateEnvironment(process.env);
}