This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathutils.ts
104 lines (91 loc) · 2.9 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import * as path from 'path';
import * as fs from 'fs';
import * as cp from 'child_process';
const NODE_SHEBANG_MATCHER = new RegExp('#! */usr/bin/env +node');
export function isJavaScript(aPath: string): boolean {
const name = path.basename(aPath).toLowerCase();
if (name.endsWith('.js')) {
return true;
}
try {
const buffer = new Buffer(30);
const fd = fs.openSync(aPath, 'r');
fs.readSync(fd, buffer, 0, buffer.length, 0);
fs.closeSync(fd);
const line = buffer.toString();
if (NODE_SHEBANG_MATCHER.test(line)) {
return true;
}
} catch (e) {
// silently ignore problems
}
return false;
}
export function random(low: number, high: number): number {
return Math.floor(Math.random() * (high - low) + low);
}
export function killTree(processId: number): void {
if (process.platform === 'win32') {
const TASK_KILL = 'C:\\Windows\\System32\\taskkill.exe';
// when killing a process in Windows its child processes are *not* killed but become root processes.
// Therefore we use TASKKILL.EXE
try {
cp.execSync(`${TASK_KILL} /F /T /PID ${processId}`);
} catch (err) {
}
} else {
// on linux and OS X we kill all direct and indirect child processes as well
try {
const cmd = path.join(__dirname, './terminateProcess.sh');
cp.spawnSync(cmd, [ processId.toString() ]);
} catch (err) {
}
}
}
export function isOnPath(program: string): boolean {
if (process.platform === 'win32') {
const WHERE = 'C:\\Windows\\System32\\where.exe';
try {
if (fs.existsSync(WHERE)) {
cp.execSync(`${WHERE} ${program}`);
} else {
// do not report error if 'where' doesn't exist
}
return true;
} catch (Exception) {
// ignore
}
} else {
const WHICH = '/usr/bin/which';
try {
if (fs.existsSync(WHICH)) {
cp.execSync(`${WHICH} '${program}'`);
} else {
// do not report error if 'which' doesn't exist
}
return true;
} catch (Exception) {
}
}
return false;
}
export function trimLastNewline(msg: string): string {
return msg.replace(/(\n|\r\n)$/, '');
}
export function extendObject<T>(toObject: T, fromObject: T): T {
for (let key in fromObject) {
if (fromObject.hasOwnProperty(key)) {
toObject[key] = fromObject[key];
}
}
return toObject;
}
export function stripBOM(s: string): string {
if (s && s[0] === '\uFEFF') {
s = s.substr(1);
}
return s;
}