UNPKG

2.67 kBJavaScriptView Raw
1/* eslint-disable global-require */
2/* eslint-disable no-console */
3/* global DICT */
4
5'use strict';
6
7(function installDiagnostic() {
8 const fs = require('fs');
9 const path = require('path');
10 const win32 = process.platform === 'win32';
11
12 if (process.env.DEBUG_PKG === '2') {
13 console.log(Object.entries(DICT));
14 }
15 function dumpLevel(filename, level, tree) {
16 let totalSize = 0;
17 const d = fs.readdirSync(filename);
18 for (let j = 0; j < d.length; j += 1) {
19 const f = path.join(filename, d[j]);
20 const realPath = fs.realpathSync(f);
21 const isSymbolicLink2 = f !== realPath;
22
23 const s = fs.statSync(f);
24 totalSize += s.size;
25
26 if (s.isDirectory() && !isSymbolicLink2) {
27 const tree1 = [];
28 totalSize += dumpLevel(f, level + 1, tree1);
29 const str =
30 (' '.padStart(level * 2, ' ') + d[j]).padEnd(40, ' ') +
31 (totalSize.toString().padStart(10, ' ') +
32 (isSymbolicLink2 ? `=> ${realPath}` : ' '));
33 tree.push(str);
34 tree1.forEach((x) => tree.push(x));
35 } else {
36 const str =
37 (' '.padStart(level * 2, ' ') + d[j]).padEnd(40, ' ') +
38 (s.size.toString().padStart(10, ' ') +
39 (isSymbolicLink2 ? `=> ${realPath}` : ' '));
40 tree.push(str);
41 }
42 }
43 return totalSize;
44 }
45 function wrap(obj, name) {
46 const f = fs[name];
47 obj[name] = (...args) => {
48 const args1 = Object.values(args);
49 console.log(
50 `fs.${name}`,
51 args1.filter((x) => typeof x === 'string')
52 );
53 return f.apply(this, args1);
54 };
55 }
56 if (process.env.DEBUG_PKG) {
57 console.log('------------------------------- virtual file system');
58 const startFolder = win32 ? 'C:\\snapshot' : '/snapshot';
59 console.log(startFolder);
60
61 const tree = [];
62 const totalSize = dumpLevel(startFolder, 1, tree);
63 console.log(tree.join('\n'));
64
65 console.log('Total size = ', totalSize);
66 if (process.env.DEBUG_PKG === '2') {
67 wrap(fs, 'openSync');
68 wrap(fs, 'open');
69 wrap(fs, 'readSync');
70 wrap(fs, 'read');
71 wrap(fs, 'writeSync');
72 wrap(fs, 'write');
73 wrap(fs, 'closeSync');
74 wrap(fs, 'readFileSync');
75 wrap(fs, 'close');
76 wrap(fs, 'readFile');
77 wrap(fs, 'readdirSync');
78 wrap(fs, 'readdir');
79 wrap(fs, 'realpathSync');
80 wrap(fs, 'realpath');
81 wrap(fs, 'statSync');
82 wrap(fs, 'stat');
83 wrap(fs, 'lstatSync');
84 wrap(fs, 'lstat');
85 wrap(fs, 'fstatSync');
86 wrap(fs, 'fstat');
87 wrap(fs, 'existsSync');
88 wrap(fs, 'exists');
89 wrap(fs, 'accessSync');
90 wrap(fs, 'access');
91 }
92 }
93})();