@ttoss/read-config-file 1.0.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/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@ttoss/read-config-file",
3
+ "version": "1.0.1",
4
+ "description": "Read a configuration file",
5
+ "author": "ttoss",
6
+ "contributors": [
7
+ "Pedro Arantes <pedro@arantespp.com> (https://arantespp.com/contact)"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/ttoss/ttoss.git",
12
+ "directory": "packages/read-config-file"
13
+ },
14
+ "type": "module",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./dist/esm/index.js",
18
+ "types": "./dist/index.d.ts"
19
+ }
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "src"
24
+ ],
25
+ "dependencies": {
26
+ "esbuild": "^0.20.2",
27
+ "import-sync": "^2.2.2",
28
+ "js-yaml": "^4.1.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/jest": "^29.5.12",
32
+ "@types/js-yaml": "^4.0.9",
33
+ "@types/react": "^18.2.79",
34
+ "jest": "^29.7.0",
35
+ "tsup": "^8.0.2",
36
+ "@ttoss/config": "^1.32.3",
37
+ "@ttoss/read-config-file-test": "^1.0.1"
38
+ },
39
+ "keywords": [
40
+ "config",
41
+ "read-config-file"
42
+ ],
43
+ "publishConfig": {
44
+ "access": "public",
45
+ "provenance": true
46
+ },
47
+ "scripts": {
48
+ "build": "tsup",
49
+ "test": "jest"
50
+ }
51
+ }
package/src/index.ts ADDED
@@ -0,0 +1,54 @@
1
+ import { loadConfig } from './loadConfig';
2
+ import fs from 'fs';
3
+ import yaml from 'js-yaml';
4
+
5
+ type ConfigInput = {
6
+ configFilePath: string;
7
+ };
8
+
9
+ export const readConfigFileSync = <ConfigFile = unknown>({
10
+ configFilePath,
11
+ }: ConfigInput): ConfigFile => {
12
+ const extension = configFilePath.split('.').pop();
13
+
14
+ if (extension === 'yaml' || extension === 'yml') {
15
+ const file = fs.readFileSync(configFilePath, 'utf8');
16
+ return yaml.load(file) as ConfigFile;
17
+ }
18
+
19
+ if (extension === 'json') {
20
+ const file = fs.readFileSync(configFilePath, 'utf8');
21
+ return JSON.parse(file);
22
+ }
23
+
24
+ if (extension === 'js') {
25
+ return require(configFilePath);
26
+ }
27
+
28
+ if (extension === 'ts') {
29
+ let result = loadConfig(configFilePath);
30
+ if (typeof result === 'function') {
31
+ result = result();
32
+ }
33
+ return result as ConfigFile;
34
+ }
35
+
36
+ throw new Error('Unsupported config file extension: ' + extension);
37
+ };
38
+
39
+ export const readConfigFile = async <ConfigFile = unknown>({
40
+ configFilePath,
41
+ }: ConfigInput): Promise<ConfigFile> => {
42
+ const extension = configFilePath.split('.').pop();
43
+
44
+ if (extension === 'ts') {
45
+ let result = loadConfig(configFilePath);
46
+ if (typeof result === 'function') {
47
+ result = result();
48
+ }
49
+ result = await Promise.resolve(result);
50
+ return result as ConfigFile;
51
+ }
52
+
53
+ return readConfigFileSync({ configFilePath });
54
+ };
@@ -0,0 +1,46 @@
1
+ import * as esbuild from 'esbuild';
2
+ import { typescriptConfig } from '@ttoss/config';
3
+ import importSync from 'import-sync';
4
+ import path from 'node:path';
5
+
6
+ export const loadConfig = <T>(entryPoint: string): T | undefined => {
7
+ const lastEntryPointName = entryPoint.split('/').pop();
8
+
9
+ const filename = lastEntryPointName?.split('.')[0] as string;
10
+
11
+ const outfile = path.resolve(process.cwd(), 'out', filename + '.js');
12
+
13
+ const projectPackageJsonPath = path.resolve(process.cwd(), 'package.json');
14
+
15
+ const projectPackageJson = importSync(projectPackageJsonPath);
16
+
17
+ const projectDependencies = Object.keys(
18
+ projectPackageJson.dependencies || {}
19
+ );
20
+
21
+ const result = esbuild.buildSync({
22
+ bundle: true,
23
+ entryPoints: [entryPoint],
24
+ external: projectDependencies,
25
+ format: 'cjs',
26
+ outfile,
27
+ platform: 'node',
28
+ target: typescriptConfig.target,
29
+ treeShaking: true,
30
+ });
31
+
32
+ if (result.errors.length > 0) {
33
+ // eslint-disable-next-line no-console
34
+ console.error('Error building config file: ', filename);
35
+ throw result.errors;
36
+ }
37
+
38
+ try {
39
+ const config = importSync(outfile);
40
+ return (config.default || config.config) as T;
41
+ } catch (error) {
42
+ // eslint-disable-next-line no-console
43
+ console.error('Failed importing build config file: ', filename);
44
+ throw error;
45
+ }
46
+ };