-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.ts
160 lines (144 loc) · 4.61 KB
/
Utils.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
import * as https from 'https';
import * as os from 'os';
import { spawn } from 'child_process';
export const noop = () => {};
export const prettyGitURL = (repo: string | null) => {
if (repo) {
return repo
.replace('git+', '')
.replace('git@', 'https://')
.replace('git://', 'https://')
.replace('github.com:', 'github.com/')
.replace('github:', 'github.com/')
.replace('gitlab.com:', 'gitlab.com/')
.replace('gitlab:', 'gitlab.com/')
.replace('bitbucket.org:', 'bitbucket.org/')
.replace('bitbucket:', 'bitbucket.org/')
.replace('.git', '');
}
return repo;
};
export const safeURL = (value: string) => {
try {
return new URL(value);
} catch {
return {
hash: null,
host: null,
hostname: null,
href: null,
origin: null,
password: null,
pathname: null,
port: null,
protocol: null,
search: null,
searchParams: null,
username: null,
};
}
};
export const ping = async (_url: string) => {
return new Promise<{result: boolean | string, status: number | undefined}>((resolve, reject) => {
const pathParts = safeURL(_url);
const options: https.RequestOptions = {
hostname: pathParts.hostname,
port: pathParts.port,
path: pathParts.pathname,
method: 'HEAD',
};
const req = https.request(options, (res) => {
const data:any[] = [];
res.on('data', chunk => {
data.push(chunk);
});
res.on('end', () => {
// For testing.
// const resultString = Buffer.concat(data).toString();
switch (res.statusCode) {
case 200:
resolve({result: true, status: 200});
break;
case 301:
case 302:
case 307:
case 308:
resolve({result: res.headers.location !== undefined ? res.headers.location : true, status: res.statusCode});
break;
case 404:
resolve({result: false, status: res.statusCode});
break;
case 429:
resolve({result: false, status: res.statusCode});
break;
default:
resolve({result: false, status: res.statusCode});
}
});
});
req.on('error', (e) => {
resolve({result: false, status: -1});
});
req.end();
});
};
export const npmDepsToPaths = async (cwd: string, deep: boolean = false) => {
const args = ['ls', '--prod', '--json', '--depth', deep ? 'Infinity' : '0'];
const deps = await npm(cwd, args).catch(e => null);
if (deps !== null) {
const result = safeParseJSON(deps);
const packagePaths: string[] = [];
if (result) {
walkPath(result.dependencies, packagePaths);
return packagePaths;
} else {
return null;
}
} else {
return null;
}
};
const safeParseJSON = (value: string) => {
try {
return JSON.parse(value);
} catch {
return null;
}
};
const npm = (cwd: string, args: string[]) => {
return new Promise<string>((resolve, reject) => {
try {
const npm = spawn(`npm${os.platform() === 'win32'?'.cmd':''}`, args, { cwd });
const buffer: Buffer[] = [];
npm.stdout.on('data', (data) => {
buffer.push(data);
});
npm.on('close', () => {
resolve(Buffer.concat(buffer).toString());
});
} catch (e) {
throw e;
}
});
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const walkPath = (data: any, results: any[]) => {
for (const key in data) {
if (Object.prototype.hasOwnProperty.call(data, key)) {
const module = data[key];
if (results.find((p) => p === key) === undefined) {
results.push(key);
}
if (module.dependencies) {
walkPath(module.dependencies, results);
}
}
}
};
export const wait = async (delay = 1000):Promise<void> => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, delay);
});
};