Skip to content

Commit

Permalink
fix(build): concatenate string when app name contains dots #947
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Nov 16, 2020
1 parent 975e3f5 commit 12920e8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/compiler/helpers/get-value-or-default.ts
Expand Up @@ -17,7 +17,7 @@ export function getValueOrDefault<T = any>(
if (configuration.projects && configuration.projects[appName]) {
const perAppValue = getValueOfPath(
configuration,
`projects.${appName}.`.concat(propertyPath),
`projects."${appName}".`.concat(propertyPath),
);
if (perAppValue !== undefined) {
return perAppValue as T;
Expand All @@ -30,18 +30,38 @@ export function getValueOrDefault<T = any>(
return value;
}

function getValueOfPath<T = any>(
export function getValueOfPath<T = any>(
object: Record<string, any>,
propertyPath: string,
): T {
const fragments = propertyPath.split('.');

let propertyValue = object;
let isConcatInProgress = false;
let path = '';

for (const fragment of fragments) {
if (!propertyValue) {
break;
}
propertyValue = propertyValue[fragment];
/**
* When path is escaped with "" double quotes,
* concatenate the property path.
* Reference: https://github.com/nestjs/nest-cli/issues/947
*/
if (fragment.startsWith('"')) {
path += fragment.replace('"', '') + '.';
isConcatInProgress = true;
continue;
} else if (isConcatInProgress) {
path += '.';
continue;
} else if (fragment.endsWith('"')) {
path += fragment.replace('"', '');
isConcatInProgress = false;
}
propertyValue = propertyValue[path];
path = '';
}
return propertyValue as T;
}
25 changes: 25 additions & 0 deletions test/lib/compiler/helpers/get-value-or-default.spec.ts
Expand Up @@ -170,4 +170,29 @@ describe('Get Value or Default', () => {
);
expect(value).toBeUndefined();
});

it('should concatenate property path when app name contains dots', async () => {
let configuration: Required<Configuration> = {
monorepo: true,
sourceRoot: '',
entryFile: '',
projects: {
'test.project.v1.api': {
compilerOptions: {},
},
},
language: '',
collection: '',
compilerOptions: {
webpack: true,
},
generateOptions: {},
};
let value = getValueOrDefault(
configuration,
'compilerOptions.webpack',
'test.project.v1.api',
);
expect(value).toEqual(true);
});
});

0 comments on commit 12920e8

Please sign in to comment.