mybase 1.1.25 → 1.1.27

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.25",
3
+ "version": "1.1.27",
4
4
  "description": "",
5
5
  "main": "mybase.js",
6
6
  "scripts": {
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.ensureFolder = void 0;
30
+ const debug_1 = __importDefault(require("debug"));
31
+ const fs = __importStar(require("fs"));
32
+ const dbg = (0, debug_1.default)('_mybase:ensureFolder');
33
+ function ensureFolder(folderPath, options) {
34
+ options = options || {};
35
+ options.recursive = true;
36
+ dbg('ensureFolder()', folderPath);
37
+ if (fs.existsSync(folderPath)) {
38
+ if (!fs.lstatSync(folderPath).isDirectory()) {
39
+ dbg(`${folderPath} exists and is not a folder`);
40
+ throw new Error(`${folderPath} exists and is not a folder`);
41
+ }
42
+ dbg(`${folderPath} exists and is a folder`);
43
+ return folderPath;
44
+ }
45
+ dbg(`creating ${folderPath}`, options);
46
+ fs.mkdirSync(folderPath, options);
47
+ return folderPath;
48
+ }
49
+ exports.ensureFolder = ensureFolder;
50
+ //# sourceMappingURL=ensureFolder.js.map
@@ -0,0 +1,79 @@
1
+ import { ensureFolder } from './ensureFolder'
2
+ import * as fs from 'fs'
3
+ import { wait } from '../index'
4
+
5
+ describe('ensureFolder', () => {
6
+ let folderPath: string
7
+ let filePath: string
8
+
9
+ beforeEach(async () => {
10
+ folderPath = `/tmp/ensureFolder.test.${Date.now()}`
11
+ filePath = `/tmp/ensureFolder.test.${Date.now()}.file`
12
+ await wait(1/1000)
13
+ })
14
+
15
+ it('should create a folder if it does not exist and return the folder path', () => {
16
+ const folder = ensureFolder(folderPath)
17
+ expect(folder).toBe(folderPath)
18
+ expect(fs.existsSync(folderPath)).toBe(true)
19
+ })
20
+
21
+ it('should throw an error if the folder path exists and is not a folder', () => {
22
+ fs.writeFileSync(filePath, 'test')
23
+ expect(() => ensureFolder(filePath)).toThrow('exists and is not a folder')
24
+ })
25
+
26
+ it('creating same folder twice should return the same folder path', () => {
27
+ expect(ensureFolder(folderPath)).toBe(folderPath)
28
+ expect(fs.existsSync(folderPath)).toBe(true)
29
+ expect(ensureFolder(folderPath)).toBe(folderPath)
30
+ })
31
+
32
+ it('we should be able to create recursively a folders and default to recursive mode', () => {
33
+ let sfolder= ensureFolder(`${folderPath}/subfolder/subsubfolder`)
34
+ expect(sfolder).toBe(`${folderPath}/subfolder/subsubfolder`)
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);
53
+ })
54
+
55
+ it('try to create a file where we do not have write permissions should throw an error', () => {
56
+ expect(() => ensureFolder('/dev/ensureFolder.test.file')).toThrow()
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
+ });
79
+ })
@@ -0,0 +1,21 @@
1
+ import debug from 'debug'
2
+ import * as fs from 'fs';
3
+ import { MakeDirectoryOptions } from 'fs';
4
+ const dbg = debug('_mybase:ensureFolder')
5
+
6
+ export function ensureFolder(folderPath: string, options?: MakeDirectoryOptions | null) : string {
7
+ options = options || {}
8
+ options.recursive = true
9
+ dbg('ensureFolder()', folderPath)
10
+ if (fs.existsSync(folderPath)) {
11
+ if (!fs.lstatSync(folderPath).isDirectory()) {
12
+ dbg(`${folderPath} exists and is not a folder`)
13
+ throw new Error(`${folderPath} exists and is not a folder`);
14
+ }
15
+ dbg(`${folderPath} exists and is a folder`)
16
+ return folderPath
17
+ }
18
+ dbg(`creating ${folderPath}`,options)
19
+ fs.mkdirSync(folderPath, options)
20
+ return folderPath
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.d.ts CHANGED
@@ -23,3 +23,5 @@ export * from "./funcs/getMysql2";
23
23
  export * from "./funcs/initMysql2Pool";
24
24
  export * from "./funcs/randomUTFString";
25
25
  export * from "./funcs/MaxRuntimeHours";
26
+ export * from "./funcs/ensureFolder";
27
+
@@ -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 {