@intlayer/config 1.0.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/LICENSE +21 -0
- package/dist/cjs/client.cjs +48 -0
- package/dist/cjs/client.cjs.map +1 -0
- package/dist/cjs/client.d.ts +16 -0
- package/dist/cjs/configFile/buildConfigurationFields.cjs +182 -0
- package/dist/cjs/configFile/buildConfigurationFields.cjs.map +1 -0
- package/dist/cjs/configFile/buildConfigurationFields.d.ts +10 -0
- package/dist/cjs/configFile/getConfiguration.cjs +90 -0
- package/dist/cjs/configFile/getConfiguration.cjs.map +1 -0
- package/dist/cjs/configFile/getConfiguration.d.ts +12 -0
- package/dist/cjs/configFile/index.cjs +35 -0
- package/dist/cjs/configFile/index.cjs.map +1 -0
- package/dist/cjs/configFile/index.d.ts +6 -0
- package/dist/cjs/configFile/loadConfigurationFile.cjs +110 -0
- package/dist/cjs/configFile/loadConfigurationFile.cjs.map +1 -0
- package/dist/cjs/configFile/loadConfigurationFile.d.ts +8 -0
- package/dist/cjs/configFile/searchConfigurationFile.cjs +63 -0
- package/dist/cjs/configFile/searchConfigurationFile.cjs.map +1 -0
- package/dist/cjs/configFile/searchConfigurationFile.d.ts +9 -0
- package/dist/cjs/defaultValues/internationalization.cjs +42 -0
- package/dist/cjs/defaultValues/internationalization.cjs.map +1 -0
- package/dist/cjs/defaultValues/internationalization.d.ts +6 -0
- package/dist/cjs/defaultValues/locales.cjs +266 -0
- package/dist/cjs/defaultValues/locales.cjs.map +1 -0
- package/dist/cjs/defaultValues/locales.d.ts +234 -0
- package/dist/cjs/defaultValues/middleware.cjs +49 -0
- package/dist/cjs/defaultValues/middleware.cjs.map +1 -0
- package/dist/cjs/defaultValues/middleware.d.ts +15 -0
- package/dist/cjs/defaultValues/server.cjs +61 -0
- package/dist/cjs/defaultValues/server.cjs.map +1 -0
- package/dist/cjs/defaultValues/server.d.ts +23 -0
- package/dist/cjs/envVariables/formatEnvVariable.cjs +52 -0
- package/dist/cjs/envVariables/formatEnvVariable.cjs.map +1 -0
- package/dist/cjs/envVariables/formatEnvVariable.d.ts +3 -0
- package/dist/cjs/envVariables/getConfiguration.cjs +170 -0
- package/dist/cjs/envVariables/getConfiguration.cjs.map +1 -0
- package/dist/cjs/envVariables/getConfiguration.d.ts +21 -0
- package/dist/cjs/envVariables/index.cjs +48 -0
- package/dist/cjs/envVariables/index.cjs.map +1 -0
- package/dist/cjs/envVariables/index.d.ts +10 -0
- package/dist/cjs/envVariables/utils.cjs +56 -0
- package/dist/cjs/envVariables/utils.cjs.map +1 -0
- package/dist/cjs/envVariables/utils.d.ts +16 -0
- package/dist/cjs/index.cjs +53 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/types/index.cjs +21 -0
- package/dist/cjs/types/index.cjs.map +1 -0
- package/dist/cjs/types/index.d.ts +89 -0
- package/dist/esm/client.d.mts +16 -0
- package/dist/esm/client.mjs +17 -0
- package/dist/esm/client.mjs.map +1 -0
- package/dist/esm/configFile/buildConfigurationFields.d.mts +10 -0
- package/dist/esm/configFile/buildConfigurationFields.mjs +159 -0
- package/dist/esm/configFile/buildConfigurationFields.mjs.map +1 -0
- package/dist/esm/configFile/getConfiguration.d.mts +12 -0
- package/dist/esm/configFile/getConfiguration.mjs +52 -0
- package/dist/esm/configFile/getConfiguration.mjs.map +1 -0
- package/dist/esm/configFile/index.d.mts +6 -0
- package/dist/esm/configFile/index.mjs +2 -0
- package/dist/esm/configFile/index.mjs.map +1 -0
- package/dist/esm/configFile/loadConfigurationFile.d.mts +8 -0
- package/dist/esm/configFile/loadConfigurationFile.mjs +76 -0
- package/dist/esm/configFile/loadConfigurationFile.mjs.map +1 -0
- package/dist/esm/configFile/searchConfigurationFile.d.mts +9 -0
- package/dist/esm/configFile/searchConfigurationFile.mjs +32 -0
- package/dist/esm/configFile/searchConfigurationFile.mjs.map +1 -0
- package/dist/esm/defaultValues/internationalization.d.mts +6 -0
- package/dist/esm/defaultValues/internationalization.mjs +5 -0
- package/dist/esm/defaultValues/internationalization.mjs.map +1 -0
- package/dist/esm/defaultValues/locales.d.mts +234 -0
- package/dist/esm/defaultValues/locales.mjs +235 -0
- package/dist/esm/defaultValues/locales.mjs.map +1 -0
- package/dist/esm/defaultValues/middleware.d.mts +15 -0
- package/dist/esm/defaultValues/middleware.mjs +15 -0
- package/dist/esm/defaultValues/middleware.mjs.map +1 -0
- package/dist/esm/defaultValues/server.d.mts +23 -0
- package/dist/esm/defaultValues/server.mjs +23 -0
- package/dist/esm/defaultValues/server.mjs.map +1 -0
- package/dist/esm/envVariables/formatEnvVariable.d.mts +3 -0
- package/dist/esm/envVariables/formatEnvVariable.mjs +21 -0
- package/dist/esm/envVariables/formatEnvVariable.mjs.map +1 -0
- package/dist/esm/envVariables/getConfiguration.d.mts +21 -0
- package/dist/esm/envVariables/getConfiguration.mjs +110 -0
- package/dist/esm/envVariables/getConfiguration.mjs.map +1 -0
- package/dist/esm/envVariables/index.d.mts +10 -0
- package/dist/esm/envVariables/index.mjs +17 -0
- package/dist/esm/envVariables/index.mjs.map +1 -0
- package/dist/esm/envVariables/utils.d.mts +16 -0
- package/dist/esm/envVariables/utils.mjs +25 -0
- package/dist/esm/envVariables/utils.mjs.map +1 -0
- package/dist/esm/index.d.mts +22 -0
- package/dist/esm/index.mjs +21 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/types/index.d.mts +89 -0
- package/dist/esm/types/index.mjs +1 -0
- package/dist/esm/types/index.mjs.map +1 -0
- package/package.json +88 -0
- package/src/client.ts +16 -0
- package/src/configFile/buildConfigurationFields.ts +214 -0
- package/src/configFile/getConfiguration.ts +75 -0
- package/src/configFile/index.ts +1 -0
- package/src/configFile/loadConfigurationFile.ts +97 -0
- package/src/configFile/searchConfigurationFile.ts +48 -0
- package/src/defaultValues/internationalization.ts +9 -0
- package/src/defaultValues/locales.ts +463 -0
- package/src/defaultValues/middleware.ts +11 -0
- package/src/defaultValues/server.ts +18 -0
- package/src/envVariables/formatEnvVariable.ts +32 -0
- package/src/envVariables/getConfiguration.ts +121 -0
- package/src/envVariables/index.ts +8 -0
- package/src/envVariables/utils.ts +47 -0
- package/src/index.ts +23 -0
- package/src/types/index.ts +144 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { join } from 'path';
|
|
2
|
+
import { DEFAULT_LOCALE, LOCALES } from '../defaultValues/internationalization';
|
|
3
|
+
import {
|
|
4
|
+
BASE_PATH,
|
|
5
|
+
COOKIE_NAME,
|
|
6
|
+
HEADER_NAME,
|
|
7
|
+
NO_PREFIX,
|
|
8
|
+
PREFIX_DEFAULT,
|
|
9
|
+
SERVER_SET_COOKIE,
|
|
10
|
+
} from '../defaultValues/middleware';
|
|
11
|
+
import {
|
|
12
|
+
BUNDLE_DIR_NAME,
|
|
13
|
+
CONTENT_DIR_NAME,
|
|
14
|
+
DICTIONARIES_DIR_NAME,
|
|
15
|
+
FILE_EXTENSIONS,
|
|
16
|
+
RESULT_DIR_NAME,
|
|
17
|
+
EXCLUDED_PATHS,
|
|
18
|
+
BUNDLE_FILE_EXTENSION,
|
|
19
|
+
TYPES_DIR_NAME,
|
|
20
|
+
MAIN_DIR_NAME,
|
|
21
|
+
MODULE_AUGMENTATION_DIR_NAME,
|
|
22
|
+
} from '../defaultValues/server';
|
|
23
|
+
import type {
|
|
24
|
+
BaseDerivedConfig,
|
|
25
|
+
ContentConfig,
|
|
26
|
+
CustomIntlayerConfig,
|
|
27
|
+
PatternsContentConfig,
|
|
28
|
+
InternationalizationConfig,
|
|
29
|
+
IntlayerConfig,
|
|
30
|
+
MiddlewareConfig,
|
|
31
|
+
BaseContentConfig,
|
|
32
|
+
ResultDirDerivedConfig,
|
|
33
|
+
} from '../types';
|
|
34
|
+
import type { GetConfigurationOptions } from './getConfiguration';
|
|
35
|
+
|
|
36
|
+
let storedConfiguration: IntlayerConfig;
|
|
37
|
+
|
|
38
|
+
const buildInternationalizationFields = (
|
|
39
|
+
customConfiguration?: Partial<InternationalizationConfig>
|
|
40
|
+
): InternationalizationConfig => ({
|
|
41
|
+
/**
|
|
42
|
+
* Internationalization configuration
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
// Locales available in the application
|
|
46
|
+
locales: customConfiguration?.locales ?? LOCALES,
|
|
47
|
+
|
|
48
|
+
// Default locale of the application for fallback
|
|
49
|
+
defaultLocale: customConfiguration?.defaultLocale ?? DEFAULT_LOCALE,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const buildMiddlewareFields = (
|
|
53
|
+
customConfiguration?: Partial<MiddlewareConfig>
|
|
54
|
+
): MiddlewareConfig => ({
|
|
55
|
+
/**
|
|
56
|
+
* Middleware configuration
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
// Header name to get the locale
|
|
60
|
+
headerName: customConfiguration?.headerName ?? HEADER_NAME,
|
|
61
|
+
|
|
62
|
+
// Cookie name to get the locale
|
|
63
|
+
cookieName: customConfiguration?.cookieName ?? COOKIE_NAME,
|
|
64
|
+
|
|
65
|
+
// Prefix the default locale in the URL
|
|
66
|
+
prefixDefault: customConfiguration?.prefixDefault ?? PREFIX_DEFAULT,
|
|
67
|
+
|
|
68
|
+
// Base path
|
|
69
|
+
basePath: customConfiguration?.basePath ?? BASE_PATH,
|
|
70
|
+
|
|
71
|
+
// Set cookie on server
|
|
72
|
+
serverSetCookie: customConfiguration?.serverSetCookie ?? SERVER_SET_COOKIE,
|
|
73
|
+
|
|
74
|
+
// No prefix
|
|
75
|
+
noPrefix: customConfiguration?.noPrefix ?? NO_PREFIX,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const buildContentFields = (
|
|
79
|
+
customConfiguration?: Partial<ContentConfig>
|
|
80
|
+
): ContentConfig => {
|
|
81
|
+
const notDerivedContentConfig: BaseContentConfig = {
|
|
82
|
+
/**
|
|
83
|
+
* Content configurations
|
|
84
|
+
*/
|
|
85
|
+
|
|
86
|
+
// File extensions of content to look for
|
|
87
|
+
fileExtensions: customConfiguration?.fileExtensions ?? FILE_EXTENSIONS,
|
|
88
|
+
|
|
89
|
+
// Directory name of the project
|
|
90
|
+
baseDir: customConfiguration?.baseDir ?? process.cwd(),
|
|
91
|
+
|
|
92
|
+
// Directory name where the content is stored
|
|
93
|
+
contentDirName: customConfiguration?.contentDirName ?? CONTENT_DIR_NAME,
|
|
94
|
+
|
|
95
|
+
// Result directory name
|
|
96
|
+
resultDirName: customConfiguration?.resultDirName ?? RESULT_DIR_NAME,
|
|
97
|
+
|
|
98
|
+
// Module augmentation directory name
|
|
99
|
+
moduleAugmentationDirName:
|
|
100
|
+
customConfiguration?.moduleAugmentationDirName ??
|
|
101
|
+
MODULE_AUGMENTATION_DIR_NAME,
|
|
102
|
+
|
|
103
|
+
// Bundle directory name
|
|
104
|
+
bundleDirName: customConfiguration?.bundleDirName ?? BUNDLE_DIR_NAME,
|
|
105
|
+
|
|
106
|
+
// Bundle file extension
|
|
107
|
+
bundleFileExtension:
|
|
108
|
+
customConfiguration?.bundleFileExtension ?? BUNDLE_FILE_EXTENSION,
|
|
109
|
+
|
|
110
|
+
// Dictionary directory name
|
|
111
|
+
dictionariesDirName:
|
|
112
|
+
customConfiguration?.dictionariesDirName ?? DICTIONARIES_DIR_NAME,
|
|
113
|
+
|
|
114
|
+
// Types directory name
|
|
115
|
+
typeDirName: customConfiguration?.typeDirName ?? TYPES_DIR_NAME,
|
|
116
|
+
|
|
117
|
+
// Main directory name
|
|
118
|
+
mainDirName: customConfiguration?.mainDirName ?? MAIN_DIR_NAME,
|
|
119
|
+
|
|
120
|
+
// Directories to exclude
|
|
121
|
+
excludedPath: customConfiguration?.excludedPath ?? EXCLUDED_PATHS,
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const baseDirDerivedConfiguration: BaseDerivedConfig = {
|
|
125
|
+
// Directory where the content is stored
|
|
126
|
+
contentDir: join(
|
|
127
|
+
notDerivedContentConfig.baseDir,
|
|
128
|
+
notDerivedContentConfig.contentDirName
|
|
129
|
+
),
|
|
130
|
+
|
|
131
|
+
// Directory where the result will be stored
|
|
132
|
+
resultDir: join(
|
|
133
|
+
notDerivedContentConfig.baseDir,
|
|
134
|
+
notDerivedContentConfig.resultDirName
|
|
135
|
+
),
|
|
136
|
+
|
|
137
|
+
// Directory where the module augmentation will be stored
|
|
138
|
+
moduleAugmentationDir: join(
|
|
139
|
+
notDerivedContentConfig.baseDir,
|
|
140
|
+
notDerivedContentConfig.moduleAugmentationDirName
|
|
141
|
+
),
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const resultDirDerivedConfiguration: ResultDirDerivedConfig = {
|
|
145
|
+
// Directory where the bundle will be stored
|
|
146
|
+
bundleDir: join(
|
|
147
|
+
baseDirDerivedConfiguration.resultDir,
|
|
148
|
+
notDerivedContentConfig.bundleDirName
|
|
149
|
+
),
|
|
150
|
+
|
|
151
|
+
// Directory where the dictionaries will be stored
|
|
152
|
+
dictionariesDir: join(
|
|
153
|
+
baseDirDerivedConfiguration.resultDir,
|
|
154
|
+
notDerivedContentConfig.dictionariesDirName
|
|
155
|
+
),
|
|
156
|
+
|
|
157
|
+
// Directory where the types will be stored
|
|
158
|
+
typesDir: join(
|
|
159
|
+
baseDirDerivedConfiguration.resultDir,
|
|
160
|
+
notDerivedContentConfig.typeDirName
|
|
161
|
+
),
|
|
162
|
+
|
|
163
|
+
// Directory where the main files will be stored
|
|
164
|
+
mainDir: join(
|
|
165
|
+
baseDirDerivedConfiguration.resultDir,
|
|
166
|
+
notDerivedContentConfig.mainDirName
|
|
167
|
+
),
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const patternsConfiguration: PatternsContentConfig = {
|
|
171
|
+
// Pattern of files to watch
|
|
172
|
+
watchedFilesPattern: notDerivedContentConfig.fileExtensions.map(
|
|
173
|
+
(ext) => `/**/*${ext}`
|
|
174
|
+
),
|
|
175
|
+
|
|
176
|
+
// Pattern of files to watch including the relative path
|
|
177
|
+
watchedFilesPatternWithPath: notDerivedContentConfig.fileExtensions.map(
|
|
178
|
+
(ext) => `${baseDirDerivedConfiguration.contentDir}/**/*${ext}`
|
|
179
|
+
),
|
|
180
|
+
|
|
181
|
+
// Pattern of files to output
|
|
182
|
+
outputFilesPatternWithPath: `${resultDirDerivedConfiguration.dictionariesDir}/**/*.json`,
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
...notDerivedContentConfig,
|
|
187
|
+
...baseDirDerivedConfiguration,
|
|
188
|
+
...resultDirDerivedConfiguration,
|
|
189
|
+
...patternsConfiguration,
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const buildConfigurationFields = (
|
|
194
|
+
options: GetConfigurationOptions,
|
|
195
|
+
customConfiguration?: CustomIntlayerConfig
|
|
196
|
+
): IntlayerConfig => {
|
|
197
|
+
const internationalizationConfig = buildInternationalizationFields(
|
|
198
|
+
customConfiguration?.internationalization
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
const middlewareConfig = buildMiddlewareFields(
|
|
202
|
+
customConfiguration?.middleware
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
const contentConfig = buildContentFields(customConfiguration?.content);
|
|
206
|
+
|
|
207
|
+
storedConfiguration = {
|
|
208
|
+
internationalization: internationalizationConfig,
|
|
209
|
+
middleware: middlewareConfig,
|
|
210
|
+
content: contentConfig,
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
return storedConfiguration;
|
|
214
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { relative } from 'path';
|
|
2
|
+
import type { CustomIntlayerConfig, IntlayerConfig } from '../types';
|
|
3
|
+
import { buildConfigurationFields } from './buildConfigurationFields';
|
|
4
|
+
import { loadConfigurationFile } from './loadConfigurationFile';
|
|
5
|
+
import { searchConfigurationFile } from './searchConfigurationFile';
|
|
6
|
+
|
|
7
|
+
let storedConfiguration: IntlayerConfig | undefined;
|
|
8
|
+
let storedConfigurationFilePath: string | undefined;
|
|
9
|
+
let storedNumCustomConfiguration: number | undefined;
|
|
10
|
+
|
|
11
|
+
export type GetConfigurationOptions = {
|
|
12
|
+
baseDir: string;
|
|
13
|
+
verbose: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const BASE_DIR_PATH = process.env.INTLAYER_BASE_DIR_PATH ?? process.cwd();
|
|
17
|
+
const defaultOptions: GetConfigurationOptions = {
|
|
18
|
+
baseDir: BASE_DIR_PATH,
|
|
19
|
+
verbose: false,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const getConfiguration = (
|
|
23
|
+
options?: Partial<GetConfigurationOptions>
|
|
24
|
+
): IntlayerConfig => {
|
|
25
|
+
const mergedOptions = { ...defaultOptions, ...options };
|
|
26
|
+
const { baseDir, verbose } = mergedOptions;
|
|
27
|
+
|
|
28
|
+
if (!storedConfiguration) {
|
|
29
|
+
// Search for configuration files
|
|
30
|
+
const { configurationFilePath, numCustomConfiguration } =
|
|
31
|
+
searchConfigurationFile(baseDir);
|
|
32
|
+
|
|
33
|
+
// Load the custom configuration
|
|
34
|
+
let customConfiguration: CustomIntlayerConfig | undefined;
|
|
35
|
+
if (configurationFilePath) {
|
|
36
|
+
customConfiguration = loadConfigurationFile(configurationFilePath);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Save the configuration to avoid reading the file again
|
|
40
|
+
storedConfiguration = buildConfigurationFields(
|
|
41
|
+
mergedOptions,
|
|
42
|
+
customConfiguration
|
|
43
|
+
);
|
|
44
|
+
storedConfigurationFilePath = configurationFilePath;
|
|
45
|
+
storedNumCustomConfiguration = numCustomConfiguration;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Log warning if multiple configuration files are found
|
|
49
|
+
if (verbose)
|
|
50
|
+
logConfigFileResult(
|
|
51
|
+
storedNumCustomConfiguration,
|
|
52
|
+
storedConfigurationFilePath
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return storedConfiguration;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const logConfigFileResult = (
|
|
59
|
+
numCustomConfiguration?: number,
|
|
60
|
+
configurationFilePath?: string
|
|
61
|
+
) => {
|
|
62
|
+
if (numCustomConfiguration === 0) {
|
|
63
|
+
console.info('Configuration file not found, using default configuration.');
|
|
64
|
+
} else {
|
|
65
|
+
const relativeOutputPath = relative(BASE_DIR_PATH, configurationFilePath!);
|
|
66
|
+
|
|
67
|
+
if (numCustomConfiguration === 1) {
|
|
68
|
+
console.info(`Configuration file found: ${relativeOutputPath}.`);
|
|
69
|
+
} else {
|
|
70
|
+
console.warn(
|
|
71
|
+
`Multiple configuration files found, using ${relativeOutputPath}.`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './getConfiguration';
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
import { createRequire } from 'module';
|
|
3
|
+
import { type Context, runInNewContext } from 'vm';
|
|
4
|
+
import { type BuildOptions, buildSync, type BuildResult } from 'esbuild';
|
|
5
|
+
import type { CustomIntlayerConfig } from '../types';
|
|
6
|
+
|
|
7
|
+
const isESModule = typeof import.meta.url === 'string';
|
|
8
|
+
|
|
9
|
+
const sandboxContext: Context = {
|
|
10
|
+
exports: {
|
|
11
|
+
default: {},
|
|
12
|
+
},
|
|
13
|
+
module: {
|
|
14
|
+
exports: {},
|
|
15
|
+
},
|
|
16
|
+
console,
|
|
17
|
+
require: isESModule ? createRequire(import.meta.url) : require,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const transformationOption: BuildOptions = {
|
|
21
|
+
loader: {
|
|
22
|
+
'.js': 'js',
|
|
23
|
+
'.jsx': 'jsx',
|
|
24
|
+
'.mjs': 'js',
|
|
25
|
+
'.ts': 'ts',
|
|
26
|
+
'.tsx': 'tsx',
|
|
27
|
+
'.cjs': 'js',
|
|
28
|
+
'.json': 'json',
|
|
29
|
+
},
|
|
30
|
+
format: 'cjs', // Output format as commonjs
|
|
31
|
+
target: 'es2017',
|
|
32
|
+
packages: 'external',
|
|
33
|
+
write: false,
|
|
34
|
+
bundle: true,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export const loadConfigurationFile = (
|
|
38
|
+
configFilePath: string
|
|
39
|
+
): CustomIntlayerConfig | undefined => {
|
|
40
|
+
let customConfiguration: CustomIntlayerConfig | undefined = undefined;
|
|
41
|
+
|
|
42
|
+
const configFileExtension = configFilePath.split('.').pop() ?? '';
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
if (configFileExtension === 'json') {
|
|
46
|
+
// Assume JSON
|
|
47
|
+
return require(configFilePath);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Rest is JS, MJS or TS
|
|
51
|
+
|
|
52
|
+
const moduleResult: BuildResult = buildSync({
|
|
53
|
+
entryPoints: [configFilePath],
|
|
54
|
+
|
|
55
|
+
...transformationOption,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const moduleResultString = moduleResult.outputFiles?.[0].text;
|
|
59
|
+
|
|
60
|
+
if (!moduleResultString) {
|
|
61
|
+
console.error('Configuration file could not be loaded.');
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
runInNewContext(moduleResultString, sandboxContext);
|
|
66
|
+
|
|
67
|
+
if (
|
|
68
|
+
sandboxContext.exports.default &&
|
|
69
|
+
Object.keys(sandboxContext.exports.default).length > 0
|
|
70
|
+
) {
|
|
71
|
+
// ES Module
|
|
72
|
+
customConfiguration = sandboxContext.exports.default;
|
|
73
|
+
} else if (
|
|
74
|
+
sandboxContext.module.exports.defaults &&
|
|
75
|
+
Object.keys(sandboxContext.module.exports.defaults).length > 0
|
|
76
|
+
) {
|
|
77
|
+
// CommonJS
|
|
78
|
+
customConfiguration = sandboxContext.module.exports.default;
|
|
79
|
+
} else if (
|
|
80
|
+
sandboxContext.module.exports.default &&
|
|
81
|
+
Object.keys(sandboxContext.module.exports.default).length > 0
|
|
82
|
+
) {
|
|
83
|
+
// ES Module
|
|
84
|
+
customConfiguration = sandboxContext.module.exports.default;
|
|
85
|
+
} else if (
|
|
86
|
+
sandboxContext.module.exports &&
|
|
87
|
+
Object.keys(sandboxContext.module.exports).length > 0
|
|
88
|
+
) {
|
|
89
|
+
// Other
|
|
90
|
+
customConfiguration = sandboxContext.module.exports;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return customConfiguration;
|
|
94
|
+
} catch (error) {
|
|
95
|
+
console.error('Error:', error);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
|
|
4
|
+
const EXTENSION = ['ts', 'js', 'json', 'cjs', 'mjs', ''];
|
|
5
|
+
const CONFIGURATION_FILE_NAME_1 = 'intlayer.config';
|
|
6
|
+
const CONFIGURATION_FILE_NAME_2 = '.intlayerrc';
|
|
7
|
+
|
|
8
|
+
const intLayerConfigFiles = EXTENSION.filter(
|
|
9
|
+
(extension) => extension !== ''
|
|
10
|
+
).map((ext) => `${CONFIGURATION_FILE_NAME_1}.${ext}`);
|
|
11
|
+
|
|
12
|
+
const configurationFiles = [...intLayerConfigFiles, CONFIGURATION_FILE_NAME_2];
|
|
13
|
+
|
|
14
|
+
type SearchConfigurationFileResult = {
|
|
15
|
+
configurationFilePath?: string;
|
|
16
|
+
numCustomConfiguration: number;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const searchConfigurationFile = (
|
|
20
|
+
configFilePath: string
|
|
21
|
+
): SearchConfigurationFileResult => {
|
|
22
|
+
let configurationFilePath: string | undefined = undefined;
|
|
23
|
+
let numCustomConfiguration = 0;
|
|
24
|
+
|
|
25
|
+
for (const fileName of configurationFiles) {
|
|
26
|
+
try {
|
|
27
|
+
const filePath = resolve(configFilePath, fileName);
|
|
28
|
+
|
|
29
|
+
// Check if the file exists
|
|
30
|
+
if (!existsSync(filePath)) {
|
|
31
|
+
continue;
|
|
32
|
+
} else {
|
|
33
|
+
numCustomConfiguration += 1;
|
|
34
|
+
|
|
35
|
+
if (!configurationFilePath) {
|
|
36
|
+
configurationFilePath = filePath;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} catch (error) {
|
|
40
|
+
// Return "Cannot use import statement outside a module"
|
|
41
|
+
console.error(`${fileName}: ${error as string}`);
|
|
42
|
+
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { configurationFilePath, numCustomConfiguration };
|
|
48
|
+
};
|