Skip to content

feat: expose isShared property from projectData #4202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/definitions/project.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ interface IProjectData extends ICreateProjectData {
gradleFilesDirectoryPath: string;
infoPlistPath: string;
buildXcconfigPath: string;
/**
* Defines if the project is a code sharing one.
* Value is true when project has nsconfig.json and it has `shared: true` in it.
*/
isShared: boolean;

/**
* Initializes project data with the given project directory. If none supplied defaults to --path option or cwd.
Expand Down
5 changes: 3 additions & 2 deletions lib/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class ProjectData implements IProjectData {
public appGradlePath: string;
public gradleFilesDirectoryPath: string;
public buildXcconfigPath: string;
public isShared: boolean;

constructor(private $fs: IFileSystem,
private $errors: IErrors,
Expand Down Expand Up @@ -131,7 +132,7 @@ export class ProjectData implements IProjectData {
this.appGradlePath = path.join(this.gradleFilesDirectoryPath, constants.APP_GRADLE_FILE_NAME);
this.infoPlistPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.INFO_PLIST_FILE_NAME);
this.buildXcconfigPath = path.join(this.appResourcesDirectoryPath, this.$devicePlatformsConstants.iOS, constants.BUILD_XCCONFIG_FILE_NAME);

this.isShared = !!(this.nsConfig && this.nsConfig.shared);
return;
}

Expand Down Expand Up @@ -202,7 +203,7 @@ export class ProjectData implements IProjectData {
}

public getNsConfigRelativePath(): string {
return constants.CONFIG_NS_FILE_NAME;
return constants.CONFIG_NS_FILE_NAME;
}

private resolveToProjectDir(pathToResolve: string, projectDir?: string): string {
Expand Down
3 changes: 1 addition & 2 deletions lib/services/analytics/analytics-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ export class AnalyticsService implements IAnalyticsService, IDisposable {
if (projectDir) {
const projectData = this.$projectDataService.getProjectData(projectDir);
customDimensions[GoogleAnalyticsCustomDimensions.projectType] = projectData.projectType;
const isShared = !!(projectData.nsConfig && projectData.nsConfig.shared);
customDimensions[GoogleAnalyticsCustomDimensions.isShared] = isShared.toString();
customDimensions[GoogleAnalyticsCustomDimensions.isShared] = projectData.isShared.toString();
}

return customDimensions;
Expand Down
75 changes: 58 additions & 17 deletions test/project-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,45 @@ describe("projectData", () => {
return testInjector;
};

const prepareTest = (opts?: { packageJsonData?: { dependencies?: IStringDictionary, devDependencies: IStringDictionary }, nsconfigData?: { shared: boolean } }): IProjectData => {
const testInjector = createTestInjector();
const fs = testInjector.resolve("fs");
fs.exists = (filePath: string) => filePath && (path.basename(filePath) === "package.json" || (path.basename(filePath) === "nsconfig.json" && opts && opts.nsconfigData));

fs.readText = (filePath: string) => {
if (path.basename(filePath) === "package.json") {
return JSON.stringify({
nativescript: {
id: "com.test.testid"
},
dependencies: opts && opts.packageJsonData && opts.packageJsonData.dependencies,
devDependencies: opts && opts.packageJsonData && opts.packageJsonData.devDependencies
});
} else if (path.basename(filePath) === "nsconfig.json" && opts && opts.nsconfigData) {
return JSON.stringify(opts.nsconfigData);
}

return null;
};

const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
projectHelper.projectDir = "projectDir";

const projectData: IProjectData = testInjector.resolve("projectData");
projectData.initializeProjectData();

return projectData;
};

describe("projectType", () => {

const assertProjectType = (dependencies: any, devDependencies: any, expectedProjecType: string) => {
const testInjector = createTestInjector();
const fs = testInjector.resolve("fs");
fs.exists = (filePath: string) => filePath && path.basename(filePath) === "package.json";

fs.readText = () => (JSON.stringify({
nativescript: {
id: "com.test.testid"
},
dependencies: dependencies,
devDependencies: devDependencies
}));

const projectHelper: IProjectHelper = testInjector.resolve("projectHelper");
projectHelper.projectDir = "projectDir";

const projectData: IProjectData = testInjector.resolve("projectData");
projectData.initializeProjectData();
const projectData = prepareTest({
packageJsonData: {
dependencies,
devDependencies
}
});
assert.deepEqual(projectData.projectType, expectedProjecType);
};

Expand Down Expand Up @@ -93,4 +112,26 @@ describe("projectData", () => {
assertProjectType(null, null, "Pure JavaScript");
});
});

describe("isShared", () => {
it("is false when nsconfig.json does not exist", () => {
const projectData = prepareTest();
assert.isFalse(projectData.isShared);
});

it("is false when nsconfig.json exists, but shared value is not populated", () => {
const projectData = prepareTest({ nsconfigData: { shared: undefined } });
assert.isFalse(projectData.isShared);
});

it("is false when nsconfig.json exists and shared value is false", () => {
const projectData = prepareTest({ nsconfigData: { shared: false } });
assert.isFalse(projectData.isShared);
});

it("is true when nsconfig.json exists and shared value is true", () => {
const projectData = prepareTest({ nsconfigData: { shared: true } });
assert.isTrue(projectData.isShared);
});
});
});
3 changes: 2 additions & 1 deletion test/services/analytics/analytics-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ const createTestInjector = (opts?: { projectHelperErrorMsg?: string, projectDir?
testInjector.register("projectDataService", {
getProjectData: (projectDir?: string): IProjectData => {
return <any>{
projectType: sampleProjectType
projectType: sampleProjectType,
isShared: false
};
}
});
Expand Down
1 change: 1 addition & 0 deletions test/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ export class ProjectDataStub implements IProjectData {
public appGradlePath: string;
public gradleFilesDirectoryPath: string;
public buildXcconfigPath: string;
public isShared: boolean;

public initializeProjectData(projectDir?: string): void {
this.projectDir = this.projectDir || projectDir;
Expand Down