Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 780c960

Browse files
Merge pull request #1029 from telerik/kerezov/find-project-dir
Recursively look for projectDir when executing hooks
2 parents f7a0dc9 + cb1e0d9 commit 780c960

File tree

3 files changed

+110
-2
lines changed

3 files changed

+110
-2
lines changed

helpers.ts

+19
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,25 @@ export function getPidFromiOSSimulatorLogs(applicationIdentifier: string, logLin
452452
return null;
453453
}
454454

455+
export function getValueFromNestedObject(obj: any, key: string): any {
456+
function _getValueRecursive(_obj: any, _key: string): any[] {
457+
if (_.has(_obj, _key)) {
458+
return [_obj];
459+
}
460+
461+
const res: any[] = [];
462+
_.forEach(_obj, (v, k) => {
463+
if (typeof v === "object" && typeof k === "string" && !_.startsWith(k, '$') && !_.endsWith(k.toLowerCase(), "service") && (v = _getValueRecursive(v, _key)).length) {
464+
res.push.apply(res, v);
465+
}
466+
});
467+
468+
return res;
469+
}
470+
471+
return _.head(_getValueRecursive(obj, key));
472+
}
473+
455474
//--- begin part copied from AngularJS
456475

457476
//The MIT License

services/hooks-service.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from "path";
22
import * as util from "util";
3-
import { annotate } from "../helpers";
3+
import { annotate, getValueFromNestedObject } from "../helpers";
44

55
class Hook implements IHook {
66
constructor(public name: string,
@@ -69,7 +69,12 @@ export class HooksService implements IHooksService {
6969
}
7070

7171
const hookArgs: any = hookArguments && hookArguments[this.hookArgsName];
72-
const projectDir = hookArgs && (hookArgs.projectDir || (hookArgs.projectData && hookArgs.projectData.projectDir));
72+
let projectDir = hookArgs && hookArgs.projectDir;
73+
if (!projectDir && hookArgs) {
74+
const candidate = getValueFromNestedObject(hookArgs, "projectDir");
75+
projectDir = candidate && candidate.projectDir;
76+
}
77+
7378
this.$logger.trace(`Project dir from hooksArgs is: ${projectDir}.`);
7479

7580
this.initialize(projectDir);

test/unit-tests/helpers.ts

+84
Original file line numberDiff line numberDiff line change
@@ -457,4 +457,88 @@ describe("helpers", () => {
457457
_.each(getPidFromiOSSimulatorLogsTestData, testData => assertPidTestData(testData));
458458
});
459459
});
460+
461+
describe("getValueFromNestedObject", () => {
462+
interface IValueFromNestedObjectTestData extends ITestData {
463+
key: string;
464+
}
465+
466+
const key = "key";
467+
const dollarKey = "$key";
468+
const dollarTestObj = { [dollarKey]: "value" };
469+
const serviceKey = "keyEndingWithService";
470+
const serviceTestObj = { [serviceKey]: "value" };
471+
const testObj = { key };
472+
const getValueFromNestedObjectTestData: IValueFromNestedObjectTestData[] = [
473+
{
474+
key,
475+
input: {},
476+
expectedResult: undefined
477+
},
478+
{
479+
key,
480+
input: testObj,
481+
expectedResult: testObj
482+
},
483+
{
484+
key,
485+
input: { nestedKey: testObj },
486+
expectedResult: testObj
487+
},
488+
{
489+
key,
490+
input: { nestedKey: { anotherNestedKey: testObj } },
491+
expectedResult: testObj
492+
},
493+
{
494+
key,
495+
input: { otherKey: "otherValue" },
496+
expectedResult: undefined
497+
},
498+
{
499+
key,
500+
input: { otherKey: "otherValue" },
501+
expectedResult: undefined
502+
},
503+
{
504+
key: dollarKey,
505+
input: dollarTestObj,
506+
expectedResult: dollarTestObj
507+
},
508+
{
509+
key: dollarKey,
510+
input: { nestedKey: dollarTestObj },
511+
expectedResult: dollarTestObj
512+
},
513+
{
514+
key: dollarKey,
515+
input: { "$nestedKey": dollarTestObj },
516+
expectedResult: undefined
517+
},
518+
{
519+
key: serviceKey,
520+
input: serviceTestObj,
521+
expectedResult: serviceTestObj
522+
},
523+
{
524+
key: serviceKey,
525+
input: { nestedKey: serviceTestObj },
526+
expectedResult: serviceTestObj
527+
},
528+
{
529+
key: serviceKey,
530+
input: { nestedKeyService: serviceTestObj },
531+
expectedResult: undefined
532+
}
533+
];
534+
535+
const assertValueFromNestedObjectTestData = (testData: IValueFromNestedObjectTestData) => {
536+
const actualResult = helpers.getValueFromNestedObject(testData.input, testData.key);
537+
assert.deepEqual(actualResult, testData.expectedResult, `For input ${JSON.stringify(testData.input)}, the expected result is: ${JSON.stringify(testData.expectedResult || "undefined")}, but actual result is: ${JSON.stringify(actualResult || "undefined")}.`);
538+
};
539+
540+
it("returns expected result", () => {
541+
_.each(getValueFromNestedObjectTestData, testData => assertValueFromNestedObjectTestData(testData));
542+
});
543+
});
460544
});

0 commit comments

Comments
 (0)