@ttoss/read-config-file 1.1.6 → 2.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/README.md CHANGED
@@ -7,7 +7,9 @@ This package is a simple utility to read a configuration file in multiple format
7
7
  - .js
8
8
  - .ts
9
9
 
10
- This package is ESM only.
10
+ ## ESM Only
11
+
12
+ This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c).
11
13
 
12
14
  ## Installation
13
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/read-config-file",
3
- "version": "1.1.6",
3
+ "version": "2.0.0",
4
4
  "description": "Read a configuration file",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -46,6 +46,6 @@
46
46
  },
47
47
  "scripts": {
48
48
  "build": "tsup",
49
- "test": "jest"
49
+ "test": "jest --projects tests/unit"
50
50
  }
51
51
  }
package/dist/index.d.cts DELETED
@@ -1,8 +0,0 @@
1
- type ConfigInput = {
2
- configFilePath: string;
3
- options?: any;
4
- };
5
- declare const readConfigFileSync: <ConfigFile = unknown>({ configFilePath, options, }: ConfigInput) => ConfigFile;
6
- declare const readConfigFile: <ConfigFile = unknown>({ configFilePath, options, }: ConfigInput) => Promise<ConfigFile>;
7
-
8
- export { readConfigFile, readConfigFileSync };
package/dist/index.js DELETED
@@ -1,132 +0,0 @@
1
- /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
- "use strict";
3
-
4
- var __create = Object.create;
5
- var __defProp = Object.defineProperty;
6
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
- var __getOwnPropNames = Object.getOwnPropertyNames;
8
- var __getProtoOf = Object.getPrototypeOf;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __export = (target, all) => {
11
- for (var name in all) __defProp(target, name, {
12
- get: all[name],
13
- enumerable: true
14
- });
15
- };
16
- var __copyProps = (to, from, except, desc) => {
17
- if (from && typeof from === "object" || typeof from === "function") {
18
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
19
- get: () => from[key],
20
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
21
- });
22
- }
23
- return to;
24
- };
25
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
- // If the importer is in node compatibility mode or this is not an ESM
27
- // file that has been converted to a CommonJS file using a Babel-
28
- // compatible transform (i.e. "__esModule" has not been set), then set
29
- // "default" to the CommonJS "module.exports" for node compatibility.
30
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
31
- value: mod,
32
- enumerable: true
33
- }) : target, mod));
34
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
35
- value: true
36
- }), mod);
37
-
38
- // src/index.ts
39
- var src_exports = {};
40
- __export(src_exports, {
41
- readConfigFile: () => readConfigFile,
42
- readConfigFileSync: () => readConfigFileSync
43
- });
44
- module.exports = __toCommonJS(src_exports);
45
-
46
- // src/loadConfig.ts
47
- var esbuild = __toESM(require("esbuild"), 1);
48
- var import_import_sync = __toESM(require("import-sync"), 1);
49
- var import_node_path = __toESM(require("path"), 1);
50
- var loadConfig = entryPoint => {
51
- const lastEntryPointName = entryPoint.split("/").pop();
52
- const filename = lastEntryPointName?.split(".")[0];
53
- const outfile = import_node_path.default.resolve(process.cwd(), "out", filename + ".js");
54
- const result = esbuild.buildSync({
55
- bundle: true,
56
- entryPoints: [entryPoint],
57
- /**
58
- * ttoss packages cannot be market as external because it'd break the CI.
59
- * On CI, ttoss packages point to the TS main file, not the compiled
60
- * ones. See more details here https://github.com/ttoss/ttoss/issues/541.
61
- */
62
- external: [],
63
- format: "cjs",
64
- outfile,
65
- platform: "node",
66
- target: "ES2021",
67
- treeShaking: true
68
- });
69
- if (result.errors.length > 0) {
70
- console.error("Error building config file: ", filename);
71
- throw result.errors;
72
- }
73
- try {
74
- const config = (0, import_import_sync.default)(outfile);
75
- return config.default || config.config;
76
- } catch (error) {
77
- console.error("Failed importing build config file: ", filename);
78
- throw error;
79
- }
80
- };
81
-
82
- // src/index.ts
83
- var import_fs = __toESM(require("fs"), 1);
84
- var import_js_yaml = __toESM(require("js-yaml"), 1);
85
- var readConfigFileSync = ({
86
- configFilePath,
87
- options
88
- }) => {
89
- const extension = configFilePath.split(".").pop();
90
- if (extension === "yaml" || extension === "yml") {
91
- const file = import_fs.default.readFileSync(configFilePath, "utf8");
92
- return import_js_yaml.default.load(file);
93
- }
94
- if (extension === "json") {
95
- const file = import_fs.default.readFileSync(configFilePath, "utf8");
96
- return JSON.parse(file);
97
- }
98
- if (extension === "js") {
99
- return require(configFilePath);
100
- }
101
- if (extension === "ts") {
102
- let result = loadConfig(configFilePath);
103
- if (typeof result === "function") {
104
- result = result(options);
105
- }
106
- return result;
107
- }
108
- throw new Error("Unsupported config file extension: " + extension);
109
- };
110
- var readConfigFile = async ({
111
- configFilePath,
112
- options
113
- }) => {
114
- const extension = configFilePath.split(".").pop();
115
- if (extension === "ts") {
116
- let result = loadConfig(configFilePath);
117
- if (typeof result === "function") {
118
- result = result(options);
119
- }
120
- result = await Promise.resolve(result);
121
- return result;
122
- }
123
- return readConfigFileSync({
124
- configFilePath,
125
- options
126
- });
127
- };
128
- // Annotate the CommonJS export names for ESM import in node:
129
- 0 && (module.exports = {
130
- readConfigFile,
131
- readConfigFileSync
132
- });