check-disk-space 3.3.0 → 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,11 +65,20 @@ 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(release) {
53
- const major = parseInt(release.split('.')[0], 10);
54
- return major > 6;
70
+ async function hasPowerShell3(dependencies) {
71
+ const major = parseInt(dependencies.release.split('.')[0], 10);
72
+ if (major <= 6) {
73
+ return false;
74
+ }
75
+ try {
76
+ await dependencies.cpExecFile('where', ['powershell'], { windowsHide: true });
77
+ return true;
78
+ }
79
+ catch (error) {
80
+ return false;
81
+ }
55
82
  }
56
83
 
57
84
  /**
@@ -61,12 +88,12 @@ function hasPowerShell3(release) {
61
88
  * @param dependencies - Dependencies container
62
89
  */
63
90
  function checkDiskSpace(directoryPath, dependencies = {
64
- platform: process.platform,
65
- release: os.release(),
66
- fsExistsSync: fs.existsSync,
67
- pathNormalize: path.normalize,
68
- pathSep: path.sep,
69
- cpExecFile: child_process.execFile,
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),
70
97
  }) {
71
98
  // Note: This function contains other functions in order
72
99
  // to wrap them in a common context and make unit tests easier
@@ -79,9 +106,12 @@ function checkDiskSpace(directoryPath, dependencies = {
79
106
  * @param coefficient - The size coefficient to get bytes instead of kB
80
107
  */
81
108
  function mapOutput(stdout, filter, mapping, coefficient) {
82
- const parsed = stdout.trim().split('\n').slice(1).map(line => {
83
- return line.trim().split(/\s+(?=[\d/])/);
84
- });
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
85
115
  const filtered = parsed.filter(filter);
86
116
  if (filtered.length === 0) {
87
117
  throw new NoMatchError();
@@ -101,36 +131,28 @@ function checkDiskSpace(directoryPath, dependencies = {
101
131
  * @param mapping - Map between column index and normalized column name
102
132
  * @param coefficient - The size coefficient to get bytes instead of kB
103
133
  */
104
- function check(cmd, filter, mapping, coefficient = 1) {
105
- return new Promise((resolve, reject) => {
106
- const [file, ...args] = cmd;
107
- /* istanbul ignore if */
108
- if (file === undefined) {
109
- return Promise.reject(new Error('cmd must contain at least one item'));
110
- }
111
- dependencies.cpExecFile(file, args, (error, stdout) => {
112
- if (error) {
113
- reject(error);
114
- }
115
- try {
116
- resolve(mapOutput(stdout, filter, mapping, coefficient));
117
- }
118
- catch (error2) {
119
- reject(error2);
120
- }
121
- });
122
- });
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
+ }
123
147
  }
124
148
  /**
125
149
  * Build the check call for win32
126
150
  *
127
151
  * @param directoryPath - The file/folder path from where we want to know disk space
128
152
  */
129
- function checkWin32(directoryPath) {
153
+ async function checkWin32(directoryPath) {
130
154
  if (directoryPath.charAt(1) !== ':') {
131
- return new Promise((resolve, reject) => {
132
- reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
133
- });
155
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
134
156
  }
135
157
  const powershellCmd = [
136
158
  'powershell',
@@ -142,7 +164,7 @@ function checkDiskSpace(directoryPath, dependencies = {
142
164
  'get',
143
165
  'size,freespace,caption',
144
166
  ];
145
- const cmd = hasPowerShell3(dependencies.release) ? powershellCmd : wmicCmd;
167
+ const cmd = await hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
146
168
  return check(cmd, driveData => {
147
169
  // Only get the drive which match the path
148
170
  const driveLetter = driveData[0];
@@ -158,13 +180,11 @@ function checkDiskSpace(directoryPath, dependencies = {
158
180
  *
159
181
  * @param directoryPath - The file/folder path from where we want to know disk space
160
182
  */
161
- function checkUnix(directoryPath) {
183
+ async function checkUnix(directoryPath) {
162
184
  if (!dependencies.pathNormalize(directoryPath).startsWith(dependencies.pathSep)) {
163
- return new Promise((resolve, reject) => {
164
- reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
165
- });
185
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
166
186
  }
167
- const pathToCheck = getFirstExistingParentPath(directoryPath, dependencies);
187
+ const pathToCheck = await getFirstExistingParentPath(directoryPath, dependencies);
168
188
  return check([
169
189
  'df',
170
190
  '-Pk',
@@ -186,5 +206,5 @@ function checkDiskSpace(directoryPath, dependencies = {
186
206
 
187
207
  exports.InvalidPathError = InvalidPathError;
188
208
  exports.NoMatchError = NoMatchError;
189
- exports["default"] = checkDiskSpace;
209
+ exports.default = checkDiskSpace;
190
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,14 +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;
20
+ cpExecFile: (file: string, args: ReadonlyArray<string> | undefined | null, options: {
21
+ windowsHide: true;
22
+ }) => Promise<{
23
+ stdout: string;
24
+ stderr: string;
25
+ }>;
23
26
  };
24
27
 
25
28
  /**
@@ -28,12 +31,12 @@ declare type Dependencies = {
28
31
  * @param directoryPath - The file/folder path from where we want to know disk space
29
32
  * @param dependencies - Dependencies container
30
33
  */
31
- declare function getFirstExistingParentPath(directoryPath: string, dependencies: Dependencies): string;
34
+ declare function getFirstExistingParentPath(directoryPath: string, dependencies: Dependencies): Promise<string>;
32
35
 
33
36
  /**
34
37
  * `free` and `size` are in bytes
35
38
  */
36
- declare type DiskSpace = {
39
+ type DiskSpace = {
37
40
  diskPath: string;
38
41
  free: number;
39
42
  size: number;
@@ -1,7 +1,9 @@
1
- import { execFile } 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,11 +61,20 @@ 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(release) {
49
- const major = parseInt(release.split('.')[0], 10);
50
- return major > 6;
66
+ async function hasPowerShell3(dependencies) {
67
+ const major = parseInt(dependencies.release.split('.')[0], 10);
68
+ if (major <= 6) {
69
+ return false;
70
+ }
71
+ try {
72
+ await dependencies.cpExecFile('where', ['powershell'], { windowsHide: true });
73
+ return true;
74
+ }
75
+ catch (error) {
76
+ return false;
77
+ }
51
78
  }
52
79
 
53
80
  /**
@@ -57,12 +84,12 @@ function hasPowerShell3(release) {
57
84
  * @param dependencies - Dependencies container
58
85
  */
59
86
  function checkDiskSpace(directoryPath, dependencies = {
60
- platform: process.platform,
87
+ platform,
61
88
  release: release(),
62
- fsExistsSync: existsSync,
89
+ fsAccess: access,
63
90
  pathNormalize: normalize,
64
91
  pathSep: sep,
65
- cpExecFile: execFile,
92
+ cpExecFile: promisify(execFile),
66
93
  }) {
67
94
  // Note: This function contains other functions in order
68
95
  // to wrap them in a common context and make unit tests easier
@@ -75,9 +102,12 @@ function checkDiskSpace(directoryPath, dependencies = {
75
102
  * @param coefficient - The size coefficient to get bytes instead of kB
76
103
  */
77
104
  function mapOutput(stdout, filter, mapping, coefficient) {
78
- const parsed = stdout.trim().split('\n').slice(1).map(line => {
79
- return line.trim().split(/\s+(?=[\d/])/);
80
- });
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
81
111
  const filtered = parsed.filter(filter);
82
112
  if (filtered.length === 0) {
83
113
  throw new NoMatchError();
@@ -97,36 +127,28 @@ function checkDiskSpace(directoryPath, dependencies = {
97
127
  * @param mapping - Map between column index and normalized column name
98
128
  * @param coefficient - The size coefficient to get bytes instead of kB
99
129
  */
100
- function check(cmd, filter, mapping, coefficient = 1) {
101
- return new Promise((resolve, reject) => {
102
- const [file, ...args] = cmd;
103
- /* istanbul ignore if */
104
- if (file === undefined) {
105
- return Promise.reject(new Error('cmd must contain at least one item'));
106
- }
107
- dependencies.cpExecFile(file, args, (error, stdout) => {
108
- if (error) {
109
- reject(error);
110
- }
111
- try {
112
- resolve(mapOutput(stdout, filter, mapping, coefficient));
113
- }
114
- catch (error2) {
115
- reject(error2);
116
- }
117
- });
118
- });
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
+ }
119
143
  }
120
144
  /**
121
145
  * Build the check call for win32
122
146
  *
123
147
  * @param directoryPath - The file/folder path from where we want to know disk space
124
148
  */
125
- function checkWin32(directoryPath) {
149
+ async function checkWin32(directoryPath) {
126
150
  if (directoryPath.charAt(1) !== ':') {
127
- return new Promise((resolve, reject) => {
128
- reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
129
- });
151
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
130
152
  }
131
153
  const powershellCmd = [
132
154
  'powershell',
@@ -138,7 +160,7 @@ function checkDiskSpace(directoryPath, dependencies = {
138
160
  'get',
139
161
  'size,freespace,caption',
140
162
  ];
141
- const cmd = hasPowerShell3(dependencies.release) ? powershellCmd : wmicCmd;
163
+ const cmd = await hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
142
164
  return check(cmd, driveData => {
143
165
  // Only get the drive which match the path
144
166
  const driveLetter = driveData[0];
@@ -154,13 +176,11 @@ function checkDiskSpace(directoryPath, dependencies = {
154
176
  *
155
177
  * @param directoryPath - The file/folder path from where we want to know disk space
156
178
  */
157
- function checkUnix(directoryPath) {
179
+ async function checkUnix(directoryPath) {
158
180
  if (!dependencies.pathNormalize(directoryPath).startsWith(dependencies.pathSep)) {
159
- return new Promise((resolve, reject) => {
160
- reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
161
- });
181
+ return Promise.reject(new InvalidPathError(`The following path is invalid (should start by ${dependencies.pathSep}): ${directoryPath}`));
162
182
  }
163
- const pathToCheck = getFirstExistingParentPath(directoryPath, dependencies);
183
+ const pathToCheck = await getFirstExistingParentPath(directoryPath, dependencies);
164
184
  return check([
165
185
  'df',
166
186
  '-Pk',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "check-disk-space",
3
- "version": "3.3.0",
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",