ensure-dir-tiny 1.0.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,48 @@
1
+ # This workflow will run tests using node and then publish a package to npm using Trusted Publishing (OIDC)
2
+ # For more information see: https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow
3
+
4
+ name: ensure-dir-tiny NPM Package
5
+
6
+ on:
7
+ push:
8
+ branches: [ main ]
9
+ pull_request:
10
+ branches: [ main ]
11
+
12
+ jobs:
13
+ build:
14
+ runs-on: ubuntu-latest
15
+
16
+ # Skip Dependabot PRs for CI/CD
17
+ if: ${{ github.actor != 'dependabot[bot]' }}
18
+
19
+ steps:
20
+ - uses: actions/checkout@v4
21
+ - uses: actions/setup-node@v4
22
+ with:
23
+ node-version: lts/*
24
+
25
+ - run: npm install
26
+ - run: npm test
27
+
28
+ publish-npm:
29
+ needs: build
30
+ runs-on: ubuntu-latest
31
+
32
+ # Skip Dependabot PRs
33
+ if: ${{ github.actor != 'dependabot[bot]' }}
34
+
35
+ # Required for OIDC Trusted Publishing
36
+ permissions:
37
+ contents: read
38
+ id-token: write # 👈 REQUIRED for Trusted Publishing
39
+
40
+ steps:
41
+ - uses: actions/checkout@v4
42
+ - uses: actions/setup-node@v4
43
+ with:
44
+ node-version: lts/*
45
+ registry-url: https://registry.npmjs.org/
46
+
47
+ - run: npm install
48
+ - run: npm publish
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Alex
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # ensure-dir
2
+
3
+ <p align="center"><a href="https://nodei.co/npm/ensure-dir/"><img src="https://nodei.co/npm/ensure-dir.png"></a></a></p>
4
+ <p align="center">
5
+ <img src="https://img.shields.io/badge/License-MIT-yellow.svg">
6
+ </p>
7
+
8
+ * 📁 Lightweight package that ensures that the directory at the given path exists. Creates it recursively if it doesn't. Prevents `"File Not Found"` errors when saving files.
9
+ * ♻️ Works seamlessly with `CommonJS`, `ESM` and `TypeScript`
10
+
11
+ # 📦 Install via [NPM](https://www.npmjs.com/package/contains-emoji)
12
+
13
+ ```bash
14
+ $ npm i ensure-dir
15
+ ```
16
+
17
+ # 💻 Usage
18
+
19
+ - See examples below
20
+
21
+ ## CommonJS
22
+ ```javascript
23
+ const path = require('path');
24
+ const fs = require('fs');
25
+ const ensureDir = require('ensure-dir');
26
+
27
+ const testDir = path.join(__dirname, 'demo/a/b/c');
28
+
29
+ console.log('Creating directory:', testDir);
30
+ ensureDir(testDir);
31
+
32
+ if (fs.existsSync(testDir)) {
33
+ console.log('✅ Directory exists — module works');
34
+ } else {
35
+ console.log('❌ Directory not created');
36
+ }
37
+
38
+ // --| Run again to test idempotency
39
+ ensureDir(testDir);
40
+ console.log('✅ Called twice without crashing');
41
+
42
+ ```
43
+
44
+ ## ESM
45
+ ```javascript
46
+ import path from 'path';
47
+ import fs from 'fs';
48
+ import ensureDir from 'ensure-dir';
49
+ import { fileURLToPath } from 'url';
50
+
51
+ // __dirname replacement in ESM
52
+ const __filename = fileURLToPath(import.meta.url);
53
+ const __dirname = path.dirname(__filename);
54
+
55
+ const testDir = path.join(__dirname, 'demo/a/b/c');
56
+
57
+ console.log('Creating directory:', testDir);
58
+ ensureDir(testDir);
59
+
60
+ if (fs.existsSync(testDir)) {
61
+ console.log('✅ Directory exists — module works');
62
+ } else {
63
+ console.log('❌ Directory not created');
64
+ }
65
+
66
+ // --| Run again to test idempotency
67
+ ensureDir(testDir);
68
+ console.log('✅ Called twice without crashing');
69
+ ```
70
+
71
+ ## TypeScript
72
+ ```javascript
73
+ import path from 'path';
74
+ import fs from 'fs';
75
+ import ensureDir from 'ensure-dir';
76
+
77
+ const testDir: string = path.join(__dirname, 'demo/a/b/c');
78
+ console.log('Creating directory:', testDir);
79
+
80
+ ensureDir(testDir);
81
+
82
+ if (fs.existsSync(testDir)) {
83
+ console.log('✅ Directory exists — module works');
84
+ } else {
85
+ console.log('❌ Directory not created');
86
+ }
87
+
88
+ ensureDir(testDir);
89
+ console.log('✅ Called twice without crashing');
90
+ ```
package/index.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Ensures that the directory at the given path exists. Creates it recursively if it doesn't.
3
+ * Prevents "File Not Found" errors when saving files.
4
+ *
5
+ * @param {string} path - The path of the directory to ensure.
6
+ * @returns {void}
7
+ *
8
+ * @example
9
+ * ensureDirectory("./data/logs");
10
+ */
11
+ declare function ensureDirectory(path: string): void;
12
+
13
+ export = ensureDirectory;
package/index.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * ensure-dir-tiny - 📁 Ensures that the directory at the given path exists. Creates it recursively if it doesn't. Prevents "File Not Found" errors when saving files.
3
+ * @version: v1.0.0
4
+ * @link: https://github.com/tutyamxx/ensure-dir-tiny
5
+ * @license: MIT
6
+ **/
7
+
8
+ const fs = require('fs');
9
+
10
+ /**
11
+ * Ensures that the directory at the given path exists. Creates it recursively if it doesn't.
12
+ * Prevents "File Not Found" errors when saving files.
13
+ *
14
+ * @param {string} path - The path of the directory to ensure.
15
+ * @returns {void}
16
+ *
17
+ * @example
18
+ * ensureDir("./data/logs");
19
+ */
20
+ const ensureDir = path => fs.existsSync(path) || fs.mkdirSync(path, { recursive: true });
21
+
22
+ // --| CommonJS export
23
+ module.exports = ensureDir;
24
+
25
+ // --| ESM default export for `import` statements
26
+ module.exports.default = ensureDir;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "ensure-dir-tiny",
3
+ "version": "1.0.0",
4
+ "description": "📁 Checks if a folder exists; if not, it creates it. This prevents \"File Not Found\" errors when saving data.",
5
+ "main": "index.js",
6
+ "module": "index.js",
7
+ "types": "index.d.ts",
8
+ "exports": {
9
+ "require": "./index.js",
10
+ "import": "./index.js",
11
+ "types": "./index.d.ts"
12
+ },
13
+ "scripts": {
14
+ "test": "jest --verbose"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/tutyamxx/ensure-dir-tiny.git"
19
+ },
20
+ "keywords": [
21
+ "ensure-dir-tiny",
22
+ "ensure-dir",
23
+ "ensure-directory",
24
+ "directory-exists",
25
+ "dir-exists",
26
+ "fs",
27
+ "mkdir",
28
+ "ensure"
29
+ ],
30
+ "author": "tuty",
31
+ "license": "ISC",
32
+ "type": "commonjs",
33
+ "bugs": {
34
+ "url": "https://github.com/tutyamxx/ensure-dir-tiny/issues"
35
+ },
36
+ "homepage": "https://github.com/tutyamxx/ensure-dir-tiny#readme",
37
+ "devDependencies": {
38
+ "jest": "^30.2.0"
39
+ }
40
+ }
@@ -0,0 +1,88 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const ensureDir = require('../index.js');
4
+
5
+ describe('ensureDir', () => {
6
+ const baseDir = path.join(__dirname, 'tmp_test');
7
+
8
+ const cleanup = () => {
9
+ if (fs.existsSync(baseDir)) {
10
+ fs.rmSync(baseDir, { recursive: true, force: true });
11
+ }
12
+ };
13
+
14
+ beforeEach(cleanup);
15
+ afterAll(cleanup);
16
+
17
+ test('Creates a new directory if it does not exist', () => {
18
+ const dir = path.join(baseDir, 'newDir');
19
+
20
+ ensureDir(dir);
21
+ expect(fs.existsSync(dir)).toBe(true);
22
+ });
23
+
24
+ test('Does not throw if directory already exists', () => {
25
+ const dir = path.join(baseDir, 'existingDir');
26
+
27
+ fs.mkdirSync(dir, { recursive: true });
28
+ expect(() => ensureDir(dir)).not.toThrow();
29
+ });
30
+
31
+ test('Creates nested directories recursively', () => {
32
+ const nestedDir = path.join(baseDir, 'a/b/c/d');
33
+
34
+ ensureDir(nestedDir);
35
+ expect(fs.existsSync(nestedDir)).toBe(true);
36
+ });
37
+
38
+ test('Is safe to call multiple times (idempotent)', () => {
39
+ const dir = path.join(baseDir, 'repeat');
40
+
41
+ ensureDir(dir);
42
+ ensureDir(dir);
43
+ ensureDir(dir);
44
+
45
+ expect(fs.existsSync(dir)).toBe(true);
46
+ });
47
+
48
+ test('Works with trailing slash', () => {
49
+ const dir = path.join(baseDir, 'slash/') ;
50
+
51
+ ensureDir(dir);
52
+ expect(fs.existsSync(dir)).toBe(true);
53
+ });
54
+
55
+ test('Works with absolute paths', () => {
56
+ const abs = path.resolve(baseDir, 'absoluteDir');
57
+
58
+ ensureDir(abs);
59
+ expect(fs.existsSync(abs)).toBe(true);
60
+ });
61
+
62
+ test('Works with relative paths', () => {
63
+ const rel = './tmp_test/relativeDir';
64
+
65
+ ensureDir(rel);
66
+ expect(fs.existsSync(rel)).toBe(true);
67
+ });
68
+
69
+ test('Handles directory names with spaces', () => {
70
+ const dir = path.join(baseDir, 'dir with spaces');
71
+
72
+ ensureDir(dir);
73
+ expect(fs.existsSync(dir)).toBe(true);
74
+ });
75
+
76
+ test('Does nothing if path points to an existing file', () => {
77
+ const filePath = path.join(baseDir, 'file.txt');
78
+ ensureDir(baseDir);
79
+ fs.writeFileSync(filePath, 'x');
80
+
81
+ expect(() => ensureDir(filePath)).not.toThrow();
82
+ expect(fs.statSync(filePath).isFile()).toBe(true);
83
+ });
84
+
85
+ test('Throws on empty path', () => {
86
+ expect(() => ensureDir('')).toThrow();
87
+ });
88
+ });