1 | const fs = require('fs');
|
2 | const {versionGteLt} = require('../dist/util');
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function internalModuleReadJSON(path) {
|
11 | let string
|
12 | try {
|
13 | string = fs.readFileSync(path, 'utf8')
|
14 | } catch (e) {
|
15 | if (e.code === 'ENOENT') return []
|
16 | throw e
|
17 | }
|
18 |
|
19 |
|
20 |
|
21 | const containsKeys = true
|
22 | return [string, containsKeys]
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function internalModuleStat(path) {
|
32 | const stat = fs.statSync(path, { throwIfNoEntry: false });
|
33 | if(!stat) return -1;
|
34 | if(stat.isFile()) return 0;
|
35 | if(stat.isDirectory()) return 1;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function internalModuleStatInefficient(path) {
|
43 | try {
|
44 | const stat = fs.statSync(path);
|
45 | if(stat.isFile()) return 0;
|
46 | if(stat.isDirectory()) return 1;
|
47 | } catch(e) {
|
48 | return -e.errno || -1;
|
49 | }
|
50 | }
|
51 |
|
52 | const statSupportsThrowIfNoEntry = versionGteLt(process.versions.node, '15.3.0') ||
|
53 | versionGteLt(process.versions.node, '14.17.0', '15.0.0');
|
54 |
|
55 | module.exports = {
|
56 | internalModuleReadJSON,
|
57 | internalModuleStat: statSupportsThrowIfNoEntry ? internalModuleStat : internalModuleStatInefficient
|
58 | };
|