check-disk-space 3.3.1 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,10 +2,12 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var child_process = require('child_process');
6
- var fs = require('fs');
7
- var os = require('os');
8
- var path = require('path');
5
+ var node_child_process = require('node:child_process');
6
+ var promises = require('node:fs/promises');
7
+ var node_os = require('node:os');
8
+ var node_path = require('node:path');
9
+ var node_process = require('node:process');
10
+ var node_util = require('node:util');
9
11
 
10
12
  class InvalidPathError extends Error {
11
13
  constructor(message) {
@@ -25,18 +27,34 @@ class NoMatchError extends Error {
25
27
  }
26
28
  }
27
29
 
30
+ /**
31
+ * Tells if directory exists
32
+ *
33
+ * @param directoryPath - The file/folder path
34
+ * @param dependencies - Dependencies container
35
+ */
36
+ async function isDirectoryExisting(directoryPath, dependencies) {
37
+ try {
38
+ await dependencies.fsAccess(directoryPath);
39
+ return Promise.resolve(true);
40
+ }
41
+ catch (error) {
42
+ return Promise.resolve(false);
43
+ }
44
+ }
45
+
28
46
  /**
29
47
  * Get the first existing parent path
30
48
  *
31
49
  * @param directoryPath - The file/folder path from where we want to know disk space
32
50
  * @param dependencies - Dependencies container
33
51
  */
34
- function getFirstExistingParentPath(directoryPath, dependencies) {
52
+ async function getFirstExistingParentPath(directoryPath, dependencies) {
35
53
  let parentDirectoryPath = directoryPath;
36
- let parentDirectoryFound = dependencies.fsExistsSync(parentDirectoryPath);
54
+ let parentDirectoryFound = await isDirectoryExisting(parentDirectoryPath, dependencies);
37
55
  while (!parentDirectoryFound) {
38
56
  parentDirectoryPath = dependencies.pathNormalize(parentDirectoryPath + '/..');
39
- parentDirectoryFound = dependencies.fsExistsSync(parentDirectoryPath);
57
+ parentDirectoryFound = await isDirectoryExisting(parentDirectoryPath, dependencies);
40
58
  }
41
59
  return parentDirectoryPath;
42
60
  }
@@ -47,20 +65,18 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
47
65
  * Note: 6.* is Windows 7
48
66
  * Note: PowerShell 3 is natively available since Windows 8
49
67
  *
50
- * @param release - OS Release number
68
+ * @param dependencies - Dependencies Injection Container
51
69
  */
52
- function hasPowerShell3(dependencies) {
70
+ async function hasPowerShell3(dependencies) {
53
71
  const major = parseInt(dependencies.release.split('.')[0], 10);
54
- if (major > 6) {
55
- try {
56
- dependencies.cpExecFileSync('powershell');
57
- return true;
58
- }
59
- catch (err) {
60
- return false;
61
- }
72
+ if (major <= 6) {
73
+ return false;
74
+ }
75
+ try {
76
+ await dependencies.cpExecFile('where', ['powershell'], { windowsHide: true });
77
+ return true;
62
78
  }
63
- else {
79
+ catch (error) {
64
80
  return false;
65
81
  }
66
82
  }
@@ -72,13 +88,12 @@ function hasPowerShell3(dependencies) {
72
88
  * @param dependencies - Dependencies container
73
89
  */
74
90
  function checkDiskSpace(directoryPath, dependencies = {
75
- platform: process.platform,
76
- release: os.release(),
77
- fsExistsSync: fs.existsSync,
78
- pathNormalize: path.normalize,
79
- pathSep: path.sep,
80
- cpExecFile: child_process.execFile,
81
- cpExecFileSync: child_process.execFileSync,
91
+ platform: node_process.platform,
92
+ release: node_os.release(),
93
+ fsAccess: promises.access,
94
+ pathNormalize: node_path.normalize,
95
+ pathSep: node_path.sep,
96
+ cpExecFile: node_util.promisify(node_child_process.execFile),
82
97
  }) {
83
98
  // Note: This function contains other functions in order
84
99
  // to wrap them in a common context and make unit tests easier
@@ -91,9 +106,12 @@ function checkDiskSpace(directoryPath, dependencies = {
91
106
  * @param coefficient - The size coefficient to get bytes instead of kB
92
107
  */
93
108
  function mapOutput(stdout, filter, mapping, coefficient) {
94
- const parsed = stdout.trim().split('\n').slice(1).map(line => {
95
- return line.trim().split(/\s+(?=[\d/])/);
96
- });
109
+ const parsed = stdout
110
+ .split('\n') // Split lines
111
+ .map(line => line.trim()) // Trim all lines
112
+ .filter(line => line.length !== 0) // Remove empty lines
113
+ .slice(1) // Remove header
114
+ .map(line => line.split(/\s+(?=[\d/])/)); // Split on spaces to get columns
97
115
  const filtered = parsed.filter(filter);
98
116
  if (filtered.length === 0) {
99
117
  throw new NoMatchError();
@@ -113,36 +131,28 @@ function checkDiskSpace(directoryPath, dependencies = {
113
131
  * @param mapping - Map between column index and normalized column name
114
132
  * @param coefficient - The size coefficient to get bytes instead of kB
115
133
  */
116
- function check(cmd, filter, mapping, coefficient = 1) {
117
- return new Promise((resolve, reject) => {
118
- const [file, ...args] = cmd;
119
- /* istanbul ignore if */
120
- if (file === undefined) {
121
- return Promise.reject(new Error('cmd must contain at least one item'));
122
- }
123
- dependencies.cpExecFile(file, args, (error, stdout) => {
124
- if (error) {
125
- reject(error);
126
- }
127
- try {
128
- resolve(mapOutput(stdout, filter, mapping, coefficient));
129
- }
130
- catch (error2) {
131
- reject(error2);
132
- }
133
- });
134
- });
134
+ async function check(cmd, filter, mapping, coefficient = 1) {
135
+ const [file, ...args] = cmd;
136
+ /* istanbul ignore if */
137
+ if (file === undefined) {
138
+ return Promise.reject(new Error('cmd must contain at least one item'));
139
+ }
140
+ try {
141
+ const { stdout } = await dependencies.cpExecFile(file, args, { windowsHide: true });
142
+ return mapOutput(stdout, filter, mapping, coefficient);
143
+ }
144
+ catch (error) {
145
+ return Promise.reject(error);
146
+ }
135
147
  }
136
148
  /**
137
149
  * Build the check call for win32
138
150
  *
139
151
  * @param directoryPath - The file/folder path from where we want to know disk space
140
152
  */
141
- function checkWin32(directoryPath) {
153
+ async function checkWin32(directoryPath) {
142
154
  if (directoryPath.charAt(1) !== ':') {
143
- return new Promise((resolve, reject) => {
144
- reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
145
- });
155
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
146
156
  }
147
157
  const powershellCmd = [
148
158
  'powershell',
@@ -154,7 +164,7 @@ function checkDiskSpace(directoryPath, dependencies = {
154
164
  'get',
155
165
  'size,freespace,caption',
156
166
  ];
157
- const cmd = hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
167
+ const cmd = await hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
158
168
  return check(cmd, driveData => {
159
169
  // Only get the drive which match the path
160
170
  const driveLetter = driveData[0];
@@ -170,13 +180,11 @@ function checkDiskSpace(directoryPath, dependencies = {
170
180
  *
171
181
  * @param directoryPath - The file/folder path from where we want to know disk space
172
182
  */
173
- function checkUnix(directoryPath) {
183
+ async function checkUnix(directoryPath) {
174
184
  if (!dependencies.pathNormalize(directoryPath).startsWith(dependencies.pathSep)) {
175
- return new Promise((resolve, reject) => {
176
- reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
177
- });
185
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
178
186
  }
179
- const pathToCheck = getFirstExistingParentPath(directoryPath, dependencies);
187
+ const pathToCheck = await getFirstExistingParentPath(directoryPath, dependencies);
180
188
  return check([
181
189
  'df',
182
190
  '-Pk',
@@ -198,5 +206,5 @@ function checkDiskSpace(directoryPath, dependencies = {
198
206
 
199
207
  exports.InvalidPathError = InvalidPathError;
200
208
  exports.NoMatchError = NoMatchError;
201
- exports["default"] = checkDiskSpace;
209
+ exports.default = checkDiskSpace;
202
210
  exports.getFirstExistingParentPath = getFirstExistingParentPath;
@@ -1,6 +1,5 @@
1
- import { ChildProcess, ExecException } from 'child_process';
2
- import { existsSync } from 'fs';
3
- import { normalize, sep } from 'path';
1
+ import { access } from 'node:fs/promises';
2
+ import { normalize, sep } from 'node:path';
4
3
 
5
4
  declare class InvalidPathError extends Error {
6
5
  name: string;
@@ -12,15 +11,18 @@ declare class NoMatchError extends Error {
12
11
  constructor(message?: string);
13
12
  }
14
13
 
15
- declare type ExecFileException = ExecException & NodeJS.ErrnoException;
16
- declare type Dependencies = {
14
+ type Dependencies = {
17
15
  platform: NodeJS.Platform;
18
16
  release: string;
19
- fsExistsSync: typeof existsSync;
17
+ fsAccess: typeof access;
20
18
  pathNormalize: typeof normalize;
21
19
  pathSep: typeof sep;
22
- cpExecFile: (file: string, args: ReadonlyArray<string> | undefined | null, callback: (error: ExecFileException | null, stdout: string, stderr: string) => void) => ChildProcess;
23
- cpExecFileSync: (command: string) => Buffer;
20
+ cpExecFile: (file: string, args: ReadonlyArray<string> | undefined | null, options: {
21
+ windowsHide: true;
22
+ }) => Promise<{
23
+ stdout: string;
24
+ stderr: string;
25
+ }>;
24
26
  };
25
27
 
26
28
  /**
@@ -29,12 +31,12 @@ declare type Dependencies = {
29
31
  * @param directoryPath - The file/folder path from where we want to know disk space
30
32
  * @param dependencies - Dependencies container
31
33
  */
32
- declare function getFirstExistingParentPath(directoryPath: string, dependencies: Dependencies): string;
34
+ declare function getFirstExistingParentPath(directoryPath: string, dependencies: Dependencies): Promise<string>;
33
35
 
34
36
  /**
35
37
  * `free` and `size` are in bytes
36
38
  */
37
- declare type DiskSpace = {
39
+ type DiskSpace = {
38
40
  diskPath: string;
39
41
  free: number;
40
42
  size: number;
@@ -1,7 +1,9 @@
1
- import { execFile, execFileSync } from 'child_process';
2
- import { existsSync } from 'fs';
3
- import { release } from 'os';
4
- import { normalize, sep } from 'path';
1
+ import { execFile } from 'node:child_process';
2
+ import { access } from 'node:fs/promises';
3
+ import { release } from 'node:os';
4
+ import { normalize, sep } from 'node:path';
5
+ import { platform } from 'node:process';
6
+ import { promisify } from 'node:util';
5
7
 
6
8
  class InvalidPathError extends Error {
7
9
  constructor(message) {
@@ -21,18 +23,34 @@ class NoMatchError extends Error {
21
23
  }
22
24
  }
23
25
 
26
+ /**
27
+ * Tells if directory exists
28
+ *
29
+ * @param directoryPath - The file/folder path
30
+ * @param dependencies - Dependencies container
31
+ */
32
+ async function isDirectoryExisting(directoryPath, dependencies) {
33
+ try {
34
+ await dependencies.fsAccess(directoryPath);
35
+ return Promise.resolve(true);
36
+ }
37
+ catch (error) {
38
+ return Promise.resolve(false);
39
+ }
40
+ }
41
+
24
42
  /**
25
43
  * Get the first existing parent path
26
44
  *
27
45
  * @param directoryPath - The file/folder path from where we want to know disk space
28
46
  * @param dependencies - Dependencies container
29
47
  */
30
- function getFirstExistingParentPath(directoryPath, dependencies) {
48
+ async function getFirstExistingParentPath(directoryPath, dependencies) {
31
49
  let parentDirectoryPath = directoryPath;
32
- let parentDirectoryFound = dependencies.fsExistsSync(parentDirectoryPath);
50
+ let parentDirectoryFound = await isDirectoryExisting(parentDirectoryPath, dependencies);
33
51
  while (!parentDirectoryFound) {
34
52
  parentDirectoryPath = dependencies.pathNormalize(parentDirectoryPath + '/..');
35
- parentDirectoryFound = dependencies.fsExistsSync(parentDirectoryPath);
53
+ parentDirectoryFound = await isDirectoryExisting(parentDirectoryPath, dependencies);
36
54
  }
37
55
  return parentDirectoryPath;
38
56
  }
@@ -43,20 +61,18 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
43
61
  * Note: 6.* is Windows 7
44
62
  * Note: PowerShell 3 is natively available since Windows 8
45
63
  *
46
- * @param release - OS Release number
64
+ * @param dependencies - Dependencies Injection Container
47
65
  */
48
- function hasPowerShell3(dependencies) {
66
+ async function hasPowerShell3(dependencies) {
49
67
  const major = parseInt(dependencies.release.split('.')[0], 10);
50
- if (major > 6) {
51
- try {
52
- dependencies.cpExecFileSync('powershell');
53
- return true;
54
- }
55
- catch (err) {
56
- return false;
57
- }
68
+ if (major <= 6) {
69
+ return false;
70
+ }
71
+ try {
72
+ await dependencies.cpExecFile('where', ['powershell'], { windowsHide: true });
73
+ return true;
58
74
  }
59
- else {
75
+ catch (error) {
60
76
  return false;
61
77
  }
62
78
  }
@@ -68,13 +84,12 @@ function hasPowerShell3(dependencies) {
68
84
  * @param dependencies - Dependencies container
69
85
  */
70
86
  function checkDiskSpace(directoryPath, dependencies = {
71
- platform: process.platform,
87
+ platform,
72
88
  release: release(),
73
- fsExistsSync: existsSync,
89
+ fsAccess: access,
74
90
  pathNormalize: normalize,
75
91
  pathSep: sep,
76
- cpExecFile: execFile,
77
- cpExecFileSync: execFileSync,
92
+ cpExecFile: promisify(execFile),
78
93
  }) {
79
94
  // Note: This function contains other functions in order
80
95
  // to wrap them in a common context and make unit tests easier
@@ -87,9 +102,12 @@ function checkDiskSpace(directoryPath, dependencies = {
87
102
  * @param coefficient - The size coefficient to get bytes instead of kB
88
103
  */
89
104
  function mapOutput(stdout, filter, mapping, coefficient) {
90
- const parsed = stdout.trim().split('\n').slice(1).map(line => {
91
- return line.trim().split(/\s+(?=[\d/])/);
92
- });
105
+ const parsed = stdout
106
+ .split('\n') // Split lines
107
+ .map(line => line.trim()) // Trim all lines
108
+ .filter(line => line.length !== 0) // Remove empty lines
109
+ .slice(1) // Remove header
110
+ .map(line => line.split(/\s+(?=[\d/])/)); // Split on spaces to get columns
93
111
  const filtered = parsed.filter(filter);
94
112
  if (filtered.length === 0) {
95
113
  throw new NoMatchError();
@@ -109,36 +127,28 @@ function checkDiskSpace(directoryPath, dependencies = {
109
127
  * @param mapping - Map between column index and normalized column name
110
128
  * @param coefficient - The size coefficient to get bytes instead of kB
111
129
  */
112
- function check(cmd, filter, mapping, coefficient = 1) {
113
- return new Promise((resolve, reject) => {
114
- const [file, ...args] = cmd;
115
- /* istanbul ignore if */
116
- if (file === undefined) {
117
- return Promise.reject(new Error('cmd must contain at least one item'));
118
- }
119
- dependencies.cpExecFile(file, args, (error, stdout) => {
120
- if (error) {
121
- reject(error);
122
- }
123
- try {
124
- resolve(mapOutput(stdout, filter, mapping, coefficient));
125
- }
126
- catch (error2) {
127
- reject(error2);
128
- }
129
- });
130
- });
130
+ async function check(cmd, filter, mapping, coefficient = 1) {
131
+ const [file, ...args] = cmd;
132
+ /* istanbul ignore if */
133
+ if (file === undefined) {
134
+ return Promise.reject(new Error('cmd must contain at least one item'));
135
+ }
136
+ try {
137
+ const { stdout } = await dependencies.cpExecFile(file, args, { windowsHide: true });
138
+ return mapOutput(stdout, filter, mapping, coefficient);
139
+ }
140
+ catch (error) {
141
+ return Promise.reject(error);
142
+ }
131
143
  }
132
144
  /**
133
145
  * Build the check call for win32
134
146
  *
135
147
  * @param directoryPath - The file/folder path from where we want to know disk space
136
148
  */
137
- function checkWin32(directoryPath) {
149
+ async function checkWin32(directoryPath) {
138
150
  if (directoryPath.charAt(1) !== ':') {
139
- return new Promise((resolve, reject) => {
140
- reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
141
- });
151
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
142
152
  }
143
153
  const powershellCmd = [
144
154
  'powershell',
@@ -150,7 +160,7 @@ function checkDiskSpace(directoryPath, dependencies = {
150
160
  'get',
151
161
  'size,freespace,caption',
152
162
  ];
153
- const cmd = hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
163
+ const cmd = await hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
154
164
  return check(cmd, driveData => {
155
165
  // Only get the drive which match the path
156
166
  const driveLetter = driveData[0];
@@ -166,13 +176,11 @@ function checkDiskSpace(directoryPath, dependencies = {
166
176
  *
167
177
  * @param directoryPath - The file/folder path from where we want to know disk space
168
178
  */
169
- function checkUnix(directoryPath) {
179
+ async function checkUnix(directoryPath) {
170
180
  if (!dependencies.pathNormalize(directoryPath).startsWith(dependencies.pathSep)) {
171
- return new Promise((resolve, reject) => {
172
- reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
173
- });
181
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
174
182
  }
175
- const pathToCheck = getFirstExistingParentPath(directoryPath, dependencies);
183
+ const pathToCheck = await getFirstExistingParentPath(directoryPath, dependencies);
176
184
  return check([
177
185
  'df',
178
186
  '-Pk',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "check-disk-space",
3
- "version": "3.3.1",
3
+ "version": "3.4.0",
4
4
  "description": "Light multi-platform disk space checker without third party for Node.js",
5
5
  "files": [
6
6
  "dist",
@@ -11,11 +11,12 @@
11
11
  "types": "./dist/check-disk-space.d.ts",
12
12
  "exports": {
13
13
  "import": "./dist/check-disk-space.mjs",
14
- "require": "./dist/check-disk-space.cjs"
14
+ "require": "./dist/check-disk-space.cjs",
15
+ "types": "./dist/check-disk-space.d.ts"
15
16
  },
16
17
  "scripts": {
17
18
  "build:lib": "rollup --config",
18
- "build:dts": "rollup --config rollup.dts.config.js",
19
+ "build:dts": "rollup --config rollup.dts.config.mjs",
19
20
  "build": "npm-run-all build:lib build:dts",
20
21
  "lint": "eslint . --cache",
21
22
  "lint:fix": "eslint --fix .",
@@ -39,29 +40,29 @@
39
40
  ]
40
41
  },
41
42
  "engines": {
42
- "node": ">=12"
43
+ "node": ">=16"
43
44
  },
44
45
  "devDependencies": {
45
- "@alex-d/eslint-config": "2.0.0",
46
+ "@alex-d/eslint-config": "2.2.0",
46
47
  "@istanbuljs/nyc-config-typescript": "1.0.2",
47
- "@rollup/plugin-typescript": "8.3.1",
48
- "@types/node": "12.20.46",
49
- "@typescript-eslint/eslint-plugin": "5.13.0",
50
- "@typescript-eslint/parser": "5.13.0",
51
- "ava": "4.1.0",
52
- "eslint": "8.10.0",
53
- "eslint-plugin-ava": "13.2.0",
54
- "eslint-plugin-import": "2.25.4",
55
- "eslint-plugin-simple-import-sort": "7.0.0",
48
+ "@rollup/plugin-typescript": "11.1.1",
49
+ "@types/node": "16.11.7",
50
+ "@typescript-eslint/eslint-plugin": "5.59.6",
51
+ "@typescript-eslint/parser": "5.59.6",
52
+ "ava": "5.2.0",
53
+ "eslint": "8.41.0",
54
+ "eslint-plugin-ava": "14.0.0",
55
+ "eslint-plugin-import": "2.27.5",
56
+ "eslint-plugin-simple-import-sort": "10.0.0",
56
57
  "npm-run-all": "4.1.5",
57
58
  "nyc": "15.1.0",
58
- "rollup": "2.69.2",
59
- "rollup-plugin-dts": "4.2.0",
59
+ "rollup": "3.22.0",
60
+ "rollup-plugin-dts": "5.3.0",
60
61
  "source-map-support": "0.5.21",
61
- "ts-node": "10.7.0",
62
- "tsconfig-paths": "3.13.0",
63
- "tslib": "2.3.1",
64
- "typescript": "4.6.2"
62
+ "ts-node": "10.9.1",
63
+ "tsconfig-paths": "4.2.0",
64
+ "tslib": "2.5.2",
65
+ "typescript": "5.0.4"
65
66
  },
66
67
  "repository": {
67
68
  "type": "git",