@js-utils-kit/fs 1.3.0 → 1.5.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.
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`fs`);c=s(c);let l=require(`path`);l=s(l);let u=require(`archiver`);u=s(u);function d({format:e,source:t,destination:n,options:r={},log:i=!0,onSuccess:a}){let o=l.default.resolve(t);if(!c.default.existsSync(o)||!c.default.statSync(o).isDirectory())throw Error(`Source directory "${t}" does not exist or is not a directory.`);let s=c.default.createWriteStream(n);e===`zip`&&(r={...r,zlib:{level:9}});let d=(0,u.default)(e,r);return new Promise((e,r)=>{s.on(`close`,()=>{let t=d.pointer();i&&console.log(`${n} created: ${t} total bytes`),a&&a(t),e()}),d.on(`error`,e=>{r(e)}),d.pipe(s),d.directory(t,!1),d.finalize().catch(e=>r(e instanceof Error?e:Error(String(e))))})}exports.createArchive=d;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`fs`);c=s(c);let l=require(`path`);l=s(l);let u=require(`archiver`);u=s(u);let d=require(`node:fs/promises`),f=require(`url`);function p({format:e,source:t,destination:n,options:r={},log:i=!0,onSuccess:a}){let o=l.default.resolve(t);if(!c.default.existsSync(o)||!c.default.statSync(o).isDirectory())throw Error(`Source directory "${t}" does not exist or is not a directory.`);let s=c.default.createWriteStream(n);e===`zip`&&(r={...r,zlib:{level:9}});let d=(0,u.default)(e,r);return new Promise((e,r)=>{s.on(`close`,()=>{let t=d.pointer();i&&console.log(`${n} created: ${t} total bytes`),a&&a(t),e()}),d.on(`error`,e=>{r(e)}),d.pipe(s),d.directory(t,!1),d.finalize().catch(e=>r(e instanceof Error?e:Error(String(e))))})}const m=typeof __filename<`u`;async function h(e){try{return await(0,d.access)(e),!0}catch{return!1}}function g(e){if(!e)throw Error(`locateModuleFile requires import.meta.url (ESM) or __filename (CJS).`);return e.startsWith(`file:`)?(0,f.fileURLToPath)(e):e}function _(e){return l.default.dirname(g(e))}function v(e,t){let n=_(t),r=l.default.resolve(n,e);if(l.default.relative(n,r).startsWith(`..`))throw Error(`Resolved path escapes module directory.`);return r}function y(e){return e.replace(/\\/g,`/`)}function b(e){return e.replace(/\//g,`\\`)}function x(e){return e.replace(/[/\\]/g,l.default.sep)}exports.createArchive=p,exports.exists=h,exports.hasCommonJSFilename=m,exports.locateModuleDirectory=_,exports.locateModuleFile=g,exports.resolveModuleRelative=v,exports.toPlatformPath=x,exports.toPosixPath=y,exports.toWinPath=b;
package/dist/index.d.cts CHANGED
@@ -33,4 +33,186 @@ declare function createArchive({
33
33
  onSuccess
34
34
  }: CreateArchiveOptions): Promise<void>;
35
35
  //#endregion
36
- export { createArchive };
36
+ //#region src/env.d.ts
37
+ /**
38
+ * Determines whether the current runtime provides the CommonJS `__filename` variable.
39
+ *
40
+ * @remarks
41
+ * - In CommonJS environments, Node.js injects a module-scoped `__filename` variable representing the absolute path of the current module file.
42
+ * - In pure ESM environments, `__filename` does not exist.
43
+ * - This helper allows environment-aware branching while remaining testable (e.g., via mocking in unit tests).
44
+ *
45
+ * @returns `true` if `__filename` is available in the current runtime; otherwise `false`.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * if (hasCommonJSFilename) {
50
+ * console.log("Running in CommonJS environment");
51
+ * } else {
52
+ * console.log("Running in ESM environment");
53
+ * }
54
+ */
55
+ declare const hasCommonJSFilename: boolean;
56
+ //#endregion
57
+ //#region src/file.d.ts
58
+ /** Check whether a file or directory exists */
59
+ declare function exists(/** Absolute or relative path to check */
60
+
61
+ path: string): Promise<boolean>;
62
+ //#endregion
63
+ //#region src/locator.d.ts
64
+ /**
65
+ * Returns the absolute file path of a module.
66
+ *
67
+ * @remarks
68
+ * This utility supports both:
69
+ * - **ESM** → pass `import.meta.url`
70
+ * - **CommonJS** → pass `__filename`
71
+ *
72
+ * The path MUST be explicitly provided. Automatic runtime detection is intentionally not performed to avoid incorrect module resolution.
73
+ *
74
+ * @returns Absolute path to the provided module file.
75
+ *
76
+ * @throws {Error} If `metaUrlOrPath` is not provided.
77
+ *
78
+ * @example ESM usage
79
+ * ```ts
80
+ * const file = locateModuleFile(import.meta.url);
81
+ * console.log(file);
82
+ * ```
83
+ *
84
+ * @example CommonJS usage
85
+ * ```ts
86
+ * const file = locateModuleFile(__filename);
87
+ * console.log(file);
88
+ * ```
89
+ */
90
+ declare function locateModuleFile(/** `import.meta.url` (ESM) or `__filename` (CJS) */
91
+
92
+ metaUrlOrPath: string): string;
93
+ /**
94
+ * Returns the absolute directory path of a module.
95
+ *
96
+ * @remarks
97
+ * Internally derives the directory from {@link locateModuleFile}.
98
+ *
99
+ * @returns Absolute directory path.
100
+ *
101
+ * @example ESM
102
+ * ```ts
103
+ * const dir = locateModuleDirectory(import.meta.url);
104
+ * ```
105
+ *
106
+ * @example CommonJS
107
+ * ```ts
108
+ * const dir = locateModuleDirectory(__filename);
109
+ * ```
110
+ */
111
+ declare function locateModuleDirectory(/** `import.meta.url` (ESM) or `__filename` (CJS) */
112
+
113
+ metaUrlOrPath: string): string;
114
+ /**
115
+ * Resolves a path relative to the provided module's directory.
116
+ *
117
+ * @remarks
118
+ * Useful for loading internal assets such as:
119
+ * - `.hbs` templates
120
+ * - JSON files
121
+ * - SQL schemas
122
+ * - bundled static resources
123
+ *
124
+ * Unlike `process.cwd()`, this resolves relative to the module
125
+ * file location, making it safe for usage inside `node_modules`.
126
+ *
127
+ * @returns Absolute resolved path.
128
+ *
129
+ * @throws {Error}
130
+ * If module path is not provided.
131
+ *
132
+ * @example ESM
133
+ * ```ts
134
+ * const templatePath = resolveModuleRelative(
135
+ * "../templates/welcome.hbs",
136
+ * import.meta.url
137
+ * );
138
+ * ```
139
+ *
140
+ * @example CommonJS
141
+ * ```ts
142
+ * const templatePath = resolveModuleRelative(
143
+ * "../templates/welcome.hbs",
144
+ * __filename
145
+ * );
146
+ * ```
147
+ */
148
+ declare function resolveModuleRelative(/** Path relative to the module directory */relativePath: string, /** `import.meta.url` (ESM) or `__filename` (CJS) */metaUrlOrPath: string): string;
149
+ //#endregion
150
+ //#region src/path.d.ts
151
+ /**
152
+ * Converts a file system path to POSIX format.
153
+ *
154
+ * @remarks
155
+ * - Replaces all backslashes (`\`) with forward slashes (`/`).
156
+ * - Does NOT perform full path normalization (e.g., resolving `.` or `..` segments).
157
+ *
158
+ * @returns The path using POSIX separators (`/`).
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * toPosixPath('C:\\Users\\TenE\\project')
163
+ * // => 'C:/Users/TenE/project'
164
+ * ```
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * toPosixPath('src\\utils\\file.ts')
169
+ * // => 'src/utils/file.ts'
170
+ * ```
171
+ */
172
+ declare function toPosixPath(/** The path to convert */
173
+
174
+ p: string): string;
175
+ /**
176
+ * Converts a file system path to Windows (Win32) format.
177
+ *
178
+ * On POSIX systems (Linux/macOS), all forward slashes (`/`) are replaced with backslashes (`\`).
179
+ *
180
+ * @returns The path using Windows separators (`\`).
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * toWinPath('src/utils/file.ts')
185
+ * // => 'src\\utils\\file.ts'
186
+ * ```
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * toWinPath('/usr/local/bin')
191
+ * // => '\\usr\\local\\bin'
192
+ * ```
193
+ */
194
+ declare function toWinPath(/** The path to convert */
195
+
196
+ p: string): string;
197
+ /**
198
+ * Converts a file system path to the current platform-specific format.
199
+ *
200
+ * This replaces both forward slashes (`/`) and backslashes (`\`) with the platform's separator (`path.sep`).
201
+ *
202
+ * - Windows → `\`
203
+ * - macOS/Linux → `/`
204
+ *
205
+ * @returns The path using the current OS separator.
206
+ *
207
+ * @example Mixed Separators
208
+ * ```ts
209
+ * toPlatformPath('src/utils/file.ts')
210
+ * // On Windows: 'src\\utils\\file.ts'
211
+ * // On POSIX: 'src/utils/file.ts'
212
+ * ```
213
+ */
214
+ declare function toPlatformPath(/** The path to convert */
215
+
216
+ p: string): string;
217
+ //#endregion
218
+ export { createArchive, exists, hasCommonJSFilename, locateModuleDirectory, locateModuleFile, resolveModuleRelative, toPlatformPath, toPosixPath, toWinPath };
package/dist/index.d.mts CHANGED
@@ -33,4 +33,186 @@ declare function createArchive({
33
33
  onSuccess
34
34
  }: CreateArchiveOptions): Promise<void>;
35
35
  //#endregion
36
- export { createArchive };
36
+ //#region src/env.d.ts
37
+ /**
38
+ * Determines whether the current runtime provides the CommonJS `__filename` variable.
39
+ *
40
+ * @remarks
41
+ * - In CommonJS environments, Node.js injects a module-scoped `__filename` variable representing the absolute path of the current module file.
42
+ * - In pure ESM environments, `__filename` does not exist.
43
+ * - This helper allows environment-aware branching while remaining testable (e.g., via mocking in unit tests).
44
+ *
45
+ * @returns `true` if `__filename` is available in the current runtime; otherwise `false`.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * if (hasCommonJSFilename) {
50
+ * console.log("Running in CommonJS environment");
51
+ * } else {
52
+ * console.log("Running in ESM environment");
53
+ * }
54
+ */
55
+ declare const hasCommonJSFilename: boolean;
56
+ //#endregion
57
+ //#region src/file.d.ts
58
+ /** Check whether a file or directory exists */
59
+ declare function exists(/** Absolute or relative path to check */
60
+
61
+ path: string): Promise<boolean>;
62
+ //#endregion
63
+ //#region src/locator.d.ts
64
+ /**
65
+ * Returns the absolute file path of a module.
66
+ *
67
+ * @remarks
68
+ * This utility supports both:
69
+ * - **ESM** → pass `import.meta.url`
70
+ * - **CommonJS** → pass `__filename`
71
+ *
72
+ * The path MUST be explicitly provided. Automatic runtime detection is intentionally not performed to avoid incorrect module resolution.
73
+ *
74
+ * @returns Absolute path to the provided module file.
75
+ *
76
+ * @throws {Error} If `metaUrlOrPath` is not provided.
77
+ *
78
+ * @example ESM usage
79
+ * ```ts
80
+ * const file = locateModuleFile(import.meta.url);
81
+ * console.log(file);
82
+ * ```
83
+ *
84
+ * @example CommonJS usage
85
+ * ```ts
86
+ * const file = locateModuleFile(__filename);
87
+ * console.log(file);
88
+ * ```
89
+ */
90
+ declare function locateModuleFile(/** `import.meta.url` (ESM) or `__filename` (CJS) */
91
+
92
+ metaUrlOrPath: string): string;
93
+ /**
94
+ * Returns the absolute directory path of a module.
95
+ *
96
+ * @remarks
97
+ * Internally derives the directory from {@link locateModuleFile}.
98
+ *
99
+ * @returns Absolute directory path.
100
+ *
101
+ * @example ESM
102
+ * ```ts
103
+ * const dir = locateModuleDirectory(import.meta.url);
104
+ * ```
105
+ *
106
+ * @example CommonJS
107
+ * ```ts
108
+ * const dir = locateModuleDirectory(__filename);
109
+ * ```
110
+ */
111
+ declare function locateModuleDirectory(/** `import.meta.url` (ESM) or `__filename` (CJS) */
112
+
113
+ metaUrlOrPath: string): string;
114
+ /**
115
+ * Resolves a path relative to the provided module's directory.
116
+ *
117
+ * @remarks
118
+ * Useful for loading internal assets such as:
119
+ * - `.hbs` templates
120
+ * - JSON files
121
+ * - SQL schemas
122
+ * - bundled static resources
123
+ *
124
+ * Unlike `process.cwd()`, this resolves relative to the module
125
+ * file location, making it safe for usage inside `node_modules`.
126
+ *
127
+ * @returns Absolute resolved path.
128
+ *
129
+ * @throws {Error}
130
+ * If module path is not provided.
131
+ *
132
+ * @example ESM
133
+ * ```ts
134
+ * const templatePath = resolveModuleRelative(
135
+ * "../templates/welcome.hbs",
136
+ * import.meta.url
137
+ * );
138
+ * ```
139
+ *
140
+ * @example CommonJS
141
+ * ```ts
142
+ * const templatePath = resolveModuleRelative(
143
+ * "../templates/welcome.hbs",
144
+ * __filename
145
+ * );
146
+ * ```
147
+ */
148
+ declare function resolveModuleRelative(/** Path relative to the module directory */relativePath: string, /** `import.meta.url` (ESM) or `__filename` (CJS) */metaUrlOrPath: string): string;
149
+ //#endregion
150
+ //#region src/path.d.ts
151
+ /**
152
+ * Converts a file system path to POSIX format.
153
+ *
154
+ * @remarks
155
+ * - Replaces all backslashes (`\`) with forward slashes (`/`).
156
+ * - Does NOT perform full path normalization (e.g., resolving `.` or `..` segments).
157
+ *
158
+ * @returns The path using POSIX separators (`/`).
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * toPosixPath('C:\\Users\\TenE\\project')
163
+ * // => 'C:/Users/TenE/project'
164
+ * ```
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * toPosixPath('src\\utils\\file.ts')
169
+ * // => 'src/utils/file.ts'
170
+ * ```
171
+ */
172
+ declare function toPosixPath(/** The path to convert */
173
+
174
+ p: string): string;
175
+ /**
176
+ * Converts a file system path to Windows (Win32) format.
177
+ *
178
+ * On POSIX systems (Linux/macOS), all forward slashes (`/`) are replaced with backslashes (`\`).
179
+ *
180
+ * @returns The path using Windows separators (`\`).
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * toWinPath('src/utils/file.ts')
185
+ * // => 'src\\utils\\file.ts'
186
+ * ```
187
+ *
188
+ * @example
189
+ * ```ts
190
+ * toWinPath('/usr/local/bin')
191
+ * // => '\\usr\\local\\bin'
192
+ * ```
193
+ */
194
+ declare function toWinPath(/** The path to convert */
195
+
196
+ p: string): string;
197
+ /**
198
+ * Converts a file system path to the current platform-specific format.
199
+ *
200
+ * This replaces both forward slashes (`/`) and backslashes (`\`) with the platform's separator (`path.sep`).
201
+ *
202
+ * - Windows → `\`
203
+ * - macOS/Linux → `/`
204
+ *
205
+ * @returns The path using the current OS separator.
206
+ *
207
+ * @example Mixed Separators
208
+ * ```ts
209
+ * toPlatformPath('src/utils/file.ts')
210
+ * // On Windows: 'src\\utils\\file.ts'
211
+ * // On POSIX: 'src/utils/file.ts'
212
+ * ```
213
+ */
214
+ declare function toPlatformPath(/** The path to convert */
215
+
216
+ p: string): string;
217
+ //#endregion
218
+ export { createArchive, exists, hasCommonJSFilename, locateModuleDirectory, locateModuleFile, resolveModuleRelative, toPlatformPath, toPosixPath, toWinPath };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import e from"fs";import t from"path";import n from"archiver";function r({format:r,source:i,destination:a,options:o={},log:s=!0,onSuccess:c}){let l=t.resolve(i);if(!e.existsSync(l)||!e.statSync(l).isDirectory())throw Error(`Source directory "${i}" does not exist or is not a directory.`);let u=e.createWriteStream(a);r===`zip`&&(o={...o,zlib:{level:9}});let d=n(r,o);return new Promise((e,t)=>{u.on(`close`,()=>{let t=d.pointer();s&&console.log(`${a} created: ${t} total bytes`),c&&c(t),e()}),d.on(`error`,e=>{t(e)}),d.pipe(u),d.directory(i,!1),d.finalize().catch(e=>t(e instanceof Error?e:Error(String(e))))})}export{r as createArchive};
1
+ import e from"fs";import t from"path";import n from"archiver";import{access as r}from"node:fs/promises";import{fileURLToPath as i}from"url";function a({format:r,source:i,destination:a,options:o={},log:s=!0,onSuccess:c}){let l=t.resolve(i);if(!e.existsSync(l)||!e.statSync(l).isDirectory())throw Error(`Source directory "${i}" does not exist or is not a directory.`);let u=e.createWriteStream(a);r===`zip`&&(o={...o,zlib:{level:9}});let d=n(r,o);return new Promise((e,t)=>{u.on(`close`,()=>{let t=d.pointer();s&&console.log(`${a} created: ${t} total bytes`),c&&c(t),e()}),d.on(`error`,e=>{t(e)}),d.pipe(u),d.directory(i,!1),d.finalize().catch(e=>t(e instanceof Error?e:Error(String(e))))})}const o=typeof __filename<`u`;async function s(e){try{return await r(e),!0}catch{return!1}}function c(e){if(!e)throw Error(`locateModuleFile requires import.meta.url (ESM) or __filename (CJS).`);return e.startsWith(`file:`)?i(e):e}function l(e){return t.dirname(c(e))}function u(e,n){let r=l(n),i=t.resolve(r,e);if(t.relative(r,i).startsWith(`..`))throw Error(`Resolved path escapes module directory.`);return i}function d(e){return e.replace(/\\/g,`/`)}function f(e){return e.replace(/\//g,`\\`)}function p(e){return e.replace(/[/\\]/g,t.sep)}export{a as createArchive,s as exists,o as hasCommonJSFilename,l as locateModuleDirectory,c as locateModuleFile,u as resolveModuleRelative,p as toPlatformPath,d as toPosixPath,f as toWinPath};
package/package.json CHANGED
@@ -1,14 +1,7 @@
1
1
  {
2
2
  "name": "@js-utils-kit/fs",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "File system utilities",
5
- "private": false,
6
- "license": "MIT",
7
- "author": {
8
- "name": "Sriman",
9
- "email": "136729116+TenEplaysOfficial@users.noreply.github.com",
10
- "url": "https://tene.vercel.app"
11
- },
12
5
  "homepage": "https://js-utils.js.org",
13
6
  "repository": {
14
7
  "type": "git",
@@ -18,25 +11,32 @@
18
11
  "bugs": {
19
12
  "url": "https://github.com/teneplaysofficial/js-utils-kit/issues"
20
13
  },
14
+ "license": "MIT",
15
+ "author": {
16
+ "name": "Sriman",
17
+ "email": "136729116+TenEplaysOfficial@users.noreply.github.com",
18
+ "url": "https://tene.vercel.app"
19
+ },
20
+ "private": false,
21
21
  "files": [
22
22
  "dist"
23
23
  ],
24
- "engines": {
25
- "node": ">=22"
26
- },
27
24
  "type": "module",
28
25
  "main": "./dist/index.cjs",
29
26
  "module": "./dist/index.mjs",
30
- "types": "./dist/index.d.cts",
31
27
  "exports": {
32
28
  ".": {
33
- "require": "./dist/index.cjs",
34
- "import": "./dist/index.mjs"
29
+ "import": "./dist/index.mjs",
30
+ "require": "./dist/index.cjs"
35
31
  }
36
32
  },
33
+ "types": "./dist/index.d.cts",
34
+ "engines": {
35
+ "node": ">=22"
36
+ },
37
37
  "dependencies": {
38
38
  "archiver": "^7.0.1",
39
- "@js-utils-kit/types": "1.3.0"
39
+ "@js-utils-kit/types": "1.4.0"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "tsdown",