Skip to content

Commit ef1048b

Browse files
Add checks for all cached packages
NPM Cache gets broken in some cases. Make sure to validate the sahsum of the cached package. Also validate specific directories in our `tns` related packages from npm, for example tns runtimes must have "framework" directory in the root of the unpacked dir. Fixes NativeScript/NativeScript#763
1 parent 5d83392 commit ef1048b

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

lib/npm-installation-manager.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import * as constants from "./constants";
99
export class NpmInstallationManager implements INpmInstallationManager {
1010
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
1111
private versionsCache: IDictionary<string[]>;
12+
private packageSpecificDirectories: IStringDictionary = {
13+
"tns-android": constants.PROJECT_FRAMEWORK_FOLDER_NAME,
14+
"tns-ios": constants.PROJECT_FRAMEWORK_FOLDER_NAME,
15+
"tns-template-hello-world": constants.APP_RESOURCES_FOLDER_NAME
16+
};
1217

1318
constructor(private $npm: INodePackageManager,
1419
private $logger: ILogger,
@@ -36,7 +41,7 @@ export class NpmInstallationManager implements INpmInstallationManager {
3641
}
3742

3843
if(!this.isShasumOfPackageCorrect(packageName, version).wait()) {
39-
// In some cases the package is not fully downloaded and the framework directory is missing
44+
// In some cases the package is not fully downloaded and there are missing directories
4045
// Try removing the old package and add the real one to cache again
4146
this.addCleanCopyToCache(packageName, version).wait();
4247
}
@@ -94,7 +99,7 @@ export class NpmInstallationManager implements INpmInstallationManager {
9499
return (() => {
95100
this.$npm.cache(packageName, version).wait();
96101
let packagePath = path.join(this.getCacheRootPath(), packageName, version, "package");
97-
if(!this.isPackageUnpacked(packagePath).wait()) {
102+
if(!this.isPackageUnpacked(packagePath, packageName).wait()) {
98103
this.cacheUnpack(packageName, version).wait();
99104
}
100105
}).future<void>()();
@@ -128,13 +133,7 @@ export class NpmInstallationManager implements INpmInstallationManager {
128133
} else {
129134
version = version || this.getLatestVersion(packageName).wait();
130135
let packagePath = this.getCachedPackagePath(packageName, version);
131-
if (!this.isPackageCached(packagePath).wait()) {
132-
this.$npm.cache(packageName, version).wait();
133-
}
134-
135-
if(!this.isPackageUnpacked(packagePath).wait()) {
136-
this.cacheUnpack(packageName, version).wait();
137-
}
136+
this.addToCache(packageName, version).wait();
138137
return packagePath;
139138
}
140139
}).future<string>()();
@@ -151,15 +150,17 @@ export class NpmInstallationManager implements INpmInstallationManager {
151150
return this.$npm.install(packageName, pathToSave);
152151
}
153152

154-
private isPackageCached(packagePath: string): IFuture<boolean> {
155-
return this.$fs.exists(packagePath);
156-
}
157-
158-
private isPackageUnpacked(packagePath: string): IFuture<boolean> {
153+
private isPackageUnpacked(packagePath: string, packageName: string): IFuture<boolean> {
159154
return (() => {
155+
let additionalDirectoryToCheck = this.packageSpecificDirectories[packageName];
160156
return this.$fs.getFsStats(packagePath).wait().isDirectory() &&
161-
this.$fs.exists(path.join(packagePath, "framework")).wait() &&
162-
this.$fs.enumerateFilesInDirectorySync(path.join(packagePath, "framework")).length > 1;
157+
(!additionalDirectoryToCheck || this.hasFilesInDirectory(path.join(packagePath, additionalDirectoryToCheck)).wait());
158+
}).future<boolean>()();
159+
}
160+
161+
private hasFilesInDirectory(directory: string): IFuture<boolean> {
162+
return ((): boolean => {
163+
return this.$fs.exists(directory).wait() && this.$fs.enumerateFilesInDirectorySync(directory).length > 0;
163164
}).future<boolean>()();
164165
}
165166
}

0 commit comments

Comments
 (0)