mybase 1.1.25 → 1.1.26
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 +1 -1
- package/ts/funcs/ensureFolder.js +47 -0
- package/ts/funcs/ensureFolder.test.ts +41 -0
- package/ts/funcs/ensureFolder.ts +17 -0
- package/ts/index.d.ts +2 -0
package/package.json
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
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) {
|
|
34
|
+
dbg('ensureFolder()', folderPath);
|
|
35
|
+
if (fs.existsSync(folderPath)) {
|
|
36
|
+
if (!fs.lstatSync(folderPath).isDirectory()) {
|
|
37
|
+
dbg(`${folderPath} exists and is not a folder`);
|
|
38
|
+
throw new Error(`${folderPath} exists and is not a folder`);
|
|
39
|
+
}
|
|
40
|
+
dbg(`${folderPath} exists and is a folder`);
|
|
41
|
+
return folderPath;
|
|
42
|
+
}
|
|
43
|
+
dbg(`creating ${folderPath}`);
|
|
44
|
+
return fs.mkdirSync(folderPath, { recursive: true }) ? folderPath : undefined;
|
|
45
|
+
}
|
|
46
|
+
exports.ensureFolder = ensureFolder;
|
|
47
|
+
//# sourceMappingURL=ensureFolder.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
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', () => {
|
|
33
|
+
const sfolder= ensureFolder(`${folderPath}/subfolder/subsubfolder`)
|
|
34
|
+
expect(sfolder).toBe(`${folderPath}/subfolder/subsubfolder`)
|
|
35
|
+
if (sfolder) expect(fs.existsSync(sfolder)).toBe(true)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('try to create a file where we do not have write permissions should throw an error', () => {
|
|
39
|
+
expect(() => ensureFolder('/dev/ensureFolder.test.file')).toThrow()
|
|
40
|
+
})
|
|
41
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import debug from 'debug'
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
const dbg = debug('_mybase:ensureFolder')
|
|
4
|
+
|
|
5
|
+
export function ensureFolder(folderPath: string) : string | undefined {
|
|
6
|
+
dbg('ensureFolder()', folderPath)
|
|
7
|
+
if (fs.existsSync(folderPath)) {
|
|
8
|
+
if (!fs.lstatSync(folderPath).isDirectory()) {
|
|
9
|
+
dbg(`${folderPath} exists and is not a folder`)
|
|
10
|
+
throw new Error(`${folderPath} exists and is not a folder`);
|
|
11
|
+
}
|
|
12
|
+
dbg(`${folderPath} exists and is a folder`)
|
|
13
|
+
return folderPath
|
|
14
|
+
}
|
|
15
|
+
dbg(`creating ${folderPath}`)
|
|
16
|
+
return fs.mkdirSync(folderPath, { recursive: true }) ? folderPath : undefined
|
|
17
|
+
}
|