@stonyx/utils 0.2.3-beta.15 → 0.2.3-beta.16

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/file.d.ts CHANGED
@@ -2,7 +2,7 @@ interface FileOptions {
2
2
  json?: boolean;
3
3
  }
4
4
  interface ReadFileOptions extends FileOptions {
5
- missingFileCallback?: (filePath: string) => unknown;
5
+ missingFileCallback?: (filePath: string) => string | Record<string, unknown>;
6
6
  }
7
7
  interface DeleteFileOptions {
8
8
  ignoreAccessFailure?: boolean;
@@ -20,16 +20,19 @@ interface FileImportMeta {
20
20
  stats: import('fs').Stats;
21
21
  path: string;
22
22
  }
23
- export declare function createFile(filePath: string, data: unknown, options?: FileOptions): Promise<void>;
24
- export declare function updateFile(filePath: string, data: unknown, options?: FileOptions): Promise<void>;
23
+ export declare function createFile(filePath: string, data: string | Record<string, unknown>, options?: FileOptions): Promise<void>;
24
+ export declare function updateFile(filePath: string, data: string | Record<string, unknown>, options?: FileOptions): Promise<void>;
25
25
  interface CopyFileOptions {
26
26
  overwrite?: boolean;
27
27
  }
28
28
  export declare function copyFile(sourcePath: string, targetPath: string, options?: CopyFileOptions): Promise<boolean>;
29
- export declare function readFile(filePath: string, options?: ReadFileOptions): Promise<unknown>;
29
+ export declare function readFile(filePath: string, options: ReadFileOptions & {
30
+ json: true;
31
+ }): Promise<Record<string, unknown>>;
32
+ export declare function readFile(filePath: string, options?: ReadFileOptions): Promise<string>;
30
33
  export declare function deleteFile(filePath: string, options?: DeleteFileOptions): Promise<void>;
31
34
  export declare function deleteDirectory(dir: string): Promise<void>;
32
35
  export declare function createDirectory(dir: string): Promise<void>;
33
- export declare function forEachFileImport(dir: string, callback: (output: unknown, meta: FileImportMeta) => void, options?: ForEachFileImportOptions): Promise<void>;
36
+ export declare function forEachFileImport(dir: string, callback: (output: unknown, meta: FileImportMeta) => void | Promise<void>, options?: ForEachFileImportOptions): Promise<void>;
34
37
  export declare function fileExists(filePath: string): Promise<boolean>;
35
38
  export {};
package/dist/file.js CHANGED
@@ -3,11 +3,14 @@ import { kebabCaseToCamelCase } from './string.js';
3
3
  import { objToJson } from './object.js';
4
4
  import { promises as fsp } from 'fs';
5
5
  import path from 'path';
6
+ function isNodeError(error) {
7
+ return error instanceof Error && 'code' in error;
8
+ }
6
9
  export async function createFile(filePath, data, options = {}) {
7
10
  try {
8
11
  filePath = path.resolve(filePath);
9
12
  await createDirectory(path.dirname(filePath));
10
- await fsp.writeFile(filePath, options.json ? objToJson(data) : data, 'utf8');
13
+ await fsp.writeFile(filePath, options.json ? objToJson(data) : String(data), 'utf8');
11
14
  }
12
15
  catch (error) {
13
16
  throw new Error(String(error));
@@ -17,7 +20,7 @@ export async function updateFile(filePath, data, options = {}) {
17
20
  try {
18
21
  await fsp.access(filePath);
19
22
  const swapFile = `${filePath}.temp-${getTimestamp()}`;
20
- await fsp.writeFile(swapFile, options.json ? objToJson(data) : data);
23
+ await fsp.writeFile(swapFile, options.json ? objToJson(data) : String(data));
21
24
  await fsp.rename(swapFile, filePath);
22
25
  }
23
26
  catch (error) {
@@ -56,7 +59,7 @@ export async function readFile(filePath, options = {}) {
56
59
  }
57
60
  catch (error) {
58
61
  const { missingFileCallback } = options;
59
- if (error.code === 'ENOENT' && missingFileCallback) {
62
+ if (isNodeError(error) && error.code === 'ENOENT' && missingFileCallback) {
60
63
  return missingFileCallback(filePath);
61
64
  }
62
65
  throw new Error(String(error));
@@ -122,7 +125,7 @@ export async function fileExists(filePath) {
122
125
  await fsp.access(filePath);
123
126
  return true;
124
127
  }
125
- catch (error) {
128
+ catch {
126
129
  return false;
127
130
  }
128
131
  }
package/dist/object.d.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  export declare function deepCopy<T>(obj: T): T;
2
- export declare function objToJson(obj: unknown, format?: string | number): string;
2
+ type JsonValue = string | number | boolean | null | JsonValue[] | {
3
+ [key: string]: JsonValue;
4
+ };
5
+ export declare function objToJson(obj: JsonValue | Record<string, unknown>, format?: string | number): string;
3
6
  export declare function makeArray<T>(obj: T | T[]): T[];
4
7
  interface MergeOptions {
5
8
  ignoreNewKeys?: boolean;
6
9
  }
7
10
  export declare function mergeObject(obj1: Record<string, unknown>, obj2: Record<string, unknown>, options?: MergeOptions): Record<string, unknown>;
8
11
  export declare function get(obj: Record<string, unknown>, path: string): unknown;
9
- export declare function get(obj: unknown, path?: unknown): void;
12
+ export declare function get(obj: unknown, path?: unknown): undefined;
10
13
  export declare function getOrSet<K, V>(map: Map<K, V>, key: K, defaultValue: V | (() => V)): V;
11
14
  export {};
package/dist/object.js CHANGED
@@ -52,7 +52,9 @@ export function get(obj, path) {
52
52
  export function getOrSet(map, key, defaultValue) {
53
53
  if (!(map instanceof Map))
54
54
  throw new Error('First argument to getOrSet must be a Map.');
55
- if (!map.has(key))
56
- map.set(key, typeof defaultValue === "function" ? defaultValue() : defaultValue);
55
+ if (!map.has(key)) {
56
+ const value = typeof defaultValue === 'function' ? defaultValue() : defaultValue;
57
+ map.set(key, value);
58
+ }
57
59
  return map.get(key);
58
60
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "keywords": [
4
4
  "stonyx-module"
5
5
  ],
6
- "version": "0.2.3-beta.15",
6
+ "version": "0.2.3-beta.16",
7
7
  "description": "Utils module for Stonyx Framework",
8
8
  "repository": {
9
9
  "type": "git",