envlock-core 0.6.0 → 0.6.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/dist/index.cjs ADDED
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ ENVIRONMENTS: () => ENVIRONMENTS,
24
+ checkBinary: () => checkBinary,
25
+ findFreePort: () => findFreePort,
26
+ hasBinary: () => hasBinary,
27
+ log: () => log,
28
+ runWithSecrets: () => runWithSecrets,
29
+ setVerbose: () => setVerbose,
30
+ validateEnvFilePath: () => validateEnvFilePath,
31
+ validateOnePasswordEnvId: () => validateOnePasswordEnvId
32
+ });
33
+ module.exports = __toCommonJS(src_exports);
34
+
35
+ // src/invoke.ts
36
+ var import_node_child_process2 = require("child_process");
37
+
38
+ // src/detect.ts
39
+ var import_node_child_process = require("child_process");
40
+
41
+ // src/logger.ts
42
+ var verbose = false;
43
+ function setVerbose(flag) {
44
+ verbose = flag;
45
+ }
46
+ var log = {
47
+ debug: (msg) => {
48
+ if (verbose) process.stderr.write(`[envlock:debug] ${msg}
49
+ `);
50
+ },
51
+ info: (msg) => {
52
+ process.stderr.write(`[envlock] ${msg}
53
+ `);
54
+ },
55
+ warn: (msg) => {
56
+ process.stderr.write(`[envlock] Warning: ${msg}
57
+ `);
58
+ },
59
+ error: (msg) => {
60
+ process.stderr.write(`[envlock] Error: ${msg}
61
+ `);
62
+ }
63
+ };
64
+
65
+ // src/detect.ts
66
+ var WHICH = process.platform === "win32" ? "where" : "which";
67
+ function hasBinary(name) {
68
+ try {
69
+ (0, import_node_child_process.execFileSync)(WHICH, [name], { stdio: "pipe" });
70
+ return true;
71
+ } catch {
72
+ return false;
73
+ }
74
+ }
75
+ function checkBinary(name, installHint) {
76
+ if (!hasBinary(name)) {
77
+ throw new Error(`[envlock] '${name}' not found in PATH.
78
+ ${installHint}`);
79
+ }
80
+ log.debug(`Binary check: ${name} found`);
81
+ }
82
+
83
+ // src/invoke.ts
84
+ function runWithSecrets(options) {
85
+ const { envFile, environment, onePasswordEnvId, command, args } = options;
86
+ checkBinary(
87
+ "dotenvx",
88
+ "Install dotenvx: npm install -g @dotenvx/dotenvx\nOr add it as a dev dependency."
89
+ );
90
+ const privateKeyVar = `DOTENV_PRIVATE_KEY_${environment.toUpperCase()}`;
91
+ const keyAlreadyInjected = !!process.env[privateKeyVar];
92
+ let result;
93
+ if (keyAlreadyInjected) {
94
+ log.debug(`Spawning: dotenvx run -f ${envFile} -- ${command} ${args.join(" ")}`);
95
+ result = (0, import_node_child_process2.spawnSync)(
96
+ "dotenvx",
97
+ ["run", "-f", envFile, "--", command, ...args],
98
+ { stdio: "inherit" }
99
+ );
100
+ if (result.error) {
101
+ throw new Error(`[envlock] Failed to spawn 'dotenvx': ${result.error.message}`);
102
+ }
103
+ } else {
104
+ checkBinary(
105
+ "op",
106
+ "Install 1Password CLI: brew install --cask 1password-cli@beta\nThen sign in: op signin"
107
+ );
108
+ log.debug(`Spawning: op run --environment ${onePasswordEnvId} -- dotenvx run -f ${envFile} -- ${command} ${args.join(" ")}`);
109
+ result = (0, import_node_child_process2.spawnSync)(
110
+ "op",
111
+ [
112
+ "run",
113
+ "--environment",
114
+ onePasswordEnvId,
115
+ "--",
116
+ "dotenvx",
117
+ "run",
118
+ "-f",
119
+ envFile,
120
+ "--",
121
+ command,
122
+ ...args
123
+ ],
124
+ { stdio: "inherit" }
125
+ );
126
+ if (result.error) {
127
+ throw new Error(`[envlock] Failed to spawn 'op': ${result.error.message}`);
128
+ }
129
+ }
130
+ process.exit(result.status ?? 1);
131
+ }
132
+
133
+ // src/validate.ts
134
+ var import_node_path = require("path");
135
+ var OP_ENV_ID_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
136
+ function validateOnePasswordEnvId(id) {
137
+ if (!id || !OP_ENV_ID_PATTERN.test(id)) {
138
+ throw new Error(
139
+ `[envlock] Invalid onePasswordEnvId: "${id}". Must be a lowercase alphanumeric string (hyphens allowed), e.g. 'ca6uypwvab5mevel44gqdc2zae'.`
140
+ );
141
+ }
142
+ }
143
+ function validateEnvFilePath(envFile, cwd) {
144
+ if (envFile.includes("\0")) {
145
+ throw new Error(`[envlock] Invalid env file path: null bytes are not allowed.`);
146
+ }
147
+ const resolved = (0, import_node_path.resolve)(cwd, envFile);
148
+ const base = (0, import_node_path.resolve)(cwd);
149
+ const rel = (0, import_node_path.relative)(base, resolved);
150
+ if (rel.startsWith("..") || (0, import_node_path.isAbsolute)(rel)) {
151
+ throw new Error(
152
+ `[envlock] Invalid env file path: "${envFile}" resolves outside the project directory.`
153
+ );
154
+ }
155
+ }
156
+
157
+ // src/types.ts
158
+ var ENVIRONMENTS = {
159
+ development: "development",
160
+ staging: "staging",
161
+ production: "production"
162
+ };
163
+
164
+ // src/find-port.ts
165
+ var import_node_net = require("net");
166
+ function isPortFree(port) {
167
+ return new Promise((resolve2) => {
168
+ const server = (0, import_node_net.createServer)();
169
+ server.once("error", () => resolve2(false));
170
+ server.once("listening", () => server.close(() => resolve2(true)));
171
+ server.listen(port, "127.0.0.1");
172
+ });
173
+ }
174
+ async function findFreePort(preferred) {
175
+ for (let port = preferred; port <= preferred + 10; port++) {
176
+ if (await isPortFree(port)) return port;
177
+ }
178
+ throw new Error(
179
+ `[envlock] No free port found in range ${preferred}\u2013${preferred + 10}.`
180
+ );
181
+ }
182
+ // Annotate the CommonJS export names for ESM import in node:
183
+ 0 && (module.exports = {
184
+ ENVIRONMENTS,
185
+ checkBinary,
186
+ findFreePort,
187
+ hasBinary,
188
+ log,
189
+ runWithSecrets,
190
+ setVerbose,
191
+ validateEnvFilePath,
192
+ validateOnePasswordEnvId
193
+ });
@@ -0,0 +1,46 @@
1
+ declare const ENVIRONMENTS: {
2
+ readonly development: "development";
3
+ readonly staging: "staging";
4
+ readonly production: "production";
5
+ };
6
+ type Environment = keyof typeof ENVIRONMENTS;
7
+ interface EnvlockOptions {
8
+ onePasswordEnvId: string;
9
+ envFiles?: Partial<Record<Environment, string>>;
10
+ }
11
+ interface EnvlockConfig {
12
+ /**
13
+ * Your 1Password Environment ID.
14
+ * Can alternatively be set via the ENVLOCK_OP_ENV_ID environment variable.
15
+ */
16
+ onePasswordEnvId?: string;
17
+ envFiles?: Partial<Record<Environment, string>>;
18
+ commands?: Record<string, string>;
19
+ }
20
+
21
+ interface RunWithSecretsOptions {
22
+ envFile: string;
23
+ environment: Environment;
24
+ onePasswordEnvId: string;
25
+ command: string;
26
+ args: string[];
27
+ }
28
+ declare function runWithSecrets(options: RunWithSecretsOptions): void;
29
+
30
+ declare function hasBinary(name: string): boolean;
31
+ declare function checkBinary(name: string, installHint: string): void;
32
+
33
+ declare function validateOnePasswordEnvId(id: string): void;
34
+ declare function validateEnvFilePath(envFile: string, cwd: string): void;
35
+
36
+ declare function setVerbose(flag: boolean): void;
37
+ declare const log: {
38
+ debug: (msg: string) => void;
39
+ info: (msg: string) => void;
40
+ warn: (msg: string) => void;
41
+ error: (msg: string) => void;
42
+ };
43
+
44
+ declare function findFreePort(preferred: number): Promise<number>;
45
+
46
+ export { ENVIRONMENTS, type Environment, type EnvlockConfig, type EnvlockOptions, type RunWithSecretsOptions, checkBinary, findFreePort, hasBinary, log, runWithSecrets, setVerbose, validateEnvFilePath, validateOnePasswordEnvId };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "envlock-core",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "type": "module",
5
5
  "description": "Core 1Password + dotenvx secret injection logic for envlock",
6
6
  "license": "MIT",
@@ -22,10 +22,12 @@
22
22
  },
23
23
  "exports": {
24
24
  ".": {
25
- "import": "./dist/index.js",
26
- "types": "./dist/index.d.ts"
25
+ "types": "./dist/index.d.ts",
26
+ "require": "./dist/index.cjs",
27
+ "import": "./dist/index.js"
27
28
  }
28
29
  },
30
+ "main": "./dist/index.cjs",
29
31
  "files": [
30
32
  "dist"
31
33
  ],