binhend 2.1.24 → 2.1.26

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.24",
3
+ "version": "2.1.26",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -1,15 +1,12 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const { isEmptyArray, isArray, isObject } = require('./utils/types');
3
+ const { isEmptyArray, isArray, isObject, mustString, isNullish } = require('./utils/types');
4
4
 
5
5
  // @ts-ignore
6
- function ConfigLoader(configObject, { module } = {}) {
6
+ function ConfigLoader(configObject, { rootPath } = {}) {
7
7
  const configs = configObject || {};
8
- const rootPath = module?.path || require.main.path;
9
8
 
10
- this.getConfigs = () => {
11
- return configs;
12
- };
9
+ rootPath = mustString(rootPath) || require.main.path;
13
10
 
14
11
  function getConfigPosition(key) {
15
12
  return key == undefined ? configs : configs[key] || (configs[key] = {});
@@ -18,12 +15,12 @@ function ConfigLoader(configObject, { module } = {}) {
18
15
  // @ts-ignore
19
16
  this.object = (object, { key, filter: filters } = {}) => {
20
17
  try {
21
- var config = getConfigPosition(key);
22
- var filtered = isArray(filters) ? filter(object, filters) : object;
18
+ let config = getConfigPosition(key);
19
+ let filtered = isArray(filters) ? filter(object, filters) : object;
23
20
  Object.assign(config, filtered);
24
21
  }
25
- catch(e) {
26
- return failed(e, object);
22
+ catch (error) {
23
+ failed(error, object);
27
24
  }
28
25
 
29
26
  return this;
@@ -33,20 +30,37 @@ function ConfigLoader(configObject, { module } = {}) {
33
30
  return this.object(cli(), options);
34
31
  };
35
32
 
36
- this.json = (filepath, options) => {
37
- var filePath = path.resolve(rootPath, filepath);
38
- return this.object(json(filePath), options);
33
+ this.json = (filepath, options = {}) => {
34
+ let filePath = path.resolve(rootPath, filepath);
35
+ let jsonObject = json(filePath);
36
+
37
+ if (options.hasOwnProperty('env')) {
38
+ let env = options.env;
39
+ for (let key in jsonObject) {
40
+ let value = jsonObject[key];
41
+ if (isObject(value) && value.hasOwnProperty(env)) {
42
+ // if object has no "env" key, it means the intended value is the whole object => no new assignment
43
+ jsonObject[key] = value[env];
44
+ }
45
+ }
46
+ }
47
+
48
+ return this.object(jsonObject, options);
39
49
  };
40
50
 
41
51
  this.file = (filepath, options) => {
42
- var filePath = path.resolve(rootPath, filepath);
52
+ let filePath = path.resolve(rootPath, filepath);
43
53
  return this.object(file(filePath, options?.encoding), options);
44
54
  };
45
55
  }
46
56
 
57
+ ConfigLoader.cli = cli;
58
+ ConfigLoader.json = json;
59
+ ConfigLoader.file = file;
60
+
47
61
  function filter(object, filterKeys) {
48
62
  if (!isObject(object) || isEmptyArray(filterKeys)) return {};
49
-
63
+
50
64
  var filtered = {};
51
65
 
52
66
  filterKeys.forEach((field) => {
@@ -62,7 +76,7 @@ function json(path) {
62
76
  try {
63
77
  return require.main.require(path);
64
78
  }
65
- catch(e) {
79
+ catch (e) {
66
80
  return failed(e, path);
67
81
  }
68
82
  }
@@ -82,7 +96,7 @@ function file(path, encoding) {
82
96
 
83
97
  return processKeyValueStrings(lines);
84
98
  }
85
- catch(e) {
99
+ catch (e) {
86
100
  return failed(e, path);
87
101
  }
88
102
  }
@@ -96,7 +110,7 @@ function processKeyValueStrings(strings) {
96
110
 
97
111
  var regex_is_comment = /^#/;
98
112
 
99
- strings.forEach(function(string) {
113
+ strings.forEach(function (string) {
100
114
  if (regex_is_comment.test(string.trim())) return;
101
115
 
102
116
  var parsed = parseKeyValue(string);
@@ -53,7 +53,8 @@ module.exports = (options) => {
53
53
 
54
54
  // Handle preflight OPTIONS request
55
55
  if (req.method === 'OPTIONS' && !preflightContinue) {
56
- return res.sendStatus(mustNumber(optionsSuccessStatus, 204));
56
+ var statusCode = mustNumber(optionsSuccessStatus) || 204; // 204 - No Content
57
+ return res.sendStatus(statusCode);
57
58
  }
58
59
 
59
60
  return next();
@@ -30,16 +30,22 @@ function generate(sourceRootPath, outputRootPath, callbackDone) {
30
30
  writeToFileIfNew(fileOutputPath + '.js', cssModuleCode);
31
31
  // not return here, but wait for next "if" statement to be copied to build folder, then return there.
32
32
  }
33
-
34
- if (fileExtension !== '.js' && fileExtension !== '.mjs') {
33
+
34
+ if (fileExtension !== '.js' && fileExtension !== '.cjs' && fileExtension !== '.mjs') {
35
35
  // any files not JS (.css, .jpg, etc.) will be copied to build folder.
36
- return cloneFileIfNew(fileSourcePath, fileOutputPath);
36
+ cloneFileIfNew(fileSourcePath, fileOutputPath);
37
+ return;
37
38
  }
38
39
 
39
- var content = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
40
- var code = PREFIX_CODE + mapConfigValues(content.trim()) + '\r\n\r\n;binh.final(module);';
40
+ var fileContent = readFileSync(fileSourcePath, { encoding: 'utf8', flag: 'r' });
41
+ var formatCode = mapConfigValues(fileContent.trim()); // for .cjs (common js) and .mjs (module js) files, not build code but still bind config values
42
+
43
+ if (fileExtension === '.js') {
44
+ // for .js files, build code and bind config values
45
+ formatCode = PREFIX_CODE + formatCode + '\r\n\r\n;binh.final(module);';
46
+ }
41
47
 
42
- writeToFileIfNew(fileOutputPath, code);
48
+ writeToFileIfNew(fileOutputPath, formatCode);
43
49
  }
44
50
  catch (error) {
45
51
  printError('Failed formatting file:', fileSourcePath, error);