check-disk-space 3.1.0 → 3.3.1

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.
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var child_process = require('child_process');
6
6
  var fs = require('fs');
7
+ var os = require('os');
7
8
  var path = require('path');
8
9
 
9
10
  class InvalidPathError extends Error {
@@ -40,6 +41,30 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
40
41
  return parentDirectoryPath;
41
42
  }
42
43
 
44
+ /**
45
+ * Tell if PowerShell 3 is available based on Windows version
46
+ *
47
+ * Note: 6.* is Windows 7
48
+ * Note: PowerShell 3 is natively available since Windows 8
49
+ *
50
+ * @param release - OS Release number
51
+ */
52
+ function hasPowerShell3(dependencies) {
53
+ 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
+ }
62
+ }
63
+ else {
64
+ return false;
65
+ }
66
+ }
67
+
43
68
  /**
44
69
  * Check disk space
45
70
  *
@@ -48,11 +73,15 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
48
73
  */
49
74
  function checkDiskSpace(directoryPath, dependencies = {
50
75
  platform: process.platform,
76
+ release: os.release(),
51
77
  fsExistsSync: fs.existsSync,
52
78
  pathNormalize: path.normalize,
53
79
  pathSep: path.sep,
54
80
  cpExecFile: child_process.execFile,
81
+ cpExecFileSync: child_process.execFileSync,
55
82
  }) {
83
+ // Note: This function contains other functions in order
84
+ // to wrap them in a common context and make unit tests easier
56
85
  /**
57
86
  * Maps command output to a normalized object {diskPath, free, size}
58
87
  *
@@ -89,7 +118,7 @@ function checkDiskSpace(directoryPath, dependencies = {
89
118
  const [file, ...args] = cmd;
90
119
  /* istanbul ignore if */
91
120
  if (file === undefined) {
92
- return Promise.reject('cmd must contain at least one item');
121
+ return Promise.reject(new Error('cmd must contain at least one item'));
93
122
  }
94
123
  dependencies.cpExecFile(file, args, (error, stdout) => {
95
124
  if (error) {
@@ -115,7 +144,18 @@ function checkDiskSpace(directoryPath, dependencies = {
115
144
  reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
116
145
  });
117
146
  }
118
- return check(['wmic', 'logicaldisk', 'get', 'size,freespace,caption'], driveData => {
147
+ const powershellCmd = [
148
+ 'powershell',
149
+ 'Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size',
150
+ ];
151
+ const wmicCmd = [
152
+ 'wmic',
153
+ 'logicaldisk',
154
+ 'get',
155
+ 'size,freespace,caption',
156
+ ];
157
+ const cmd = hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
158
+ return check(cmd, driveData => {
119
159
  // Only get the drive which match the path
120
160
  const driveLetter = driveData[0];
121
161
  return directoryPath.toUpperCase().startsWith(driveLetter.toUpperCase());
@@ -137,7 +177,12 @@ function checkDiskSpace(directoryPath, dependencies = {
137
177
  });
138
178
  }
139
179
  const pathToCheck = getFirstExistingParentPath(directoryPath, dependencies);
140
- return check(['df', '-Pk', '--', pathToCheck], () => true, // We should only get one line, so we did not need to filter
180
+ return check([
181
+ 'df',
182
+ '-Pk',
183
+ '--',
184
+ pathToCheck,
185
+ ], () => true, // We should only get one line, so we did not need to filter
141
186
  {
142
187
  diskPath: 5,
143
188
  free: 3,
@@ -153,5 +198,5 @@ function checkDiskSpace(directoryPath, dependencies = {
153
198
 
154
199
  exports.InvalidPathError = InvalidPathError;
155
200
  exports.NoMatchError = NoMatchError;
156
- exports.default = checkDiskSpace;
201
+ exports["default"] = checkDiskSpace;
157
202
  exports.getFirstExistingParentPath = getFirstExistingParentPath;
@@ -1,4 +1,4 @@
1
- import { ExecFileException, ChildProcess } from 'child_process';
1
+ import { ChildProcess, ExecException } from 'child_process';
2
2
  import { existsSync } from 'fs';
3
3
  import { normalize, sep } from 'path';
4
4
 
@@ -12,12 +12,15 @@ declare class NoMatchError extends Error {
12
12
  constructor(message?: string);
13
13
  }
14
14
 
15
+ declare type ExecFileException = ExecException & NodeJS.ErrnoException;
15
16
  declare type Dependencies = {
16
17
  platform: NodeJS.Platform;
18
+ release: string;
17
19
  fsExistsSync: typeof existsSync;
18
20
  pathNormalize: typeof normalize;
19
21
  pathSep: typeof sep;
20
22
  cpExecFile: (file: string, args: ReadonlyArray<string> | undefined | null, callback: (error: ExecFileException | null, stdout: string, stderr: string) => void) => ChildProcess;
23
+ cpExecFileSync: (command: string) => Buffer;
21
24
  };
22
25
 
23
26
  /**
@@ -45,5 +48,4 @@ declare type DiskSpace = {
45
48
  */
46
49
  declare function checkDiskSpace(directoryPath: string, dependencies?: Dependencies): Promise<DiskSpace>;
47
50
 
48
- export default checkDiskSpace;
49
- export { Dependencies, DiskSpace, InvalidPathError, NoMatchError, getFirstExistingParentPath };
51
+ export { Dependencies, DiskSpace, InvalidPathError, NoMatchError, checkDiskSpace as default, getFirstExistingParentPath };
@@ -1,5 +1,6 @@
1
- import { execFile } from 'child_process';
1
+ import { execFile, execFileSync } from 'child_process';
2
2
  import { existsSync } from 'fs';
3
+ import { release } from 'os';
3
4
  import { normalize, sep } from 'path';
4
5
 
5
6
  class InvalidPathError extends Error {
@@ -36,6 +37,30 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
36
37
  return parentDirectoryPath;
37
38
  }
38
39
 
40
+ /**
41
+ * Tell if PowerShell 3 is available based on Windows version
42
+ *
43
+ * Note: 6.* is Windows 7
44
+ * Note: PowerShell 3 is natively available since Windows 8
45
+ *
46
+ * @param release - OS Release number
47
+ */
48
+ function hasPowerShell3(dependencies) {
49
+ 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
+ }
58
+ }
59
+ else {
60
+ return false;
61
+ }
62
+ }
63
+
39
64
  /**
40
65
  * Check disk space
41
66
  *
@@ -44,11 +69,15 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
44
69
  */
45
70
  function checkDiskSpace(directoryPath, dependencies = {
46
71
  platform: process.platform,
72
+ release: release(),
47
73
  fsExistsSync: existsSync,
48
74
  pathNormalize: normalize,
49
75
  pathSep: sep,
50
76
  cpExecFile: execFile,
77
+ cpExecFileSync: execFileSync,
51
78
  }) {
79
+ // Note: This function contains other functions in order
80
+ // to wrap them in a common context and make unit tests easier
52
81
  /**
53
82
  * Maps command output to a normalized object {diskPath, free, size}
54
83
  *
@@ -85,7 +114,7 @@ function checkDiskSpace(directoryPath, dependencies = {
85
114
  const [file, ...args] = cmd;
86
115
  /* istanbul ignore if */
87
116
  if (file === undefined) {
88
- return Promise.reject('cmd must contain at least one item');
117
+ return Promise.reject(new Error('cmd must contain at least one item'));
89
118
  }
90
119
  dependencies.cpExecFile(file, args, (error, stdout) => {
91
120
  if (error) {
@@ -111,7 +140,18 @@ function checkDiskSpace(directoryPath, dependencies = {
111
140
  reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
112
141
  });
113
142
  }
114
- return check(['wmic', 'logicaldisk', 'get', 'size,freespace,caption'], driveData => {
143
+ const powershellCmd = [
144
+ 'powershell',
145
+ 'Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size',
146
+ ];
147
+ const wmicCmd = [
148
+ 'wmic',
149
+ 'logicaldisk',
150
+ 'get',
151
+ 'size,freespace,caption',
152
+ ];
153
+ const cmd = hasPowerShell3(dependencies) ? powershellCmd : wmicCmd;
154
+ return check(cmd, driveData => {
115
155
  // Only get the drive which match the path
116
156
  const driveLetter = driveData[0];
117
157
  return directoryPath.toUpperCase().startsWith(driveLetter.toUpperCase());
@@ -133,7 +173,12 @@ function checkDiskSpace(directoryPath, dependencies = {
133
173
  });
134
174
  }
135
175
  const pathToCheck = getFirstExistingParentPath(directoryPath, dependencies);
136
- return check(['df', '-Pk', '--', pathToCheck], () => true, // We should only get one line, so we did not need to filter
176
+ return check([
177
+ 'df',
178
+ '-Pk',
179
+ '--',
180
+ pathToCheck,
181
+ ], () => true, // We should only get one line, so we did not need to filter
137
182
  {
138
183
  diskPath: 5,
139
184
  free: 3,
@@ -147,5 +192,4 @@ function checkDiskSpace(directoryPath, dependencies = {
147
192
  return checkUnix(directoryPath);
148
193
  }
149
194
 
150
- export default checkDiskSpace;
151
- export { InvalidPathError, NoMatchError, getFirstExistingParentPath };
195
+ export { InvalidPathError, NoMatchError, checkDiskSpace as default, getFirstExistingParentPath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "check-disk-space",
3
- "version": "3.1.0",
3
+ "version": "3.3.1",
4
4
  "description": "Light multi-platform disk space checker without third party for Node.js",
5
5
  "files": [
6
6
  "dist",
@@ -42,26 +42,26 @@
42
42
  "node": ">=12"
43
43
  },
44
44
  "devDependencies": {
45
- "@alex-d/eslint-config": "1.0.3",
46
- "@istanbuljs/nyc-config-typescript": "^1.0.1",
47
- "@rollup/plugin-typescript": "8.2.1",
48
- "@types/node": "15.12.2",
49
- "@typescript-eslint/eslint-plugin": "4.26.1",
50
- "@typescript-eslint/parser": "4.26.1",
51
- "ava": "3.15.0",
52
- "eslint": "7.28.0",
53
- "eslint-plugin-ava": "12.0.0",
54
- "eslint-plugin-import": "2.23.4",
45
+ "@alex-d/eslint-config": "2.0.0",
46
+ "@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
55
  "eslint-plugin-simple-import-sort": "7.0.0",
56
56
  "npm-run-all": "4.1.5",
57
- "nyc": "^15.1.0",
58
- "rollup": "2.51.2",
59
- "rollup-plugin-dts": "3.0.2",
60
- "source-map-support": "^0.5.19",
61
- "ts-node": "^10.0.0",
62
- "tsconfig-paths": "^3.9.0",
63
- "tslib": "^2.3.0",
64
- "typescript": "4.3.2"
57
+ "nyc": "15.1.0",
58
+ "rollup": "2.69.2",
59
+ "rollup-plugin-dts": "4.2.0",
60
+ "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"
65
65
  },
66
66
  "repository": {
67
67
  "type": "git",