-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnode-package-manager.ts
183 lines (158 loc) · 5.67 KB
/
node-package-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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
///<reference path=".d.ts"/>
"use strict";
import Future = require("fibers/future");
import npm = require("npm");
import path = require("path");
import semver = require("semver");
import shell = require("shelljs");
import helpers = require("./common/helpers");
import constants = require("./constants");
import options = require("./common/options");
export class NodePackageManager implements INodePackageManager {
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
private static NPM_REGISTRY_URL = "http://registry.npmjs.org/";
private versionsCache: IDictionary<string[]>;
constructor(private $logger: ILogger,
private $errors: IErrors,
private $httpClient: Server.IHttpClient,
private $fs: IFileSystem,
private $lockfile: ILockFile) {
this.versionsCache = {};
this.load().wait();
}
public getCacheRootPath(): string {
return npm.cache;
}
public addToCache(packageName: string, version: string): IFuture<void> {
return (() => {
this.addToCacheCore(packageName, version).wait();
var packagePath = path.join(npm.cache, packageName, version, "package");
if(!this.isPackageUnpacked(packagePath).wait()) {
this.cacheUnpack(packageName, version).wait();
}
}).future<void>()();
}
public load(config?: any): IFuture<void> {
var future = new Future<void>();
npm.load(config, (err) => {
if(err) {
future.throw(err);
} else {
future.return();
}
});
return future;
}
public install(packageName: string, opts?: INpmInstallOptions): IFuture<string> {
return (() => {
this.$lockfile.lock().wait();
try {
var packageToInstall = packageName;
var pathToSave = (opts && opts.pathToSave) || npm.cache;
var version = (opts && opts.version) || null;
return this.installCore(packageToInstall, pathToSave, version).wait();
} catch(error) {
this.$logger.debug(error);
this.$errors.fail("%s. Error: %s", NodePackageManager.NPM_LOAD_FAILED, error);
} finally {
this.$lockfile.unlock().wait();
}
}).future<string>()();
}
public getLatestVersion(packageName: string): IFuture<string> {
return (() => {
var versions = this.getAvailableVersions(packageName).wait();
versions = _.sortBy(versions, (ver: string) => { return ver; });
return versions.reverse()[0];
}).future<string>()();
}
public getCachedPackagePath(packageName: string, version: string): string {
return path.join(npm.cache, packageName, version, "package");
}
private installCore(packageName: string, pathToSave: string, version: string): IFuture<string> {
return (() => {
if (options.frameworkPath) {
if (this.$fs.getFsStats(options.frameworkPath).wait().isFile()) {
this.npmInstall(packageName, pathToSave, version).wait();
var pathToNodeModules = path.join(pathToSave, "node_modules");
var folders = this.$fs.readDirectory(pathToNodeModules).wait();
return path.join(pathToNodeModules, folders[0]);
}
return options.frameworkPath;
} else {
version = version || this.getLatestVersion(packageName).wait();
var packagePath = this.getCachedPackagePath(packageName, version);
if (!this.isPackageCached(packagePath).wait()) {
this.addToCacheCore(packageName, version).wait();
}
if(!this.isPackageUnpacked(packagePath).wait()) {
this.cacheUnpack(packageName, version).wait();
}
return packagePath;
}
}).future<string>()();
}
private npmInstall(packageName: string, pathToSave: string, version: string): IFuture<void> {
this.$logger.out("Installing ", packageName);
var incrementedVersion = semver.inc(version, constants.ReleaseType.MINOR);
if (!options.frameworkPath && packageName.indexOf("@") < 0) {
packageName = packageName + "@<" + incrementedVersion;
}
var future = new Future<void>();
npm.commands["install"](pathToSave, packageName, (err: Error, data: any) => {
if(err) {
future.throw(err);
} else {
this.$logger.out("Installed ", packageName);
future.return(data);
}
});
return future;
}
private isPackageCached(packagePath: string): IFuture<boolean> {
return this.$fs.exists(packagePath);
}
private isPackageUnpacked(packagePath: string): IFuture<boolean> {
return (() => {
return this.$fs.getFsStats(packagePath).wait().isDirectory() &&
this.$fs.enumerateFilesInDirectorySync(packagePath).length > 1;
}).future<boolean>()();
}
private addToCacheCore(packageName: string, version: string): IFuture<void> {
var future = new Future<void>();
// cache.add = function (pkg, ver, where, scrub, cb)
npm.commands["cache"].add(packageName, version, undefined, false, (err: Error, data: any) => {
if(err) {
future.throw(err);
} else {
future.return();
}
});
return future;
}
public cacheUnpack(packageName: string, version: string, unpackTarget?: string): IFuture<void> {
var future = new Future<void>();
unpackTarget = unpackTarget || path.join(npm.cache, packageName, version, "package");
// function unpack (pkg, ver, unpackTarget, dMode, fMode, uid, gid, cb)
npm.commands["cache"].unpack(packageName, version, unpackTarget, null, null, null, null, (err: Error, data: any) => {
if(err) {
future.throw(err);
} else {
future.return();
}
});
return future;
}
private getAvailableVersions(packageName: string): IFuture<string[]> {
return (() => {
if(!this.versionsCache[packageName]) {
var url = NodePackageManager.NPM_REGISTRY_URL + packageName;
var response = this.$httpClient.httpRequest(url).wait().body;
var json = JSON.parse(response);
this.versionsCache[packageName] = _.keys(json.versions);
}
return this.versionsCache[packageName];
}).future<string[]>()();
}
}
$injector.register("npm", NodePackageManager);