@intlayer/config 6.1.6 → 7.0.0-canary.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/cjs/_virtual/rolldown_runtime.cjs +34 -0
- package/dist/cjs/alias.cjs +27 -100
- package/dist/cjs/alias.cjs.map +1 -1
- package/dist/cjs/built.cjs +7 -25
- package/dist/cjs/built.cjs.map +1 -1
- package/dist/cjs/bundleJSFile.cjs +30 -51
- package/dist/cjs/bundleJSFile.cjs.map +1 -1
- package/dist/cjs/client.cjs +19 -52
- package/dist/cjs/configFile/buildConfigurationFields.cjs +100 -657
- package/dist/cjs/configFile/buildConfigurationFields.cjs.map +1 -1
- package/dist/cjs/configFile/getConfiguration.cjs +60 -114
- package/dist/cjs/configFile/getConfiguration.cjs.map +1 -1
- package/dist/cjs/configFile/index.cjs +8 -25
- package/dist/cjs/configFile/loadConfigurationFile.cjs +22 -47
- package/dist/cjs/configFile/loadConfigurationFile.cjs.map +1 -1
- package/dist/cjs/configFile/searchConfigurationFile.cjs +49 -55
- package/dist/cjs/configFile/searchConfigurationFile.cjs.map +1 -1
- package/dist/cjs/defaultValues/build.cjs +23 -35
- package/dist/cjs/defaultValues/build.cjs.map +1 -1
- package/dist/cjs/defaultValues/content.cjs +68 -70
- package/dist/cjs/defaultValues/content.cjs.map +1 -1
- package/dist/cjs/defaultValues/editor.cjs +29 -41
- package/dist/cjs/defaultValues/editor.cjs.map +1 -1
- package/dist/cjs/defaultValues/index.cjs +62 -0
- package/dist/cjs/defaultValues/index.cjs.map +1 -0
- package/dist/cjs/defaultValues/internationalization.cjs +23 -34
- package/dist/cjs/defaultValues/internationalization.cjs.map +1 -1
- package/dist/cjs/defaultValues/log.cjs +17 -29
- package/dist/cjs/defaultValues/log.cjs.map +1 -1
- package/dist/cjs/defaultValues/routing.cjs +38 -0
- package/dist/cjs/defaultValues/routing.cjs.map +1 -0
- package/dist/cjs/extractErrorMessage.cjs +95 -0
- package/dist/cjs/extractErrorMessage.cjs.map +1 -0
- package/dist/cjs/index.cjs +67 -98
- package/dist/cjs/loadEnvFile.cjs +30 -51
- package/dist/cjs/loadEnvFile.cjs.map +1 -1
- package/dist/cjs/loadExternalFile/bundleFile.cjs +74 -0
- package/dist/cjs/loadExternalFile/bundleFile.cjs.map +1 -0
- package/dist/cjs/loadExternalFile/loadExternalFile.cjs +86 -0
- package/dist/cjs/loadExternalFile/loadExternalFile.cjs.map +1 -0
- package/dist/cjs/loadExternalFile/parseFileContent.cjs +67 -0
- package/dist/cjs/loadExternalFile/parseFileContent.cjs.map +1 -0
- package/dist/cjs/loadExternalFile/transpileTSToMJS.cjs +60 -0
- package/dist/cjs/loadExternalFile/transpileTSToMJS.cjs.map +1 -0
- package/dist/cjs/logger.cjs +124 -152
- package/dist/cjs/logger.cjs.map +1 -1
- package/dist/cjs/package.cjs +12 -0
- package/dist/cjs/package.cjs.map +1 -0
- package/dist/cjs/retryManager.cjs +42 -49
- package/dist/cjs/retryManager.cjs.map +1 -1
- package/dist/cjs/utils/ESMxCJSHelpers.cjs +24 -33
- package/dist/cjs/utils/ESMxCJSHelpers.cjs.map +1 -1
- package/dist/cjs/utils/cache.cjs +426 -0
- package/dist/cjs/utils/cache.cjs.map +1 -0
- package/dist/cjs/utils/clearModuleCache.cjs +23 -46
- package/dist/cjs/utils/clearModuleCache.cjs.map +1 -1
- package/dist/cjs/utils/extractErrorMessage.cjs +95 -0
- package/dist/cjs/utils/extractErrorMessage.cjs.map +1 -0
- package/dist/cjs/utils/getExtension.cjs +12 -35
- package/dist/cjs/utils/getExtension.cjs.map +1 -1
- package/dist/cjs/utils/getPackageJsonPath.cjs +37 -0
- package/dist/cjs/utils/getPackageJsonPath.cjs.map +1 -0
- package/dist/cjs/utils/logStack.cjs +16 -0
- package/dist/cjs/utils/logStack.cjs.map +1 -0
- package/dist/cjs/utils/normalizePath.cjs +15 -27
- package/dist/cjs/utils/normalizePath.cjs.map +1 -1
- package/dist/esm/_virtual/rolldown_runtime.mjs +17 -0
- package/dist/esm/alias.mjs +23 -74
- package/dist/esm/alias.mjs.map +1 -1
- package/dist/esm/built.mjs +5 -3
- package/dist/esm/built.mjs.map +1 -1
- package/dist/esm/bundleJSFile.mjs +26 -25
- package/dist/esm/bundleJSFile.mjs.map +1 -1
- package/dist/esm/client.mjs +4 -31
- package/dist/esm/configFile/buildConfigurationFields.mjs +97 -674
- package/dist/esm/configFile/buildConfigurationFields.mjs.map +1 -1
- package/dist/esm/configFile/getConfiguration.mjs +52 -75
- package/dist/esm/configFile/getConfiguration.mjs.map +1 -1
- package/dist/esm/configFile/index.mjs +5 -3
- package/dist/esm/configFile/loadConfigurationFile.mjs +22 -23
- package/dist/esm/configFile/loadConfigurationFile.mjs.map +1 -1
- package/dist/esm/configFile/searchConfigurationFile.mjs +45 -30
- package/dist/esm/configFile/searchConfigurationFile.mjs.map +1 -1
- package/dist/esm/defaultValues/build.mjs +16 -11
- package/dist/esm/defaultValues/build.mjs.map +1 -1
- package/dist/esm/defaultValues/content.mjs +48 -34
- package/dist/esm/defaultValues/content.mjs.map +1 -1
- package/dist/esm/defaultValues/editor.mjs +17 -11
- package/dist/esm/defaultValues/editor.mjs.map +1 -1
- package/dist/esm/defaultValues/index.mjs +21 -0
- package/dist/esm/defaultValues/index.mjs.map +1 -0
- package/dist/esm/defaultValues/internationalization.mjs +13 -7
- package/dist/esm/defaultValues/internationalization.mjs.map +1 -1
- package/dist/esm/defaultValues/log.mjs +10 -4
- package/dist/esm/defaultValues/log.mjs.map +1 -1
- package/dist/esm/defaultValues/routing.mjs +25 -0
- package/dist/esm/defaultValues/routing.mjs.map +1 -0
- package/dist/esm/extractErrorMessage.mjs +94 -0
- package/dist/esm/extractErrorMessage.mjs.map +1 -0
- package/dist/esm/index.mjs +18 -61
- package/dist/esm/loadEnvFile.mjs +27 -17
- package/dist/esm/loadEnvFile.mjs.map +1 -1
- package/dist/esm/loadExternalFile/bundleFile.mjs +68 -0
- package/dist/esm/loadExternalFile/bundleFile.mjs.map +1 -0
- package/dist/esm/loadExternalFile/loadExternalFile.mjs +81 -0
- package/dist/esm/loadExternalFile/loadExternalFile.mjs.map +1 -0
- package/dist/esm/loadExternalFile/parseFileContent.mjs +63 -0
- package/dist/esm/loadExternalFile/parseFileContent.mjs.map +1 -0
- package/dist/esm/loadExternalFile/transpileTSToMJS.mjs +55 -0
- package/dist/esm/loadExternalFile/transpileTSToMJS.mjs.map +1 -0
- package/dist/esm/logger.mjs +110 -115
- package/dist/esm/logger.mjs.map +1 -1
- package/dist/esm/package.mjs +6 -0
- package/dist/esm/package.mjs.map +1 -0
- package/dist/esm/retryManager.mjs +40 -23
- package/dist/esm/retryManager.mjs.map +1 -1
- package/dist/esm/utils/ESMxCJSHelpers.mjs +20 -6
- package/dist/esm/utils/ESMxCJSHelpers.mjs.map +1 -1
- package/dist/esm/utils/cache.mjs +414 -0
- package/dist/esm/utils/cache.mjs.map +1 -0
- package/dist/esm/utils/clearModuleCache.mjs +22 -21
- package/dist/esm/utils/clearModuleCache.mjs.map +1 -1
- package/dist/esm/utils/extractErrorMessage.mjs +94 -0
- package/dist/esm/utils/extractErrorMessage.mjs.map +1 -0
- package/dist/esm/utils/getExtension.mjs +11 -11
- package/dist/esm/utils/getExtension.mjs.map +1 -1
- package/dist/esm/utils/getPackageJsonPath.mjs +33 -0
- package/dist/esm/utils/getPackageJsonPath.mjs.map +1 -0
- package/dist/esm/utils/logStack.mjs +16 -0
- package/dist/esm/utils/logStack.mjs.map +1 -0
- package/dist/esm/utils/normalizePath.mjs +14 -3
- package/dist/esm/utils/normalizePath.mjs.map +1 -1
- package/dist/types/alias.d.ts +20 -12
- package/dist/types/alias.d.ts.map +1 -1
- package/dist/types/built.d.ts +7 -6
- package/dist/types/built.d.ts.map +1 -1
- package/dist/types/bundleJSFile.d.ts +97 -93
- package/dist/types/bundleJSFile.d.ts.map +1 -1
- package/dist/types/client.d.ts +3 -5
- package/dist/types/configFile/buildConfigurationFields.d.ts +7 -2
- package/dist/types/configFile/buildConfigurationFields.d.ts.map +1 -1
- package/dist/types/configFile/getConfiguration.d.ts +19 -15
- package/dist/types/configFile/getConfiguration.d.ts.map +1 -1
- package/dist/types/configFile/index.d.ts +4 -3
- package/dist/types/configFile/loadConfigurationFile.d.ts +8 -3
- package/dist/types/configFile/loadConfigurationFile.d.ts.map +1 -1
- package/dist/types/configFile/searchConfigurationFile.d.ts +6 -4
- package/dist/types/configFile/searchConfigurationFile.d.ts.map +1 -1
- package/dist/types/defaultValues/build.d.ts +10 -4
- package/dist/types/defaultValues/build.d.ts.map +1 -1
- package/dist/types/defaultValues/content.d.ts +23 -16
- package/dist/types/defaultValues/content.d.ts.map +1 -1
- package/dist/types/defaultValues/editor.d.ts +14 -9
- package/dist/types/defaultValues/editor.d.ts.map +1 -1
- package/dist/types/defaultValues/index.d.ts +14 -0
- package/dist/types/defaultValues/index.d.ts.map +1 -0
- package/dist/types/defaultValues/internationalization.d.ts +12 -6
- package/dist/types/defaultValues/internationalization.d.ts.map +1 -1
- package/dist/types/defaultValues/log.d.ts +7 -2
- package/dist/types/defaultValues/log.d.ts.map +1 -1
- package/dist/types/defaultValues/routing.d.ts +14 -0
- package/dist/types/defaultValues/routing.d.ts.map +1 -0
- package/dist/types/extractErrorMessage.d.ts +5 -0
- package/dist/types/extractErrorMessage.d.ts.map +1 -0
- package/dist/types/index.d.ts +21 -17
- package/dist/types/loadEnvFile.d.ts +8 -5
- package/dist/types/loadEnvFile.d.ts.map +1 -1
- package/dist/types/loadExternalFile/bundleFile.d.ts +10 -0
- package/dist/types/loadExternalFile/bundleFile.d.ts.map +1 -0
- package/dist/types/loadExternalFile/loadExternalFile.d.ts +25 -0
- package/dist/types/loadExternalFile/loadExternalFile.d.ts.map +1 -0
- package/dist/types/loadExternalFile/parseFileContent.d.ts +29 -0
- package/dist/types/loadExternalFile/parseFileContent.d.ts.map +1 -0
- package/dist/types/loadExternalFile/transpileTSToMJS.d.ts +9 -0
- package/dist/types/loadExternalFile/transpileTSToMJS.d.ts.map +1 -0
- package/dist/types/logger.d.ts +41 -37
- package/dist/types/logger.d.ts.map +1 -1
- package/dist/types/retryManager.d.ts +26 -8
- package/dist/types/retryManager.d.ts.map +1 -1
- package/dist/types/utils/ESMxCJSHelpers.d.ts +16 -2
- package/dist/types/utils/ESMxCJSHelpers.d.ts.map +1 -1
- package/dist/types/utils/cache.d.ts +50 -0
- package/dist/types/utils/cache.d.ts.map +1 -0
- package/dist/types/utils/clearModuleCache.d.ts +4 -1
- package/dist/types/utils/clearModuleCache.d.ts.map +1 -1
- package/dist/types/utils/extractErrorMessage.d.ts +5 -0
- package/dist/types/utils/extractErrorMessage.d.ts.map +1 -0
- package/dist/types/utils/getExtension.d.ts +6 -2
- package/dist/types/utils/getExtension.d.ts.map +1 -1
- package/dist/types/utils/getPackageJsonPath.d.ts +10 -0
- package/dist/types/utils/getPackageJsonPath.d.ts.map +1 -0
- package/dist/types/utils/logStack.d.ts +7 -0
- package/dist/types/utils/logStack.d.ts.map +1 -0
- package/dist/types/utils/normalizePath.d.ts +4 -1
- package/dist/types/utils/normalizePath.d.ts.map +1 -1
- package/package.json +40 -35
- package/LICENSE +0 -202
- package/dist/cjs/client.cjs.map +0 -1
- package/dist/cjs/configFile/index.cjs.map +0 -1
- package/dist/cjs/defaultValues/middleware.cjs +0 -47
- package/dist/cjs/defaultValues/middleware.cjs.map +0 -1
- package/dist/cjs/getSandboxContext.cjs +0 -64
- package/dist/cjs/getSandboxContext.cjs.map +0 -1
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/loadExternalFile.cjs +0 -105
- package/dist/cjs/loadExternalFile.cjs.map +0 -1
- package/dist/cjs/types/config.cjs +0 -17
- package/dist/cjs/types/config.cjs.map +0 -1
- package/dist/cjs/types/locales.cjs +0 -310
- package/dist/cjs/types/locales.cjs.map +0 -1
- package/dist/esm/client.mjs.map +0 -1
- package/dist/esm/configFile/index.mjs.map +0 -1
- package/dist/esm/defaultValues/middleware.mjs +0 -17
- package/dist/esm/defaultValues/middleware.mjs.map +0 -1
- package/dist/esm/getSandboxContext.mjs +0 -40
- package/dist/esm/getSandboxContext.mjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/esm/loadExternalFile.mjs +0 -81
- package/dist/esm/loadExternalFile.mjs.map +0 -1
- package/dist/esm/types/config.mjs +0 -1
- package/dist/esm/types/config.mjs.map +0 -1
- package/dist/esm/types/locales.mjs +0 -286
- package/dist/esm/types/locales.mjs.map +0 -1
- package/dist/types/client.d.ts.map +0 -1
- package/dist/types/configFile/index.d.ts.map +0 -1
- package/dist/types/defaultValues/middleware.d.ts +0 -8
- package/dist/types/defaultValues/middleware.d.ts.map +0 -1
- package/dist/types/getSandboxContext.d.ts +0 -4
- package/dist/types/getSandboxContext.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/loadExternalFile.d.ts +0 -8
- package/dist/types/loadExternalFile.d.ts.map +0 -1
- package/dist/types/types/config.d.ts +0 -659
- package/dist/types/types/config.d.ts.map +0 -1
- package/dist/types/types/locales.d.ts +0 -564
- package/dist/types/types/locales.d.ts.map +0 -1
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { colorizePath, logger } from "../logger.mjs";
|
|
2
|
+
import { ESMxCJSRequire } from "../utils/ESMxCJSHelpers.mjs";
|
|
3
|
+
import { parseFileContent } from "./parseFileContent.mjs";
|
|
4
|
+
import { transpileTSToMJS, transpileTSToMJSSync } from "./transpileTSToMJS.mjs";
|
|
5
|
+
import { extname } from "node:path";
|
|
6
|
+
import { readFile } from "node:fs/promises";
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
|
|
9
|
+
//#region src/loadExternalFile/loadExternalFile.ts
|
|
10
|
+
/**
|
|
11
|
+
* Load the content declaration from the given path
|
|
12
|
+
*
|
|
13
|
+
* Accepts JSON, JS, MJS and TS files as configuration
|
|
14
|
+
*/
|
|
15
|
+
const loadExternalFileSync = (filePath, options) => {
|
|
16
|
+
const fileExtension = extname(filePath);
|
|
17
|
+
const safeProjectRequire = options?.projectRequire ?? ESMxCJSRequire;
|
|
18
|
+
try {
|
|
19
|
+
if (fileExtension === "json") {
|
|
20
|
+
delete safeProjectRequire.cache[safeProjectRequire.resolve(filePath)];
|
|
21
|
+
return safeProjectRequire(filePath);
|
|
22
|
+
}
|
|
23
|
+
const moduleResultString = transpileTSToMJSSync(readFileSync(filePath, "utf-8"), filePath);
|
|
24
|
+
if (!moduleResultString) {
|
|
25
|
+
logger("File could not be loaded.", { level: "error" });
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const fileContent = parseFileContent(moduleResultString, {
|
|
29
|
+
projectRequire: options?.projectRequire,
|
|
30
|
+
envVarOptions: options?.envVarOptions,
|
|
31
|
+
additionalEnvVars: options?.additionalEnvVars,
|
|
32
|
+
mocks: options?.mocks,
|
|
33
|
+
aliases: options?.aliases
|
|
34
|
+
});
|
|
35
|
+
if (typeof fileContent === "undefined") {
|
|
36
|
+
logger(`File file could not be loaded. Path : ${filePath}`);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
return fileContent;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
logger([`Error: ${error.message} - `, JSON.stringify(error.stack, null, 2)], { level: "error" });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Load the content declaration from the given path
|
|
46
|
+
*
|
|
47
|
+
* Accepts JSON, JS, MJS and TS files as configuration
|
|
48
|
+
*/
|
|
49
|
+
const loadExternalFile = async (filePath, options) => {
|
|
50
|
+
const fileExtension = extname(filePath);
|
|
51
|
+
const safeProjectRequire = options?.projectRequire ?? ESMxCJSRequire;
|
|
52
|
+
try {
|
|
53
|
+
if (fileExtension === "json") {
|
|
54
|
+
delete safeProjectRequire.cache[safeProjectRequire.resolve(filePath)];
|
|
55
|
+
return safeProjectRequire(filePath);
|
|
56
|
+
}
|
|
57
|
+
const moduleResultString = await transpileTSToMJS(await readFile(filePath, "utf-8"), filePath, options?.buildOptions);
|
|
58
|
+
if (!moduleResultString) {
|
|
59
|
+
logger("File could not be loaded.", { level: "error" });
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const fileContent = parseFileContent(moduleResultString, {
|
|
63
|
+
projectRequire: options?.projectRequire,
|
|
64
|
+
envVarOptions: options?.envVarOptions,
|
|
65
|
+
additionalEnvVars: options?.additionalEnvVars,
|
|
66
|
+
mocks: options?.mocks,
|
|
67
|
+
aliases: options?.aliases
|
|
68
|
+
});
|
|
69
|
+
if (typeof fileContent === "undefined") {
|
|
70
|
+
logger(`File file could not be loaded. Path : ${colorizePath(filePath)}`);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
return fileContent;
|
|
74
|
+
} catch (error) {
|
|
75
|
+
logger([`Error: ${error.message} - `, JSON.stringify(error.stack, null, 2)], { level: "error" });
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
//#endregion
|
|
80
|
+
export { loadExternalFile, loadExternalFileSync };
|
|
81
|
+
//# sourceMappingURL=loadExternalFile.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loadExternalFile.mjs","names":["moduleResultString: string | undefined"],"sources":["../../../src/loadExternalFile/loadExternalFile.ts"],"sourcesContent":["import { readFileSync } from 'node:fs';\nimport { readFile } from 'node:fs/promises';\nimport { extname } from 'node:path';\nimport type { IntlayerConfig } from '@intlayer/types';\nimport type { BuildOptions, Plugin } from 'esbuild';\nimport { colorizePath, logger } from '../logger';\nimport { ESMxCJSRequire } from '../utils/ESMxCJSHelpers';\nimport {\n parseFileContent,\n type SandBoxContextOptions,\n} from './parseFileContent';\nimport { transpileTSToMJS, transpileTSToMJSSync } from './transpileTSToMJS';\n\nexport type ESBuildPlugin = Plugin;\n\nexport type LoadExternalFileOptions = {\n configuration?: IntlayerConfig;\n buildOptions?: BuildOptions;\n} & SandBoxContextOptions;\n\n/**\n * Load the content declaration from the given path\n *\n * Accepts JSON, JS, MJS and TS files as configuration\n */\nexport const loadExternalFileSync = (\n filePath: string,\n options?: LoadExternalFileOptions\n): any | undefined => {\n const fileExtension = extname(filePath);\n const safeProjectRequire = options?.projectRequire ?? ESMxCJSRequire;\n\n try {\n if (fileExtension === 'json') {\n // Remove cache to force getting fresh content\n delete safeProjectRequire.cache[safeProjectRequire.resolve(filePath)];\n // Assume JSON\n return safeProjectRequire(filePath);\n }\n\n // Rest is JS, MJS or TS\n const code = readFileSync(filePath, 'utf-8');\n\n const moduleResultString: string | undefined = transpileTSToMJSSync(\n code,\n filePath\n );\n\n if (!moduleResultString) {\n logger('File could not be loaded.', { level: 'error' });\n return undefined;\n }\n\n const fileContent = parseFileContent(moduleResultString, {\n projectRequire: options?.projectRequire,\n envVarOptions: options?.envVarOptions,\n additionalEnvVars: options?.additionalEnvVars,\n mocks: options?.mocks,\n aliases: options?.aliases,\n });\n\n if (typeof fileContent === 'undefined') {\n logger(`File file could not be loaded. Path : ${filePath}`);\n return undefined;\n }\n\n return fileContent;\n } catch (error) {\n logger(\n [\n `Error: ${(error as Error).message} - `,\n JSON.stringify((error as Error).stack, null, 2),\n ],\n {\n level: 'error',\n }\n );\n }\n};\n\n/**\n * Load the content declaration from the given path\n *\n * Accepts JSON, JS, MJS and TS files as configuration\n */\nexport const loadExternalFile = async (\n filePath: string,\n options?: LoadExternalFileOptions\n): Promise<any | undefined> => {\n const fileExtension = extname(filePath);\n const safeProjectRequire = options?.projectRequire ?? ESMxCJSRequire;\n\n try {\n if (fileExtension === 'json') {\n // Remove cache to force getting fresh content\n delete safeProjectRequire.cache[safeProjectRequire.resolve(filePath)];\n // Assume JSON\n return safeProjectRequire(filePath);\n }\n\n // Rest is JS, MJS or TS\n const code = await readFile(filePath, 'utf-8');\n\n const moduleResultString: string | undefined = await transpileTSToMJS(\n code,\n filePath,\n options?.buildOptions\n );\n\n if (!moduleResultString) {\n logger('File could not be loaded.', { level: 'error' });\n return undefined;\n }\n\n const fileContent = parseFileContent(moduleResultString, {\n projectRequire: options?.projectRequire,\n envVarOptions: options?.envVarOptions,\n additionalEnvVars: options?.additionalEnvVars,\n mocks: options?.mocks,\n aliases: options?.aliases,\n });\n\n if (typeof fileContent === 'undefined') {\n logger(`File file could not be loaded. Path : ${colorizePath(filePath)}`);\n return undefined;\n }\n\n return fileContent;\n } catch (error) {\n logger(\n [\n `Error: ${(error as Error).message} - `,\n JSON.stringify((error as Error).stack, null, 2),\n ],\n {\n level: 'error',\n }\n );\n }\n};\n"],"mappings":";;;;;;;;;;;;;;AAyBA,MAAa,wBACX,UACA,YACoB;CACpB,MAAM,gBAAgB,QAAQ,SAAS;CACvC,MAAM,qBAAqB,SAAS,kBAAkB;AAEtD,KAAI;AACF,MAAI,kBAAkB,QAAQ;AAE5B,UAAO,mBAAmB,MAAM,mBAAmB,QAAQ,SAAS;AAEpE,UAAO,mBAAmB,SAAS;;EAMrC,MAAMA,qBAAyC,qBAFlC,aAAa,UAAU,QAAQ,EAI1C,SACD;AAED,MAAI,CAAC,oBAAoB;AACvB,UAAO,6BAA6B,EAAE,OAAO,SAAS,CAAC;AACvD;;EAGF,MAAM,cAAc,iBAAiB,oBAAoB;GACvD,gBAAgB,SAAS;GACzB,eAAe,SAAS;GACxB,mBAAmB,SAAS;GAC5B,OAAO,SAAS;GAChB,SAAS,SAAS;GACnB,CAAC;AAEF,MAAI,OAAO,gBAAgB,aAAa;AACtC,UAAO,yCAAyC,WAAW;AAC3D;;AAGF,SAAO;UACA,OAAO;AACd,SACE,CACE,UAAW,MAAgB,QAAQ,MACnC,KAAK,UAAW,MAAgB,OAAO,MAAM,EAAE,CAChD,EACD,EACE,OAAO,SACR,CACF;;;;;;;;AASL,MAAa,mBAAmB,OAC9B,UACA,YAC6B;CAC7B,MAAM,gBAAgB,QAAQ,SAAS;CACvC,MAAM,qBAAqB,SAAS,kBAAkB;AAEtD,KAAI;AACF,MAAI,kBAAkB,QAAQ;AAE5B,UAAO,mBAAmB,MAAM,mBAAmB,QAAQ,SAAS;AAEpE,UAAO,mBAAmB,SAAS;;EAMrC,MAAMA,qBAAyC,MAAM,iBAFxC,MAAM,SAAS,UAAU,QAAQ,EAI5C,UACA,SAAS,aACV;AAED,MAAI,CAAC,oBAAoB;AACvB,UAAO,6BAA6B,EAAE,OAAO,SAAS,CAAC;AACvD;;EAGF,MAAM,cAAc,iBAAiB,oBAAoB;GACvD,gBAAgB,SAAS;GACzB,eAAe,SAAS;GACxB,mBAAmB,SAAS;GAC5B,OAAO,SAAS;GAChB,SAAS,SAAS;GACnB,CAAC;AAEF,MAAI,OAAO,gBAAgB,aAAa;AACtC,UAAO,yCAAyC,aAAa,SAAS,GAAG;AACzE;;AAGF,SAAO;UACA,OAAO;AACd,SACE,CACE,UAAW,MAAgB,QAAQ,MACnC,KAAK,UAAW,MAAgB,OAAO,MAAM,EAAE,CAChD,EACD,EACE,OAAO,SACR,CACF"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { ESMxCJSRequire } from "../utils/ESMxCJSHelpers.mjs";
|
|
2
|
+
import { loadEnvFile } from "../loadEnvFile.mjs";
|
|
3
|
+
import { runInNewContext } from "node:vm";
|
|
4
|
+
import * as esbuild from "esbuild";
|
|
5
|
+
|
|
6
|
+
//#region src/loadExternalFile/parseFileContent.ts
|
|
7
|
+
const getSandBoxContext = (options) => {
|
|
8
|
+
const { envVarOptions, projectRequire, additionalEnvVars, mocks, aliases } = options ?? {};
|
|
9
|
+
let additionalGlobalVar = {};
|
|
10
|
+
const baseRequire = typeof projectRequire === "function" ? projectRequire : ESMxCJSRequire;
|
|
11
|
+
const mockedRequire = (() => {
|
|
12
|
+
const mockTable = Object.assign({ esbuild }, mocks);
|
|
13
|
+
const aliasTable = Object.assign({}, aliases);
|
|
14
|
+
const wrappedRequire = function mockableRequire(id) {
|
|
15
|
+
const target = aliasTable?.[id] ? aliasTable[id] : id;
|
|
16
|
+
if (mockTable && Object.hasOwn(mockTable, target)) return mockTable[target];
|
|
17
|
+
if (target !== id && mockTable && Object.hasOwn(mockTable, id)) return mockTable[id];
|
|
18
|
+
return baseRequire(target);
|
|
19
|
+
};
|
|
20
|
+
wrappedRequire.resolve = baseRequire.resolve.bind(baseRequire);
|
|
21
|
+
wrappedRequire.main = baseRequire.main;
|
|
22
|
+
wrappedRequire.extensions = baseRequire.extensions;
|
|
23
|
+
wrappedRequire.cache = baseRequire.cache;
|
|
24
|
+
return wrappedRequire;
|
|
25
|
+
})();
|
|
26
|
+
try {
|
|
27
|
+
additionalGlobalVar = { React: baseRequire("react") };
|
|
28
|
+
} catch (_err) {}
|
|
29
|
+
const sandboxContext = {
|
|
30
|
+
exports: { default: {} },
|
|
31
|
+
module: { exports: {} },
|
|
32
|
+
process: {
|
|
33
|
+
...process,
|
|
34
|
+
env: {
|
|
35
|
+
...process.env,
|
|
36
|
+
...loadEnvFile(envVarOptions),
|
|
37
|
+
...additionalEnvVars
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
console,
|
|
41
|
+
require: mockedRequire,
|
|
42
|
+
...additionalGlobalVar
|
|
43
|
+
};
|
|
44
|
+
Object.getOwnPropertyNames(globalThis).forEach((key) => {
|
|
45
|
+
if (!(key in sandboxContext)) sandboxContext[key] = globalThis[key];
|
|
46
|
+
});
|
|
47
|
+
return sandboxContext;
|
|
48
|
+
};
|
|
49
|
+
const parseFileContent = (fileContentString, options) => {
|
|
50
|
+
const sandboxContext = getSandBoxContext(options);
|
|
51
|
+
runInNewContext(`"use strict";\n${fileContentString}`, sandboxContext);
|
|
52
|
+
const candidates = [
|
|
53
|
+
sandboxContext.exports?.default,
|
|
54
|
+
sandboxContext.module?.exports?.defaults,
|
|
55
|
+
sandboxContext.module?.exports?.default,
|
|
56
|
+
sandboxContext.module?.exports
|
|
57
|
+
];
|
|
58
|
+
for (const candidate of candidates) if (candidate && typeof candidate === "object" && Object.keys(candidate).length > 0) return candidate;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
//#endregion
|
|
62
|
+
export { getSandBoxContext, parseFileContent };
|
|
63
|
+
//# sourceMappingURL=parseFileContent.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parseFileContent.mjs","names":["baseRequire: NodeJS.Require","mockedRequire: NodeJS.Require","sandboxContext: Context","candidates: unknown[]"],"sources":["../../../src/loadExternalFile/parseFileContent.ts"],"sourcesContent":["import { type Context, runInNewContext } from 'node:vm';\nimport * as esbuild from 'esbuild';\nimport { type LoadEnvFileOptions, loadEnvFile } from '../loadEnvFile';\nimport { ESMxCJSRequire } from '../utils/ESMxCJSHelpers';\n\nexport type SandBoxContextOptions = {\n envVarOptions?: LoadEnvFileOptions;\n projectRequire?: NodeJS.Require;\n additionalEnvVars?: Record<string, string>;\n /**\n * Map of specifier -> mocked export to be returned when code in the VM calls require(specifier).\n * Example:\n * mocks: {\n * '@intlayer/config/built': { getConfig: () => ({}), Locales: {} }\n * }\n */\n mocks?: Record<string, any>;\n /**\n * Optional alias map if you want to redirect specifiers.\n * Useful when user code imports a subpath you want to collapse.\n * Example:\n * aliases: { '@intlayer/config/built': '@intlayer/config' }\n */\n aliases?: Record<string, string>;\n};\n\nexport const getSandBoxContext = (options?: SandBoxContextOptions): Context => {\n const { envVarOptions, projectRequire, additionalEnvVars, mocks, aliases } =\n options ?? {};\n\n let additionalGlobalVar = {};\n\n const baseRequire: NodeJS.Require =\n typeof projectRequire === 'function' ? projectRequire : ESMxCJSRequire;\n\n // Wrap require to honor mocks and aliases inside the VM\n const mockedRequire: NodeJS.Require = (() => {\n const mockTable = Object.assign(\n {\n esbuild,\n },\n mocks\n );\n const aliasTable = Object.assign({}, aliases);\n\n const wrappedRequire = function mockableRequire(id: string) {\n const target = aliasTable?.[id] ? aliasTable[id] : id;\n\n if (mockTable && Object.hasOwn(mockTable, target)) {\n return mockTable[target];\n }\n\n // If the original id was aliased, allow mocks to be defined on either key.\n if (target !== id && mockTable && Object.hasOwn(mockTable, id)) {\n return mockTable[id];\n }\n\n return baseRequire(target);\n } as NodeJS.Require;\n\n // Mirror NodeJS.Require properties\n wrappedRequire.resolve = baseRequire.resolve.bind(baseRequire);\n wrappedRequire.main = baseRequire.main;\n wrappedRequire.extensions = baseRequire.extensions;\n wrappedRequire.cache = baseRequire.cache;\n\n return wrappedRequire;\n })();\n\n try {\n // Dynamically try to require React if it's installed in the project\n additionalGlobalVar = {\n React: baseRequire('react'),\n };\n } catch (_err) {\n // React is not installed, so we don't inject it\n }\n\n const sandboxContext: Context = {\n exports: {\n default: {},\n },\n module: {\n exports: {},\n },\n process: {\n ...process,\n env: {\n ...process.env,\n ...loadEnvFile(envVarOptions),\n ...additionalEnvVars,\n },\n },\n console,\n require: mockedRequire,\n ...additionalGlobalVar,\n };\n\n // Dynamically inject all global variables\n Object.getOwnPropertyNames(globalThis).forEach((key) => {\n if (!(key in sandboxContext)) {\n sandboxContext[key] = globalThis[key as keyof typeof globalThis];\n }\n });\n\n return sandboxContext;\n};\n\nexport const parseFileContent = <T>(\n fileContentString: string,\n options?: SandBoxContextOptions\n): T | undefined => {\n const sandboxContext = getSandBoxContext(options);\n\n // Force strict mode so illegal writes throw instead of silently failing.\n runInNewContext(`\"use strict\";\\n${fileContentString}`, sandboxContext);\n\n const candidates: unknown[] = [\n sandboxContext.exports?.default,\n sandboxContext.module?.exports?.defaults,\n sandboxContext.module?.exports?.default,\n sandboxContext.module?.exports,\n ];\n\n for (const candidate of candidates) {\n if (\n candidate &&\n typeof candidate === 'object' &&\n Object.keys(candidate as object).length > 0\n ) {\n return candidate as T;\n }\n }\n};\n"],"mappings":";;;;;;AA0BA,MAAa,qBAAqB,YAA6C;CAC7E,MAAM,EAAE,eAAe,gBAAgB,mBAAmB,OAAO,YAC/D,WAAW,EAAE;CAEf,IAAI,sBAAsB,EAAE;CAE5B,MAAMA,cACJ,OAAO,mBAAmB,aAAa,iBAAiB;CAG1D,MAAMC,uBAAuC;EAC3C,MAAM,YAAY,OAAO,OACvB,EACE,SACD,EACD,MACD;EACD,MAAM,aAAa,OAAO,OAAO,EAAE,EAAE,QAAQ;EAE7C,MAAM,iBAAiB,SAAS,gBAAgB,IAAY;GAC1D,MAAM,SAAS,aAAa,MAAM,WAAW,MAAM;AAEnD,OAAI,aAAa,OAAO,OAAO,WAAW,OAAO,CAC/C,QAAO,UAAU;AAInB,OAAI,WAAW,MAAM,aAAa,OAAO,OAAO,WAAW,GAAG,CAC5D,QAAO,UAAU;AAGnB,UAAO,YAAY,OAAO;;AAI5B,iBAAe,UAAU,YAAY,QAAQ,KAAK,YAAY;AAC9D,iBAAe,OAAO,YAAY;AAClC,iBAAe,aAAa,YAAY;AACxC,iBAAe,QAAQ,YAAY;AAEnC,SAAO;KACL;AAEJ,KAAI;AAEF,wBAAsB,EACpB,OAAO,YAAY,QAAQ,EAC5B;UACM,MAAM;CAIf,MAAMC,iBAA0B;EAC9B,SAAS,EACP,SAAS,EAAE,EACZ;EACD,QAAQ,EACN,SAAS,EAAE,EACZ;EACD,SAAS;GACP,GAAG;GACH,KAAK;IACH,GAAG,QAAQ;IACX,GAAG,YAAY,cAAc;IAC7B,GAAG;IACJ;GACF;EACD;EACA,SAAS;EACT,GAAG;EACJ;AAGD,QAAO,oBAAoB,WAAW,CAAC,SAAS,QAAQ;AACtD,MAAI,EAAE,OAAO,gBACX,gBAAe,OAAO,WAAW;GAEnC;AAEF,QAAO;;AAGT,MAAa,oBACX,mBACA,YACkB;CAClB,MAAM,iBAAiB,kBAAkB,QAAQ;AAGjD,iBAAgB,kBAAkB,qBAAqB,eAAe;CAEtE,MAAMC,aAAwB;EAC5B,eAAe,SAAS;EACxB,eAAe,QAAQ,SAAS;EAChC,eAAe,QAAQ,SAAS;EAChC,eAAe,QAAQ;EACxB;AAED,MAAK,MAAM,aAAa,WACtB,KACE,aACA,OAAO,cAAc,YACrB,OAAO,KAAK,UAAoB,CAAC,SAAS,EAE1C,QAAO"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getLoader } from "./bundleFile.mjs";
|
|
2
|
+
import { dirname, extname, join } from "node:path";
|
|
3
|
+
import { build, buildSync } from "esbuild";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
|
|
6
|
+
//#region src/loadExternalFile/transpileTSToMJS.ts
|
|
7
|
+
const getTransformationOptions = (filePath) => ({
|
|
8
|
+
loader: {
|
|
9
|
+
".js": "js",
|
|
10
|
+
".jsx": "jsx",
|
|
11
|
+
".mjs": "js",
|
|
12
|
+
".ts": "ts",
|
|
13
|
+
".tsx": "tsx",
|
|
14
|
+
".cjs": "js",
|
|
15
|
+
".json": "json",
|
|
16
|
+
".md": "text",
|
|
17
|
+
".mdx": "text"
|
|
18
|
+
},
|
|
19
|
+
format: "cjs",
|
|
20
|
+
target: "node20",
|
|
21
|
+
platform: "node",
|
|
22
|
+
write: false,
|
|
23
|
+
packages: "external",
|
|
24
|
+
bundle: true,
|
|
25
|
+
tsconfig: join(process.cwd(), "tsconfig.json"),
|
|
26
|
+
define: { "import.meta.url": JSON.stringify(pathToFileURL(filePath).href) }
|
|
27
|
+
});
|
|
28
|
+
const transpileTSToMJSSync = (code, filePath, options) => {
|
|
29
|
+
return buildSync({
|
|
30
|
+
stdin: {
|
|
31
|
+
contents: code,
|
|
32
|
+
loader: getLoader(extname(filePath)),
|
|
33
|
+
resolveDir: dirname(filePath),
|
|
34
|
+
sourcefile: filePath
|
|
35
|
+
},
|
|
36
|
+
...getTransformationOptions(filePath),
|
|
37
|
+
...options
|
|
38
|
+
}).outputFiles?.[0].text;
|
|
39
|
+
};
|
|
40
|
+
const transpileTSToMJS = async (code, filePath, options) => {
|
|
41
|
+
return (await build({
|
|
42
|
+
stdin: {
|
|
43
|
+
contents: code,
|
|
44
|
+
loader: getLoader(extname(filePath)),
|
|
45
|
+
resolveDir: dirname(filePath),
|
|
46
|
+
sourcefile: filePath
|
|
47
|
+
},
|
|
48
|
+
...getTransformationOptions(filePath),
|
|
49
|
+
...options
|
|
50
|
+
})).outputFiles?.[0].text;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
export { transpileTSToMJS, transpileTSToMJSSync };
|
|
55
|
+
//# sourceMappingURL=transpileTSToMJS.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transpileTSToMJS.mjs","names":[],"sources":["../../../src/loadExternalFile/transpileTSToMJS.ts"],"sourcesContent":["import { dirname, extname, join } from 'node:path';\nimport { pathToFileURL } from 'node:url';\nimport {\n type BuildOptions,\n type BuildResult,\n build,\n buildSync,\n type Plugin,\n} from 'esbuild';\nimport { getLoader } from './bundleFile';\n\nexport type ESBuildPlugin = Plugin;\n\nconst getTransformationOptions = (filePath: string): BuildOptions => ({\n loader: {\n '.js': 'js',\n '.jsx': 'jsx',\n '.mjs': 'js',\n '.ts': 'ts',\n '.tsx': 'tsx',\n '.cjs': 'js',\n '.json': 'json',\n '.md': 'text',\n '.mdx': 'text',\n },\n format: 'cjs',\n target: 'node20',\n platform: 'node',\n write: false,\n packages: 'external',\n bundle: true,\n tsconfig: join(process.cwd(), 'tsconfig.json'),\n define: {\n 'import.meta.url': JSON.stringify(pathToFileURL(filePath).href),\n },\n});\n\nexport const transpileTSToMJSSync = (\n code: string,\n filePath: string,\n options?: BuildOptions\n): string | undefined => {\n const extension = extname(filePath);\n const loader = getLoader(extension);\n\n const moduleResult: BuildResult = buildSync({\n stdin: {\n contents: code,\n loader,\n resolveDir: dirname(filePath), // Add resolveDir to resolve imports relative to the file's location\n sourcefile: filePath, // Add sourcefile for better error messages\n },\n ...getTransformationOptions(filePath),\n ...options,\n });\n\n const moduleResultString = moduleResult.outputFiles?.[0].text;\n\n return moduleResultString;\n};\n\nexport const transpileTSToMJS = async (\n code: string,\n filePath: string,\n options?: BuildOptions\n): Promise<string | undefined> => {\n const extension = extname(filePath);\n const loader = getLoader(extension);\n\n const moduleResult: BuildResult = await build({\n stdin: {\n contents: code,\n loader,\n resolveDir: dirname(filePath), // Add resolveDir to resolve imports relative to the file's location\n sourcefile: filePath, // Add sourcefile for better error messages\n },\n ...getTransformationOptions(filePath),\n ...options,\n });\n\n const moduleResultString = moduleResult.outputFiles?.[0].text;\n\n return moduleResultString;\n};\n"],"mappings":";;;;;;AAaA,MAAM,4BAA4B,cAAoC;CACpE,QAAQ;EACN,OAAO;EACP,QAAQ;EACR,QAAQ;EACR,OAAO;EACP,QAAQ;EACR,QAAQ;EACR,SAAS;EACT,OAAO;EACP,QAAQ;EACT;CACD,QAAQ;CACR,QAAQ;CACR,UAAU;CACV,OAAO;CACP,UAAU;CACV,QAAQ;CACR,UAAU,KAAK,QAAQ,KAAK,EAAE,gBAAgB;CAC9C,QAAQ,EACN,mBAAmB,KAAK,UAAU,cAAc,SAAS,CAAC,KAAK,EAChE;CACF;AAED,MAAa,wBACX,MACA,UACA,YACuB;AAiBvB,QAbkC,UAAU;EAC1C,OAAO;GACL,UAAU;GACV,QALW,UADG,QAAQ,SAAS,CACA;GAM/B,YAAY,QAAQ,SAAS;GAC7B,YAAY;GACb;EACD,GAAG,yBAAyB,SAAS;EACrC,GAAG;EACJ,CAAC,CAEsC,cAAc,GAAG;;AAK3D,MAAa,mBAAmB,OAC9B,MACA,UACA,YACgC;AAiBhC,SAbkC,MAAM,MAAM;EAC5C,OAAO;GACL,UAAU;GACV,QALW,UADG,QAAQ,SAAS,CACA;GAM/B,YAAY,QAAQ,SAAS;GAC7B,YAAY;GACb;EACD,GAAG,yBAAyB,SAAS;EACrC,GAAG;EACJ,CAAC,EAEsC,cAAc,GAAG"}
|
package/dist/esm/logger.mjs
CHANGED
|
@@ -1,129 +1,124 @@
|
|
|
1
|
+
//#region src/logger.ts
|
|
1
2
|
const logger = (content, details) => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
if (level === "warn") {
|
|
21
|
-
return warn(...flatContent);
|
|
22
|
-
}
|
|
23
|
-
if (level === "error") {
|
|
24
|
-
return error(...flatContent);
|
|
25
|
-
}
|
|
26
|
-
log(...flatContent);
|
|
3
|
+
const isVerbose = details?.isVerbose ?? false;
|
|
4
|
+
const mode = details?.config?.mode ?? "default";
|
|
5
|
+
const level = details?.level ?? "info";
|
|
6
|
+
const prefix = details?.config?.prefix ? details?.config?.prefix : void 0;
|
|
7
|
+
const log = details?.config?.log ?? console.log;
|
|
8
|
+
const info = details?.config?.info ?? console.info;
|
|
9
|
+
const warn = details?.config?.warn ?? console.warn;
|
|
10
|
+
const error = details?.config?.error ?? console.error;
|
|
11
|
+
const debug = details?.config?.debug ?? console.debug;
|
|
12
|
+
if (mode === "disabled") return;
|
|
13
|
+
if (isVerbose && mode !== "verbose") return;
|
|
14
|
+
const flatContent = prefix ? [prefix, ...[content].flat()] : [content].flat();
|
|
15
|
+
if (level === "debug") return debug(...flatContent);
|
|
16
|
+
if (level === "info") return info(...flatContent);
|
|
17
|
+
if (level === "warn") return warn(...flatContent);
|
|
18
|
+
if (level === "error") return error(...flatContent);
|
|
19
|
+
log(...flatContent);
|
|
27
20
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
21
|
+
let ANSIColors = /* @__PURE__ */ function(ANSIColors$1) {
|
|
22
|
+
ANSIColors$1["RESET"] = "\x1B[0m";
|
|
23
|
+
ANSIColors$1["GREY"] = "\x1B[90m";
|
|
24
|
+
ANSIColors$1["GREY_DARK"] = "\x1B[38;5;239m";
|
|
25
|
+
ANSIColors$1["BLUE"] = "\x1B[34m";
|
|
26
|
+
ANSIColors$1["RED"] = "\x1B[31m";
|
|
27
|
+
ANSIColors$1["GREEN"] = "\x1B[32m";
|
|
28
|
+
ANSIColors$1["YELLOW"] = "\x1B[38;5;226m";
|
|
29
|
+
ANSIColors$1["MAGENTA"] = "\x1B[35m";
|
|
30
|
+
ANSIColors$1["BEIGE"] = "\x1B[38;5;3m";
|
|
31
|
+
ANSIColors$1["ORANGE"] = "\x1B[38;5;208m";
|
|
32
|
+
ANSIColors$1["CYAN"] = "\x1B[36m";
|
|
33
|
+
ANSIColors$1["WHITE"] = "\x1B[37m";
|
|
34
|
+
return ANSIColors$1;
|
|
35
|
+
}({});
|
|
36
|
+
const spinnerFrames = [
|
|
37
|
+
"⠋",
|
|
38
|
+
"⠙",
|
|
39
|
+
"⠹",
|
|
40
|
+
"⠸",
|
|
41
|
+
"⠼",
|
|
42
|
+
"⠴",
|
|
43
|
+
"⠦",
|
|
44
|
+
"⠧",
|
|
45
|
+
"⠇",
|
|
46
|
+
"⠏"
|
|
47
|
+
];
|
|
48
|
+
/**
|
|
49
|
+
* The appLogger function takes the logger and merges it with the configuration from the intlayer config file.
|
|
50
|
+
* It allows overriding the default configuration by passing a config object in the details parameter.
|
|
51
|
+
* The configuration is merged with the default configuration from the intlayer config file.
|
|
52
|
+
*/
|
|
43
53
|
const getAppLogger = (configuration, globalDetails) => (content, details) => logger(content, {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
54
|
+
...details ?? {},
|
|
55
|
+
config: {
|
|
56
|
+
...configuration?.log,
|
|
57
|
+
...globalDetails?.config,
|
|
58
|
+
...details?.config ?? {}
|
|
59
|
+
}
|
|
50
60
|
});
|
|
51
|
-
const colorize = (s, color, reset) => color ? `${color}${s}${reset ? typeof reset === "boolean" ?
|
|
52
|
-
const colorizeLocales = (locales, color =
|
|
53
|
-
const colorizeKey = (keyPath, color =
|
|
54
|
-
const colorizePath = (path, color =
|
|
61
|
+
const colorize = (s, color, reset) => color ? `${color}${s}${reset ? typeof reset === "boolean" ? ANSIColors.RESET : reset : ANSIColors.RESET}` : s;
|
|
62
|
+
const colorizeLocales = (locales, color = ANSIColors.GREEN, reset = ANSIColors.RESET) => [locales].flat().map((locale) => colorize(locale, color, reset)).join(`, `);
|
|
63
|
+
const colorizeKey = (keyPath, color = ANSIColors.BEIGE, reset = ANSIColors.RESET) => [keyPath].flat().map((key) => colorize(key, color, reset)).join(`, `);
|
|
64
|
+
const colorizePath = (path, color = ANSIColors.GREY, reset = ANSIColors.RESET) => [path].flat().map((p) => colorize(p, color, reset)).join(`, `);
|
|
65
|
+
/**
|
|
66
|
+
* Colorize numeric value using Intl.NumberFormat and optional ANSI colors.
|
|
67
|
+
*
|
|
68
|
+
* Examples:
|
|
69
|
+
* colorizeNumber(2, [{ pluralRule: 'one' , color: ANSIColors.GREEN}, { pluralRule: 'other' , color: ANSIColors.RED}]) // "'\x1b[31m2\x1b[0m"
|
|
70
|
+
*/
|
|
55
71
|
const colorizeNumber = (number, options = {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
72
|
+
zero: ANSIColors.BLUE,
|
|
73
|
+
one: ANSIColors.BLUE,
|
|
74
|
+
two: ANSIColors.BLUE,
|
|
75
|
+
few: ANSIColors.BLUE,
|
|
76
|
+
many: ANSIColors.BLUE,
|
|
77
|
+
other: ANSIColors.BLUE
|
|
62
78
|
}) => {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return colorize(number.toString(), color);
|
|
79
|
+
if (number === 0) {
|
|
80
|
+
const color$1 = options.zero ?? ANSIColors.GREEN;
|
|
81
|
+
return colorize(number.toString(), color$1);
|
|
82
|
+
}
|
|
83
|
+
const color = options[new Intl.PluralRules("en").select(number)];
|
|
84
|
+
return colorize(number.toString(), color);
|
|
70
85
|
};
|
|
71
86
|
const removeColor = (text) => text.replace(/\x1b\[[0-9;]*m/g, "");
|
|
72
87
|
const getLength = (length) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
if (Array.isArray(length) && length.every((l) => typeof l === "string")) {
|
|
81
|
-
value = Math.max(...length.map((str) => str.length));
|
|
82
|
-
}
|
|
83
|
-
if (Array.isArray(length) && length.every((l) => typeof l === "number")) {
|
|
84
|
-
value = Math.max(...length);
|
|
85
|
-
}
|
|
86
|
-
return Math.max(value, 0);
|
|
88
|
+
let value = 0;
|
|
89
|
+
if (typeof length === "number") value = length;
|
|
90
|
+
if (typeof length === "string") value = length.length;
|
|
91
|
+
if (Array.isArray(length) && length.every((l) => typeof l === "string")) value = Math.max(...length.map((str) => str.length));
|
|
92
|
+
if (Array.isArray(length) && length.every((l) => typeof l === "number")) value = Math.max(...length);
|
|
93
|
+
return Math.max(value, 0);
|
|
87
94
|
};
|
|
88
95
|
const defaultColonOptions = {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
96
|
+
colSize: 0,
|
|
97
|
+
minSize: 0,
|
|
98
|
+
maxSize: Infinity,
|
|
99
|
+
pad: "right",
|
|
100
|
+
padChar: "0"
|
|
94
101
|
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Create a string of spaces of a given length.
|
|
104
|
+
*
|
|
105
|
+
* @param colSize - The length of the string to create.
|
|
106
|
+
* @returns A string of spaces.
|
|
107
|
+
*/
|
|
108
|
+
const colon = (text, options) => [text].flat().map((text$1) => {
|
|
109
|
+
const { colSize, minSize, maxSize, pad, padChar } = {
|
|
110
|
+
...defaultColonOptions,
|
|
111
|
+
...options ?? {}
|
|
112
|
+
};
|
|
113
|
+
const length = getLength(colSize);
|
|
114
|
+
const spacesLength = Math.max(minSize, Math.min(maxSize, length - removeColor(text$1).length));
|
|
115
|
+
if (pad === "left") return `${" ".repeat(spacesLength)}${text$1}`;
|
|
116
|
+
return `${text$1}${" ".repeat(spacesLength)}`;
|
|
109
117
|
}).join("");
|
|
110
|
-
const x = colorize("
|
|
111
|
-
const v = colorize("
|
|
112
|
-
const clock = colorize("
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
colon,
|
|
117
|
-
colorize,
|
|
118
|
-
colorizeKey,
|
|
119
|
-
colorizeLocales,
|
|
120
|
-
colorizeNumber,
|
|
121
|
-
colorizePath,
|
|
122
|
-
getAppLogger,
|
|
123
|
-
logger,
|
|
124
|
-
removeColor,
|
|
125
|
-
spinnerFrames,
|
|
126
|
-
v,
|
|
127
|
-
x
|
|
128
|
-
};
|
|
118
|
+
const x = colorize("✗", ANSIColors.RED);
|
|
119
|
+
const v = colorize("✓", ANSIColors.GREEN);
|
|
120
|
+
const clock = colorize("⏲", ANSIColors.BLUE);
|
|
121
|
+
|
|
122
|
+
//#endregion
|
|
123
|
+
export { ANSIColors, clock, colon, colorize, colorizeKey, colorizeLocales, colorizeNumber, colorizePath, getAppLogger, logger, removeColor, spinnerFrames, v, x };
|
|
129
124
|
//# sourceMappingURL=logger.mjs.map
|
package/dist/esm/logger.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/logger.ts"],"sourcesContent":["import type { CustomIntlayerConfig } from '
|
|
1
|
+
{"version":3,"file":"logger.mjs","names":["logger: Logger","color","value: number","text"],"sources":["../../src/logger.ts"],"sourcesContent":["import type { CustomIntlayerConfig, Locale } from '@intlayer/types';\n\nexport type Details = {\n isVerbose?: boolean;\n level?: 'info' | 'warn' | 'error' | 'debug';\n config?: CustomIntlayerConfig['log'];\n};\n\nexport type Logger = (content: any, details?: Details) => void;\n\nexport const logger: Logger = (content, details) => {\n const isVerbose = details?.isVerbose ?? false;\n const mode = details?.config?.mode ?? 'default';\n const level = details?.level ?? 'info';\n const prefix = details?.config?.prefix ? details?.config?.prefix : undefined;\n const log = details?.config?.log ?? console.log;\n const info = details?.config?.info ?? console.info;\n const warn = details?.config?.warn ?? console.warn;\n const error = details?.config?.error ?? console.error;\n const debug = details?.config?.debug ?? console.debug;\n\n if (mode === 'disabled') return;\n\n if (isVerbose && mode !== 'verbose') return;\n\n const flatContent = prefix ? [prefix, ...[content].flat()] : [content].flat();\n\n if (level === 'debug') {\n return debug(...flatContent);\n }\n\n if (level === 'info') {\n return info(...flatContent);\n }\n\n if (level === 'warn') {\n return warn(...flatContent);\n }\n\n if (level === 'error') {\n return error(...flatContent);\n }\n\n log(...flatContent);\n};\n\nexport enum ANSIColors {\n RESET = '\\x1b[0m',\n GREY = '\\x1b[90m',\n GREY_DARK = '\\x1b[38;5;239m',\n BLUE = '\\x1b[34m',\n RED = '\\x1b[31m',\n GREEN = '\\x1b[32m',\n YELLOW = '\\x1b[38;5;226m',\n MAGENTA = '\\x1b[35m',\n BEIGE = '\\x1b[38;5;3m',\n ORANGE = '\\x1b[38;5;208m',\n CYAN = '\\x1b[36m',\n WHITE = '\\x1b[37m',\n}\n\nexport const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];\n\n/**\n * The appLogger function takes the logger and merges it with the configuration from the intlayer config file.\n * It allows overriding the default configuration by passing a config object in the details parameter.\n * The configuration is merged with the default configuration from the intlayer config file.\n */\nexport const getAppLogger =\n (configuration?: CustomIntlayerConfig, globalDetails?: Details) =>\n (content: any, details?: Details) =>\n logger(content, {\n ...(details ?? {}),\n config: {\n ...configuration?.log,\n ...globalDetails?.config,\n ...(details?.config ?? {}),\n },\n });\n\nexport const colorize = (\n s: string,\n color?: ANSIColors,\n reset?: boolean | ANSIColors\n): string =>\n color\n ? `${color}${s}${reset ? (typeof reset === 'boolean' ? ANSIColors.RESET : reset) : ANSIColors.RESET}`\n : s;\n\nexport const colorizeLocales = (\n locales: Locale | Locale[],\n color = ANSIColors.GREEN,\n reset: boolean | ANSIColors = ANSIColors.RESET\n) =>\n [locales]\n .flat()\n .map((locale) => colorize(locale, color, reset))\n .join(`, `);\n\nexport const colorizeKey = (\n keyPath: string | string[],\n color = ANSIColors.BEIGE,\n reset: boolean | ANSIColors = ANSIColors.RESET\n) =>\n [keyPath]\n .flat()\n .map((key) => colorize(key, color, reset))\n .join(`, `);\n\nexport const colorizePath = (\n path: string | string[],\n color = ANSIColors.GREY,\n reset: boolean | ANSIColors = ANSIColors.RESET\n) =>\n [path]\n .flat()\n .map((p) => colorize(p, color, reset))\n .join(`, `);\n\n/**\n * Colorize numeric value using Intl.NumberFormat and optional ANSI colors.\n *\n * Examples:\n * colorizeNumber(2, [{ pluralRule: 'one' , color: ANSIColors.GREEN}, { pluralRule: 'other' , color: ANSIColors.RED}]) // \"'\\x1b[31m2\\x1b[0m\"\n */\nexport const colorizeNumber = (\n number: number,\n options: Partial<Record<Intl.LDMLPluralRule, ANSIColors>> = {\n zero: ANSIColors.BLUE,\n one: ANSIColors.BLUE,\n two: ANSIColors.BLUE,\n few: ANSIColors.BLUE,\n many: ANSIColors.BLUE,\n other: ANSIColors.BLUE,\n }\n): string => {\n if (number === 0) {\n const color = options.zero ?? ANSIColors.GREEN;\n return colorize(number.toString(), color);\n }\n\n const rule = new Intl.PluralRules('en').select(number);\n const color = options[rule];\n return colorize(number.toString(), color);\n};\n\nexport const removeColor = (text: string) =>\n // biome-ignore lint/suspicious/noControlCharactersInRegex: we need to remove the color codes\n text.replace(/\\x1b\\[[0-9;]*m/g, '');\n\nconst getLength = (length: number | number[] | string | string[]): number => {\n let value: number = 0;\n if (typeof length === 'number') {\n value = length;\n }\n if (typeof length === 'string') {\n value = length.length;\n }\n if (Array.isArray(length) && length.every((l) => typeof l === 'string')) {\n value = Math.max(...length.map((str) => str.length));\n }\n if (Array.isArray(length) && length.every((l) => typeof l === 'number')) {\n value = Math.max(...length);\n }\n return Math.max(value, 0);\n};\n\nconst defaultColonOptions = {\n colSize: 0,\n minSize: 0,\n maxSize: Infinity,\n pad: 'right',\n padChar: '0',\n};\n\n/**\n * Create a string of spaces of a given length.\n *\n * @param colSize - The length of the string to create.\n * @returns A string of spaces.\n */\nexport const colon = (\n text: string | string[],\n options?: {\n colSize?: number | number[] | string | string[];\n minSize?: number;\n maxSize?: number;\n pad?: 'left' | 'right';\n padChar?: string;\n }\n): string =>\n [text]\n .flat()\n .map((text) => {\n const { colSize, minSize, maxSize, pad, padChar } = {\n ...defaultColonOptions,\n ...(options ?? {}),\n };\n\n const length = getLength(colSize);\n const spacesLength = Math.max(\n minSize!,\n Math.min(maxSize!, length - removeColor(text).length)\n );\n\n if (pad === 'left') {\n return `${' '.repeat(spacesLength)}${text}`;\n }\n\n return `${text}${' '.repeat(spacesLength)}`;\n })\n .join('');\n\nexport const x = colorize('✗', ANSIColors.RED);\nexport const v = colorize('✓', ANSIColors.GREEN);\nexport const clock = colorize('⏲', ANSIColors.BLUE);\n"],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"package.mjs","names":[],"sources":["../../package.json"],"sourcesContent":["{\n \"name\": \"@intlayer/config\",\n \"version\": \"7.0.0-canary.0\",\n \"private\": false,\n \"description\": \"Retrieve Intlayer configurations and manage environment variables for both server-side and client-side environments.\",\n \"keywords\": [\n \"intlayer\",\n \"layer\",\n \"abstraction\",\n \"data\",\n \"internationalization\",\n \"i18n\",\n \"typescript\",\n \"javascript\",\n \"json\",\n \"file\"\n ],\n \"homepage\": \"https://intlayer.org\",\n \"bugs\": {\n \"url\": \"https://github.com/aymericzip/intlayer/issues\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/aymericzip/intlayer.git\"\n },\n \"license\": \"Apache-2.0\",\n \"author\": {\n \"name\": \"Aymeric PINEAU\",\n \"url\": \"https://github.com/aymericzip\"\n },\n \"contributors\": [\n {\n \"name\": \"Aymeric Pineau\",\n \"email\": \"ay.pineau@gmail.com\",\n \"url\": \"https://github.com/aymericzip\"\n }\n ],\n \"sideEffects\": false,\n \"exports\": {\n \".\": {\n \"types\": \"./dist/types/index.d.ts\",\n \"browser\": \"./dist/esm/client.mjs\",\n \"require\": \"./dist/cjs/index.cjs\",\n \"import\": \"./dist/esm/index.mjs\"\n },\n \"./client\": {\n \"types\": \"./dist/types/client.d.ts\",\n \"require\": \"./dist/cjs/client.cjs\",\n \"import\": \"./dist/esm/client.mjs\"\n },\n \"./built\": {\n \"types\": \"./dist/types/built.d.ts\",\n \"require\": \"./dist/cjs/built.cjs\",\n \"import\": \"./dist/esm/built.mjs\"\n },\n \"./package.json\": \"./package.json\"\n },\n \"main\": \"dist/cjs/index.cjs\",\n \"module\": \"dist/esm/index.mjs\",\n \"types\": \"dist/types/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \".\": [\n \"./dist/types/index.d.ts\"\n ],\n \"client\": [\n \"./dist/types/client.d.ts\"\n ],\n \"built\": [\n \"./dist/types/built.d.ts\"\n ],\n \"package.json\": [\n \"./package.json\"\n ]\n }\n },\n \"files\": [\n \"./dist\",\n \"./package.json\"\n ],\n \"scripts\": {\n \"build\": \"tsdown --config tsdown.config.ts\",\n \"build:ci\": \"tsdown --config tsdown.config.ts\",\n \"clean\": \"rimraf ./dist .turbo\",\n \"dev\": \"tsdown --config tsdown.config.ts --watch\",\n \"format\": \"biome format . --check\",\n \"format:fix\": \"biome format --write .\",\n \"lint\": \"biome lint .\",\n \"lint:fix\": \"biome lint --write .\",\n \"prepublish\": \"cp -f ../../../README.md ./README.md\",\n \"test\": \"vitest run\",\n \"test:watch\": \"vitest\",\n \"typecheck\": \"tsc --noEmit --project tsconfig.types.json\"\n },\n \"dependencies\": {\n \"@intlayer/types\": \"workspace:*\",\n \"deepmerge\": \"4.3.1\",\n \"dotenv\": \"16.6.1\",\n \"esbuild\": \"0.25.2\"\n },\n \"devDependencies\": {\n \"@types/node\": \"24.9.1\",\n \"@utils/ts-config\": \"workspace:*\",\n \"@utils/ts-config-types\": \"workspace:*\",\n \"@utils/tsdown-config\": \"workspace:*\",\n \"rimraf\": \"6.0.1\",\n \"tsdown\": \"0.15.9\",\n \"typescript\": \"5.9.3\",\n \"vitest\": \"4.0.3\"\n },\n \"peerDependencies\": {\n \"intlayer\": \"workspace:*\",\n \"react\": \">=16.0.0\"\n },\n \"peerDependenciesMeta\": {\n \"intlayer\": {\n \"optional\": true\n },\n \"react\": {\n \"optional\": true\n }\n },\n \"engines\": {\n \"node\": \">=14.18\"\n },\n \"bug\": {\n \"url\": \"https://github.com/aymericzip/intlayer/issues\"\n }\n}\n"],"mappings":";cAEa"}
|
|
@@ -1,28 +1,45 @@
|
|
|
1
1
|
import { logger } from "./logger.mjs";
|
|
2
|
+
import { extractErrorMessage } from "./utils/extractErrorMessage.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/retryManager.ts
|
|
2
5
|
const DEFAULT_MAX_RETRY = 3;
|
|
3
6
|
const DEFAULT_DELAY = 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Wrap an async function `fn` so it’s retried on failure.
|
|
9
|
+
*
|
|
10
|
+
* @param fn The async function to wrap
|
|
11
|
+
* @param options { maxRetry, delay }
|
|
12
|
+
* @returns A new function with the same signature as `fn`
|
|
13
|
+
*/
|
|
14
|
+
const retryManager = (fn, { maxRetry = DEFAULT_MAX_RETRY, delay = DEFAULT_DELAY, onError, onMaxTryReached } = {}) => async (...args) => {
|
|
15
|
+
let lastError;
|
|
16
|
+
for (let attempt = 0; attempt <= maxRetry; attempt++) try {
|
|
17
|
+
return await fn(...args);
|
|
18
|
+
} catch (err) {
|
|
19
|
+
lastError = err;
|
|
20
|
+
const error = extractErrorMessage(err);
|
|
21
|
+
if (attempt >= maxRetry) {
|
|
22
|
+
if (onMaxTryReached) {
|
|
23
|
+
onMaxTryReached?.({
|
|
24
|
+
error,
|
|
25
|
+
attempt,
|
|
26
|
+
maxRetry
|
|
27
|
+
});
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
throw err;
|
|
31
|
+
}
|
|
32
|
+
if (onError) onError?.({
|
|
33
|
+
error,
|
|
34
|
+
attempt,
|
|
35
|
+
maxRetry
|
|
36
|
+
});
|
|
37
|
+
else logger(error, { level: "error" });
|
|
38
|
+
if (delay > 0) await new Promise((res) => setTimeout(res, delay));
|
|
39
|
+
}
|
|
40
|
+
throw lastError ?? /* @__PURE__ */ new Error("Unexpected: retry loop completed without result");
|
|
27
41
|
};
|
|
42
|
+
|
|
43
|
+
//#endregion
|
|
44
|
+
export { retryManager };
|
|
28
45
|
//# sourceMappingURL=retryManager.mjs.map
|