envlock-core 0.1.0 → 0.3.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
@@ -2,7 +2,13 @@
2
2
 
3
3
  Framework-agnostic 1Password + dotenvx secret injection logic.
4
4
 
5
- > Most users should install [`envlock`](https://www.npmjs.com/package/envlock) instead. This package is for integrating envlock with frameworks other than Next.js.
5
+ > Most users should install [`envlock-next`](https://www.npmjs.com/package/envlock-next) instead. This package is for integrating envlock with frameworks other than Next.js.
6
+
7
+ ## Prerequisites
8
+
9
+ - [1Password CLI](https://developer.1password.com/docs/cli/get-started/) (`op`) installed and signed in
10
+ - [dotenvx](https://dotenvx.com/docs/install) installed (`npm install -g @dotenvx/dotenvx`)
11
+ - Encrypted `.env.*` files committed to your repo (see [dotenvx quickstart](https://dotenvx.com/docs/quickstart))
6
12
 
7
13
  ## Install
8
14
 
@@ -10,33 +16,64 @@ Framework-agnostic 1Password + dotenvx secret injection logic.
10
16
  pnpm add envlock-core
11
17
  ```
12
18
 
13
- ## API
14
-
15
- ### `runWithSecrets(options)`
19
+ ## Usage
16
20
 
17
- Runs a command with secrets injected from 1Password via dotenvx. If `DOTENV_PRIVATE_KEY_<ENV>` is already set (e.g. in CI), it skips `op run` and calls `dotenvx run` directly.
21
+ ### `envlock.config.js`
18
22
 
19
- ```ts
20
- import { runWithSecrets } from 'envlock-core';
23
+ Create `envlock.config.js` in your project root:
21
24
 
22
- runWithSecrets({
23
- envFile: '.env.production',
24
- environment: 'production',
25
+ ```js
26
+ // envlock.config.js
27
+ export default {
25
28
  onePasswordEnvId: 'ca6uypwvab5mevel44gqdc2zae',
26
- command: 'node',
27
- args: ['server.js'],
28
- });
29
+ envFiles: {
30
+ development: '.env.development',
31
+ staging: '.env.staging',
32
+ production: '.env.production',
33
+ },
34
+ commands: {
35
+ dev: 'node server.js --watch',
36
+ start: 'node server.js --port 3000',
37
+ build: 'node build.js',
38
+ },
39
+ };
40
+ ```
41
+
42
+ Then wire up your `package.json` scripts:
43
+
44
+ ```json
45
+ {
46
+ "scripts": {
47
+ "dev": "envlock dev",
48
+ "build": "envlock build --production",
49
+ "start": "envlock start --production"
50
+ }
51
+ }
29
52
  ```
30
53
 
54
+ Pass `--staging` or `--production` to switch environments. For ad-hoc commands, pass the command directly without a config key:
55
+
56
+ ```bash
57
+ envlock node server.js --production
58
+ ```
59
+
60
+ Set `ENVLOCK_OP_ENV_ID` to provide the 1Password Environment ID via env var instead of the config file. In CI, set `DOTENV_PRIVATE_KEY_<ENV>` directly and `op run` is skipped automatically.
61
+
62
+ ## API
63
+
64
+ ### `runWithSecrets(options)`
65
+
66
+ Runs a command with secrets injected from 1Password via dotenvx. If `DOTENV_PRIVATE_KEY_<ENV>` is already set (e.g. in CI), it skips `op run` and calls `dotenvx run` directly.
67
+
31
68
  **Options:**
32
69
 
33
- | Option | Type | Description |
34
- |--------|------|-------------|
35
- | `envFile` | `string` | Path to the encrypted dotenvx env file |
36
- | `environment` | `string` | Environment name (`development`, `staging`, `production`) |
37
- | `onePasswordEnvId` | `string` | Your 1Password Environment ID |
38
- | `command` | `string` | The command to run |
39
- | `args` | `string[]` | Arguments to pass to the command |
70
+ | Option | Type | Description |
71
+ | ------------------ | ------------- | --------------------------------------------------------- |
72
+ | `envFile` | `string` | Path to the encrypted dotenvx env file |
73
+ | `environment` | `Environment` | Environment name (`development`, `staging`, `production`) |
74
+ | `onePasswordEnvId` | `string` | Your 1Password Environment ID |
75
+ | `command` | `string` | The command to run |
76
+ | `args` | `string[]` | Arguments to pass to the command |
40
77
 
41
78
  ### `validateOnePasswordEnvId(id)`
42
79
 
@@ -57,20 +94,28 @@ Calls `process.exit(1)` with a helpful message if `name` is not in `PATH`.
57
94
  ## Types
58
95
 
59
96
  ```ts
60
- type Environment = 'development' | 'staging' | 'production';
97
+ const ENVIRONMENTS = {
98
+ development: 'development',
99
+ staging: 'staging',
100
+ production: 'production',
101
+ } as const;
102
+
103
+ type Environment = keyof typeof ENVIRONMENTS;
104
+
105
+ interface EnvlockConfig {
106
+ onePasswordEnvId?: string; // or set ENVLOCK_OP_ENV_ID env var
107
+ envFiles?: Partial<Record<Environment, string>>;
108
+ commands?: Record<string, string>;
109
+ }
61
110
 
62
111
  interface EnvlockOptions {
63
112
  onePasswordEnvId: string;
64
- envFiles?: {
65
- development?: string;
66
- staging?: string;
67
- production?: string;
68
- };
113
+ envFiles?: Partial<Record<Environment, string>>;
69
114
  }
70
115
 
71
116
  interface RunWithSecretsOptions {
72
117
  envFile: string;
73
- environment: string;
118
+ environment: Environment;
74
119
  onePasswordEnvId: string;
75
120
  command: string;
76
121
  args: string[];
@@ -0,0 +1,195 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli/index.ts
4
+ import { pathToFileURL as pathToFileURL2 } from "url";
5
+
6
+ // src/types.ts
7
+ var ENVIRONMENTS = {
8
+ development: "development",
9
+ staging: "staging",
10
+ production: "production"
11
+ };
12
+
13
+ // src/invoke.ts
14
+ import { spawnSync } from "child_process";
15
+
16
+ // src/detect.ts
17
+ import { execFileSync } from "child_process";
18
+ var WHICH = process.platform === "win32" ? "where" : "which";
19
+ function hasBinary(name) {
20
+ try {
21
+ execFileSync(WHICH, [name], { stdio: "pipe" });
22
+ return true;
23
+ } catch {
24
+ return false;
25
+ }
26
+ }
27
+ function checkBinary(name, installHint) {
28
+ if (!hasBinary(name)) {
29
+ console.error(`[envlock] '${name}' not found in PATH.
30
+ ${installHint}`);
31
+ process.exit(1);
32
+ }
33
+ }
34
+
35
+ // src/invoke.ts
36
+ function runWithSecrets(options) {
37
+ const { envFile, environment, onePasswordEnvId, command, args } = options;
38
+ checkBinary(
39
+ "dotenvx",
40
+ "Install dotenvx: npm install -g @dotenvx/dotenvx\nOr add it as a dev dependency."
41
+ );
42
+ const privateKeyVar = `DOTENV_PRIVATE_KEY_${environment.toUpperCase()}`;
43
+ const keyAlreadyInjected = !!process.env[privateKeyVar];
44
+ let result;
45
+ if (keyAlreadyInjected) {
46
+ result = spawnSync(
47
+ "dotenvx",
48
+ ["run", "-f", envFile, "--", command, ...args],
49
+ { stdio: "inherit" }
50
+ );
51
+ } else {
52
+ checkBinary(
53
+ "op",
54
+ "Install 1Password CLI: brew install --cask 1password-cli@beta\nThen sign in: op signin"
55
+ );
56
+ result = spawnSync(
57
+ "op",
58
+ [
59
+ "run",
60
+ "--environment",
61
+ onePasswordEnvId,
62
+ "--",
63
+ "dotenvx",
64
+ "run",
65
+ "-f",
66
+ envFile,
67
+ "--",
68
+ command,
69
+ ...args
70
+ ],
71
+ { stdio: "inherit" }
72
+ );
73
+ }
74
+ process.exit(result.status ?? 1);
75
+ }
76
+
77
+ // src/validate.ts
78
+ import { isAbsolute, relative, resolve } from "path";
79
+ var OP_ENV_ID_PATTERN = /^[a-z0-9][a-z0-9-]*$/;
80
+ function validateOnePasswordEnvId(id) {
81
+ if (!id || !OP_ENV_ID_PATTERN.test(id)) {
82
+ throw new Error(
83
+ `[envlock] Invalid onePasswordEnvId: "${id}". Must be a lowercase alphanumeric string (hyphens allowed), e.g. 'ca6uypwvab5mevel44gqdc2zae'.`
84
+ );
85
+ }
86
+ }
87
+ function validateEnvFilePath(envFile, cwd) {
88
+ if (envFile.includes("\0")) {
89
+ throw new Error(`[envlock] Invalid env file path: null bytes are not allowed.`);
90
+ }
91
+ const resolved = resolve(cwd, envFile);
92
+ const base = resolve(cwd);
93
+ const rel = relative(base, resolved);
94
+ if (rel.startsWith("..") || isAbsolute(rel)) {
95
+ throw new Error(
96
+ `[envlock] Invalid env file path: "${envFile}" resolves outside the project directory.`
97
+ );
98
+ }
99
+ }
100
+
101
+ // src/cli/resolve-config.ts
102
+ import { existsSync } from "fs";
103
+ import { resolve as resolve2 } from "path";
104
+ import { pathToFileURL } from "url";
105
+ var CONFIG_CANDIDATES = [
106
+ "envlock.config.js",
107
+ "envlock.config.mjs"
108
+ ];
109
+ async function resolveConfig(cwd) {
110
+ for (const candidate of CONFIG_CANDIDATES) {
111
+ const fullPath = resolve2(cwd, candidate);
112
+ if (!existsSync(fullPath)) continue;
113
+ try {
114
+ const mod = await import(pathToFileURL(fullPath).href);
115
+ const config = mod.default ?? mod;
116
+ if (config && typeof config === "object") {
117
+ return config;
118
+ }
119
+ } catch (err) {
120
+ console.warn(
121
+ `[envlock] Failed to load ${candidate}: ${err instanceof Error ? err.message : String(err)}`
122
+ );
123
+ }
124
+ }
125
+ return null;
126
+ }
127
+
128
+ // src/cli/index.ts
129
+ var ARGUMENT_FLAGS = {
130
+ staging: "--staging",
131
+ production: "--production"
132
+ };
133
+ var DEFAULT_ENV_FILES = {
134
+ development: ".env.development",
135
+ staging: ".env.staging",
136
+ production: ".env.production"
137
+ };
138
+ function splitCommand(cmd) {
139
+ const parts = [];
140
+ const re = /[^\s"']+|"([^"]*)"|'([^']*)'/g;
141
+ let match;
142
+ while ((match = re.exec(cmd)) !== null) {
143
+ parts.push(match[1] ?? match[2] ?? match[0]);
144
+ }
145
+ return parts;
146
+ }
147
+ async function run(argv, cwd = process.cwd()) {
148
+ const environment = argv.includes(ARGUMENT_FLAGS.production) ? ENVIRONMENTS.production : argv.includes(ARGUMENT_FLAGS.staging) ? ENVIRONMENTS.staging : ENVIRONMENTS.development;
149
+ const passthrough = argv.filter(
150
+ (f) => f !== ARGUMENT_FLAGS.staging && f !== ARGUMENT_FLAGS.production
151
+ );
152
+ const config = await resolveConfig(cwd);
153
+ const firstArg = passthrough[0];
154
+ let command;
155
+ let args;
156
+ if (firstArg === void 0) {
157
+ const available = config?.commands ? Object.keys(config.commands).join(", ") : "none";
158
+ throw new Error(`[envlock] No command specified. Available commands: ${available}`);
159
+ }
160
+ if (config?.commands && firstArg in config.commands) {
161
+ const cmdString = config.commands[firstArg];
162
+ if (!cmdString || cmdString.trim() === "") {
163
+ throw new Error(`[envlock] Command "${firstArg}" is empty in envlock.config.js.`);
164
+ }
165
+ const parts = splitCommand(cmdString);
166
+ command = parts[0];
167
+ args = parts.slice(1);
168
+ } else if (config?.commands && Object.keys(config.commands).length > 0 && passthrough.length === 1) {
169
+ throw new Error(
170
+ `[envlock] Unknown command "${firstArg}". Available: ${Object.keys(config.commands).join(", ")}`
171
+ );
172
+ } else {
173
+ command = firstArg;
174
+ args = passthrough.slice(1);
175
+ }
176
+ const onePasswordEnvId = process.env["ENVLOCK_OP_ENV_ID"] ?? config?.onePasswordEnvId;
177
+ if (!onePasswordEnvId) {
178
+ throw new Error(
179
+ "[envlock] No onePasswordEnvId found. Set it in envlock.config.js or via ENVLOCK_OP_ENV_ID env var."
180
+ );
181
+ }
182
+ validateOnePasswordEnvId(onePasswordEnvId);
183
+ const envFile = config?.envFiles?.[environment] ?? DEFAULT_ENV_FILES[environment];
184
+ validateEnvFilePath(envFile, cwd);
185
+ runWithSecrets({ envFile, environment, onePasswordEnvId, command, args });
186
+ }
187
+ if (import.meta.url === pathToFileURL2(process.argv[1] ?? "").href) {
188
+ run(process.argv.slice(2)).catch((err) => {
189
+ console.error(err instanceof Error ? err.message : String(err));
190
+ process.exit(1);
191
+ });
192
+ }
193
+ export {
194
+ run
195
+ };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,26 @@
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
+
1
21
  interface RunWithSecretsOptions {
2
22
  envFile: string;
3
- environment: string;
23
+ environment: Environment;
4
24
  onePasswordEnvId: string;
5
25
  command: string;
6
26
  args: string[];
@@ -13,14 +33,4 @@ declare function checkBinary(name: string, installHint: string): void;
13
33
  declare function validateOnePasswordEnvId(id: string): void;
14
34
  declare function validateEnvFilePath(envFile: string, cwd: string): void;
15
35
 
16
- type Environment = "development" | "staging" | "production";
17
- interface EnvlockOptions {
18
- onePasswordEnvId: string;
19
- envFiles?: {
20
- development?: string;
21
- staging?: string;
22
- production?: string;
23
- };
24
- }
25
-
26
- export { type Environment, type EnvlockOptions, type RunWithSecretsOptions, checkBinary, hasBinary, runWithSecrets, validateEnvFilePath, validateOnePasswordEnvId };
36
+ export { ENVIRONMENTS, type Environment, type EnvlockConfig, type EnvlockOptions, type RunWithSecretsOptions, checkBinary, hasBinary, runWithSecrets, validateEnvFilePath, validateOnePasswordEnvId };
package/dist/index.js CHANGED
@@ -85,7 +85,15 @@ function validateEnvFilePath(envFile, cwd) {
85
85
  );
86
86
  }
87
87
  }
88
+
89
+ // src/types.ts
90
+ var ENVIRONMENTS = {
91
+ development: "development",
92
+ staging: "staging",
93
+ production: "production"
94
+ };
88
95
  export {
96
+ ENVIRONMENTS,
89
97
  checkBinary,
90
98
  hasBinary,
91
99
  runWithSecrets,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "envlock-core",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "Core 1Password + dotenvx secret injection logic for envlock",
6
6
  "license": "MIT",
@@ -17,6 +17,9 @@
17
17
  "engines": {
18
18
  "node": ">=18"
19
19
  },
20
+ "bin": {
21
+ "envlock": "./dist/cli/index.js"
22
+ },
20
23
  "exports": {
21
24
  ".": {
22
25
  "import": "./dist/index.js",