-
-
Notifications
You must be signed in to change notification settings - Fork 197
feat(Analytics): Respect playground key from package.json file #3396
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule common
updated
3 files
+31 −0 | declarations.d.ts | |
+10 −0 | services/analytics/google-analytics-custom-dimensions.d.ts | |
+10 −1 | services/commands-service.ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
lib/services/analytics/google-analytics-custom-dimensions.d.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export class PlaygroundService implements IPlaygroundService { | ||
constructor(private $fs: IFileSystem, | ||
private $projectDataService: IProjectDataService, | ||
private $userSettingsService: IUserSettingsService) { } | ||
|
||
public async getPlaygroundInfo(projectDir?: string): Promise<IPlaygroundInfo> { | ||
const projectData = this.getProjectData(projectDir); | ||
if (projectData) { | ||
const projectFileContent = this.$fs.readJson(projectData.projectFilePath); | ||
if (this.hasPlaygroundKey(projectFileContent)) { | ||
const id = projectFileContent.nativescript.playground.id; | ||
let usedTutorial = projectFileContent.nativescript.playground.usedTutorial || false; | ||
|
||
// In case when usedTutorial=true is already saved in userSettings file, we shouldn't overwrite it | ||
const playgroundInfo = await this.getPlaygroundInfoFromUserSettingsFile(); | ||
if (playgroundInfo && playgroundInfo.usedTutorial) { | ||
usedTutorial = true; | ||
} | ||
|
||
delete projectFileContent.nativescript.playground; | ||
this.$fs.writeJson(projectData.projectFilePath, projectFileContent); | ||
|
||
const result = { id , usedTutorial }; | ||
await this.$userSettingsService.saveSettings(<any>{playground: result}); | ||
return result; | ||
} | ||
} | ||
|
||
return this.getPlaygroundInfoFromUserSettingsFile(); | ||
} | ||
|
||
private getProjectData(projectDir: string): IProjectData { | ||
try { | ||
return this.$projectDataService.getProjectData(projectDir); | ||
} catch (e) { | ||
// in case command is executed in non-project folder | ||
return null; | ||
} | ||
} | ||
|
||
private hasPlaygroundKey(projectFileContent: any): boolean { | ||
return projectFileContent && projectFileContent.nativescript && projectFileContent.nativescript.playground && projectFileContent.nativescript.playground.id; | ||
} | ||
|
||
private async getPlaygroundInfoFromUserSettingsFile(): Promise<IPlaygroundInfo> { | ||
return this.$userSettingsService.getSettingValue<IPlaygroundInfo>("playground"); | ||
} | ||
} | ||
$injector.register('playgroundService', PlaygroundService); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
import { assert } from "chai"; | ||
import { FileSystemStub } from "../stubs"; | ||
import { PlaygroundService } from "../../lib/services/playground-service"; | ||
import { Yok } from "../../lib/common/yok"; | ||
|
||
let userSettings: any = null; | ||
|
||
function createTestInjector(): IInjector { | ||
const testInjector = new Yok(); | ||
|
||
testInjector.register("playgroundService", PlaygroundService); | ||
testInjector.register("fs", FileSystemStub); | ||
testInjector.register("projectDataService", {}); | ||
testInjector.register("userSettingsService", {}); | ||
testInjector.register("injector", testInjector); | ||
|
||
return testInjector; | ||
} | ||
|
||
function mockPlaygroundService(testInjector: IInjector, data?: { projectData?: any, nativescriptKey?: any, userSettingsData?: any}) { | ||
const projectDataService = testInjector.resolve<IProjectDataService>("projectDataService"); | ||
projectDataService.getProjectData = () => (data && data.projectData) || <IProjectData>{}; | ||
|
||
const userSettingsService = testInjector.resolve("userSettingsService"); | ||
userSettingsService.getSettingValue = async (keyName: string) => { | ||
return data && data.userSettingsData ? data.userSettingsData[keyName] : null; | ||
}; | ||
userSettingsService.saveSettings = async (settings: any) => { userSettings = settings; }; | ||
|
||
const fs = testInjector.resolve<IFileSystem>("fs"); | ||
fs.readJson = () => (data && data.nativescriptKey) || {}; | ||
} | ||
|
||
describe("PlaygroundService", () => { | ||
let testInjector: IInjector = null; | ||
let playgroundService: IPlaygroundService = null; | ||
|
||
beforeEach(() => { | ||
testInjector = createTestInjector(); | ||
playgroundService = testInjector.resolve("playgroundService"); | ||
}); | ||
|
||
describe("getPlaygroundInfo", () => { | ||
it("should return null when projectDir is not specified and no playground data is saved in userSettings file", async () => { | ||
mockPlaygroundService(testInjector, { userSettingsData: null }); | ||
const result = await playgroundService.getPlaygroundInfo(); | ||
assert.equal(result, null); | ||
}); | ||
it("should return saved playgroundData from userSettings file when projectDir is not specified and playground data is already saved in userSettings file", async () => { | ||
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: false}}}); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
const expectedResult = {id: "test-playground-identifier", usedTutorial: false}; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
it("should return null when projectFile has no nativescript key in package.json file and no playground data is saved in userSettings file", async () => { | ||
mockPlaygroundService(testInjector, { userSettingsData: null }); | ||
const result = await playgroundService.getPlaygroundInfo(); | ||
assert.equal(result, null); | ||
}); | ||
it("should return saved playgroundData from userSettings file when projectFile has no nativescript key in package.json and some playground data is already saved in userSettings file", async () => { | ||
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: true}}}); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
const expectedResult = {id: "test-playground-identifier", usedTutorial: true}; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
|
||
describe("should return playgroundInfo when project has playground key in package.json", () => { | ||
it("and no usedTutorial", async () => { | ||
const nativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "test-guid" | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey }); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
const expectedResult = { id: "test-guid", usedTutorial: false }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
it("and usedTutorial is true", async() => { | ||
const nativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "test-guid", | ||
usedTutorial: true | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey }); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
const expectedResult = { id: 'test-guid', usedTutorial: true }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
it("and usedTutorial is false", async () => { | ||
const nativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "playground-test-guid", | ||
usedTutorial: false | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey }); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
const expectedResult = { id: "playground-test-guid", usedTutorial: false }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
}); | ||
|
||
describe("should return playgroundInfo from userSettings file", () => { | ||
it("when usedTutorial is true", async () => { | ||
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: true}}}); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
const expectedResult = { id: 'test-playground-identifier', usedTutorial: true }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
it("when usedTutorial is false", async () => { | ||
mockPlaygroundService(testInjector, { userSettingsData: {playground: {id: "test-playground-identifier", usedTutorial: false}}}); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
const expectedResult = { id: 'test-playground-identifier', usedTutorial: false }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
}); | ||
|
||
it("should return undefined when userSettings file does not have playground key", async () => { | ||
mockPlaygroundService(testInjector, { userSettingsData: {}}); | ||
const actualResult = await playgroundService.getPlaygroundInfo(); | ||
assert.deepEqual(actualResult, undefined); | ||
}); | ||
it("should replace playgroundId when another id is already saved in userSettings file", async () => { | ||
const nativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "test-guid" | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey }); | ||
let actualResult = await playgroundService.getPlaygroundInfo(); | ||
let expectedResult = { id: "test-guid", usedTutorial: false }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
|
||
const secondNativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "another-test-guid" | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey: secondNativescriptKey }); | ||
actualResult = await playgroundService.getPlaygroundInfo(); | ||
expectedResult = { id: 'another-test-guid', usedTutorial: false }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
assert.deepEqual(userSettings, { playground: { id: 'another-test-guid', usedTutorial: false }}); | ||
}); | ||
it("should replace usedTutorial when false value is already saved in userSettings file", async () => { | ||
const nativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "test-guid", | ||
usedTutorial: false | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey }); | ||
let actualResult = await playgroundService.getPlaygroundInfo(); | ||
let expectedResult = { id: "test-guid", usedTutorial: false }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
|
||
const secondNativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "another-test-guid", | ||
usedTutorial: true | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey: secondNativescriptKey, userSettingsData: {playground: { id: "test-guid", usedTutorial: false }} }); | ||
actualResult = await playgroundService.getPlaygroundInfo(); | ||
expectedResult = { id: 'another-test-guid', usedTutorial: true }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
it("shouldn't replace usedTutorial when true value is already saved in userSettings file", async () => { | ||
const nativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "test-guid", | ||
usedTutorial: true | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey }); | ||
let actualResult = await playgroundService.getPlaygroundInfo(); | ||
let expectedResult = { id: "test-guid", usedTutorial: true }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
|
||
const secondNativescriptKey = { | ||
nativescript: { | ||
playground: { | ||
id: "another-test-guid", | ||
usedTutorial: false | ||
} | ||
} | ||
}; | ||
mockPlaygroundService(testInjector, { nativescriptKey: secondNativescriptKey, userSettingsData: {playground: { id: "test-guid", usedTutorial: true }} }); | ||
actualResult = await playgroundService.getPlaygroundInfo(); | ||
expectedResult = { id: 'another-test-guid', usedTutorial: true }; | ||
assert.deepEqual(actualResult, expectedResult); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please note when this method returns null or undefined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do it here - https://github.com/NativeScript/nativescript-cli/pull/3396/files#diff-eaac680656b280b3360a73aac495df29R908. Is it ok?