@p8ec/shared 3.0.3 → 3.1.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.
@@ -2,7 +2,7 @@
2
2
  # https://github.com/evilmartians/lefthook/blob/master/docs/configuration.md
3
3
 
4
4
  ### Pre-commit hook ###
5
- # 1. Block commits to main, master, and release branches
5
+ # 1. Block commits to main, master, and release branches, except for specific files
6
6
  pre-commit:
7
7
  commands:
8
8
  git:
@@ -15,14 +15,17 @@ pre-commit:
15
15
  exit 0
16
16
  fi
17
17
 
18
- ALLOWED_FILES=${ALLOWED_FILES:-"package.json"}
18
+ # Enable extended globbing for pattern matching
19
+ shopt -s globstar extglob
20
+
21
+ ALLOWED_FILES=${ALLOWED_FILES:-"package.json,.idea/*"}
19
22
  IFS=',' read -r -a ALLOWED_ARRAY <<< "$ALLOWED_FILES"
20
23
 
21
24
  # Check every staged file; if any differs from the allowed files, block the commit
22
- for f in $STAGED_FILES; do
25
+ while IFS= read -r f; do
23
26
  MATCHED=false
24
27
  for allowed in "${ALLOWED_ARRAY[@]}"; do
25
- if [[ "$f" == "$allowed" || "$f" == */"$allowed" ]]; then
28
+ if [[ "$f" == "$allowed" || "$f" == */"$allowed" || "$f" == $allowed ]]; then
26
29
  MATCHED=true
27
30
  break
28
31
  fi
@@ -32,10 +35,9 @@ pre-commit:
32
35
  echo "Direct commits to main, master, and release branches are not allowed, except for: $ALLOWED_FILES"
33
36
  exit 1
34
37
  fi
35
- done
38
+ done <<< "$STAGED_FILES"
36
39
  fi
37
40
 
38
-
39
41
  ### Commit message hook ###
40
42
  # 1. Allow only commit messages that follow a conventional commit format
