envlock-core 0.2.0 → 0.4.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 +55 -12
- package/dist/cli/index.js +209 -0
- package/dist/index.d.ts +10 -1
- package/package.json +12 -11
- package/LICENSE +0 -21
package/README.md
CHANGED
|
@@ -4,30 +4,67 @@ Framework-agnostic 1Password + dotenvx secret injection logic.
|
|
|
4
4
|
|
|
5
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
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))
|
|
12
|
+
|
|
7
13
|
## Install
|
|
8
14
|
|
|
9
15
|
```bash
|
|
10
16
|
pnpm add envlock-core
|
|
11
17
|
```
|
|
12
18
|
|
|
13
|
-
##
|
|
19
|
+
## Usage
|
|
14
20
|
|
|
15
|
-
### `
|
|
21
|
+
### `envlock.config.js`
|
|
16
22
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
import { runWithSecrets } from 'envlock-core';
|
|
23
|
+
Create `envlock.config.js` in your project root:
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
```js
|
|
26
|
+
// envlock.config.js
|
|
27
|
+
export default {
|
|
25
28
|
onePasswordEnvId: 'ca6uypwvab5mevel44gqdc2zae',
|
|
26
|
-
|
|
27
|
-
|
|
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
|
+
};
|
|
29
40
|
```
|
|
30
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
|
+
}
|
|
52
|
+
```
|
|
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
70
|
| Option | Type | Description |
|
|
@@ -65,6 +102,12 @@ const ENVIRONMENTS = {
|
|
|
65
102
|
|
|
66
103
|
type Environment = keyof typeof ENVIRONMENTS;
|
|
67
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
|
+
}
|
|
110
|
+
|
|
68
111
|
interface EnvlockOptions {
|
|
69
112
|
onePasswordEnvId: string;
|
|
70
113
|
envFiles?: Partial<Record<Environment, string>>;
|
|
@@ -0,0 +1,209 @@
|
|
|
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 (firstArg === "run") {
|
|
161
|
+
if (config?.commands?.["run"]) {
|
|
162
|
+
console.warn(
|
|
163
|
+
'[envlock] Warning: "run" is a reserved subcommand. The config command named "run" is ignored.\nRename it in envlock.config.js to use it as a named command.'
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
const runArgs = passthrough.slice(1);
|
|
167
|
+
if (runArgs.length === 0) {
|
|
168
|
+
throw new Error(
|
|
169
|
+
"[envlock] Usage: envlock run <command> [args...]\nExample: envlock run node server.js --port 4000"
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
command = runArgs[0];
|
|
173
|
+
args = runArgs.slice(1);
|
|
174
|
+
} else if (config?.commands && firstArg in config.commands) {
|
|
175
|
+
const cmdString = config.commands[firstArg];
|
|
176
|
+
if (!cmdString || cmdString.trim() === "") {
|
|
177
|
+
throw new Error(`[envlock] Command "${firstArg}" is empty in envlock.config.js.`);
|
|
178
|
+
}
|
|
179
|
+
const parts = splitCommand(cmdString);
|
|
180
|
+
command = parts[0];
|
|
181
|
+
args = parts.slice(1);
|
|
182
|
+
} else if (config?.commands && Object.keys(config.commands).length > 0 && passthrough.length === 1) {
|
|
183
|
+
throw new Error(
|
|
184
|
+
`[envlock] Unknown command "${firstArg}". Available: ${Object.keys(config.commands).join(", ")}`
|
|
185
|
+
);
|
|
186
|
+
} else {
|
|
187
|
+
command = firstArg;
|
|
188
|
+
args = passthrough.slice(1);
|
|
189
|
+
}
|
|
190
|
+
const onePasswordEnvId = process.env["ENVLOCK_OP_ENV_ID"] ?? config?.onePasswordEnvId;
|
|
191
|
+
if (!onePasswordEnvId) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
"[envlock] No onePasswordEnvId found. Set it in envlock.config.js or via ENVLOCK_OP_ENV_ID env var."
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
validateOnePasswordEnvId(onePasswordEnvId);
|
|
197
|
+
const envFile = config?.envFiles?.[environment] ?? DEFAULT_ENV_FILES[environment];
|
|
198
|
+
validateEnvFilePath(envFile, cwd);
|
|
199
|
+
runWithSecrets({ envFile, environment, onePasswordEnvId, command, args });
|
|
200
|
+
}
|
|
201
|
+
if (import.meta.url === pathToFileURL2(process.argv[1] ?? "").href) {
|
|
202
|
+
run(process.argv.slice(2)).catch((err) => {
|
|
203
|
+
console.error(err instanceof Error ? err.message : String(err));
|
|
204
|
+
process.exit(1);
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
export {
|
|
208
|
+
run
|
|
209
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,15 @@ interface EnvlockOptions {
|
|
|
8
8
|
onePasswordEnvId: string;
|
|
9
9
|
envFiles?: Partial<Record<Environment, string>>;
|
|
10
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
|
+
}
|
|
11
20
|
|
|
12
21
|
interface RunWithSecretsOptions {
|
|
13
22
|
envFile: string;
|
|
@@ -24,4 +33,4 @@ declare function checkBinary(name: string, installHint: string): void;
|
|
|
24
33
|
declare function validateOnePasswordEnvId(id: string): void;
|
|
25
34
|
declare function validateEnvFilePath(envFile: string, cwd: string): void;
|
|
26
35
|
|
|
27
|
-
export { ENVIRONMENTS, 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "envlock-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Core 1Password + dotenvx secret injection logic for envlock",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,25 +17,26 @@
|
|
|
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",
|
|
23
26
|
"types": "./dist/index.d.ts"
|
|
24
27
|
}
|
|
25
28
|
},
|
|
26
|
-
"files": [
|
|
27
|
-
"dist"
|
|
28
|
-
],
|
|
29
|
-
"devDependencies": {
|
|
30
|
-
"@types/node": "^20.14.10",
|
|
31
|
-
"tsup": "^8.0.0",
|
|
32
|
-
"typescript": "^5.8.2",
|
|
33
|
-
"vitest": "^3.0.0"
|
|
34
|
-
},
|
|
29
|
+
"files": ["dist"],
|
|
35
30
|
"scripts": {
|
|
36
31
|
"build": "tsup",
|
|
37
32
|
"dev": "tsup --watch",
|
|
38
33
|
"test": "vitest run",
|
|
39
34
|
"test:watch": "vitest"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^20.14.10",
|
|
38
|
+
"tsup": "^8.0.0",
|
|
39
|
+
"typescript": "^5.8.2",
|
|
40
|
+
"vitest": "^3.0.0"
|
|
40
41
|
}
|
|
41
|
-
}
|
|
42
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Benjamin Davies
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|