@js-utils-kit/fs 1.2.0 → 1.4.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(`url`);function f({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 p=typeof __filename<`u`;function m(e){if(!e)throw Error(`locateModuleFile requires import.meta.url (ESM) or __filename (CJS).`);return e.startsWith(`file:`)?(0,d.fileURLToPath)(e):e}function h(e){return l.default.dirname(m(e))}function g(e,t){let n=h(t),r=l.default.resolve(n,e);if(l.default.relative(n,r).startsWith(`..`))throw Error(`Resolved path escapes module directory.`);return r}function _(e){return e.replace(/\\/g,`/`)}function v(e){return e.replace(/\//g,`\\`)}function y(e){return e.replace(/[/\\]/g,l.default.sep)}exports.createArchive=f,exports.hasCommonJSFilename=p,exports.locateModuleDirectory=h,exports.locateModuleFile=m,exports.resolveModuleRelative=g,exports.toPlatformPath=y,exports.toPosixPath=_,exports.toWinPath=v;
package/dist/index.d.cts CHANGED
@@ -33,4 +33,180 @@ 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/locator.d.ts
58
+ /**
59
+ * Returns the absolute file path of a module.
60
+ *
61
+ * @remarks
62
+ * This utility supports both:
63
+ * - **ESM** → pass `import.meta.url`
64
+ * - **CommonJS** → pass `__filename`
65
+ *
66
+ * The path MUST be explicitly provided. Automatic runtime detection is intentionally not performed to avoid incorrect module resolution.
67
+ *
68
+ * @returns Absolute path to the provided module file.
69
+ *
70
+ * @throws {Error} If `metaUrlOrPath` is not provided.
71
+ *
72
+ * @example ESM usage
73
+ * ```ts
74
+ * const file = locateModuleFile(import.meta.url);
75
+ * console.log(file);
76
+ * ```
77
+ *
78
+ * @example CommonJS usage
79
+ * ```ts
80
+ * const file = locateModuleFile(__filename);
81
+ * console.log(file);
82
+ * ```
83
+ */
84
+ declare function locateModuleFile(/** `import.meta.url` (ESM) or `__filename` (CJS) */
85
+
86
+ metaUrlOrPath: string): string;
87
+ /**
88
+ * Returns the absolute directory path of a module.
89
+ *
90
+ * @remarks
91
+ * Internally derives the directory from {@link locateModuleFile}.
92
+ *
93
+ * @returns Absolute directory path.
94
+ *
95
+ * @example ESM
96
+ * ```ts
97
+ * const dir = locateModuleDirectory(import.meta.url);
98
+ * ```
99
+ *
100
+ * @example CommonJS
101
+ * ```ts
102
+ * const dir = locateModuleDirectory(__filename);
103
+ * ```
104
+ */
105
+ declare function locateModuleDirectory(/** `import.meta.url` (ESM) or `__filename` (CJS) */
106
+
107
+ metaUrlOrPath: string): string;
108
+ /**
109
+ * Resolves a path relative to the provided module's directory.
110
+ *
111
+ * @remarks
112
+ * Useful for loading internal assets such as:
113
+ * - `.hbs` templates
114
+ * - JSON files
115
+ * - SQL schemas
116
+ * - bundled static resources
117
+ *
118
+ * Unlike `process.cwd()`, this resolves relative to the module
119
+ * file location, making it safe for usage inside `node_modules`.
120
+ *
121
+ * @returns Absolute resolved path.
122
+ *
123
+ * @throws {Error}
124
+ * If module path is not provided.
125
+ *
126
+ * @example ESM
127
+ * ```ts
128
+ * const templatePath = resolveModuleRelative(
129
+ * "../templates/welcome.hbs",
130
+ * import.meta.url
131
+ * );
132
+ * ```
133
+ *
134
+ * @example CommonJS
135
+ * ```ts
136
+ * const templatePath = resolveModuleRelative(
137
+ * "../templates/welcome.hbs",
138
+ * __filename
139
+ * );
140
+ * ```
141
+ */
142
+ declare function resolveModuleRelative(/** Path relative to the module directory */relativePath: string, /** `import.meta.url` (ESM) or `__filename` (CJS) */metaUrlOrPath: string): string;
143
+ //#endregion
144
+ //#region src/path.d.ts
145
+ /**
146
+ * Converts a file system path to POSIX format.
147
+ *
148
+ * @remarks
149
+ * - Replaces all backslashes (`\`) with forward slashes (`/`).
150
+ * - Does NOT perform full path normalization (e.g., resolving `.` or `..` segments).
151
+ *
152
+ * @returns The path using POSIX separators (`/`).
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * toPosixPath('C:\\Users\\TenE\\project')
157
+ * // => 'C:/Users/TenE/project'
158
+ * ```
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * toPosixPath('src\\utils\\file.ts')
163
+ * // => 'src/utils/file.ts'
164
+ * ```
165
+ */
166
+ declare function toPosixPath(/** The path to convert */
167
+
168
+ p: string): string;
169
+ /**
170
+ * Converts a file system path to Windows (Win32) format.
171
+ *
172
+ * On POSIX systems (Linux/macOS), all forward slashes (`/`) are replaced with backslashes (`\`).
173
+ *
174
+ * @returns The path using Windows separators (`\`).
175
+ *
176
+ * @example
177
+ * ```ts
178
+ * toWinPath('src/utils/file.ts')
179
+ * // => 'src\\utils\\file.ts'
180
+ * ```
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * toWinPath('/usr/local/bin')
185
+ * // => '\\usr\\local\\bin'
186
+ * ```
187
+ */
188
+ declare function toWinPath(/** The path to convert */
189
+
190
+ p: string): string;
191
+ /**
192
+ * Converts a file system path to the current platform-specific format.
193
+ *
194
+ * This replaces both forward slashes (`/`) and backslashes (`\`) with the platform's separator (`path.sep`).
195
+ *
196
+ * - Windows → `\`
197
+ * - macOS/Linux → `/`
198
+ *
199
+ * @returns The path using the current OS separator.
200
+ *
201
+ * @example Mixed Separators
202
+ * ```ts
203
+ * toPlatformPath('src/utils/file.ts')
204
+ * // On Windows: 'src\\utils\\file.ts'
205
+ * // On POSIX: 'src/utils/file.ts'
206
+ * ```
207
+ */
208
+ declare function toPlatformPath(/** The path to convert */
209
+
210
+ p: string): string;
211
+ //#endregion
212
+ export { createArchive, hasCommonJSFilename, locateModuleDirectory, locateModuleFile, resolveModuleRelative, toPlatformPath, toPosixPath, toWinPath };
package/dist/index.d.mts CHANGED
@@ -33,4 +33,180 @@ 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/locator.d.ts
58
+ /**
59
+ * Returns the absolute file path of a module.
60
+ *
61
+ * @remarks
62
+ * This utility supports both:
63
+ * - **ESM** → pass `import.meta.url`
64
+ * - **CommonJS** → pass `__filename`
65
+ *
66
+ * The path MUST be explicitly provided. Automatic runtime detection is intentionally not performed to avoid incorrect module resolution.
67
+ *
68
+ * @returns Absolute path to the provided module file.
69
+ *
70
+ * @throws {Error} If `metaUrlOrPath` is not provided.
71
+ *
72
+ * @example ESM usage
73
+ * ```ts
74
+ * const file = locateModuleFile(import.meta.url);
75
+ * console.log(file);
76
+ * ```
77
+ *
78
+ * @example CommonJS usage
79
+ * ```ts
80
+ * const file = locateModuleFile(__filename);
81
+ * console.log(file);
82
+ * ```
83
+ */
84
+ declare function locateModuleFile(/** `import.meta.url` (ESM) or `__filename` (CJS) */
85
+
86
+ metaUrlOrPath: string): string;
87
+ /**
88
+ * Returns the absolute directory path of a module.
89
+ *
90
+ * @remarks
91
+ * Internally derives the directory from {@link locateModuleFile}.
92
+ *
93
+ * @returns Absolute directory path.
94
+ *
95
+ * @example ESM
96
+ * ```ts
97
+ * const dir = locateModuleDirectory(import.meta.url);
98
+ * ```
99
+ *
100
+ * @example CommonJS
101
+ * ```ts
102
+ * const dir = locateModuleDirectory(__filename);
103
+ * ```
104
+ */
105
+ declare function locateModuleDirectory(/** `import.meta.url` (ESM) or `__filename` (CJS) */
106
+
107
+ metaUrlOrPath: string): string;
108
+ /**
109
+ * Resolves a path relative to the provided module's directory.
110
+ *
111
+ * @remarks
112
+ * Useful for loading internal assets such as:
113
+ * - `.hbs` templates
114
+ * - JSON files
115
+ * - SQL schemas
116
+ * - bundled static resources
117
+ *
118
+ * Unlike `process.cwd()`, this resolves relative to the module
119
+ * file location, making it safe for usage inside `node_modules`.
120
+ *
121
+ * @returns Absolute resolved path.
122
+ *
123
+ * @throws {Error}
124
+ * If module path is not provided.
125
+ *
126
+ * @example ESM
127
+ * ```ts
128
+ * const templatePath = resolveModuleRelative(
129
+ * "../templates/welcome.hbs",
130
+ * import.meta.url
131
+ * );
132
+ * ```
133
+ *
134
+ * @example CommonJS
135
+ * ```ts
136
+ * const templatePath = resolveModuleRelative(
137
+ * "../templates/welcome.hbs",
138
+ * __filename
139
+ * );
140
+ * ```
141
+ */
142
+ declare function resolveModuleRelative(/** Path relative to the module directory */relativePath: string, /** `import.meta.url` (ESM) or `__filename` (CJS) */metaUrlOrPath: string): string;
143
+ //#endregion
144
+ //#region src/path.d.ts
145
+ /**
146
+ * Converts a file system path to POSIX format.
147
+ *
148
+ * @remarks
149
+ * - Replaces all backslashes (`\`) with forward slashes (`/`).
150
+ * - Does NOT perform full path normalization (e.g., resolving `.` or `..` segments).
151
+ *
152
+ * @returns The path using POSIX separators (`/`).
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * toPosixPath('C:\\Users\\TenE\\project')
157
+ * // => 'C:/Users/TenE/project'
158
+ * ```
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * toPosixPath('src\\utils\\file.ts')
163
+ * // => 'src/utils/file.ts'
164
+ * ```
165
+ */
166
+ declare function toPosixPath(/** The path to convert */
167
+
168
+ p: string): string;
169
+ /**
170
+ * Converts a file system path to Windows (Win32) format.
171
+ *
172
+ * On POSIX systems (Linux/macOS), all forward slashes (`/`) are replaced with backslashes (`\`).
173
+ *
174
+ * @returns The path using Windows separators (`\`).
175
+ *
176
+ * @example
177
+ * ```ts
178
+ * toWinPath('src/utils/file.ts')
179
+ * // => 'src\\utils\\file.ts'
180
+ * ```
181
+ *
182
+ * @example
183
+ * ```ts
184
+ * toWinPath('/usr/local/bin')
185
+ * // => '\\usr\\local\\bin'
186
+ * ```
187
+ */
188
+ declare function toWinPath(/** The path to convert */
189
+
190
+ p: string): string;
191
+ /**
192
+ * Converts a file system path to the current platform-specific format.
193
+ *
194
+ * This replaces both forward slashes (`/`) and backslashes (`\`) with the platform's separator (`path.sep`).
195
+ *
196
+ * - Windows → `\`
197
+ * - macOS/Linux → `/`
198
+ *
199
+ * @returns The path using the current OS separator.
200
+ *
201
+ * @example Mixed Separators
202
+ * ```ts
203
+ * toPlatformPath('src/utils/file.ts')
204
+ * // On Windows: 'src\\utils\\file.ts'
205
+ * // On POSIX: 'src/utils/file.ts'
206
+ * ```
207
+ */
208
+ declare function toPlatformPath(/** The path to convert */
209
+
210
+ p: string): string;
211
+ //#endregion
212
+ export { createArchive, 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{fileURLToPath as r}from"url";function i({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 a=typeof __filename<`u`;function o(e){if(!e)throw Error(`locateModuleFile requires import.meta.url (ESM) or __filename (CJS).`);return e.startsWith(`file:`)?r(e):e}function s(e){return t.dirname(o(e))}function c(e,n){let r=s(n),i=t.resolve(r,e);if(t.relative(r,i).startsWith(`..`))throw Error(`Resolved path escapes module directory.`);return i}function l(e){return e.replace(/\\/g,`/`)}function u(e){return e.replace(/\//g,`\\`)}function d(e){return e.replace(/[/\\]/g,t.sep)}export{i as createArchive,a as hasCommonJSFilename,s as locateModuleDirectory,o as locateModuleFile,c as resolveModuleRelative,d as toPlatformPath,l as toPosixPath,u as toWinPath};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@js-utils-kit/fs",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "File system utilities",
5
5
  "private": false,
6
6
  "license": "MIT",
@@ -30,13 +30,13 @@
30
30
  "types": "./dist/index.d.cts",
31
31
  "exports": {
32
32
  ".": {
33
- "require": "./dist/index.cjs",
34
- "import": "./dist/index.mjs"
33
+ "import": "./dist/index.mjs",
34
+ "require": "./dist/index.cjs"
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
38
  "archiver": "^7.0.1",
39
- "@js-utils-kit/types": "1.2.0"
39
+ "@js-utils-kit/types": "1.3.0"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "tsdown",