-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnpm-interactions.ts
134 lines (107 loc) · 3.32 KB
/
npm-interactions.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
/**
* Rotten Deps NPM Interactions library
* @module
*/
import * as proc from 'child_process';
export interface PackageDetails {
time: Record<string, string>;
name: string;
}
export interface OutdatedPackage {
current: string;
wanted: string;
latest: string;
location: string;
}
export interface OutdatedData {
[key: string]: OutdatedPackage;
}
interface OutdatedRequest {
(): Promise<OutdatedData|Error>;
}
interface DetailsRequest {
(): Promise<PackageDetails|Error>;
}
interface ListDependency {
version: string;
resolved: string;
}
interface ListResponse {
version: string,
name: string,
dependencies: Record<string, ListDependency>;
}
interface ListWithHelpers {
data: ListResponse;
getListOfInstalledDependencies: () => Array<string>;
}
interface ListRequest {
(): Promise<ListWithHelpers | Error>;
}
/**
* Creates a function for running `npm outdated`
*/
export const createOutdatedRequest = (): OutdatedRequest => {
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const args = ['outdated', '--json'];
/* NPM 7.x included a breaking change to the exit codes for the `outdated` command.
Prior to v7 the command would exit 1 if you had any outdated dependencies.
After v7 it exits 0 */
return () => new Promise(resolve => {
proc.execFile(command, args, { encoding: 'utf8' }, (err, stdout) => {
if (err) {
if (!err.message.includes('Command failed')) resolve(err);
if (err.code !== 1) resolve(err);
// hail mary attempt to parse the stdout in spite of the error
try {
resolve(JSON.parse(stdout));
} catch {
resolve(err);
}
}
resolve(JSON.parse(stdout));
});
});
};
/**
* Creates a function to run the `npm view` command for a specific dependency
* @param dependencyName
*/
export const createDetailsRequest = (dependencyName: string): DetailsRequest => {
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const args = ['view', '--json', dependencyName];
return (): Promise<PackageDetails | Error> => new Promise(resolve => {
proc.execFile(command, args, { encoding: 'utf8' }, (err, stdout) => {
if (err) resolve(err);
resolve(JSON.parse(stdout));
});
});
};
const isDependencyList = (result: ListResponse | any): result is ListResponse =>
(result as ListResponse).version !== undefined
&& (result as ListResponse).name !== undefined
&& (result as ListResponse).dependencies !== undefined;
/**
* Uses the `npm ls` command to get a list of installed dependencies of a project.
* @param prod if this is set to true dev dependencies will be ignored
*/
export const createListRequest = (prod = false): ListRequest => {
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const args = ['ls', '--json'];
if (prod) args.push('--prod');
return () => new Promise(resolve => {
proc.execFile(command, args, { encoding: 'utf8' }, (err, stdout) => {
const results = JSON.parse(stdout);
if (!isDependencyList(results)) resolve(new Error('unexpected list response'));
resolve({
data: results,
getListOfInstalledDependencies: () => Object.getOwnPropertyNames(results.dependencies),
});
});
});
};
export default {
createOutdatedRequest,
createDetailsRequest,
createListRequest,
};