check-disk-space 1.3.0 → 2.1.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.
@@ -0,0 +1,4 @@
1
+ # These are supported funding model platforms
2
+
3
+ github: [Alex-D]
4
+ patreon: AlexandreDemode
package/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  sudo: false
2
2
  language: node_js
3
3
  node_js:
4
+ - '12'
5
+ - '10'
4
6
  - '8'
5
- - '7'
6
- - '6'
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017 Alexandre Demode
3
+ Copyright (c) 2017-2019 Alexandre Demode
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/index.js CHANGED
@@ -11,6 +11,7 @@ const InvalidPathError = function (message) {
11
11
  this.name = 'InvalidPathError'
12
12
  this.message = message || ''
13
13
  }
14
+
14
15
  util.inherits(InvalidPathError, Error)
15
16
 
16
17
  const NoMatchError = function (message) {
@@ -18,6 +19,7 @@ const NoMatchError = function (message) {
18
19
  this.name = 'NoMatchError'
19
20
  this.message = message || ''
20
21
  }
22
+
21
23
  util.inherits(NoMatchError, Error)
22
24
 
23
25
  /**
@@ -39,9 +41,11 @@ function mapOutput(stdout, filter, mapping, coefficient = 1) {
39
41
  if (filtered.length === 0) {
40
42
  throw new NoMatchError()
41
43
  }
44
+
42
45
  filtered = filtered[0]
43
46
 
44
47
  return {
48
+ diskPath: filtered[mapping.diskPath],
45
49
  free: parseInt(filtered[mapping.free], 10) * coefficient,
46
50
  size: parseInt(filtered[mapping.size], 10) * coefficient
47
51
  }
@@ -65,8 +69,8 @@ function check(cmd, filter, mapping, coefficient = 1) {
65
69
 
66
70
  try {
67
71
  resolve(mapOutput(stdout, filter, mapping, coefficient))
68
- } catch (err) {
69
- reject(err)
72
+ } catch (error2) {
73
+ reject(error2)
70
74
  }
71
75
  })
72
76
  })
@@ -86,13 +90,14 @@ function checkWin32(directoryPath) {
86
90
  }
87
91
 
88
92
  return check(
89
- `wmic logicaldisk get size,freespace,caption`,
93
+ 'wmic logicaldisk get size,freespace,caption',
90
94
  driveData => {
91
95
  // Only get the drive which match the path
92
96
  const driveLetter = driveData[0]
93
97
  return directoryPath.startsWith(driveLetter)
94
98
  },
95
99
  {
100
+ diskPath: 0,
96
101
  free: 1,
97
102
  size: 2
98
103
  }
@@ -113,9 +118,10 @@ function checkUnix(directoryPath) {
113
118
  }
114
119
 
115
120
  return check(
116
- `df -Pk ${module.exports.getFirstExistingParentPath(directoryPath)}`,
121
+ `df -Pk "${module.exports.getFirstExistingParentPath(directoryPath)}"`,
117
122
  () => true, // We should only get one line, so we did not need to filter
118
123
  {
124
+ diskPath: 5,
119
125
  free: 3,
120
126
  size: 1
121
127
  },
@@ -129,8 +135,8 @@ module.exports = (process.platform === 'win32') ? checkWin32 : checkUnix
129
135
  /**
130
136
  * Get the first existing parent path
131
137
  *
132
- * @param directoryPath
133
- * @returns {*}
138
+ * @param {String} directoryPath - The file/folder path from where we want to know disk space
139
+ * @returns {String} - The first existing parent path
134
140
  */
135
141
  module.exports.getFirstExistingParentPath = directoryPath => {
136
142
  let parentDirectoryPath = directoryPath
package/package.json CHANGED
@@ -1,14 +1,18 @@
1
1
  {
2
2
  "name": "check-disk-space",
3
- "version": "1.3.0",
4
- "description": "Light multiplartform disk space checker without third party for Node.js",
3
+ "version": "2.1.0",
4
+ "description": "Light multi-platform disk space checker without third party for Node.js",
5
5
  "main": "index.js",
6
+ "engines": {
7
+ "node": ">=8"
8
+ },
6
9
  "dependencies": {},
7
10
  "devDependencies": {
8
- "ava": "0.22.0",
9
- "eslint-plugin-ava": "^4.2.2",
10
- "npm-run-all": "^4.1.1",
11
- "xo": "^0.18.2"
11
+ "ava": "^2.0.0",
12
+ "eslint": "^5.16.0",
13
+ "eslint-plugin-ava": "^7.0.0",
14
+ "npm-run-all": "^4.1.5",
15
+ "xo": "^0.24.0"
12
16
  },
13
17
  "xo": {
14
18
  "space": true,
@@ -29,6 +33,7 @@
29
33
  "hdd",
30
34
  "free"
31
35
  ],
36
+ "types": "./typings/index.d.ts",
32
37
  "author": "Alex-D <contact@alex-d.fr> (http://alex-d.fr)",
33
38
  "license": "MIT",
34
39
  "bugs": {
package/test.js CHANGED
@@ -15,6 +15,7 @@ const platformFixtures = {
15
15
  F: 834660716544 2000396742656
16
16
  `,
17
17
  result: {
18
+ diskPath: 'D:',
18
19
  free: 0,
19
20
  size: 699783168
20
21
  }
@@ -32,6 +33,7 @@ const platformFixtures = {
32
33
  L:
33
34
  `,
34
35
  result: {
36
+ diskPath: 'C:',
35
37
  free: 1286164480,
36
38
  size: 34359734272
37
39
  }
@@ -45,6 +47,7 @@ const platformFixtures = {
45
47
  D: 0 59494400
46
48
  `,
47
49
  result: {
50
+ diskPath: 'D:',
48
51
  free: 0,
49
52
  size: 59494400
50
53
  }
@@ -58,6 +61,7 @@ const platformFixtures = {
58
61
  D: 0 4001759232
59
62
  `,
60
63
  result: {
64
+ diskPath: 'C:',
61
65
  free: 159345410048,
62
66
  size: 171204145152
63
67
  }
@@ -72,6 +76,7 @@ const platformFixtures = {
72
76
  devtmpfs 447624 0 447624 0% /dev
73
77
  `,
74
78
  result: {
79
+ diskPath: '/dev',
75
80
  free: 447624 * 1024,
76
81
  size: 447624 * 1024
77
82
  }
@@ -84,6 +89,7 @@ const platformFixtures = {
84
89
  tmpfs 89884 8 89876 1% /run/user/1000
85
90
  `,
86
91
  result: {
92
+ diskPath: '/run/user/1000',
87
93
  free: 89876 * 1024,
88
94
  size: 89884 * 1024
89
95
  }
@@ -96,6 +102,7 @@ const platformFixtures = {
96
102
  /dev/sdc 240234168 188729228 39278628 83% /media/Games
97
103
  `,
98
104
  result: {
105
+ diskPath: '/media/Games',
99
106
  free: 39278628 * 1024,
100
107
  size: 240234168 * 1024
101
108
  }
@@ -110,6 +117,7 @@ const platformFixtures = {
110
117
  /dev/disk0s2 145961032 20678788 125026244 15% /home
111
118
  `,
112
119
  result: {
120
+ diskPath: '/home',
113
121
  free: 125026244 * 1024,
114
122
  size: 145961032 * 1024
115
123
  }
@@ -119,7 +127,7 @@ const platformFixtures = {
119
127
 
120
128
  function mockCheckDiskSpace(platform, fixture) {
121
129
  // Clean some required paths
122
- delete require.cache[require.resolve('./')]
130
+ delete require.cache[require.resolve('.')]
123
131
 
124
132
  // Forces the platform for test purposes
125
133
  Object.defineProperty(process, 'platform', {value: platform})
@@ -131,12 +139,12 @@ function mockCheckDiskSpace(platform, fixture) {
131
139
  })
132
140
  }
133
141
 
134
- return require('./')
142
+ return require('.')
135
143
  }
136
144
 
137
145
  function mockGetFirstExistingParentPath(existingDirectoryPath) {
138
146
  // Clean some required paths
139
- delete require.cache[require.resolve('./')]
147
+ delete require.cache[require.resolve('.')]
140
148
 
141
149
  // Forces the platform for test purposes
142
150
  Object.defineProperty(process, 'platform', {value: 'linux'})
@@ -144,7 +152,7 @@ function mockGetFirstExistingParentPath(existingDirectoryPath) {
144
152
  // Mock child_process.exec
145
153
  require('fs').existsSync = directoryPath => directoryPath === existingDirectoryPath
146
154
 
147
- return require('./').getFirstExistingParentPath
155
+ return require('.').getFirstExistingParentPath
148
156
  }
149
157
 
150
158
  Object.keys(platformFixtures).forEach(platform => {
@@ -157,40 +165,40 @@ Object.keys(platformFixtures).forEach(platform => {
157
165
  })
158
166
  })
159
167
 
160
- test(`win32: path did not match any disk`, async t => {
168
+ test('win32: path did not match any disk', async t => {
161
169
  const checkDiskSpace = mockCheckDiskSpace('win32', platformFixtures.win32[0])
162
- const error = await t.throws(checkDiskSpace('Z:/shouldfail'))
170
+ const error = await t.throwsAsync(checkDiskSpace('Z:/shouldfail'))
163
171
  t.is(error.name, 'NoMatchError')
164
172
  })
165
173
 
166
- test(`win32: invalid path`, async t => {
174
+ test('win32: invalid path', async t => {
167
175
  const checkDiskSpace = mockCheckDiskSpace('win32', platformFixtures.win32[0])
168
- const error = await t.throws(checkDiskSpace('an invalid path'))
176
+ const error = await t.throwsAsync(checkDiskSpace('an invalid path'))
169
177
  t.is(error.name, 'InvalidPathError')
170
178
  })
171
179
 
172
- test(`unix: invalid path`, async t => {
180
+ test('unix: invalid path', async t => {
173
181
  const checkDiskSpace = mockCheckDiskSpace('linux', platformFixtures.linux[0])
174
- const error = await t.throws(checkDiskSpace('an invalid path'))
182
+ const error = await t.throwsAsync(checkDiskSpace('an invalid path'))
175
183
  t.is(error.name, 'InvalidPathError')
176
184
  })
177
185
 
178
- test(`exec has an error`, async t => {
186
+ test('exec has an error', async t => {
179
187
  const fixture = JSON.parse(JSON.stringify(platformFixtures.win32[0]))
180
188
  fixture.execError = new Error('some error')
181
189
 
182
190
  const checkDiskSpace = mockCheckDiskSpace('win32', fixture)
183
- const error = await t.throws(checkDiskSpace('C:/something'))
191
+ const error = await t.throwsAsync(checkDiskSpace('C:/something'))
184
192
  t.is(error.message, 'some error')
185
193
  })
186
194
 
187
- test(`unix: get first existing parent path`, t => {
195
+ test('unix: get first existing parent path', t => {
188
196
  const parentPath = '/home/Lisa'
189
197
  const getFirstExistingParentPath = mockGetFirstExistingParentPath(parentPath)
190
198
  t.is(getFirstExistingParentPath('/home/Lisa/games/Ankama/Dofus'), parentPath)
191
199
  })
192
200
 
193
- test(`unix: get first parent can be the path itself`, t => {
201
+ test('unix: get first parent can be the path itself', t => {
194
202
  const parentPath = '/home/Lisa'
195
203
  const getFirstExistingParentPath = mockGetFirstExistingParentPath(parentPath)
196
204
  t.is(getFirstExistingParentPath(parentPath), parentPath)
@@ -0,0 +1,77 @@
1
+ declare module "check-disk-space" {
2
+ namespace checkDiskSpace {
3
+ /**
4
+ * The result of the disk space check
5
+ */
6
+ interface CheckDiskSpaceResult {
7
+ /**
8
+ * How much space is left
9
+ */
10
+ free: number;
11
+ /**
12
+ * The size of the path
13
+ */
14
+ size: number;
15
+ }
16
+
17
+ /**
18
+ * Error for invalid path
19
+ */
20
+ class InvalidPathError {
21
+ constructor(message: string);
22
+ /**
23
+ * The name of the error
24
+ */
25
+ name: "InvalidPathError";
26
+ /**
27
+ * The error message
28
+ */
29
+ message: string | "";
30
+ }
31
+
32
+ /**
33
+ * Error for path which do not match the actual file system
34
+ */
35
+ class NoMatchError {
36
+ constructor(message: any);
37
+ /**
38
+ * The name of the error
39
+ */
40
+ name: "NoMatchError";
41
+ /**
42
+ * The error message
43
+ */
44
+ message: string | "";
45
+ }
46
+
47
+ /**
48
+ * Get the first existing parent path
49
+ *
50
+ * @param {string} directoryPath The file/folder path from where we want to know disk space
51
+ * @returns {string} The first existing parent path
52
+ */
53
+ function getFirstExistingParentPath(directoryPath: string): string;
54
+ }
55
+
56
+ /**
57
+ * Build the check call for unix or win32
58
+ *
59
+ * @param {string} directoryPath The file/folder path from where we want to know disk space
60
+ * @return {Promise<CheckDiskSpaceResult>} The result of the check
61
+ *
62
+ * @example
63
+ * // On Windows
64
+ * const diskSpace = await checkDiskSpace('C:/blabla/bla');
65
+ * console.log(diskSpace); // { free: 12345678, size: 98756432 }
66
+ *
67
+ * @example
68
+ * const diskSpace = await checkDiskSpace('/mnt/mygames');
69
+ * console.log(diskSpace); // { free: 12345678, size: 98756432 }
70
+ *
71
+ * @throws {NoMatchError} In case the given path does not match with the actual file system
72
+ * @throws {InvalidPathError} In case the path is invalid
73
+ */
74
+ function checkDiskSpace(directoryPath: string): Promise<checkDiskSpace.CheckDiskSpaceResult>;
75
+
76
+ export = checkDiskSpace;
77
+ }
@@ -0,0 +1,19 @@
1
+ import * as checkDiskSpace from 'check-disk-space';
2
+
3
+ (async () => {
4
+ try {
5
+ const diskSpace = await checkDiskSpace('/mnt/mygames');
6
+ diskSpace.free;
7
+ diskSpace.size;
8
+ } catch (err) {
9
+ if (err instanceof checkDiskSpace.InvalidPathError) {
10
+ console.error('Invalid Path Error');
11
+ } else if (err instanceof checkDiskSpace.NoMatchError) {
12
+ console.error('No Match Error');
13
+ } else {
14
+ console.error('Error');
15
+ }
16
+ }
17
+ })();
18
+
19
+ checkDiskSpace.getFirstExistingParentPath('/path');
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$" />
5
- <orderEntry type="inheritedJdk" />
6
- <orderEntry type="sourceFolder" forTests="false" />
7
- </component>
8
- </module>
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="JavaScriptLibraryMappings">
4
- <includedPredefinedLibrary name="Node.js Core" />
5
- </component>
6
- </project>
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="EslintConfiguration">
4
- <custom-configuration-file used="true" path="$PROJECT_DIR$/.eslintrc.yml" />
5
- </component>
6
- </project>
package/.idea/misc.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="JavaScriptSettings">
4
- <option name="languageLevel" value="ES6" />
5
- </component>
6
- </project>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/check-disk-space.iml" filepath="$PROJECT_DIR$/.idea/check-disk-space.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
- </component>
6
- </project>