@putkoff/abstract-utilities 0.1.115 → 0.1.116

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/esm/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  export { useCallback, useEffect, useRef, useState } from 'react';
2
2
  import { jsx, jsxs } from 'react/jsx-runtime';
3
- import { promises } from 'fs';
4
- import path from 'path';
5
3
 
6
4
  /**
7
5
  ***Changes**:
@@ -1304,17 +1302,41 @@ function getConfig(key) {
1304
1302
  });
1305
1303
  }
1306
1304
 
1305
+ // src/read_utils.ts
1306
+ /**
1307
+ * Attempt to load the Node-only modules.
1308
+ * Returns { fs: null, path: null } in the browser.
1309
+ */
1310
+ function tryNodeModules() {
1311
+ return __awaiter(this, void 0, void 0, function* () {
1312
+ try {
1313
+ const fsMod = yield import('fs');
1314
+ const pathMod = yield import('path');
1315
+ return {
1316
+ fs: fsMod.promises, // keep the `promises` API
1317
+ path: pathMod
1318
+ };
1319
+ }
1320
+ catch (_a) {
1321
+ return { fs: null, path: null };
1322
+ }
1323
+ });
1324
+ }
1307
1325
  /**
1308
1326
  * Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
1327
+ * In a browser this will reject immediately.
1309
1328
  */
1310
1329
  function readFileContents(relativeOrAbsolutePath) {
1311
1330
  return __awaiter(this, void 0, void 0, function* () {
1312
- // Resolve to an absolute path (so you’re always sure where it’s looking)
1331
+ const { fs, path } = yield tryNodeModules();
1332
+ if (!fs || !path) {
1333
+ throw new Error('readFileContents can only be used in Node.js');
1334
+ }
1335
+ // resolve absolute
1313
1336
  const filePath = path.isAbsolute(relativeOrAbsolutePath)
1314
1337
  ? relativeOrAbsolutePath
1315
1338
  : path.resolve(__dirname, relativeOrAbsolutePath);
1316
- // Read and return as UTF-8 text
1317
- return promises.readFile(filePath, 'utf8');
1339
+ return fs.readFile(filePath, 'utf8');
1318
1340
  });
1319
1341
  }
1320
1342
  /**
@@ -1322,42 +1344,10 @@ function readFileContents(relativeOrAbsolutePath) {
1322
1344
  */
1323
1345
  function readJsonFile(relativeOrAbsolutePath) {
1324
1346
  return __awaiter(this, void 0, void 0, function* () {
1325
- const filePath = path.isAbsolute(relativeOrAbsolutePath)
1326
- ? relativeOrAbsolutePath
1327
- : path.resolve(__dirname, relativeOrAbsolutePath);
1328
- const text = yield promises.readFile(filePath, 'utf8');
1347
+ const text = yield readFileContents(relativeOrAbsolutePath);
1329
1348
  return JSON.parse(text);
1330
1349
  });
1331
1350
  }
1332
- /**
1333
- * Read a file and optionally parse it as JSON (and even pluck a field).
1334
- *
1335
- * @param filePath Path to your file (relative to cwd or absolute)
1336
- * @param asJson If true, JSON.parse the file.
1337
- * If a string, JSON.parse then return parsed[string].
1338
- * @returns The raw string, whole parsed object, or a specific field.
1339
- */
1340
- function reader(filePath_1) {
1341
- return __awaiter(this, arguments, void 0, function* (filePath, asJson = false) {
1342
- // resolve to absolute so you know exactly where you’re reading from
1343
- const abs = path.isAbsolute(filePath)
1344
- ? filePath
1345
- : path.resolve(process.cwd(), filePath);
1346
- // 1) read file as text
1347
- const text = yield promises.readFile(abs, 'utf8');
1348
- // 2) if they want JSON, parse it
1349
- if (asJson) {
1350
- const obj = JSON.parse(text);
1351
- // 3) if they passed a string, return that property
1352
- if (typeof asJson === 'string') {
1353
- return obj[asJson];
1354
- }
1355
- return obj;
1356
- }
1357
- // 4) otherwise, just return the raw text
1358
- return text;
1359
- });
1360
- }
1361
1351
 
1362
- export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensure_list, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getEnvDir, getEnvPath, getFunctionsDir, getFunctionsUtilsDirectory, getHooksUtilsDirectory, getLibUtilsDirectory, getPublicDir, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, make_path, make_sanitized_path, normalizeUrl, readFileContents, readJsonFile, reader, requestPatch, requireToken, sanitizeFilename, secureFetchIt, stripPrefixes, truncateString };
1352
+ export { API_PREFIX, BASE_URL, Button, Checkbox, DEV_PREFIX, DOMAIN_NAME, Input, PROD_PREFIX, PROTOCOL, SUB_DIR, Spinner, alertit, currentUsername, currentUsernames, decodeJwt, eatAll, eatEnd, eatInner, eatOuter, ensure_list, fetchIt, fetchSharePatch, geAuthsUtilsDirectory, geBackupsUtilsDirectory, geConstantsUtilsDirectory, geEnvUtilsDirectory, geFetchUtilsDirectory, geFileUtilsDirectory, gePathUtilsDirectory, geStaticDirectory, geStringUtilsDirectory, geTypeUtilsDirectory, getAbsDir, getAbsPath, getAuthorizationHeader, getBaseDir, getComponentsUtilsDirectory, getConfig, getDbConfigsPath, getDistDir, getEnvDir, getEnvPath, getFunctionsDir, getFunctionsUtilsDirectory, getHooksUtilsDirectory, getLibUtilsDirectory, getPublicDir, getSchemasDirPath, getSchemasPath, getSrcDir, getSubstring, getToken, get_app_config_url, get_basename, get_dirname, get_extname, get_filename, get_splitext, get_window, get_window_location, get_window_parts, get_window_pathname, isLoggedIn, isTokenExpired, make_path, make_sanitized_path, normalizeUrl, readFileContents, readJsonFile, requestPatch, requireToken, sanitizeFilename, secureFetchIt, stripPrefixes, truncateString };
1363
1353
  //# sourceMappingURL=index.js.map