UNPKG

2.39 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getValueOfPath = exports.getValueOrDefault = void 0;
4function getValueOrDefault(configuration, propertyPath, appName, key, options = [], defaultValue) {
5 const item = options.find((option) => option.name === key);
6 const origValue = item && item.value;
7 if (origValue !== undefined && origValue !== null) {
8 return origValue;
9 }
10 if (configuration.projects && configuration.projects[appName]) {
11 // Wrap the application name in double-quotes to prevent splitting it
12 // into separate chunks
13 appName = appName && !appName.includes('"') ? `"${appName}"` : appName;
14 const perAppValue = getValueOfPath(configuration, `projects.${appName}.`.concat(propertyPath));
15 if (perAppValue !== undefined) {
16 return perAppValue;
17 }
18 }
19 let value = getValueOfPath(configuration, propertyPath);
20 if (value === undefined) {
21 value = defaultValue;
22 }
23 return value;
24}
25exports.getValueOrDefault = getValueOrDefault;
26function getValueOfPath(object, propertyPath) {
27 const fragments = propertyPath.split('.');
28 let propertyValue = object;
29 let isConcatInProgress = false;
30 let path = '';
31 for (const fragment of fragments) {
32 if (!propertyValue) {
33 break;
34 }
35 /**
36 * When path is escaped with "" double quotes,
37 * concatenate the property path.
38 * Reference: https://github.com/nestjs/nest-cli/issues/947
39 */
40 if (fragment.startsWith('"') && fragment.endsWith('"')) {
41 path = stripDoubleQuotes(fragment);
42 }
43 else if (fragment.startsWith('"')) {
44 path += stripDoubleQuotes(fragment) + '.';
45 isConcatInProgress = true;
46 continue;
47 }
48 else if (isConcatInProgress && !fragment.endsWith('"')) {
49 path += fragment + '.';
50 continue;
51 }
52 else if (fragment.endsWith('"')) {
53 path += stripDoubleQuotes(fragment);
54 isConcatInProgress = false;
55 }
56 else {
57 path = fragment;
58 }
59 propertyValue = propertyValue[path];
60 path = '';
61 }
62 return propertyValue;
63}
64exports.getValueOfPath = getValueOfPath;
65function stripDoubleQuotes(text) {
66 return text.replace(/"/g, '');
67}