@graphcommerce/next-config 9.0.4-canary.0 → 9.0.4-canary.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +28 -8
- package/__tests__/commands/copyFiles.ts +8 -5
- package/__tests__/config/utils/__snapshots__/mergeEnvIntoConfig.ts.snap +0 -905
- package/__tests__/config/utils/mergeEnvIntoConfig.ts +21 -3
- package/__tests__/config/utils/replaceConfigInString.ts +0 -1
- package/__tests__/interceptors/findPlugins.ts +270 -272
- package/__tests__/interceptors/generateInterceptors.ts +1 -0
- package/__tests__/utils/resolveDependenciesSync.ts +45 -44
- package/dist/generated/config.js +110 -116
- package/dist/index.js +3359 -26
- package/package.json +34 -8
- package/src/commands/codegen.ts +3 -3
- package/src/commands/copyFiles.ts +14 -7
- package/src/config/commands/generateConfig.ts +17 -6
- package/src/config/demoConfig.ts +0 -1
- package/src/config/loadConfig.ts +3 -9
- package/src/config/utils/mergeEnvIntoConfig.ts +9 -9
- package/src/generated/config.ts +305 -245
- package/src/interceptors/Visitor.ts +1 -1
- package/src/interceptors/findOriginalSource.ts +1 -1
- package/src/interceptors/generateInterceptor.ts +0 -2
- package/src/interceptors/parseStructure.ts +3 -3
- package/src/interceptors/writeInterceptors.ts +1 -1
- package/src/utils/resolveDependenciesSync.ts +44 -7
- package/src/withGraphCommerce.ts +30 -42
- package/tsconfig.json +1 -1
- package/__tests__/config/utils/rewriteLegancyEnv.ts +0 -78
- package/dist/commands/codegen.js +0 -18
- package/dist/commands/copyFiles.js +0 -292
- package/dist/config/commands/exportConfig.js +0 -16
- package/dist/config/commands/generateConfig.js +0 -57
- package/dist/config/demoConfig.js +0 -52
- package/dist/config/index.js +0 -19
- package/dist/config/loadConfig.js +0 -62
- package/dist/config/utils/configToImportMeta.js +0 -39
- package/dist/config/utils/diff.js +0 -33
- package/dist/config/utils/exportConfigToEnv.js +0 -31
- package/dist/config/utils/mergeEnvIntoConfig.js +0 -184
- package/dist/config/utils/replaceConfigInString.js +0 -12
- package/dist/config/utils/rewriteLegacyEnv.js +0 -115
- package/dist/interceptors/InterceptorPlugin.js +0 -108
- package/dist/interceptors/RenameVisitor.js +0 -19
- package/dist/interceptors/Visitor.js +0 -1414
- package/dist/interceptors/commands/codegenInterceptors.js +0 -22
- package/dist/interceptors/extractExports.js +0 -159
- package/dist/interceptors/findOriginalSource.js +0 -103
- package/dist/interceptors/findPlugins.js +0 -68
- package/dist/interceptors/generateInterceptor.js +0 -219
- package/dist/interceptors/generateInterceptors.js +0 -56
- package/dist/interceptors/parseStructure.js +0 -84
- package/dist/interceptors/swc.js +0 -15
- package/dist/interceptors/writeInterceptors.js +0 -44
- package/dist/utils/PackagesSort.js +0 -7
- package/dist/utils/TopologicalSort.js +0 -87
- package/dist/utils/isMonorepo.js +0 -47
- package/dist/utils/packageRoots.js +0 -31
- package/dist/utils/resolveDependenciesSync.js +0 -78
- package/dist/utils/resolveDependency.js +0 -70
- package/dist/utils/sig.js +0 -34
- package/dist/withGraphCommerce.js +0 -162
- package/src/config/utils/rewriteLegacyEnv.ts +0 -125
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.generateConfig = generateConfig;
|
|
7
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
8
|
-
const fs_1 = require("fs");
|
|
9
|
-
const cli_1 = require("@graphql-codegen/cli");
|
|
10
|
-
const core_1 = require("@swc/core");
|
|
11
|
-
const dotenv_1 = __importDefault(require("dotenv"));
|
|
12
|
-
const isMonorepo_1 = require("../../utils/isMonorepo");
|
|
13
|
-
const resolveDependenciesSync_1 = require("../../utils/resolveDependenciesSync");
|
|
14
|
-
const resolveDependency_1 = require("../../utils/resolveDependency");
|
|
15
|
-
dotenv_1.default.config();
|
|
16
|
-
const packages = [...(0, resolveDependenciesSync_1.resolveDependenciesSync)().values()].filter((p) => p !== '.');
|
|
17
|
-
const resolve = (0, resolveDependency_1.resolveDependency)();
|
|
18
|
-
const schemaLocations = packages.map((p) => `${p}/**/Config.graphqls`);
|
|
19
|
-
async function generateConfig() {
|
|
20
|
-
const resolved = resolve('@graphcommerce/next-config');
|
|
21
|
-
if (!resolved)
|
|
22
|
-
throw Error('Could not resolve @graphcommerce/next-config');
|
|
23
|
-
const targetTs = `${resolved.root}/src/generated/config.ts`;
|
|
24
|
-
const targetJs = `${resolved.root}/dist/generated/config.js`;
|
|
25
|
-
await (0, cli_1.generate)({
|
|
26
|
-
silent: true,
|
|
27
|
-
schema: ['graphql/**/Config.graphqls', ...schemaLocations],
|
|
28
|
-
generates: {
|
|
29
|
-
[targetTs]: {
|
|
30
|
-
plugins: ['typescript', 'typescript-validation-schema', 'add'],
|
|
31
|
-
config: {
|
|
32
|
-
// enumsAsTypes: true,
|
|
33
|
-
content: '/* eslint-disable */',
|
|
34
|
-
schema: 'zod',
|
|
35
|
-
notAllowEmptyString: true,
|
|
36
|
-
strictScalars: true,
|
|
37
|
-
enumsAsTypes: true,
|
|
38
|
-
scalarSchemas: {
|
|
39
|
-
Domain: 'z.string()',
|
|
40
|
-
DateTime: 'z.date()',
|
|
41
|
-
RichTextAST: 'z.object.json()',
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
...((0, isMonorepo_1.findParentPath)(process.cwd()) && {
|
|
46
|
-
'../../docs/framework/config.md': {
|
|
47
|
-
plugins: ['@graphcommerce/graphql-codegen-markdown-docs'],
|
|
48
|
-
},
|
|
49
|
-
}),
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
const result = (0, core_1.transformFileSync)(targetTs, {
|
|
53
|
-
module: { type: 'commonjs' },
|
|
54
|
-
env: { targets: { node: '16' } },
|
|
55
|
-
});
|
|
56
|
-
(0, fs_1.writeFileSync)(targetJs, result.code);
|
|
57
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.demoConfig = void 0;
|
|
4
|
-
exports.demoConfig = {
|
|
5
|
-
canonicalBaseUrl: 'https://graphcommerce.vercel.app',
|
|
6
|
-
hygraphEndpoint: 'https://eu-central-1.cdn.hygraph.com/content/ckhx7xadya6xs01yxdujt8i80/master',
|
|
7
|
-
magentoEndpoint: 'https://configurator.reachdigital.dev/graphql',
|
|
8
|
-
magentoVersion: 247,
|
|
9
|
-
storefront: [
|
|
10
|
-
{ locale: 'en', magentoStoreCode: 'en_US', defaultLocale: true },
|
|
11
|
-
{
|
|
12
|
-
locale: 'nl',
|
|
13
|
-
magentoStoreCode: 'nl_NL',
|
|
14
|
-
hygraphLocales: ['nl', 'en_us'],
|
|
15
|
-
cartDisplayPricesInclTax: true,
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
locale: 'fr-be',
|
|
19
|
-
magentoStoreCode: 'fr_BE',
|
|
20
|
-
cartDisplayPricesInclTax: true,
|
|
21
|
-
linguiLocale: 'fr',
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
locale: 'nl-be',
|
|
25
|
-
magentoStoreCode: 'nl_BE',
|
|
26
|
-
cartDisplayPricesInclTax: true,
|
|
27
|
-
linguiLocale: 'nl',
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
locale: 'en-gb',
|
|
31
|
-
magentoStoreCode: 'en_GB',
|
|
32
|
-
cartDisplayPricesInclTax: true,
|
|
33
|
-
linguiLocale: 'en',
|
|
34
|
-
},
|
|
35
|
-
{ locale: 'en-ca', magentoStoreCode: 'en_CA', linguiLocale: 'en' },
|
|
36
|
-
],
|
|
37
|
-
productFiltersPro: true,
|
|
38
|
-
productFiltersLayout: 'DEFAULT',
|
|
39
|
-
productListPaginationVariant: 'COMPACT',
|
|
40
|
-
compareVariant: 'ICON',
|
|
41
|
-
robotsAllow: false,
|
|
42
|
-
demoMode: true,
|
|
43
|
-
limitSsg: true,
|
|
44
|
-
compare: true,
|
|
45
|
-
sidebarGallery: { paginationVariant: 'DOTS' },
|
|
46
|
-
configurableVariantForSimple: true,
|
|
47
|
-
configurableVariantValues: { url: true, content: true, gallery: true },
|
|
48
|
-
recentlyViewedProducts: { enabled: true, maxCount: 20 },
|
|
49
|
-
breadcrumbs: false,
|
|
50
|
-
customerDeleteEnabled: true,
|
|
51
|
-
previewSecret: 'SECRET',
|
|
52
|
-
};
|
package/dist/config/index.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./commands/generateConfig"), exports);
|
|
18
|
-
__exportStar(require("./commands/exportConfig"), exports);
|
|
19
|
-
__exportStar(require("./loadConfig"), exports);
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.loadConfig = loadConfig;
|
|
18
|
-
/* eslint-disable no-console */
|
|
19
|
-
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
20
|
-
const cosmiconfig_1 = require("cosmiconfig");
|
|
21
|
-
const config_1 = require("../generated/config");
|
|
22
|
-
const demoConfig_1 = require("./demoConfig");
|
|
23
|
-
const mergeEnvIntoConfig_1 = require("./utils/mergeEnvIntoConfig");
|
|
24
|
-
const rewriteLegacyEnv_1 = require("./utils/rewriteLegacyEnv");
|
|
25
|
-
__exportStar(require("./utils/configToImportMeta"), exports);
|
|
26
|
-
__exportStar(require("./utils/replaceConfigInString"), exports);
|
|
27
|
-
const moduleName = 'graphcommerce';
|
|
28
|
-
const loader = (0, cosmiconfig_1.cosmiconfigSync)(moduleName);
|
|
29
|
-
function loadConfig(cwd) {
|
|
30
|
-
const isMainProcess = !process.send;
|
|
31
|
-
try {
|
|
32
|
-
const result = loader.search(cwd);
|
|
33
|
-
let confFile = result?.config;
|
|
34
|
-
if (!confFile) {
|
|
35
|
-
if (isMainProcess)
|
|
36
|
-
console.warn('No graphcommerce.config.js found in the project, using demo config');
|
|
37
|
-
confFile = demoConfig_1.demoConfig;
|
|
38
|
-
}
|
|
39
|
-
confFile ||= {};
|
|
40
|
-
const schema = (0, config_1.GraphCommerceConfigSchema)();
|
|
41
|
-
const [mergedConfig, applyResult] = (0, rewriteLegacyEnv_1.rewriteLegacyEnv)(schema, process.env,
|
|
42
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
43
|
-
confFile);
|
|
44
|
-
if (applyResult.length > 0 && isMainProcess)
|
|
45
|
-
console.log((0, mergeEnvIntoConfig_1.formatAppliedEnv)(applyResult));
|
|
46
|
-
const finalParse = schema.parse(mergedConfig);
|
|
47
|
-
if (process.env.DEBUG && isMainProcess) {
|
|
48
|
-
console.log('Parsed configuration');
|
|
49
|
-
console.log(finalParse);
|
|
50
|
-
}
|
|
51
|
-
return finalParse;
|
|
52
|
-
}
|
|
53
|
-
catch (error) {
|
|
54
|
-
if (error instanceof Error) {
|
|
55
|
-
if (isMainProcess) {
|
|
56
|
-
console.log('Error while parsing graphcommerce.config.js', error.message);
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.configToImportMeta = configToImportMeta;
|
|
4
|
-
function flattenKeys(value, initialPathPrefix, stringify) {
|
|
5
|
-
// Is a scalar:
|
|
6
|
-
if (value === null || value === undefined || typeof value === 'number') {
|
|
7
|
-
return { [initialPathPrefix]: value };
|
|
8
|
-
}
|
|
9
|
-
if (typeof value === 'string') {
|
|
10
|
-
return { [initialPathPrefix]: stringify ? JSON.stringify(value) : value };
|
|
11
|
-
}
|
|
12
|
-
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
13
|
-
return {
|
|
14
|
-
[initialPathPrefix]: stringify || Array.isArray(value) ? JSON.stringify(value) : value,
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
if (typeof value === 'object') {
|
|
18
|
-
let outputValue = value;
|
|
19
|
-
if (stringify)
|
|
20
|
-
outputValue =
|
|
21
|
-
process.env.NODE_ENV !== 'production'
|
|
22
|
-
? `{ __debug: "'${initialPathPrefix}' can not be destructured, please access deeper properties directly" }`
|
|
23
|
-
: '{}';
|
|
24
|
-
return {
|
|
25
|
-
[initialPathPrefix]: outputValue,
|
|
26
|
-
...Object.keys(value)
|
|
27
|
-
.map((key) => {
|
|
28
|
-
const deep = value[key];
|
|
29
|
-
return flattenKeys(deep, `${initialPathPrefix}.${key}`, stringify);
|
|
30
|
-
})
|
|
31
|
-
.reduce((acc, path) => ({ ...acc, ...path }), {}),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
throw Error(`Unexpected value: ${value}`);
|
|
35
|
-
}
|
|
36
|
-
/** The result of this function is passed to the webpack DefinePlugin as import.meta.graphCommerce.* */
|
|
37
|
-
function configToImportMeta(config, path = 'import.meta.graphCommerce', stringify = true) {
|
|
38
|
-
return flattenKeys(config, path, stringify);
|
|
39
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.default = diff;
|
|
4
|
-
function isObject(val) {
|
|
5
|
-
return typeof val === 'object' && val !== null;
|
|
6
|
-
}
|
|
7
|
-
function isArray(val) {
|
|
8
|
-
return Array.isArray(val);
|
|
9
|
-
}
|
|
10
|
-
/** Simple diff function, retuns the values of the second object. */
|
|
11
|
-
function diff(item1, item2) {
|
|
12
|
-
const item1Type = typeof item2;
|
|
13
|
-
const item2Type = typeof item2;
|
|
14
|
-
const isSame = item1Type === item2Type;
|
|
15
|
-
// If the types aren't the same we always have a diff
|
|
16
|
-
if (!isSame)
|
|
17
|
-
return item2;
|
|
18
|
-
if (isArray(item1) && isArray(item2)) {
|
|
19
|
-
const res = item1.map((val, idx) => diff(val, item2[idx])).filter((val) => !!val);
|
|
20
|
-
return res.length ? res : undefined;
|
|
21
|
-
}
|
|
22
|
-
if (isObject(item1) && isObject(item2)) {
|
|
23
|
-
const entriesRight = Object.fromEntries(Object.entries(item1)
|
|
24
|
-
.map(([key, val]) => [key, diff(val, item2[key])])
|
|
25
|
-
.filter((entry) => !!entry[1]));
|
|
26
|
-
const entriesLeft = Object.fromEntries(Object.entries(item2)
|
|
27
|
-
.map(([key, val]) => [key, diff(item1[key], val)])
|
|
28
|
-
.filter((entry) => !!entry[1]));
|
|
29
|
-
const entries = { ...entriesRight, ...entriesLeft };
|
|
30
|
-
return Object.keys(entries).length ? entries : undefined;
|
|
31
|
-
}
|
|
32
|
-
return item2 === item1 ? undefined : item2;
|
|
33
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.exportConfigToEnv = exportConfigToEnv;
|
|
4
|
-
const mergeEnvIntoConfig_1 = require("./mergeEnvIntoConfig");
|
|
5
|
-
const fmt = (value) => {
|
|
6
|
-
let formattedValue = value;
|
|
7
|
-
if (typeof formattedValue === 'boolean') {
|
|
8
|
-
formattedValue = formattedValue ? '1' : '0';
|
|
9
|
-
}
|
|
10
|
-
if (typeof formattedValue === 'object') {
|
|
11
|
-
formattedValue = JSON.stringify(formattedValue);
|
|
12
|
-
}
|
|
13
|
-
if (typeof formattedValue === 'number') {
|
|
14
|
-
formattedValue = String(formattedValue);
|
|
15
|
-
}
|
|
16
|
-
return formattedValue;
|
|
17
|
-
};
|
|
18
|
-
function exportConfigToEnv(config) {
|
|
19
|
-
let env = '';
|
|
20
|
-
Object.entries(config).forEach(([key, value]) => {
|
|
21
|
-
if (Array.isArray(value)) {
|
|
22
|
-
value.forEach((val, idx) => {
|
|
23
|
-
env += `${(0, mergeEnvIntoConfig_1.toEnvStr)([key, `${idx}`])}='${fmt(val)}'\n`;
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
env += `${(0, mergeEnvIntoConfig_1.toEnvStr)([key])}='${fmt(value)}'\n`;
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
return env;
|
|
31
|
-
}
|
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.toEnvStr = void 0;
|
|
7
|
-
exports.configToEnvSchema = configToEnvSchema;
|
|
8
|
-
exports.mergeEnvIntoConfig = mergeEnvIntoConfig;
|
|
9
|
-
exports.formatAppliedEnv = formatAppliedEnv;
|
|
10
|
-
/* eslint-disable import/no-extraneous-dependencies */
|
|
11
|
-
const utilities_1 = require("@apollo/client/utilities");
|
|
12
|
-
const chalk_1 = __importDefault(require("chalk"));
|
|
13
|
-
const lodash_1 = require("lodash");
|
|
14
|
-
const snakeCase_1 = __importDefault(require("lodash/snakeCase"));
|
|
15
|
-
const zod_1 = require("zod");
|
|
16
|
-
const diff_1 = __importDefault(require("./diff"));
|
|
17
|
-
const fmt = (s) => s.split(/(\d+)/).map(snakeCase_1.default).join('');
|
|
18
|
-
const toEnvStr = (path) => ['GC', ...path].map(fmt).join('_').toUpperCase();
|
|
19
|
-
exports.toEnvStr = toEnvStr;
|
|
20
|
-
const dotNotation = (pathParts) => pathParts
|
|
21
|
-
.map((v) => {
|
|
22
|
-
const idx = Number(v);
|
|
23
|
-
return !Number.isNaN(idx) ? `[${idx}]` : v;
|
|
24
|
-
})
|
|
25
|
-
.join('.');
|
|
26
|
-
function isJSON(str) {
|
|
27
|
-
if (!str)
|
|
28
|
-
return true;
|
|
29
|
-
try {
|
|
30
|
-
JSON.parse(str);
|
|
31
|
-
}
|
|
32
|
-
catch (e) {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
function configToEnvSchema(schema) {
|
|
38
|
-
const envSchema = {};
|
|
39
|
-
const envToDot = {};
|
|
40
|
-
function walk(incomming, path = []) {
|
|
41
|
-
let node = incomming;
|
|
42
|
-
if (node instanceof zod_1.ZodEffects)
|
|
43
|
-
node = node.innerType();
|
|
44
|
-
if (node instanceof zod_1.ZodOptional)
|
|
45
|
-
node = node.unwrap();
|
|
46
|
-
if (node instanceof zod_1.ZodNullable)
|
|
47
|
-
node = node.unwrap();
|
|
48
|
-
if (node instanceof zod_1.ZodDefault)
|
|
49
|
-
node = node.removeDefault();
|
|
50
|
-
if (node instanceof zod_1.ZodObject) {
|
|
51
|
-
if (path.length > 0) {
|
|
52
|
-
envSchema[(0, exports.toEnvStr)(path)] = zod_1.z
|
|
53
|
-
.string()
|
|
54
|
-
.optional()
|
|
55
|
-
.refine(isJSON, { message: 'Invalid JSON' })
|
|
56
|
-
.transform((val) => (val ? JSON.parse(val) : val));
|
|
57
|
-
envToDot[(0, exports.toEnvStr)(path)] = dotNotation(path);
|
|
58
|
-
}
|
|
59
|
-
const typeNode = node;
|
|
60
|
-
Object.keys(typeNode.shape).forEach((key) => {
|
|
61
|
-
walk(typeNode.shape[key], [...path, key]);
|
|
62
|
-
});
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (node instanceof zod_1.ZodArray) {
|
|
66
|
-
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
|
|
67
|
-
if (path.length > 0) {
|
|
68
|
-
envSchema[(0, exports.toEnvStr)(path)] = zod_1.z
|
|
69
|
-
.string()
|
|
70
|
-
.optional()
|
|
71
|
-
.refine(isJSON, { message: 'Invalid JSON' })
|
|
72
|
-
.transform((val) => (val ? JSON.parse(val) : val));
|
|
73
|
-
envToDot[(0, exports.toEnvStr)(path)] = dotNotation(path);
|
|
74
|
-
}
|
|
75
|
-
arr.forEach((key) => {
|
|
76
|
-
walk(node.element, [...path, String(key)]);
|
|
77
|
-
});
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
if (node instanceof zod_1.ZodNumber) {
|
|
81
|
-
envSchema[(0, exports.toEnvStr)(path)] = zod_1.z.coerce.number().optional();
|
|
82
|
-
envToDot[(0, exports.toEnvStr)(path)] = dotNotation(path);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
if (node instanceof zod_1.ZodString || node instanceof zod_1.ZodEnum) {
|
|
86
|
-
envSchema[(0, exports.toEnvStr)(path)] = node.optional();
|
|
87
|
-
envToDot[(0, exports.toEnvStr)(path)] = dotNotation(path);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
if (node instanceof zod_1.ZodBoolean) {
|
|
91
|
-
envSchema[(0, exports.toEnvStr)(path)] = zod_1.z
|
|
92
|
-
.enum(['true', '1', 'false', '0'])
|
|
93
|
-
.optional()
|
|
94
|
-
.transform((v) => {
|
|
95
|
-
if (v === 'true' || v === '1')
|
|
96
|
-
return true;
|
|
97
|
-
if (v === 'false' || v === '0')
|
|
98
|
-
return false;
|
|
99
|
-
return v;
|
|
100
|
-
});
|
|
101
|
-
envToDot[(0, exports.toEnvStr)(path)] = dotNotation(path);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
throw Error(`[@graphcommerce/next-config] Unknown type in schema ${node.constructor.name}. This is probably a bug please create an issue.`);
|
|
105
|
-
}
|
|
106
|
-
walk(schema);
|
|
107
|
-
return [zod_1.z.object(envSchema), envToDot];
|
|
108
|
-
}
|
|
109
|
-
const filterEnv = (env) => Object.fromEntries(Object.entries(env).filter(([key]) => key.startsWith('GC_')));
|
|
110
|
-
function mergeEnvIntoConfig(schema, config, env) {
|
|
111
|
-
const filteredEnv = filterEnv(env);
|
|
112
|
-
const newConfig = (0, utilities_1.cloneDeep)(config);
|
|
113
|
-
const [envSchema, envToDot] = configToEnvSchema(schema);
|
|
114
|
-
const result = envSchema.safeParse(filteredEnv);
|
|
115
|
-
const applyResult = [];
|
|
116
|
-
if (!result.success) {
|
|
117
|
-
Object.entries(result.error.flatten().fieldErrors).forEach(([envVar, error]) => {
|
|
118
|
-
const dotVar = envToDot[envVar];
|
|
119
|
-
const envValue = filteredEnv[envVar];
|
|
120
|
-
applyResult.push({ envVar, envValue, dotVar, error });
|
|
121
|
-
});
|
|
122
|
-
return [undefined, applyResult];
|
|
123
|
-
}
|
|
124
|
-
Object.entries(result.data).forEach(([envVar, value]) => {
|
|
125
|
-
const dotVar = envToDot[envVar];
|
|
126
|
-
const envValue = filteredEnv[envVar];
|
|
127
|
-
if (!dotVar) {
|
|
128
|
-
applyResult.push({ envVar, envValue });
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
const dotValue = (0, lodash_1.get)(newConfig, dotVar);
|
|
132
|
-
const merged = (0, utilities_1.mergeDeep)(dotValue, value);
|
|
133
|
-
const from = (0, diff_1.default)(merged, dotValue);
|
|
134
|
-
const to = (0, diff_1.default)(dotValue, merged);
|
|
135
|
-
applyResult.push({ envVar, envValue, dotVar, from, to });
|
|
136
|
-
(0, lodash_1.set)(newConfig, dotVar, merged);
|
|
137
|
-
});
|
|
138
|
-
return [newConfig, applyResult];
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Prints the applied env variables to the console
|
|
142
|
-
*
|
|
143
|
-
* The format is:
|
|
144
|
-
*
|
|
145
|
-
* - If from and to is empty, the value is unchanged: `=` (white)
|
|
146
|
-
* - If the from is empty, a new value is applied: `+` (green)
|
|
147
|
-
* - If the to is empty, a value is removed: `-` (red)
|
|
148
|
-
* - If both from and to is not empty, a value is changed: `~` (yellow)
|
|
149
|
-
*/
|
|
150
|
-
function formatAppliedEnv(applyResult) {
|
|
151
|
-
let hasError = false;
|
|
152
|
-
let hasWarning = false;
|
|
153
|
-
const lines = applyResult.map(({ from, to, envValue, envVar, dotVar, error, warning }) => {
|
|
154
|
-
const fromFmt = chalk_1.default.red(JSON.stringify(from));
|
|
155
|
-
const toFmt = chalk_1.default.green(JSON.stringify(to));
|
|
156
|
-
const envVariableFmt = `${envVar}`;
|
|
157
|
-
const dotVariableFmt = chalk_1.default.bold.underline(`${dotVar}`);
|
|
158
|
-
const baseLog = `${envVariableFmt} => ${dotVariableFmt}`;
|
|
159
|
-
if (error) {
|
|
160
|
-
hasError = true;
|
|
161
|
-
return `${chalk_1.default.red(` ⨉ ${envVariableFmt}`)} => ${error.join(', ')}`;
|
|
162
|
-
}
|
|
163
|
-
if (warning) {
|
|
164
|
-
hasWarning = true;
|
|
165
|
-
return `${chalk_1.default.yellowBright(` ‼ ${envVariableFmt}`)} => ${warning.join(', ')}`;
|
|
166
|
-
}
|
|
167
|
-
if (!dotVar)
|
|
168
|
-
return chalk_1.default.red(`${envVariableFmt} => ignored (no matching config)`);
|
|
169
|
-
if (from === undefined && to === undefined)
|
|
170
|
-
return ` = ${baseLog}: (ignored)`;
|
|
171
|
-
if (from === undefined && to !== undefined)
|
|
172
|
-
return ` ${chalk_1.default.green('+')} ${baseLog}`;
|
|
173
|
-
if (from !== undefined && to === undefined)
|
|
174
|
-
return ` ${chalk_1.default.red('-')} ${baseLog}`;
|
|
175
|
-
return ` ${chalk_1.default.yellowBright('~')} ${baseLog}`;
|
|
176
|
-
});
|
|
177
|
-
let header = chalk_1.default.blueBright('info');
|
|
178
|
-
if (hasWarning)
|
|
179
|
-
header = chalk_1.default.yellowBright('warning');
|
|
180
|
-
if (hasError)
|
|
181
|
-
header = chalk_1.default.yellowBright('error');
|
|
182
|
-
header += ' - Loaded GraphCommerce env variables';
|
|
183
|
-
return [header, ...lines].join('\n');
|
|
184
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.replaceConfigInString = replaceConfigInString;
|
|
4
|
-
const configToImportMeta_1 = require("./configToImportMeta");
|
|
5
|
-
function replaceConfigInString(str, config) {
|
|
6
|
-
let result = str;
|
|
7
|
-
const replacers = (0, configToImportMeta_1.configToImportMeta)(config, 'graphCommerce', false);
|
|
8
|
-
Object.entries(replacers).forEach(([from, to]) => {
|
|
9
|
-
result = result.replace(new RegExp(`{${from}}`, 'g'), to);
|
|
10
|
-
});
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.rewriteLegacyEnv = rewriteLegacyEnv;
|
|
7
|
-
const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
|
|
8
|
-
const mergeEnvIntoConfig_1 = require("./mergeEnvIntoConfig");
|
|
9
|
-
function rewriteLegacyEnv(schema, env, config = {}) {
|
|
10
|
-
const clonedEnv = (0, cloneDeep_1.default)(env);
|
|
11
|
-
const applied = [];
|
|
12
|
-
function renamedTo(to) {
|
|
13
|
-
return (envVar, envValue) => {
|
|
14
|
-
applied.push({
|
|
15
|
-
warning: [`should be renamed to ${to}='${envValue}'`],
|
|
16
|
-
envVar,
|
|
17
|
-
envValue,
|
|
18
|
-
});
|
|
19
|
-
clonedEnv[to] = envValue;
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
function notUsed() {
|
|
23
|
-
return (envVar, envValue) => {
|
|
24
|
-
applied.push({
|
|
25
|
-
warning: ['should be removed'],
|
|
26
|
-
envVar,
|
|
27
|
-
envValue,
|
|
28
|
-
});
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
const parsers = {
|
|
32
|
-
MAGENTO_ENDPOINT: renamedTo('GC_MAGENTO_ENDPOINT'),
|
|
33
|
-
GRAPHCMS_URL: renamedTo('GC_HYGRAPH_ENDPOINT'),
|
|
34
|
-
NEXT_PUBLIC_GRAPHQL_ENDPOINT: notUsed(),
|
|
35
|
-
IMAGE_DOMAINS: (envVar, envValue) => {
|
|
36
|
-
applied.push({
|
|
37
|
-
warning: [
|
|
38
|
-
'should be removed: will automatically add the Magento/Hygraph URL. For more advanced configurations, see: https://nextjs.org/docs/api-reference/next/image#configuration-options',
|
|
39
|
-
],
|
|
40
|
-
envVar,
|
|
41
|
-
envValue,
|
|
42
|
-
});
|
|
43
|
-
},
|
|
44
|
-
NEXT_PUBLIC_LOCALE_STORES: (envVar, envValue) => {
|
|
45
|
-
const parsed = JSON.parse(envValue);
|
|
46
|
-
applied.push({
|
|
47
|
-
warning: ['env variable is is modified, rewritten to GC_STOREFRONT.'],
|
|
48
|
-
envVar,
|
|
49
|
-
envValue,
|
|
50
|
-
});
|
|
51
|
-
clonedEnv.GC_STOREFRONT = JSON.stringify(Object.entries(parsed).map(([locale, magentoStoreCode], index) => {
|
|
52
|
-
if (!config.storefront)
|
|
53
|
-
config.storefront = [];
|
|
54
|
-
config.storefront[index] = { ...config.storefront[index], locale, magentoStoreCode };
|
|
55
|
-
return { locale, magentoStoreCode };
|
|
56
|
-
}));
|
|
57
|
-
},
|
|
58
|
-
NEXT_PUBLIC_SITE_URL: renamedTo('GC_CANONICAL_BASE_URL'),
|
|
59
|
-
NEXT_PUBLIC_GTM_ID: renamedTo('GC_GOOGLE_TAGMANAGER_ID'),
|
|
60
|
-
NEXT_PUBLIC_GOOGLE_ANALYTICS: (envVar, envValue) => {
|
|
61
|
-
if (envValue.startsWith('{')) {
|
|
62
|
-
const parsed = JSON.parse(envValue);
|
|
63
|
-
clonedEnv.GC_GOOGLE_ANALYTICS_ID = 'enabled';
|
|
64
|
-
if (!config.storefront)
|
|
65
|
-
config.storefront = [];
|
|
66
|
-
config.storefront.forEach((storefront, index) => {
|
|
67
|
-
if (parsed[storefront.locale]) {
|
|
68
|
-
clonedEnv[`GC_STOREFRONT_${index}_GOOGLE_ANALYTICS_ID`] = parsed[storefront.locale];
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
applied.push({
|
|
72
|
-
warning: ['should be rewritten to GC_STOREFRONT_*_GOOGLE_ANALYTICS_ID'],
|
|
73
|
-
envVar,
|
|
74
|
-
envValue,
|
|
75
|
-
});
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
renamedTo('GC_GOOGLE_ANALYTICS_ID');
|
|
79
|
-
},
|
|
80
|
-
NEXT_PUBLIC_GOOGLE_RECAPTCHA_V3_SITE_KEY: renamedTo('GC_GOOGLE_RECAPTCHA_KEY'),
|
|
81
|
-
NEXT_PUBLIC_DISPLAY_INCL_TAX: (envVar, envValue) => {
|
|
82
|
-
const inclTax = envValue.split(',').map((i) => i.trim());
|
|
83
|
-
if (!config.storefront)
|
|
84
|
-
config.storefront = [];
|
|
85
|
-
config.storefront.forEach((storefront, index) => {
|
|
86
|
-
if (!inclTax.includes(storefront.locale))
|
|
87
|
-
return;
|
|
88
|
-
clonedEnv[`GC_STOREFRONT_${index}_CART_DISPLAY_PRICES_INCL_TAX`] = '1';
|
|
89
|
-
});
|
|
90
|
-
applied.push({
|
|
91
|
-
warning: ['env variable is renamed, move to configuration: cartDisplayPricesInclTax'],
|
|
92
|
-
envVar,
|
|
93
|
-
envValue,
|
|
94
|
-
});
|
|
95
|
-
clonedEnv.GC_DISPLAY_PRICES_INCL_TAX = envValue;
|
|
96
|
-
},
|
|
97
|
-
PREVIEW_SECRET: renamedTo('GC_PREVIEW_SECRET'),
|
|
98
|
-
DEMO_MAGENTO_GRAPHCOMMERCE: renamedTo('GC_DEMO_MODE'),
|
|
99
|
-
};
|
|
100
|
-
if (env.SKIP_MIGRATION === '1' || env.SKIP_MIGRATION === 'true') {
|
|
101
|
-
return (0, mergeEnvIntoConfig_1.mergeEnvIntoConfig)(schema, config, clonedEnv);
|
|
102
|
-
}
|
|
103
|
-
Object.entries(env).forEach(([key, value]) => {
|
|
104
|
-
if (value === undefined)
|
|
105
|
-
return;
|
|
106
|
-
try {
|
|
107
|
-
parsers[key]?.(key, value);
|
|
108
|
-
}
|
|
109
|
-
catch (e) {
|
|
110
|
-
console.error(`Error parsing ${key}`, e);
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
const [newConfig, envApplied] = (0, mergeEnvIntoConfig_1.mergeEnvIntoConfig)(schema, config, clonedEnv);
|
|
114
|
-
return [newConfig, [...applied, ...envApplied]];
|
|
115
|
-
}
|