Skip to content

Commit d169bc5

Browse files
Use sync version of fs.readdir
Make emulatorSettingsService.canStart and fs.deleteEmptyParents, fs.isEmptyDir sync
1 parent 9a2f2ea commit d169bc5

23 files changed

+180
-169
lines changed

lib/android-tools-info.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -258,32 +258,30 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
258258
}).future<number>()();
259259
}
260260

261-
private getMatchingDir(pathToDir: string, versionRange: string): IFuture<string> {
262-
return ((): string => {
263-
let selectedVersion: string;
264-
if (this.$fs.exists(pathToDir)) {
265-
let subDirs = this.$fs.readDirectory(pathToDir).wait();
266-
this.$logger.trace(`Directories found in ${pathToDir} are ${subDirs.join(", ")}`);
267-
268-
let subDirsVersions = subDirs
269-
.map(dirName => {
270-
let dirNameGroups = dirName.match(AndroidToolsInfo.VERSION_REGEX);
271-
if (dirNameGroups) {
272-
return dirNameGroups[1];
273-
}
261+
private getMatchingDir(pathToDir: string, versionRange: string): string {
262+
let selectedVersion: string;
263+
if (this.$fs.exists(pathToDir)) {
264+
let subDirs = this.$fs.readDirectory(pathToDir);
265+
this.$logger.trace(`Directories found in ${pathToDir} are ${subDirs.join(", ")}`);
266+
267+
let subDirsVersions = subDirs
268+
.map(dirName => {
269+
let dirNameGroups = dirName.match(AndroidToolsInfo.VERSION_REGEX);
270+
if (dirNameGroups) {
271+
return dirNameGroups[1];
272+
}
274273

275-
return null;
276-
})
277-
.filter(dirName => !!dirName);
278-
this.$logger.trace(`Versions found in ${pathToDir} are ${subDirsVersions.join(", ")}`);
279-
let version = semver.maxSatisfying(subDirsVersions, versionRange);
280-
if (version) {
281-
selectedVersion = _.find(subDirs, dir => dir.indexOf(version) !== -1);
282-
}
274+
return null;
275+
})
276+
.filter(dirName => !!dirName);
277+
this.$logger.trace(`Versions found in ${pathToDir} are ${subDirsVersions.join(", ")}`);
278+
let version = semver.maxSatisfying(subDirsVersions, versionRange);
279+
if (version) {
280+
selectedVersion = _.find(subDirs, dir => dir.indexOf(version) !== -1);
283281
}
284-
this.$logger.trace("Selected version is: ", selectedVersion);
285-
return selectedVersion;
286-
}).future<string>()();
282+
}
283+
this.$logger.trace("Selected version is: ", selectedVersion);
284+
return selectedVersion;
287285
}
288286

289287
private getBuildToolsRange(): string {
@@ -296,7 +294,7 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
296294
if (this.androidHome) {
297295
let pathToBuildTools = path.join(this.androidHome, "build-tools");
298296
let buildToolsRange = this.getBuildToolsRange();
299-
buildToolsVersion = this.getMatchingDir(pathToBuildTools, buildToolsRange).wait();
297+
buildToolsVersion = this.getMatchingDir(pathToBuildTools, buildToolsRange);
300298
}
301299

302300
return buildToolsVersion;
@@ -321,7 +319,7 @@ export class AndroidToolsInfo implements IAndroidToolsInfo {
321319
let requiredAppCompatRange = this.getAppCompatRange().wait();
322320
if (this.androidHome && requiredAppCompatRange) {
323321
let pathToAppCompat = path.join(this.androidHome, "extras", "android", "m2repository", "com", "android", "support", "appcompat-v7");
324-
selectedAppCompatVersion = this.getMatchingDir(pathToAppCompat, requiredAppCompatRange).wait();
322+
selectedAppCompatVersion = this.getMatchingDir(pathToAppCompat, requiredAppCompatRange);
325323
}
326324

327325
this.$logger.trace(`Selected AppCompat version is: ${selectedAppCompatVersion}`);

lib/commands/list-platforms.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export class ListPlatformsCommand implements ICommand {
66

77
execute(args: string[]): IFuture<void> {
88
return (() => {
9-
let installedPlatforms = this.$platformService.getInstalledPlatforms().wait();
9+
let installedPlatforms = this.$platformService.getInstalledPlatforms();
1010

1111
if(installedPlatforms.length > 0) {
1212
let preparedPlatforms = this.$platformService.getPreparedPlatforms();
@@ -17,7 +17,7 @@ export class ListPlatformsCommand implements ICommand {
1717
}
1818
this.$logger.out("Installed platforms: ", helpers.formatListOfNames(installedPlatforms, "and"));
1919
} else {
20-
let formattedPlatformsList = helpers.formatListOfNames(this.$platformService.getAvailablePlatforms().wait(), "and");
20+
let formattedPlatformsList = helpers.formatListOfNames(this.$platformService.getAvailablePlatforms(), "and");
2121
this.$logger.out("Available platforms for this OS: ", formattedPlatformsList);
2222
this.$logger.out("No installed platforms found. Use $ tns platform add");
2323
}

lib/commands/update.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class UpdateCommand implements ICommand {
3030
}
3131

3232
try {
33-
this.executeCore(args, folders);
33+
this.executeCore(args, folders).wait();
3434
} catch (error) {
3535
shelljs.cp("-f", path.join(tmpDir, "package.json"), this.$projectData.projectDir);
3636
for (let folder of folders) {
@@ -51,40 +51,42 @@ export class UpdateCommand implements ICommand {
5151
}).future<boolean>()();
5252
}
5353

54-
private executeCore(args: string[], folders: string[]) {
55-
let platforms = this.$platformService.getInstalledPlatforms().wait();
56-
let availablePlatforms = this.$platformService.getAvailablePlatforms().wait();
57-
let packagePlatforms: string[] = [];
54+
private executeCore(args: string[], folders: string[]): IFuture<void> {
55+
return (() => {
56+
let platforms = this.$platformService.getInstalledPlatforms();
57+
let availablePlatforms = this.$platformService.getAvailablePlatforms();
58+
let packagePlatforms: string[] = [];
5859

59-
this.$projectDataService.initialize(this.$projectData.projectDir);
60-
for (let platform of availablePlatforms) {
61-
let platformData = this.$platformsData.getPlatformData(platform);
62-
let platformVersion = this.$projectDataService.getValue(platformData.frameworkPackageName).wait();
63-
if (platformVersion) {
64-
packagePlatforms.push(platform);
65-
this.$projectDataService.removeProperty(platformData.frameworkPackageName).wait();
60+
this.$projectDataService.initialize(this.$projectData.projectDir);
61+
for (let platform of availablePlatforms) {
62+
let platformData = this.$platformsData.getPlatformData(platform);
63+
let platformVersion = this.$projectDataService.getValue(platformData.frameworkPackageName).wait();
64+
if (platformVersion) {
65+
packagePlatforms.push(platform);
66+
this.$projectDataService.removeProperty(platformData.frameworkPackageName).wait();
67+
}
6668
}
67-
}
6869

69-
this.$platformService.removePlatforms(platforms).wait();
70-
this.$pluginsService.remove("tns-core-modules").wait();
71-
this.$pluginsService.remove("tns-core-modules-widgets").wait();
70+
this.$platformService.removePlatforms(platforms).wait();
71+
this.$pluginsService.remove("tns-core-modules").wait();
72+
this.$pluginsService.remove("tns-core-modules-widgets").wait();
7273

73-
for (let folder of folders) {
74-
shelljs.rm("-fr", folder);
75-
}
74+
for (let folder of folders) {
75+
shelljs.rm("-fr", folder);
76+
}
7677

77-
platforms = platforms.concat(packagePlatforms);
78-
if (args.length === 1) {
79-
for (let platform of platforms) {
80-
this.$platformService.addPlatforms([ platform+"@"+args[0] ]).wait();
78+
platforms = platforms.concat(packagePlatforms);
79+
if (args.length === 1) {
80+
for (let platform of platforms) {
81+
this.$platformService.addPlatforms([ platform+"@"+args[0] ]).wait();
82+
}
83+
this.$pluginsService.add("tns-core-modules@" + args[0]).wait();
84+
} else {
85+
this.$platformService.addPlatforms(platforms).wait();
86+
this.$pluginsService.add("tns-core-modules").wait();
8187
}
82-
this.$pluginsService.add("tns-core-modules@" + args[0]).wait();
83-
} else {
84-
this.$platformService.addPlatforms(platforms).wait();
85-
this.$pluginsService.add("tns-core-modules").wait();
86-
}
87-
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
88+
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
89+
}).future<void>()();
8890
}
8991

9092
allowedParameters: ICommandParameter[] = [];

lib/definitions/platform.d.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
interface IPlatformService {
22
addPlatforms(platforms: string[]): IFuture<void>;
3-
getInstalledPlatforms(): IFuture<string[]>;
4-
getAvailablePlatforms(): IFuture<string[]>;
3+
4+
/**
5+
* Gets list of all installed platforms (the ones for which <project dir>/platforms/<platform> exists).
6+
* @returns {string[]} List of currently installed platforms.
7+
*/
8+
getInstalledPlatforms(): string[];
9+
10+
/**
11+
* Gets a list of all platforms that can be used on current OS, but are not installed at the moment.
12+
* @returns {string[]} List of all available platforms.
13+
*/
14+
getAvailablePlatforms(): string[];
515

616
/**
717
* Returns a list of all currently prepared platforms.
@@ -20,8 +30,20 @@ interface IPlatformService {
2030
validatePlatformInstalled(platform: string): void;
2131
validatePlatform(platform: string): void;
2232

23-
getLatestApplicationPackageForDevice(platformData: IPlatformData): IFuture<IApplicationPackage>;
24-
getLatestApplicationPackageForEmulator(platformData: IPlatformData): IFuture<IApplicationPackage>;
33+
/**
34+
* Returns information about the latest built application for device in the current project.
35+
* @param {IPlatformData} platformData Data describing the current platform.
36+
* @returns {IApplicationPackage} Information about latest built application.
37+
*/
38+
getLatestApplicationPackageForDevice(platformData: IPlatformData): IApplicationPackage;
39+
40+
/**
41+
* Returns information about the latest built application for simulator in the current project.
42+
* @param {IPlatformData} platformData Data describing the current platform.
43+
* @returns {IApplicationPackage} Information about latest built application.
44+
*/
45+
getLatestApplicationPackageForEmulator(platformData: IPlatformData): IApplicationPackage;
46+
2547
copyLastOutput(platform: string, targetPath: string, settings: {isForDevice: boolean}): IFuture<void>;
2648
lastOutputPath(platform: string, settings: { isForDevice: boolean }): string;
2749
ensurePlatformInstalled(platform: string): IFuture<void>;

lib/providers/livesync-provider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ export class LiveSyncProvider implements ILiveSyncProvider {
6060
this.$platformService.buildPlatform(device.deviceInfo.platform, {buildForDevice: !device.isEmulator}).wait();
6161
let platformData = this.$platformsData.getPlatformData(device.deviceInfo.platform);
6262
if (device.isEmulator) {
63-
return this.$platformService.getLatestApplicationPackageForEmulator(platformData).wait().packageName;
63+
return this.$platformService.getLatestApplicationPackageForEmulator(platformData).packageName;
6464
}
6565

66-
return this.$platformService.getLatestApplicationPackageForDevice(platformData).wait().packageName;
66+
return this.$platformService.getLatestApplicationPackageForDevice(platformData).packageName;
6767
}).future<string>()();
6868
}
6969

lib/services/android-debug-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class AndroidDebugService implements IDebugService {
115115
this.$options.forDevice = !!cachedDeviceOption;
116116

117117
let platformData = this.$platformsData.getPlatformData(this.platform);
118-
packageFile = this.$platformService.getLatestApplicationPackageForDevice(platformData).wait().packageName;
118+
packageFile = this.$platformService.getLatestApplicationPackageForDevice(platformData).packageName;
119119
this.$logger.out("Using ", packageFile);
120120
}
121121

lib/services/android-project-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
168168
private cleanResValues(targetSdkVersion: number, frameworkVersion: string): IFuture<void> {
169169
return (() => {
170170
let resDestinationDir = this.getAppResourcesDestinationDirectoryPath(frameworkVersion).wait();
171-
let directoriesInResFolder = this.$fs.readDirectory(resDestinationDir).wait();
171+
let directoriesInResFolder = this.$fs.readDirectory(resDestinationDir);
172172
let directoriesToClean = directoriesInResFolder
173173
.map(dir => {
174174
return {
@@ -325,7 +325,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
325325
return (() => {
326326
let resourcesDirPath = path.join(appResourcesDirectoryPath, this.platformData.normalizedPlatformName);
327327
let valuesDirRegExp = /^values/;
328-
let resourcesDirs = this.$fs.readDirectory(resourcesDirPath).wait().filter(resDir => !resDir.match(valuesDirRegExp));
328+
let resourcesDirs = this.$fs.readDirectory(resourcesDirPath).filter(resDir => !resDir.match(valuesDirRegExp));
329329
_.each(resourcesDirs, resourceDir => {
330330
this.$fs.deleteDirectory(path.join(this.getAppResourcesDestinationDirectoryPath().wait(), resourceDir));
331331
});

lib/services/app-files-updater.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class AppFilesUpdater {
3939

4040
protected readDestinationDir(): string[] {
4141
if (this.fs.exists(this.appDestinationDirectoryPath)) {
42-
return this.fs.readDirectory(this.appDestinationDirectoryPath).wait();
42+
return this.fs.readDirectory(this.appDestinationDirectoryPath);
4343
} else {
4444
return [];
4545
}

lib/services/emulator-settings-service.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ export class EmulatorSettingsService implements Mobile.IEmulatorSettingsService
33

44
constructor(private $injector: IInjector) { }
55

6-
public canStart(platform: string): IFuture<boolean> {
7-
return (() => {
8-
let platformService = this.$injector.resolve("platformService"); // this should be resolved here due to cyclic dependency
6+
public canStart(platform: string): boolean {
7+
let platformService = this.$injector.resolve("platformService"); // this should be resolved here due to cyclic dependency
98

10-
let installedPlatforms = platformService.getInstalledPlatforms().wait();
11-
return _.includes(installedPlatforms, platform.toLowerCase());
12-
}).future<boolean>()();
9+
let installedPlatforms = platformService.getInstalledPlatforms();
10+
return _.includes(installedPlatforms, platform.toLowerCase());
1311
}
1412

1513
public get minVersion(): number {

lib/services/ios-debug-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class IOSDebugService implements IDebugService {
108108
if (this.$options.rebuild) {
109109
this.$platformService.buildPlatform(this.platform).wait();
110110
}
111-
let emulatorPackage = this.$platformService.getLatestApplicationPackageForEmulator(platformData).wait();
111+
let emulatorPackage = this.$platformService.getLatestApplicationPackageForEmulator(platformData);
112112

113113
let args = shouldBreak ? "--nativescript-debug-brk" : "--nativescript-debug-start";
114114
let child_process = this.$iOSEmulatorServices.runApplicationOnEmulator(emulatorPackage.packageName, {

0 commit comments

Comments
 (0)