-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnpm-installation-manager.ts
167 lines (144 loc) · 6.38 KB
/
npm-installation-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
///<reference path=".d.ts"/>
"use strict";
import * as path from "path";
import semver = require("semver");
import * as npm from "npm";
import * as constants from "./constants";
export class NpmInstallationManager implements INpmInstallationManager {
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
private versionsCache: IDictionary<string[]>;
private packageSpecificDirectories: IStringDictionary = {
"tns-android": constants.PROJECT_FRAMEWORK_FOLDER_NAME,
"tns-ios": constants.PROJECT_FRAMEWORK_FOLDER_NAME,
"tns-template-hello-world": constants.APP_RESOURCES_FOLDER_NAME
};
constructor(private $npm: INodePackageManager,
private $logger: ILogger,
private $lockfile: ILockFile,
private $errors: IErrors,
private $options: IOptions,
private $fs: IFileSystem) {
this.versionsCache = {};
this.$npm.load().wait();
}
public getCacheRootPath(): string {
return this.$npm.getCache();
}
public getCachedPackagePath(packageName: string, version: string): string {
return path.join(this.getCacheRootPath(), packageName, version, "package");
}
public addToCache(packageName: string, version: string): IFuture<void> {
return (() => {
let cachedPackagePath = this.getCachedPackagePath(packageName, version);
if(!this.$fs.exists(cachedPackagePath).wait()) {
this.addToCacheCore(packageName, version).wait();
}
if(!this.isShasumOfPackageCorrect(packageName, version).wait()) {
// In some cases the package is not fully downloaded and there are missing directories
// Try removing the old package and add the real one to cache again
this.addCleanCopyToCache(packageName, version).wait();
}
}).future<void>()();
}
public cacheUnpack(packageName: string, version: string, unpackTarget?: string): IFuture<void> {
unpackTarget = unpackTarget || path.join(npm.cache, packageName, version, "package");
return this.$npm.cacheUnpack(packageName, version, unpackTarget);
}
public getLatestVersion(packageName: string): IFuture<string> {
return (() => {
let data = this.$npm.view(packageName, "dist-tags").wait();
let latestVersion = _.first(_.keys(data));
this.$logger.trace("Using version %s. ", latestVersion);
return latestVersion;
}).future<string>()();
}
public install(packageName: string, opts?: INpmInstallOptions): IFuture<string> {
return (() => {
this.$lockfile.lock().wait();
try {
let packageToInstall = packageName;
let pathToSave = (opts && opts.pathToSave) || npm.cache;
let version = (opts && opts.version) || null;
return this.installCore(packageToInstall, pathToSave, version).wait();
} catch(error) {
this.$logger.debug(error);
this.$errors.fail("%s. Error: %s", NpmInstallationManager.NPM_LOAD_FAILED, error);
} finally {
this.$lockfile.unlock().wait();
}
}).future<string>()();
}
public addCleanCopyToCache(packageName: string, version: string): IFuture<void> {
return (() => {
let packagePath = path.join(this.getCacheRootPath(), packageName, version);
this.$logger.trace(`Deleting: ${packagePath}.`);
this.$fs.deleteDirectory(packagePath).wait();
this.addToCacheCore(packageName, version).wait();
if(!this.isShasumOfPackageCorrect(packageName, version).wait()) {
this.$errors.failWithoutHelp(`Unable to add package ${packageName} with version ${version} to npm cache. Try cleaning your cache and execute the command again.`);
}
}).future<void>()();
}
private addToCacheCore(packageName: string, version: string): IFuture<void> {
return (() => {
this.$npm.cache(packageName, version).wait();
let packagePath = path.join(this.getCacheRootPath(), packageName, version, "package");
if(!this.isPackageUnpacked(packagePath, packageName).wait()) {
this.cacheUnpack(packageName, version).wait();
}
}).future<void>()();
}
private isShasumOfPackageCorrect(packageName: string, version: string): IFuture<boolean> {
return ((): boolean => {
let shasumProperty = "dist.shasum";
let cachedPackagePath = this.getCachedPackagePath(packageName, version);
let realShasum = this.$npm.view(`${packageName}@${version}`, shasumProperty).wait()[version][shasumProperty];
let packageTgz = cachedPackagePath + ".tgz";
let currentShasum = "";
if(this.$fs.exists(packageTgz).wait()) {
currentShasum = this.$fs.getFileShasum(packageTgz).wait();
}
this.$logger.trace(`Checking shasum of package: ${packageName}@${version}: expected ${realShasum}, actual ${currentShasum}.`);
return realShasum === currentShasum;
}).future<boolean>()();
}
private installCore(packageName: string, pathToSave: string, version: string): IFuture<string> {
return (() => {
if (this.$options.frameworkPath) {
if (this.$fs.getFsStats(this.$options.frameworkPath).wait().isFile()) {
this.npmInstall(packageName, pathToSave, version).wait();
let pathToNodeModules = path.join(pathToSave, "node_modules");
let folders = this.$fs.readDirectory(pathToNodeModules).wait();
return path.join(pathToNodeModules, folders[0]);
}
return this.$options.frameworkPath;
} else {
version = version || this.getLatestVersion(packageName).wait();
let packagePath = this.getCachedPackagePath(packageName, version);
this.addToCache(packageName, version).wait();
return packagePath;
}
}).future<string>()();
}
private npmInstall(packageName: string, pathToSave: string, version: string): IFuture<void> {
this.$logger.out("Installing ", packageName);
let incrementedVersion = semver.inc(version, constants.ReleaseType.MINOR);
if (!this.$options.frameworkPath && packageName.indexOf("@") < 0) {
packageName = packageName + "@<" + incrementedVersion;
}
return this.$npm.install(packageName, pathToSave);
}
private isPackageUnpacked(packagePath: string, packageName: string): IFuture<boolean> {
return (() => {
let additionalDirectoryToCheck = this.packageSpecificDirectories[packageName];
return this.$fs.getFsStats(packagePath).wait().isDirectory() &&
(!additionalDirectoryToCheck || this.hasFilesInDirectory(path.join(packagePath, additionalDirectoryToCheck)).wait());
}).future<boolean>()();
}
private hasFilesInDirectory(directory: string): IFuture<boolean> {
return ((): boolean => {
return this.$fs.exists(directory).wait() && this.$fs.enumerateFilesInDirectorySync(directory).length > 0;
}).future<boolean>()();
}
}
$injector.register("npmInstallationManager", NpmInstallationManager);