Skip to content

Commit 11bb7b3

Browse files
Merge pull request #4202 from NativeScript/vladimirov/expose-isShared-property
feat: expose isShared property from projectData
2 parents 77a938c + 50603ac commit 11bb7b3

File tree

6 files changed

+70
-22
lines changed

6 files changed

+70
-22
lines changed

lib/definitions/project.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ interface IProjectData extends ICreateProjectData {
9292
gradleFilesDirectoryPath: string;
9393
infoPlistPath: string;
9494
buildXcconfigPath: string;
95+
/**
96+
* Defines if the project is a code sharing one.
97+
* Value is true when project has nsconfig.json and it has `shared: true` in it.
98+
*/
99+
isShared: boolean;
95100

96101
/**
97102
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.

lib/project-data.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class ProjectData implements IProjectData {
5959
public appGradlePath: string;
6060
public gradleFilesDirectoryPath: string;
6161
public buildXcconfigPath: string;
62+
public isShared: boolean;
6263

6364
constructor(private $fs: IFileSystem,
6465
private $errors: IErrors,
@@ -131,7 +132,7 @@ export class ProjectData implements IProjectData {
131132
this.appGradlePath = path.join(this.gradleFilesDirectoryPath, constants.APP_GRADLE_FILE_NAME);
132133
this.infoPlistPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.INFO_PLIST_FILE_NAME);
133134
this.buildXcconfigPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.BUILD_XCCONFIG_FILE_NAME);
134-
135+
this.isShared = !!(this.nsConfig && this.nsConfig.shared);
135136
return;
136137
}
137138

@@ -202,7 +203,7 @@ export class ProjectData implements IProjectData {
202203
}
203204

204205
public getNsConfigRelativePath(): string {
205-
return constants.CONFIG_NS_FILE_NAME;
206+
return constants.CONFIG_NS_FILE_NAME;
206207
}
207208

208209
private resolveToProjectDir(pathToResolve: string, projectDir?: string): string {

lib/services/analytics/analytics-service.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ export class AnalyticsService implements IAnalyticsService, IDisposable {
151151
if (projectDir) {
152152
const projectData = this.$projectDataService.getProjectData(projectDir);
153153
customDimensions[GoogleAnalyticsCustomDimensions.projectType] = projectData.projectType;
154-
const isShared = !!(projectData.nsConfig && projectData.nsConfig.shared);
155-
customDimensions[GoogleAnalyticsCustomDimensions.isShared] = isShared.toString();
154+
customDimensions[GoogleAnalyticsCustomDimensions.isShared] = projectData.isShared.toString();
156155
}
157156

158157
return customDimensions;

test/project-data.ts

+58-17
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,45 @@ describe("projectData", () => {
4242
return testInjector;
4343
};
4444

45+
const prepareTest = (opts?: { packageJsonData?: { dependencies?: IStringDictionary, devDependencies: IStringDictionary }, nsconfigData?: { shared: boolean } }): IProjectData => {
46+
const testInjector = createTestInjector();
47+
const fs = testInjector.resolve("fs");
48+
fs.exists = (filePath: string) => filePath && (path.basename(filePath) === "package.json" || (path.basename(filePath) === "nsconfig.json" && opts && opts.nsconfigData));
49+
50+
fs.readText = (filePath: string) => {
51+
if (path.basename(filePath) === "package.json") {
52+
return JSON.stringify({
53+
nativescript: {
54+
id: "com.test.testid"
55+
},
56+
dependencies: opts && opts.packageJsonData && opts.packageJsonData.dependencies,
57+
devDependencies: opts && opts.packageJsonData && opts.packageJsonData.devDependencies
58+
});
59+
} else if (path.basename(filePath) === "nsconfig.json" && opts && opts.nsconfigData) {
60+
return JSON.stringify(opts.nsconfigData);
61+
}
62+
63+
return null;
64+
};
65+
66+
const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
67+
projectHelper.projectDir = "projectDir";
68+
69+
const projectData: IProjectData = testInjector.resolve("projectData");
70+
projectData.initializeProjectData();
71+
72+
return projectData;
73+
};
74+
4575
describe("projectType", () => {
4676

4777
const assertProjectType = (dependencies: any, devDependencies: any, expectedProjecType: string) => {
48-
const testInjector = createTestInjector();
49-
const fs = testInjector.resolve("fs");
50-
fs.exists = (filePath: string) => filePath && path.basename(filePath) === "package.json";
51-
52-
fs.readText = () => (JSON.stringify({
53-
nativescript: {
54-
id: "com.test.testid"
55-
},
56-
dependencies: dependencies,
57-
devDependencies: devDependencies
58-
}));
59-
60-
const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
61-
projectHelper.projectDir = "projectDir";
62-
63-
const projectData: IProjectData = testInjector.resolve("projectData");
64-
projectData.initializeProjectData();
78+
const projectData = prepareTest({
79+
packageJsonData: {
80+
dependencies,
81+
devDependencies
82+
}
83+
});
6584
assert.deepEqual(projectData.projectType, expectedProjecType);
6685
};
6786

@@ -93,4 +112,26 @@ describe("projectData", () => {
93112
assertProjectType(null, null, "Pure JavaScript");
94113
});
95114
});
115+
116+
describe("isShared", () => {
117+
it("is false when nsconfig.json does not exist", () => {
118+
const projectData = prepareTest();
119+
assert.isFalse(projectData.isShared);
120+
});
121+
122+
it("is false when nsconfig.json exists, but shared value is not populated", () => {
123+
const projectData = prepareTest({ nsconfigData: { shared: undefined } });
124+
assert.isFalse(projectData.isShared);
125+
});
126+
127+
it("is false when nsconfig.json exists and shared value is false", () => {
128+
const projectData = prepareTest({ nsconfigData: { shared: false } });
129+
assert.isFalse(projectData.isShared);
130+
});
131+
132+
it("is true when nsconfig.json exists and shared value is true", () => {
133+
const projectData = prepareTest({ nsconfigData: { shared: true } });
134+
assert.isTrue(projectData.isShared);
135+
});
136+
});
96137
});

test/services/analytics/analytics-service.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ const createTestInjector = (opts?: { projectHelperErrorMsg?: string, projectDir?
4242
testInjector.register("projectDataService", {
4343
getProjectData: (projectDir?: string): IProjectData => {
4444
return <any>{
45-
projectType: sampleProjectType
45+
projectType: sampleProjectType,
46+
isShared: false
4647
};
4748
}
4849
});

test/stubs.ts

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ export class ProjectDataStub implements IProjectData {
323323
public appGradlePath: string;
324324
public gradleFilesDirectoryPath: string;
325325
public buildXcconfigPath: string;
326+
public isShared: boolean;
326327

327328
public initializeProjectData(projectDir?: string): void {
328329
this.projectDir = this.projectDir || projectDir;

0 commit comments

Comments
 (0)