-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnode-package-manager.ts
67 lines (56 loc) · 1.7 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
///<reference path=".d.ts"/>
import npm = require("npm");
import Future = require("fibers/future");
import shell = require("shelljs");
import path = require("path");
export class NodePackageManager implements INodePackageManager {
private static NPM_LOAD_FAILED = "Failed to retrieve data from npm. Please try again a little bit later.";
constructor(private $logger: ILogger,
private $errors: IErrors) { }
public get cache(): string {
return npm.cache;
}
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;
}
private installCore(where: string, what: string): IFuture<any> {
var future = new Future<any>();
npm.commands["install"](where, what, (err, data) => {
if(err) {
future.throw(err);
} else {
future.return(data);
}
});
return future;
}
private tryExecuteAction(action: (...args: any[]) => void, ...args: any[]): IFuture<void> {
return (() => {
try {
this.load().wait(); // It's obligatory to execute load before whatever npm function
action.apply(null, args);
} catch(error) {
this.$logger.debug(error);
this.$errors.fail(NodePackageManager.NPM_LOAD_FAILED);
}
}).future<void>()();
}
public install(packageName: string, pathToSave?: string): IFuture<string> {
return (() => {
var action = (packageName: string) => {
this.installCore(pathToSave || npm.cache, packageName).wait();
};
this.tryExecuteAction(action, packageName).wait();
return path.join(pathToSave || npm.cache, "node_modules", packageName);
}).future<string>()();
}
}
$injector.register("npm", NodePackageManager);