forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
67 lines (61 loc) · 2.06 KB
/
util.js
File metadata and controls
67 lines (61 loc) · 2.06 KB
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
import { promises as fs } from 'node:fs';
import path from 'node:path';
export async function getNodeV8Version(cwd) {
try {
const v8VersionH = await fs.readFile(
`${cwd}/deps/v8/include/v8-version.h`,
'utf8'
);
const major = parseInt(/V8_MAJOR_VERSION (\d+)/.exec(v8VersionH)[1], 10);
const minor = parseInt(/V8_MINOR_VERSION (\d+)/.exec(v8VersionH)[1], 10);
const build = parseInt(/V8_BUILD_NUMBER (\d+)/.exec(v8VersionH)[1], 10);
const patch = parseInt(/V8_PATCH_LEVEL (\d+)/.exec(v8VersionH)[1], 10);
return {
major,
minor,
build,
patch,
majorMinor: major * 10 + minor,
toString() {
return this.patch
? `${this.major}.${this.minor}.${this.build}.${this.patch}`
: `${this.major}.${this.minor}.${this.build}`;
}
};
} catch (e) {
throw new Error('Could not find V8 version');
}
};
export function filterForVersion(list, version) {
return list.filter((dep) => {
return dep.since <= version.majorMinor &&
(dep.until || Infinity) >= version.majorMinor;
});
}
export async function addToGitignore(nodeDir, value) {
const gitignorePath = path.join(nodeDir, 'deps/v8/.gitignore');
const gitignore = await fs.readFile(gitignorePath, 'utf8');
if (!gitignore.includes(value)) {
await fs.appendFile(gitignorePath, `${value}\n`);
}
}
export async function replaceGitignore(nodeDir, options) {
const gitignorePath = path.join(nodeDir, 'deps/v8/.gitignore');
let gitignore = await fs.readFile(gitignorePath, 'utf8');
if (!gitignore.includes(options.replace)) {
gitignore = gitignore.replace(options.match, options.replace);
}
await fs.writeFile(gitignorePath, gitignore);
}
export function removeDirectory(path) {
if (typeof fs.rm !== 'undefined') {
return fs.rm(path, { recursive: true, force: true });
} else {
// Node.js 12 doesn't have `rm`, and `rmdir` emits a deprecation warning in
// Node.js 16+.
return fs.rmdir(path, { recursive: true });
}
}
export function isVersionString(str) {
return /^\d+(\.\d+)+$/.test(str);
}