mybase 1.1.26 → 1.1.28

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mybase",
3
- "version": "1.1.26",
3
+ "version": "1.1.28",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -30,7 +30,9 @@ exports.ensureFolder = void 0;
30
30
  const debug_1 = __importDefault(require("debug"));
31
31
  const fs = __importStar(require("fs"));
32
32
  const dbg = (0, debug_1.default)('_mybase:ensureFolder');
33
- function ensureFolder(folderPath) {
33
+ function ensureFolder(folderPath, options) {
34
+ options = options || {};
35
+ options.recursive = true;
34
36
  dbg('ensureFolder()', folderPath);
35
37
  if (fs.existsSync(folderPath)) {
36
38
  if (!fs.lstatSync(folderPath).isDirectory()) {
@@ -40,8 +42,9 @@ function ensureFolder(folderPath) {
40
42
  dbg(`${folderPath} exists and is a folder`);
41
43
  return folderPath;
42
44
  }
43
- dbg(`creating ${folderPath}`);
44
- return fs.mkdirSync(folderPath, { recursive: true }) ? folderPath : undefined;
45
+ dbg(`creating ${folderPath}`, options);
46
+ fs.mkdirSync(folderPath, options);
47
+ return folderPath;
45
48
  }
46
49
  exports.ensureFolder = ensureFolder;
47
50
  //# sourceMappingURL=ensureFolder.js.map
@@ -29,13 +29,51 @@ describe('ensureFolder', () => {
29
29
  expect(ensureFolder(folderPath)).toBe(folderPath)
30
30
  })
31
31
 
32
- it('we should be able to create recursively a folders', () => {
33
- const sfolder= ensureFolder(`${folderPath}/subfolder/subsubfolder`)
32
+ it('we should be able to create recursively a folders and default to recursive mode', () => {
33
+ let sfolder= ensureFolder(`${folderPath}/subfolder/subsubfolder`)
34
34
  expect(sfolder).toBe(`${folderPath}/subfolder/subsubfolder`)
35
35
  if (sfolder) expect(fs.existsSync(sfolder)).toBe(true)
36
+ // test creating 10 recursive folders
37
+ for(let i=0;i<10;i++) {
38
+ sfolder= `${sfolder}/subsubsubfolder${i}`
39
+ expect(ensureFolder(sfolder)).toBe(sfolder)
40
+ if (sfolder) expect(fs.existsSync(sfolder)).toBe(true)
41
+ }
42
+ })
43
+
44
+ it('we should not be able to disable recursive mode', () => {
45
+ expect(ensureFolder(`${folderPath}/subfolder/subsubfolder`, { recursive: false })).toBe(`${folderPath}/subfolder/subsubfolder`)
46
+ })
47
+
48
+ it('we should be able to create a folder with a custom mode', () => {
49
+ let sfolder= ensureFolder(`${folderPath}/subfolder/subsubfolder`, { mode: 0o700 })
50
+ expect(sfolder).toBe(`${folderPath}/subfolder/subsubfolder`)
51
+ if (sfolder) expect(fs.existsSync(sfolder)).toBe(true)
52
+ expect(fs.statSync(sfolder).mode & 0o777).toBe(0o700);
36
53
  })
37
54
 
38
55
  it('try to create a file where we do not have write permissions should throw an error', () => {
39
56
  expect(() => ensureFolder('/dev/ensureFolder.test.file')).toThrow()
40
57
  })
58
+
59
+ it('should throw an error if folder path is empty', () => {
60
+ expect(() => ensureFolder('')).toThrow();
61
+ });
62
+
63
+ it('should throw an error if folder path is null or undefined or array or object', () => {
64
+ expect(() => ensureFolder(null as unknown as string)).toThrow();
65
+ expect(() => ensureFolder(undefined as unknown as string)).toThrow();
66
+ expect(() => ensureFolder([] as unknown as string)).toThrow();
67
+ expect(() => ensureFolder({} as unknown as string)).toThrow();
68
+ });
69
+
70
+ it('should not change the permissions of an existing folder', () => {
71
+ fs.mkdirSync(folderPath, { mode: 0o755 });
72
+ ensureFolder(folderPath, { mode: 0o700 });
73
+ expect(fs.statSync(folderPath).mode & 0o777).toBe(0o755);
74
+ });
75
+
76
+ it('should throw error for invalid mode value', () => {
77
+
78
+ });
41
79
  })
@@ -1,8 +1,11 @@
1
1
  import debug from 'debug'
2
2
  import * as fs from 'fs';
3
+ import { MakeDirectoryOptions } from 'fs';
3
4
  const dbg = debug('_mybase:ensureFolder')
4
5
 
5
- export function ensureFolder(folderPath: string) : string | undefined {
6
+ export function ensureFolder(folderPath: string, options?: MakeDirectoryOptions | null) : string {
7
+ options = options || {}
8
+ options.recursive = true
6
9
  dbg('ensureFolder()', folderPath)
7
10
  if (fs.existsSync(folderPath)) {
8
11
  if (!fs.lstatSync(folderPath).isDirectory()) {
@@ -12,6 +15,7 @@ export function ensureFolder(folderPath: string) : string | undefined {
12
15
  dbg(`${folderPath} exists and is a folder`)
13
16
  return folderPath
14
17
  }
15
- dbg(`creating ${folderPath}`)
16
- return fs.mkdirSync(folderPath, { recursive: true }) ? folderPath : undefined
18
+ dbg(`creating ${folderPath}`,options)
19
+ fs.mkdirSync(folderPath, options)
20
+ return folderPath
17
21
  }
@@ -65,7 +65,6 @@ describe('isLoopbackIP', () => {
65
65
  test('random ips should not be Loopback', () => {
66
66
  for(let i=0;i<10;i++){
67
67
  let ip = randomIP()
68
- console.log(ip)
69
68
  expect(isLoopbackIP(ip)).toBe(false)
70
69
  }
71
70
 
package/ts/index.js CHANGED
@@ -40,6 +40,7 @@ __exportStar(require("./funcs/getMysql2"), exports);
40
40
  __exportStar(require("./funcs/initMysql2Pool"), exports);
41
41
  __exportStar(require("./funcs/randomUTFString"), exports);
42
42
  __exportStar(require("./funcs/MaxRuntimeHours"), exports);
43
+ __exportStar(require("./funcs/ensureFolder"), exports);
43
44
  // models
44
45
  __exportStar(require("./models/Unixtime"), exports);
45
46
  __exportStar(require("./models/Timespan"), exports);
package/ts/index.ts CHANGED
@@ -24,7 +24,7 @@ export * from "./funcs/getMysql2"
24
24
  export * from "./funcs/initMysql2Pool"
25
25
  export * from "./funcs/randomUTFString"
26
26
  export * from "./funcs/MaxRuntimeHours"
27
-
27
+ export * from "./funcs/ensureFolder"
28
28
 
29
29
 
30
30
  // models
@@ -64,7 +64,7 @@ class IPAddress {
64
64
  return (0, __1.isLANIp)(this._ip.toString());
65
65
  }
66
66
  isPublicIP() {
67
- return !this.isLoopbackIP() && !this.isLANIP() && this.toString() !== '0.0.0.0';
67
+ return this.toString() !== '0.0.0.0' && !this.isLoopbackIP() && !this.isLANIP();
68
68
  }
69
69
  static unserialize(serializedIpString) {
70
70
  var _a, _b;
@@ -59,7 +59,7 @@ export class IPAddress {
59
59
  }
60
60
 
61
61
  public isPublicIP(): boolean {
62
- return !this.isLoopbackIP() && !this.isLANIP() && this.toString() !== '0.0.0.0'
62
+ return this.toString() !== '0.0.0.0' && !this.isLoopbackIP() && !this.isLANIP()
63
63
  }
64
64
 
65
65
  public static unserialize(serializedIpString: string): IPAddress {