|
| 1 | +export class PlaygroundService implements IPlaygroundService { |
| 2 | + constructor(private $fs: IFileSystem, |
| 3 | + private $projectDataService: IProjectDataService, |
| 4 | + private $userSettingsService: IUserSettingsService) { } |
| 5 | + |
| 6 | + public async getPlaygroundInfo(projectDir?: string): Promise<IPlaygroundInfo> { |
| 7 | + const projectData = this.getProjectData(projectDir); |
| 8 | + if (projectData) { |
| 9 | + const projectFileContent = this.$fs.readJson(projectData.projectFilePath); |
| 10 | + if (this.hasPlaygroundKey(projectFileContent)) { |
| 11 | + const id = projectFileContent.nativescript.playground.id; |
| 12 | + let usedTutorial = projectFileContent.nativescript.playground.usedTutorial || false; |
| 13 | + |
| 14 | + // In case when usedTutorial=true is already saved in userSettings file, we shouldn't overwrite it |
| 15 | + const playgroundInfo = await this.getPlaygroundInfoFromUserSettingsFile(); |
| 16 | + if (playgroundInfo && playgroundInfo.usedTutorial) { |
| 17 | + usedTutorial = true; |
| 18 | + } |
| 19 | + |
| 20 | + delete projectFileContent.nativescript.playground; |
| 21 | + this.$fs.writeJson(projectData.projectFilePath, projectFileContent); |
| 22 | + |
| 23 | + const result = { id , usedTutorial }; |
| 24 | + await this.$userSettingsService.saveSettings(<any>{playground: result}); |
| 25 | + return result; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return this.getPlaygroundInfoFromUserSettingsFile(); |
| 30 | + } |
| 31 | + |
| 32 | + private getProjectData(projectDir: string): IProjectData { |
| 33 | + try { |
| 34 | + return this.$projectDataService.getProjectData(projectDir); |
| 35 | + } catch (e) { |
| 36 | + // in case command is executed in non-project folder |
| 37 | + return null; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private hasPlaygroundKey(projectFileContent: any): boolean { |
| 42 | + return projectFileContent && projectFileContent.nativescript && projectFileContent.nativescript.playground && projectFileContent.nativescript.playground.id; |
| 43 | + } |
| 44 | + |
| 45 | + private async getPlaygroundInfoFromUserSettingsFile(): Promise<IPlaygroundInfo> { |
| 46 | + return this.$userSettingsService.getSettingValue<IPlaygroundInfo>("playground"); |
| 47 | + } |
| 48 | +} |
| 49 | +$injector.register('playgroundService', PlaygroundService); |
0 commit comments