@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/cjs/functions/read_utils/src/read_utils.d.ts +1 -9
- package/dist/cjs/index.js +28 -39
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/functions/read_utils/src/read_utils.d.ts +1 -9
- package/dist/esm/index.js +29 -39
- package/dist/esm/index.js.map +1 -1
- package/dist/functions/read_utils/imports.d.ts +0 -3
- package/dist/functions/read_utils/src/read_utils.d.ts +1 -9
- package/dist/index.d.ts +2 -10
- package/package.json +1 -1
|
@@ -1,17 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
|
|
3
|
+
* In a browser this will reject immediately.
|
|
3
4
|
*/
|
|
4
5
|
export declare function readFileContents(relativeOrAbsolutePath: string): Promise<string>;
|
|
5
6
|
/**
|
|
6
7
|
* Reads a JSON file and returns the parsed object.
|
|
7
8
|
*/
|
|
8
9
|
export declare function readJsonFile<T = any>(relativeOrAbsolutePath: string): Promise<T>;
|
|
9
|
-
/**
|
|
10
|
-
* Read a file and optionally parse it as JSON (and even pluck a field).
|
|
11
|
-
*
|
|
12
|
-
* @param filePath Path to your file (relative to cwd or absolute)
|
|
13
|
-
* @param asJson If true, JSON.parse the file.
|
|
14
|
-
* If a string, JSON.parse then return parsed[string].
|
|
15
|
-
* @returns The raw string, whole parsed object, or a specific field.
|
|
16
|
-
*/
|
|
17
|
-
export declare function reader(filePath: string, asJson?: boolean | string): Promise<any>;
|
package/dist/cjs/index.js
CHANGED
|
@@ -2,8 +2,6 @@
|
|
|
2
2
|
|
|
3
3
|
var react = require('react');
|
|
4
4
|
var jsxRuntime = require('react/jsx-runtime');
|
|
5
|
-
var fs = require('fs');
|
|
6
|
-
var path = require('path');
|
|
7
5
|
|
|
8
6
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
9
7
|
/**
|
|
@@ -1307,17 +1305,41 @@ function getConfig(key) {
|
|
|
1307
1305
|
});
|
|
1308
1306
|
}
|
|
1309
1307
|
|
|
1308
|
+
// src/read_utils.ts
|
|
1309
|
+
/**
|
|
1310
|
+
* Attempt to load the Node-only modules.
|
|
1311
|
+
* Returns { fs: null, path: null } in the browser.
|
|
1312
|
+
*/
|
|
1313
|
+
function tryNodeModules() {
|
|
1314
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1315
|
+
try {
|
|
1316
|
+
const fsMod = yield import('fs');
|
|
1317
|
+
const pathMod = yield import('path');
|
|
1318
|
+
return {
|
|
1319
|
+
fs: fsMod.promises, // keep the `promises` API
|
|
1320
|
+
path: pathMod
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
catch (_a) {
|
|
1324
|
+
return { fs: null, path: null };
|
|
1325
|
+
}
|
|
1326
|
+
});
|
|
1327
|
+
}
|
|
1310
1328
|
/**
|
|
1311
1329
|
* Reads a file at `relativeOrAbsolutePath` and returns its contents as a string.
|
|
1330
|
+
* In a browser this will reject immediately.
|
|
1312
1331
|
*/
|
|
1313
1332
|
function readFileContents(relativeOrAbsolutePath) {
|
|
1314
1333
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1315
|
-
|
|
1334
|
+
const { fs, path } = yield tryNodeModules();
|
|
1335
|
+
if (!fs || !path) {
|
|
1336
|
+
throw new Error('readFileContents can only be used in Node.js');
|
|
1337
|
+
}
|
|
1338
|
+
// resolve absolute
|
|
1316
1339
|
const filePath = path.isAbsolute(relativeOrAbsolutePath)
|
|
1317
1340
|
? relativeOrAbsolutePath
|
|
1318
1341
|
: path.resolve(__dirname, relativeOrAbsolutePath);
|
|
1319
|
-
|
|
1320
|
-
return fs.promises.readFile(filePath, 'utf8');
|
|
1342
|
+
return fs.readFile(filePath, 'utf8');
|
|
1321
1343
|
});
|
|
1322
1344
|
}
|
|
1323
1345
|
/**
|
|
@@ -1325,42 +1347,10 @@ function readFileContents(relativeOrAbsolutePath) {
|
|
|
1325
1347
|
*/
|
|
1326
1348
|
function readJsonFile(relativeOrAbsolutePath) {
|
|
1327
1349
|
return __awaiter(this, void 0, void 0, function* () {
|
|
1328
|
-
const
|
|
1329
|
-
? relativeOrAbsolutePath
|
|
1330
|
-
: path.resolve(__dirname, relativeOrAbsolutePath);
|
|
1331
|
-
const text = yield fs.promises.readFile(filePath, 'utf8');
|
|
1350
|
+
const text = yield readFileContents(relativeOrAbsolutePath);
|
|
1332
1351
|
return JSON.parse(text);
|
|
1333
1352
|
});
|
|
1334
1353
|
}
|
|
1335
|
-
/**
|
|
1336
|
-
* Read a file and optionally parse it as JSON (and even pluck a field).
|
|
1337
|
-
*
|
|
1338
|
-
* @param filePath Path to your file (relative to cwd or absolute)
|
|
1339
|
-
* @param asJson If true, JSON.parse the file.
|
|
1340
|
-
* If a string, JSON.parse then return parsed[string].
|
|
1341
|
-
* @returns The raw string, whole parsed object, or a specific field.
|
|
1342
|
-
*/
|
|
1343
|
-
function reader(filePath_1) {
|
|
1344
|
-
return __awaiter(this, arguments, void 0, function* (filePath, asJson = false) {
|
|
1345
|
-
// resolve to absolute so you know exactly where you’re reading from
|
|
1346
|
-
const abs = path.isAbsolute(filePath)
|
|
1347
|
-
? filePath
|
|
1348
|
-
: path.resolve(process.cwd(), filePath);
|
|
1349
|
-
// 1) read file as text
|
|
1350
|
-
const text = yield fs.promises.readFile(abs, 'utf8');
|
|
1351
|
-
// 2) if they want JSON, parse it
|
|
1352
|
-
if (asJson) {
|
|
1353
|
-
const obj = JSON.parse(text);
|
|
1354
|
-
// 3) if they passed a string, return that property
|
|
1355
|
-
if (typeof asJson === 'string') {
|
|
1356
|
-
return obj[asJson];
|
|
1357
|
-
}
|
|
1358
|
-
return obj;
|
|
1359
|
-
}
|
|
1360
|
-
// 4) otherwise, just return the raw text
|
|
1361
|
-
return text;
|
|
1362
|
-
});
|
|
1363
|
-
}
|
|
1364
1354
|
|
|
1365
1355
|
Object.defineProperty(exports, "useCallback", {
|
|
1366
1356
|
enumerable: true,
|
|
@@ -1447,7 +1437,6 @@ exports.make_sanitized_path = make_sanitized_path;
|
|
|
1447
1437
|
exports.normalizeUrl = normalizeUrl;
|
|
1448
1438
|
exports.readFileContents = readFileContents;
|
|
1449
1439
|
exports.readJsonFile = readJsonFile;
|
|
1450
|
-
exports.reader = reader;
|
|
1451
1440
|
exports.requestPatch = requestPatch;
|
|
1452
1441
|
exports.requireToken = requireToken;
|
|
1453
1442
|
exports.sanitizeFilename = sanitizeFilename;
|