41
43
  commit-msg:
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ /**
3
+ * 2026 Copyright P8 Enterprise Components, Inc.
4
+ * All Rights Reserved.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.env = exports.parseEnvContent = void 0;
41
+ const fs = __importStar(require("node:fs"));
42
+ const path = __importStar(require("node:path"));
43
+ const detect_1 = require("../utils/detect");
44
+ const ferramenta_1 = require("ferramenta");
45
+ /**
46
+ * Parses an.env file content into an object.
47
+ */
48
+ const parseEnvContent = (content) => {
49
+ const env = {};
50
+ const lines = content.split('\n');
51
+ for (let line of lines) {
52
+ line = line.trim();
53
+ if (!line || line.startsWith('#')) {
54
+ continue;
55
+ }
56
+ const [key, ...valueParts] = line.split('=');
57
+ if (key) {
58
+ let value = valueParts.join('=');
59
+ // Remove surrounding quotes if present
60
+ if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
61
+ value = value.slice(1, -1);
62
+ }
63
+ env[key.trim()] = value.trim();
64
+ }
65
+ }
66
+ return env;
67
+ };
68
+ exports.parseEnvContent = parseEnvContent;
69
+ /**
70
+ * Reads .env files and populates environment variables.
71
+ * @param filemask - .env file name or mask (e.g. .env*)
72
+ * @param useRoot - If true, search from project root.
73
+ * @param quiet - If true, suppress console output.
74
+ */
75
+ const env = (filemask = '.env', useRoot = false, quiet = false) => {
76
+ const startDir = useRoot ? (0, detect_1.detectRoot)() : process.cwd();
77
+ const files = (0, ferramenta_1.scanDirectory)(startDir, filemask, false);
78
+ const mergedEnv = {};
79
+ const seenInFiles = {};
80
+ for (const file of files) {
81
+ const filePath = path.isAbsolute(file) ? file : path.join(startDir, file);
82
+ if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {
83
+ const content = fs.readFileSync(filePath, 'utf8');
84
+ const parsed = (0, exports.parseEnvContent)(content);
85
+ for (const [key, value] of Object.entries(parsed)) {
86
+ seenInFiles[key] = seenInFiles[key] || [];
87
+ seenInFiles[key].push({ file, value });
88
+ // Always use the first value encountered if multiple, or later ones?
89
+ // Usually, merging .env files, the later ones might override the earlier ones.
90
+ // Let's stick with the last one for now and show a warning if values differ.
91
+ mergedEnv[key] = value;
92
+ }
93
+ }
94
+ }
95
+ // Show warnings for conflicts
96
+ for (const [key, occurrences] of Object.entries(seenInFiles)) {
97
+ if (occurrences.length > 1) {
98
+ const values = occurrences.map((o) => `${o.file}: ${o.value}`);
99
+ const uniqueValues = new Set(occurrences.map((o) => o.value));
100
+ if (uniqueValues.size > 1) {
101
+ console.warn(`Warning: Conflict for env variable '${key}' found in files: ${values.join(', ')}`);
102
+ }
103
+ }
104
+ }
105
+ // Populate process.env
106
+ for (const [key, value] of Object.entries(mergedEnv)) {
107
+ process.env[key] = value;
108
+ if (!quiet)
109
+ console.log(`${key}=${value}`);
110
+ }
111
+ return mergedEnv;
112
+ };
113
+ exports.env = env;
@@ -50,7 +50,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
50
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
51
  };
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.main = exports.cliUtils = exports.IS_DEV = exports.detectWorkspace = exports.detectPackageManager = exports.root = exports.run = exports.dirn = exports.initCleanup = exports.init = void 0;
53
+ exports.main = exports.cliUtils = exports.IS_DEV = exports.detectWorkspace = exports.detectPackageManager = exports.root = exports.run = exports.env = exports.dirn = exports.initCleanup = exports.init = void 0;
54
54
  /**
55
55
  * P8 Shared CLI tool.
56
56
  *
@@ -68,6 +68,8 @@ Object.defineProperty(exports, "init", { enumerable: true, get: function () { re
68
68
  Object.defineProperty(exports, "initCleanup", { enumerable: true, get: function () { return init_1.initCleanup; } });
69
69
  const dirn_1 = require("./cmds/dirn");
70
70
  Object.defineProperty(exports, "dirn", { enumerable: true, get: function () { return dirn_1.dirn; } });
71
+ const env_1 = require("./cmds/env");
72
+ Object.defineProperty(exports, "env", { enumerable: true, get: function () { return env_1.env; } });
71
73
  const run_1 = require("./cmds/run");
72
74
  Object.defineProperty(exports, "run", { enumerable: true, get: function () { return run_1.run; } });
73
75
  const root_1 = require("./cmds/root");
@@ -77,7 +79,7 @@ Object.defineProperty(exports, "detectPackageManager", { enumerable: true, get:
77
79
  Object.defineProperty(exports, "detectWorkspace", { enumerable: true, get: function () { return detect_1.detectWorkspace; } });
78
80
  exports.IS_DEV = process.env.NODE_ENV === 'development';
79
81
  let args = ferramenta_1.processArgs.args;
80
- const self = path.parse(ferramenta_1.processArgs.name).name;
82
+ const self = ferramenta_1.processArgs.name ? path.parse(ferramenta_1.processArgs.name).name : 'p8cli';
81
83
  const writeLn = (...args) => {
82
84
  console.log(...args);
83
85
  };
@@ -117,8 +119,15 @@ Commands:
117
119
  Returns path to the root of the repo.
118
120
  pm
119
121
  Returns the detected package manager.
120
- ws
122
+ ws [options]
121
123
  Returns true or false for detected workspace.
124
+ env [filemask] [options]
125
+ Reads .env file(s) and populates environment variables.
126
+ Arguments:
127
+ filemask: The .env file name or mask (defaults to '.env').
128
+ Options:
129
+ -r, --root: Flag to search for .env file(s) in the project root.
130
+ -q, --quiet: Flag to suppress output.
122
131
  `);
123
132
  if (exports.IS_DEV) {
124
133
  writeLn(`DEVELOPMENT MODE`);
@@ -153,6 +162,8 @@ const main = (customArgs) => __awaiter(void 0, void 0, void 0, function* () {
153
162
  case 'ws':
154
163
  exports.cliUtils.writeLn((0, detect_1.detectWorkspace)());
155
164
  break;
165
+ case 'env':
166
+ return (0, env_1.env)((parsed.positional[0] || parsed.options.f || parsed.options.filemask), !!(parsed.options.r || parsed.options.root), !!(parsed.options.q || parsed.options.quiet));
156
167
  default:
157
168
  console.error(`Unknown command: ${parsed.command}`);
158
169
  process.exit(1);
@@ -163,12 +174,12 @@ if (require.main === module) {
163
174
  (0, exports.main)()
164
175
  .then((r) => {
165
176
  if (exports.IS_DEV) {
166
- writeLn(`DEV: setup completed successfully with result: ${JSON.stringify(r)}`);
177
+ writeLn(`DEV: completed successfully with result: ${JSON.stringify(r)}`);
167
178
  }
168
179
  })
169
180
  .catch((err) => {
170
181
  if (exports.IS_DEV) {
171
- writeLn(`DEV: setup failed with error: ${JSON.stringify(err)}`);
182
+ writeLn(`DEV: failed with error: ${JSON.stringify(err)}`);
172
183
  }
173
184
  console.error(err.toString());
174
185
  process.exit(1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@p8ec/shared",
3
- "version": "3.0.3",
3
+ "version": "3.1.0",
4
4
  "description": "P(8) Global Shared Library for Javascript",
5
5
  "repository": {
6
6
  "type": "git",
@@ -36,13 +36,13 @@
36
36
  "types": "dist/types/index.d.ts",
37
37
  "license": "MIT",
38
38
  "dependencies": {
39
- "@eslint/js": "^9.39.2",
40
- "eslint": "^9.39.2",
39
+ "@eslint/js": "^10.0.1",
40
+ "eslint": "^10.0.1",
41
41
  "eslint-config-prettier": "^10.1.8",
42
- "eslint-plugin-headers": "^1.3.3",
43
- "eslint-plugin-prettier": "^5.5.4",
42
+ "eslint-plugin-headers": "^1.3.4",
43
+ "eslint-plugin-prettier": "^5.5.5",
44
44
  "ferramenta": "^1.3.2",
45
- "prettier": "^3.7.4",
46
- "typescript-eslint": "^8.52.0"
45
+ "prettier": "^3.8.1",
46
+ "typescript-eslint": "^8.57.0"
47
47
  }
48
48
  }