-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathnpm-installation-manager.ts
150 lines (124 loc) · 5.55 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
import * as path from "path";
import * as semver from "semver";
import * as constants from "./constants";
export class NpmInstallationManager implements INpmInstallationManager {
constructor(private $npm: INodePackageManager,
private $childProcess: IChildProcess,
private $logger: ILogger,
private $errors: IErrors,
private $options: IOptions,
private $fs: IFileSystem,
private $staticConfig: IStaticConfig) {
}
public getLatestVersion(packageName: string): IFuture<string> {
return (() => {
return this.getVersion(packageName, constants.PackageVersion.LATEST).wait();
}).future<string>()();
}
public getNextVersion(packageName: string): IFuture<string> {
return (() => {
return this.getVersion(packageName, constants.PackageVersion.NEXT).wait();
}).future<string>()();
}
public getLatestCompatibleVersion(packageName: string): IFuture<string> {
return (() => {
let cliVersionRange = `~${this.$staticConfig.version}`;
let latestVersion = this.getLatestVersion(packageName).wait();
if (semver.satisfies(latestVersion, cliVersionRange)) {
return latestVersion;
}
let data = this.$npm.view(packageName, { json: true, "versions": true }).wait();
return semver.maxSatisfying(data, cliVersionRange) || latestVersion;
}).future<string>()();
}
public install(packageName: string, projectDir: string, opts?: INpmInstallOptions): IFuture<any> {
return (() => {
try {
let packageToInstall = this.$options.frameworkPath || packageName;
let pathToSave = projectDir;
let version = (opts && opts.version) || null;
let dependencyType = (opts && opts.dependencyType) || null;
return this.installCore(packageToInstall, pathToSave, version, dependencyType).wait();
} catch (error) {
this.$logger.debug(error);
throw new Error(error);
}
}).future<string>()();
}
public getInspectorFromCache(inspectorNpmPackageName: string, projectDir: string): IFuture<string> {
return (() => {
let inspectorPath = path.join(projectDir, "node_modules", inspectorNpmPackageName);
// local installation takes precedence over cache
if (!this.inspectorAlreadyInstalled(inspectorPath)) {
let cachepath = this.$childProcess.exec("npm get cache").wait().trim();
let version = this.getLatestCompatibleVersion(inspectorNpmPackageName).wait();
let pathToPackageInCache = path.join(cachepath, inspectorNpmPackageName, version);
let pathToUnzippedInspector = path.join(pathToPackageInCache, "package");
if (!this.$fs.exists(pathToPackageInCache)) {
this.$childProcess.exec(`npm cache add ${inspectorNpmPackageName}@${version}`).wait();
let inspectorTgzPathInCache = path.join(pathToPackageInCache, "package.tgz");
this.$childProcess.exec(`tar -xf ${inspectorTgzPathInCache} -C ${pathToPackageInCache}`).wait();
this.$childProcess.exec(`npm install --prefix ${pathToUnzippedInspector}`).wait();
}
this.$logger.out("Using inspector from cache.");
return pathToUnzippedInspector;
}
return inspectorPath;
}).future<string>()();
}
private inspectorAlreadyInstalled(pathToInspector: string): Boolean {
if (this.$fs.exists(pathToInspector)) {
return true;
}
return false;
}
private installCore(packageName: string, pathToSave: string, version: string, dependencyType: string): IFuture<string> {
return (() => {
const possiblePackageName = path.resolve(packageName);
if (this.$fs.exists(possiblePackageName)) {
packageName = possiblePackageName;
}
if (packageName.indexOf(".tgz") >= 0) {
version = null;
}
// check if the packageName is url or local file and if it is, let npm install deal with the version
if (this.isURL(packageName) || this.$fs.exists(packageName)) {
version = null;
} else {
version = version || this.getLatestCompatibleVersion(packageName).wait();
}
let installedModuleNames = this.npmInstall(packageName, pathToSave, version, dependencyType).wait();
let installedPackageName = installedModuleNames[0];
let pathToInstalledPackage = path.join(pathToSave, "node_modules", installedPackageName);
return pathToInstalledPackage;
}).future<string>()();
}
private isURL(str: string) {
let urlRegex = '^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$';
let url = new RegExp(urlRegex, 'i');
return str.length < 2083 && url.test(str);
}
private npmInstall(packageName: string, pathToSave: string, version: string, dependencyType: string): IFuture<any> {
return (() => {
this.$logger.out("Installing ", packageName);
packageName = packageName + (version ? `@${version}` : "");
let npmOptions: any = { silent: true };
if (dependencyType) {
npmOptions[dependencyType] = true;
}
return this.$npm.install(packageName, pathToSave, npmOptions).wait();
}).future<any>()();
}
/**
* This function must not be used with packageName being a URL or local file,
* because npm view doens't work with those
*/
private getVersion(packageName: string, version: string): IFuture<string> {
return (() => {
let data: any = this.$npm.view(packageName, { json: true, "dist-tags": true }).wait();
this.$logger.trace("Using version %s. ", data[version]);
return data[version];
}).future<string>()();
}
}
$injector.register("npmInstallationManager", NpmInstallationManager);