@faasjs/load 6.0.0-beta.0 → 6.0.0-beta.1
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/README.md +6 -0
- package/dist/index.cjs +54 -6
- package/dist/index.d.ts +30 -1
- package/dist/index.mjs +50 -5
- package/package.json +7 -5
package/README.md
CHANGED
|
@@ -13,4 +13,10 @@ npm install @faasjs/load
|
|
|
13
13
|
|
|
14
14
|
## Functions
|
|
15
15
|
|
|
16
|
+
- [detectNodeRuntime](functions/detectNodeRuntime.md)
|
|
16
17
|
- [loadConfig](functions/loadConfig.md)
|
|
18
|
+
- [loadPackage](functions/loadPackage.md)
|
|
19
|
+
|
|
20
|
+
## Type Aliases
|
|
21
|
+
|
|
22
|
+
- [NodeRuntime](type-aliases/NodeRuntime.md)
|
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
var fs = require('fs');
|
|
4
4
|
var path = require('path');
|
|
5
5
|
var deep_merge = require('@faasjs/deep_merge');
|
|
6
|
-
var logger = require('@faasjs/logger');
|
|
6
|
+
var logger$1 = require('@faasjs/logger');
|
|
7
7
|
var jsYaml = require('js-yaml');
|
|
8
8
|
|
|
9
|
+
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
9
10
|
// src/load_config.ts
|
|
10
11
|
var Config = class {
|
|
11
12
|
root;
|
|
@@ -13,9 +14,9 @@ var Config = class {
|
|
|
13
14
|
origin;
|
|
14
15
|
defaults;
|
|
15
16
|
logger;
|
|
16
|
-
constructor(root, filename,
|
|
17
|
-
this.logger = new logger.Logger(
|
|
18
|
-
|
|
17
|
+
constructor(root, filename, logger2) {
|
|
18
|
+
this.logger = new logger$1.Logger(
|
|
19
|
+
logger2?.label ? `${logger2.label}] [config` : "config"
|
|
19
20
|
);
|
|
20
21
|
this.root = root;
|
|
21
22
|
if (!this.root.endsWith(path.sep)) this.root += path.sep;
|
|
@@ -52,8 +53,55 @@ var Config = class {
|
|
|
52
53
|
return this[key] || this.defaults || /* @__PURE__ */ Object.create(null);
|
|
53
54
|
}
|
|
54
55
|
};
|
|
55
|
-
function loadConfig(root, filename, staging,
|
|
56
|
-
return new Config(root, filename,
|
|
56
|
+
function loadConfig(root, filename, staging, logger2) {
|
|
57
|
+
return new Config(root, filename, logger2).get(staging);
|
|
58
|
+
}
|
|
59
|
+
var _runtime = null;
|
|
60
|
+
function detectNodeRuntime() {
|
|
61
|
+
if (_runtime) return _runtime;
|
|
62
|
+
if (typeof globalThis.require === "function" && typeof module !== "undefined")
|
|
63
|
+
return _runtime = "commonjs";
|
|
64
|
+
if (typeof ({ url: (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('index.cjs', document.baseURI).href)) }) !== "undefined") return _runtime = "module";
|
|
65
|
+
throw Error("Unknown runtime");
|
|
66
|
+
}
|
|
67
|
+
var tsxLoaded = null;
|
|
68
|
+
var logger = new logger$1.Logger("loadPackage");
|
|
69
|
+
async function loadPackage(name) {
|
|
70
|
+
const runtime = detectNodeRuntime();
|
|
71
|
+
let module2;
|
|
72
|
+
if (runtime === "module") {
|
|
73
|
+
if (tsxLoaded === null) {
|
|
74
|
+
try {
|
|
75
|
+
await import('tsx');
|
|
76
|
+
tsxLoaded = true;
|
|
77
|
+
} catch (_) {
|
|
78
|
+
tsxLoaded = false;
|
|
79
|
+
logger.warn(
|
|
80
|
+
'Recommend installing the "tsx" package for loading ts/tsx files'
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
module2 = await import(name);
|
|
85
|
+
return module2.default ? module2.default : module2;
|
|
86
|
+
}
|
|
87
|
+
if (runtime === "commonjs") {
|
|
88
|
+
if (tsxLoaded === null) {
|
|
89
|
+
try {
|
|
90
|
+
globalThis.require("tsx");
|
|
91
|
+
tsxLoaded = true;
|
|
92
|
+
} catch (_) {
|
|
93
|
+
tsxLoaded = false;
|
|
94
|
+
logger.warn(
|
|
95
|
+
'Recommend installing the "tsx" package for loading ts/tsx files'
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
module2 = globalThis.require(name);
|
|
100
|
+
return module2.default ? module2.default : module2;
|
|
101
|
+
}
|
|
102
|
+
throw Error("Unknown runtime");
|
|
57
103
|
}
|
|
58
104
|
|
|
105
|
+
exports.detectNodeRuntime = detectNodeRuntime;
|
|
59
106
|
exports.loadConfig = loadConfig;
|
|
107
|
+
exports.loadPackage = loadPackage;
|
package/dist/index.d.ts
CHANGED
|
@@ -6,4 +6,33 @@ import { Logger } from '@faasjs/logger';
|
|
|
6
6
|
*/
|
|
7
7
|
declare function loadConfig(root: string, filename: string, staging: string, logger?: Logger): Config;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
type NodeRuntime = 'commonjs' | 'module';
|
|
10
|
+
/**
|
|
11
|
+
* Detect the current JavaScript runtime environment.
|
|
12
|
+
*
|
|
13
|
+
* This function checks for the presence of `import.meta` and `require` to determine
|
|
14
|
+
* whether the runtime is using ECMAScript modules (ESM) or CommonJS modules (CJS).
|
|
15
|
+
*
|
|
16
|
+
* @returns {NodeRuntime} - Returns 'module' if the runtime is using ECMAScript modules,
|
|
17
|
+
* and 'cjs' if the runtime is using CommonJS modules.
|
|
18
|
+
* @throws {Error} - Throws an error if the runtime cannot be determined.
|
|
19
|
+
*/
|
|
20
|
+
declare function detectNodeRuntime(): NodeRuntime;
|
|
21
|
+
/**
|
|
22
|
+
* Asynchronously loads a package by its name, supporting both ESM and CommonJS runtimes.
|
|
23
|
+
*
|
|
24
|
+
* Also supports loading TypeScript and TSX files by checking for the presence of the "tsx" package.
|
|
25
|
+
*
|
|
26
|
+
* @template T - The type of the module to be loaded.
|
|
27
|
+
* @param {string} name - The name of the package to load.
|
|
28
|
+
* @returns {Promise<T>} A promise that resolves to the loaded module.
|
|
29
|
+
* @throws {Error} If the runtime is unknown.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const myModule = await loadPackage<MyModuleType>('my-module');
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function loadPackage<T = unknown>(name: string): Promise<T>;
|
|
37
|
+
|
|
38
|
+
export { type NodeRuntime, detectNodeRuntime, loadConfig, loadPackage };
|
package/dist/index.mjs
CHANGED
|
@@ -11,9 +11,9 @@ var Config = class {
|
|
|
11
11
|
origin;
|
|
12
12
|
defaults;
|
|
13
13
|
logger;
|
|
14
|
-
constructor(root, filename,
|
|
14
|
+
constructor(root, filename, logger2) {
|
|
15
15
|
this.logger = new Logger(
|
|
16
|
-
|
|
16
|
+
logger2?.label ? `${logger2.label}] [config` : "config"
|
|
17
17
|
);
|
|
18
18
|
this.root = root;
|
|
19
19
|
if (!this.root.endsWith(sep)) this.root += sep;
|
|
@@ -50,8 +50,53 @@ var Config = class {
|
|
|
50
50
|
return this[key] || this.defaults || /* @__PURE__ */ Object.create(null);
|
|
51
51
|
}
|
|
52
52
|
};
|
|
53
|
-
function loadConfig(root, filename, staging,
|
|
54
|
-
return new Config(root, filename,
|
|
53
|
+
function loadConfig(root, filename, staging, logger2) {
|
|
54
|
+
return new Config(root, filename, logger2).get(staging);
|
|
55
|
+
}
|
|
56
|
+
var _runtime = null;
|
|
57
|
+
function detectNodeRuntime() {
|
|
58
|
+
if (_runtime) return _runtime;
|
|
59
|
+
if (typeof globalThis.require === "function" && typeof module !== "undefined")
|
|
60
|
+
return _runtime = "commonjs";
|
|
61
|
+
if (typeof import.meta !== "undefined") return _runtime = "module";
|
|
62
|
+
throw Error("Unknown runtime");
|
|
63
|
+
}
|
|
64
|
+
var tsxLoaded = null;
|
|
65
|
+
var logger = new Logger("loadPackage");
|
|
66
|
+
async function loadPackage(name) {
|
|
67
|
+
const runtime = detectNodeRuntime();
|
|
68
|
+
let module2;
|
|
69
|
+
if (runtime === "module") {
|
|
70
|
+
if (tsxLoaded === null) {
|
|
71
|
+
try {
|
|
72
|
+
await import('tsx');
|
|
73
|
+
tsxLoaded = true;
|
|
74
|
+
} catch (_) {
|
|
75
|
+
tsxLoaded = false;
|
|
76
|
+
logger.warn(
|
|
77
|
+
'Recommend installing the "tsx" package for loading ts/tsx files'
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
module2 = await import(name);
|
|
82
|
+
return module2.default ? module2.default : module2;
|
|
83
|
+
}
|
|
84
|
+
if (runtime === "commonjs") {
|
|
85
|
+
if (tsxLoaded === null) {
|
|
86
|
+
try {
|
|
87
|
+
globalThis.require("tsx");
|
|
88
|
+
tsxLoaded = true;
|
|
89
|
+
} catch (_) {
|
|
90
|
+
tsxLoaded = false;
|
|
91
|
+
logger.warn(
|
|
92
|
+
'Recommend installing the "tsx" package for loading ts/tsx files'
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
module2 = globalThis.require(name);
|
|
97
|
+
return module2.default ? module2.default : module2;
|
|
98
|
+
}
|
|
99
|
+
throw Error("Unknown runtime");
|
|
55
100
|
}
|
|
56
101
|
|
|
57
|
-
export { loadConfig };
|
|
102
|
+
export { detectNodeRuntime, loadConfig, loadPackage };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/load",
|
|
3
|
-
"version": "6.0.0-beta.
|
|
3
|
+
"version": "6.0.0-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -31,14 +31,16 @@
|
|
|
31
31
|
],
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"js-yaml": "*",
|
|
34
|
-
"
|
|
35
|
-
"@faasjs/
|
|
34
|
+
"tsx": "*",
|
|
35
|
+
"@faasjs/deep_merge": "6.0.0-beta.1",
|
|
36
|
+
"@faasjs/func": "6.0.0-beta.1"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"js-yaml": "*",
|
|
40
|
+
"tsx": "*",
|
|
39
41
|
"@types/js-yaml": "*",
|
|
40
|
-
"@faasjs/deep_merge": "6.0.0-beta.
|
|
41
|
-
"@faasjs/func": "6.0.0-beta.
|
|
42
|
+
"@faasjs/deep_merge": "6.0.0-beta.1",
|
|
43
|
+
"@faasjs/func": "6.0.0-beta.1"
|
|
42
44
|
},
|
|
43
45
|
"engines": {
|
|
44
46
|
"node": ">=22.0.0",
|