check-disk-space 3.0.1 → 3.3.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.
@@ -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,19 @@ 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(release) {
53
+ const major = parseInt(release.split('.')[0], 10);
54
+ return major > 6;
55
+ }
56
+
43
57
  /**
44
58
  * Check disk space
45
59
  *
@@ -48,11 +62,14 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
48
62
  */
49
63
  function checkDiskSpace(directoryPath, dependencies = {
50
64
  platform: process.platform,
65
+ release: os.release(),
51
66
  fsExistsSync: fs.existsSync,
52
67
  pathNormalize: path.normalize,
53
68
  pathSep: path.sep,
54
69
  cpExecFile: child_process.execFile,
55
70
  }) {
71
+ // Note: This function contains other functions in order
72
+ // to wrap them in a common context and make unit tests easier
56
73
  /**
57
74
  * Maps command output to a normalized object {diskPath, free, size}
58
75
  *
@@ -89,7 +106,7 @@ function checkDiskSpace(directoryPath, dependencies = {
89
106
  const [file, ...args] = cmd;
90
107
  /* istanbul ignore if */
91
108
  if (file === undefined) {
92
- return Promise.reject('cmd must contain at least one item');
109
+ return Promise.reject(new Error('cmd must contain at least one item'));
93
110
  }
94
111
  dependencies.cpExecFile(file, args, (error, stdout) => {
95
112
  if (error) {
@@ -115,7 +132,18 @@ function checkDiskSpace(directoryPath, dependencies = {
115
132
  reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
116
133
  });
117
134
  }
118
- return check(['wmic', 'logicaldisk', 'get', 'size,freespace,caption'], driveData => {
135
+ const powershellCmd = [
136
+ 'powershell',
137
+ 'Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size',
138
+ ];
139
+ const wmicCmd = [
140
+ 'wmic',
141
+ 'logicaldisk',
142
+ 'get',
143
+ 'size,freespace,caption',
144
+ ];
145
+ const cmd = hasPowerShell3(dependencies.release) ? powershellCmd : wmicCmd;
146
+ return check(cmd, driveData => {
119
147
  // Only get the drive which match the path
120
148
  const driveLetter = driveData[0];
121
149
  return directoryPath.toUpperCase().startsWith(driveLetter.toUpperCase());
@@ -137,7 +165,12 @@ function checkDiskSpace(directoryPath, dependencies = {
137
165
  });
138
166
  }
139
167
  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
168
+ return check([
169
+ 'df',
170
+ '-Pk',
171
+ '--',
172
+ pathToCheck,
173
+ ], () => true, // We should only get one line, so we did not need to filter
141
174
  {
142
175
  diskPath: 5,
143
176
  free: 3,
@@ -153,5 +186,5 @@ function checkDiskSpace(directoryPath, dependencies = {
153
186
 
154
187
  exports.InvalidPathError = InvalidPathError;
155
188
  exports.NoMatchError = NoMatchError;
156
- exports.default = checkDiskSpace;
189
+ exports["default"] = checkDiskSpace;
157
190
  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,8 +12,10 @@ 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;
@@ -45,5 +47,4 @@ declare type DiskSpace = {
45
47
  */
46
48
  declare function checkDiskSpace(directoryPath: string, dependencies?: Dependencies): Promise<DiskSpace>;
47
49
 
48
- export default checkDiskSpace;
49
- export { Dependencies, DiskSpace, InvalidPathError, NoMatchError, getFirstExistingParentPath };
50
+ export { Dependencies, DiskSpace, InvalidPathError, NoMatchError, checkDiskSpace as default, getFirstExistingParentPath };
@@ -1,5 +1,6 @@
1
1
  import { execFile } 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,19 @@ 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(release) {
49
+ const major = parseInt(release.split('.')[0], 10);
50
+ return major > 6;
51
+ }
52
+
39
53
  /**
40
54
  * Check disk space
41
55
  *
@@ -44,11 +58,14 @@ function getFirstExistingParentPath(directoryPath, dependencies) {
44
58
  */
45
59
  function checkDiskSpace(directoryPath, dependencies = {
46
60
  platform: process.platform,
61
+ release: release(),
47
62
  fsExistsSync: existsSync,
48
63
  pathNormalize: normalize,
49
64
  pathSep: sep,
50
65
  cpExecFile: execFile,
51
66
  }) {
67
+ // Note: This function contains other functions in order
68
+ // to wrap them in a common context and make unit tests easier
52
69
  /**
53
70
  * Maps command output to a normalized object {diskPath, free, size}
54
71
  *
@@ -85,7 +102,7 @@ function checkDiskSpace(directoryPath, dependencies = {
85
102
  const [file, ...args] = cmd;
86
103
  /* istanbul ignore if */
87
104
  if (file === undefined) {
88
- return Promise.reject('cmd must contain at least one item');
105
+ return Promise.reject(new Error('cmd must contain at least one item'));
89
106
  }
90
107
  dependencies.cpExecFile(file, args, (error, stdout) => {
91
108
  if (error) {
@@ -111,7 +128,18 @@ function checkDiskSpace(directoryPath, dependencies = {
111
128
  reject(new InvalidPathError(`The following path is invalid (should be X:\\...): ${directoryPath}`));
112
129
  });
113
130
  }
114
- return check(['wmic', 'logicaldisk', 'get', 'size,freespace,caption'], driveData => {
131
+ const powershellCmd = [
132
+ 'powershell',
133
+ 'Get-CimInstance -ClassName Win32_LogicalDisk | Select-Object Caption, FreeSpace, Size',
134
+ ];
135
+ const wmicCmd = [
136
+ 'wmic',
137
+ 'logicaldisk',
138
+ 'get',
139
+ 'size,freespace,caption',
140
+ ];
141
+ const cmd = hasPowerShell3(dependencies.release) ? powershellCmd : wmicCmd;
142
+ return check(cmd, driveData => {
115
143
  // Only get the drive which match the path
116
144
  const driveLetter = driveData[0];
117
145
  return directoryPath.toUpperCase().startsWith(driveLetter.toUpperCase());
@@ -133,7 +161,12 @@ function checkDiskSpace(directoryPath, dependencies = {
133
161
  });
134
162
  }
135
163
  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
164
+ return check([
165
+ 'df',
166
+ '-Pk',
167
+ '--',
168
+ pathToCheck,
169
+ ], () => true, // We should only get one line, so we did not need to filter
137
170
  {
138
171
  diskPath: 5,
139
172
  free: 3,
@@ -147,5 +180,4 @@ function checkDiskSpace(directoryPath, dependencies = {
147
180
  return checkUnix(directoryPath);
148
181
  }
149
182
 
150
- export default checkDiskSpace;
151
- export { InvalidPathError, NoMatchError, getFirstExistingParentPath };
183
+ export { InvalidPathError, NoMatchError, checkDiskSpace as default, getFirstExistingParentPath };
package/package.json CHANGED
@@ -1,18 +1,17 @@
1
1
  {
2
2
  "name": "check-disk-space",
3
- "version": "3.0.1",
3
+ "version": "3.3.0",
4
4
  "description": "Light multi-platform disk space checker without third party for Node.js",
5
5
  "files": [
6
- "dist"
6
+ "dist",
7
+ "README.md"
7
8
  ],
8
- "main": "./dist/check-disk-space.cjs.js",
9
- "module": "./dist/check-disk-space.es.js",
9
+ "main": "./dist/check-disk-space.cjs",
10
+ "module": "./dist/check-disk-space.mjs",
10
11
  "types": "./dist/check-disk-space.d.ts",
11
12
  "exports": {
12
- ".": {
13
- "import": "./dist/check-disk-space.es.js",
14
- "require": "./dist/check-disk-space.cjs.js"
15
- }
13
+ "import": "./dist/check-disk-space.mjs",
14
+ "require": "./dist/check-disk-space.cjs"
16
15
  },
17
16
  "scripts": {
18
17
  "build:lib": "rollup --config",
@@ -43,26 +42,26 @@
43
42
  "node": ">=12"
44
43
  },
45
44
  "devDependencies": {
46
- "@alex-d/eslint-config": "1.0.3",
47
- "@istanbuljs/nyc-config-typescript": "^1.0.1",
48
- "@rollup/plugin-typescript": "8.2.1",
49
- "@types/node": "15.12.2",
50
- "@typescript-eslint/eslint-plugin": "4.26.1",
51
- "@typescript-eslint/parser": "4.26.1",
52
- "ava": "3.15.0",
53
- "eslint": "7.28.0",
54
- "eslint-plugin-ava": "12.0.0",
55
- "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",
56
55
  "eslint-plugin-simple-import-sort": "7.0.0",
57
56
  "npm-run-all": "4.1.5",
58
- "nyc": "^15.1.0",
59
- "rollup": "2.51.2",
60
- "rollup-plugin-dts": "3.0.2",
61
- "source-map-support": "^0.5.19",
62
- "ts-node": "^10.0.0",
63
- "tsconfig-paths": "^3.9.0",
64
- "tslib": "^2.3.0",
65
- "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"
66
65
  },
67
66
  "repository": {
68
67
  "type": "git",