envlock-next 0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
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.
package/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # envlock-next
2
+
3
+ Inject secrets from 1Password into your Next.js app at dev/build/start time using [dotenvx](https://dotenvx.com) encrypted env files.
4
+
5
+ ## Prerequisites
6
+
7
+ - [1Password CLI](https://developer.1password.com/docs/cli/get-started/) (`op`) installed and signed in
8
+ - [dotenvx](https://dotenvx.com/docs/install) installed (`npm install -g @dotenvx/dotenvx`)
9
+ - Encrypted `.env.*` files committed to your repo (see [dotenvx quickstart](https://dotenvx.com/docs/quickstart))
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ pnpm add envlock-next
15
+ ```
16
+
17
+ ## Setup
18
+
19
+ ### 1. Configure `next.config.js`
20
+
21
+ ```js
22
+ import { withEnvlock } from 'envlock-next';
23
+
24
+ export default withEnvlock(
25
+ {
26
+ // your existing Next.js config
27
+ },
28
+ {
29
+ onePasswordEnvId: 'ca6uypwvab5mevel44gqdc2zae', // your 1Password Environment ID
30
+ },
31
+ );
32
+ ```
33
+
34
+ Find your **Environment ID** in 1Password → Settings → Developer → Environments.
35
+
36
+ ### 2. Update `package.json` scripts
37
+
38
+ ```json
39
+ {
40
+ "scripts": {
41
+ "dev": "envlock dev",
42
+ "build": "envlock build",
43
+ "start": "envlock start"
44
+ }
45
+ }
46
+ ```
47
+
48
+ That's it. `envlock dev` will pull your dotenvx private key from 1Password, decrypt `.env.development`, and start Next.js with your secrets injected.
49
+
50
+ ## Environment flags
51
+
52
+ By default, `envlock dev` uses `.env.development`. Use flags to target other environments:
53
+
54
+ ```bash
55
+ envlock dev --staging # uses .env.staging
56
+ envlock dev --production # uses .env.production
57
+ envlock build --staging
58
+ envlock build --production
59
+ envlock start --production
60
+ ```
61
+
62
+ ## Custom env file paths
63
+
64
+ Override the default file paths in `next.config.js`:
65
+
66
+ ```js
67
+ export default withEnvlock(
68
+ {},
69
+ {
70
+ onePasswordEnvId: 'ca6uypwvab5mevel44gqdc2zae',
71
+ envFiles: {
72
+ development: '.env.local',
73
+ staging: '.env.staging.local',
74
+ production: '.env.production',
75
+ },
76
+ },
77
+ );
78
+ ```
79
+
80
+ ## Environment variable fallback
81
+
82
+ If `ENVLOCK_OP_ENV_ID` is set, envlock uses it instead of reading `next.config.js`. Useful for CI environments where you don't want to load the config file.
83
+
84
+ ```bash
85
+ ENVLOCK_OP_ENV_ID=ca6uypwvab5mevel44gqdc2zae envlock build --production
86
+ ```
87
+
88
+ ## CI usage
89
+
90
+ In CI, set `DOTENV_PRIVATE_KEY_<ENV>` directly. envlock detects this and skips `op run`, calling `dotenvx run` only:
91
+
92
+ ```yaml
93
+ - name: Build
94
+ run: pnpm build
95
+ env:
96
+ DOTENV_PRIVATE_KEY_PRODUCTION: ${{ secrets.DOTENV_PRIVATE_KEY_PRODUCTION }}
97
+ ```
98
+
99
+ ## `createEnv` wrapper
100
+
101
+ envlock re-exports a `createEnv` wrapper around [`@t3-oss/env-nextjs`](https://env.t3.gg) with sensible defaults:
102
+
103
+ ```js
104
+ // src/env.js
105
+ import { createEnv } from 'envlock-next';
106
+ import { z } from 'zod';
107
+
108
+ export const env = createEnv({
109
+ server: {
110
+ DATABASE_URL: z.string().url(),
111
+ API_SECRET: z.string().min(1),
112
+ },
113
+ client: {
114
+ NEXT_PUBLIC_APP_URL: z.string().url(),
115
+ },
116
+ runtimeEnv: {
117
+ DATABASE_URL: process.env.DATABASE_URL,
118
+ API_SECRET: process.env.API_SECRET,
119
+ NEXT_PUBLIC_APP_URL: process.env.NEXT_PUBLIC_APP_URL,
120
+ },
121
+ });
122
+ ```
123
+
124
+ Defaults applied: `emptyStringAsUndefined: true`, `skipValidation` reads from `SKIP_ENV_VALIDATION` env var.
125
+
126
+ Requires `@t3-oss/env-nextjs` and `zod` as peer dependencies:
127
+
128
+ ```bash
129
+ pnpm add @t3-oss/env-nextjs zod
130
+ ```
131
+
132
+ ## License
133
+
134
+ MIT — [Benjamin Davies](https://github.com/BenDavies1218)
@@ -0,0 +1,91 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/cli/index.ts
4
+ import { Command } from "commander";
5
+ import { runWithSecrets, validateEnvFilePath } from "envlock-core";
6
+
7
+ // src/cli/resolve-config.ts
8
+ import { existsSync } from "fs";
9
+ import { resolve } from "path";
10
+ import { pathToFileURL } from "url";
11
+ import { validateOnePasswordEnvId } from "envlock-core";
12
+ var CONFIG_CANDIDATES = ["next.config.js", "next.config.mjs"];
13
+ async function resolveConfig(cwd) {
14
+ for (const candidate of CONFIG_CANDIDATES) {
15
+ const fullPath = resolve(cwd, candidate);
16
+ if (!existsSync(fullPath)) continue;
17
+ try {
18
+ const mod = await import(pathToFileURL(fullPath).href);
19
+ const config = mod.default ?? mod;
20
+ if (config && typeof config === "object" && "__envlock" in config && config.__envlock && typeof config.__envlock === "object" && "onePasswordEnvId" in config.__envlock) {
21
+ return config.__envlock;
22
+ }
23
+ } catch (err) {
24
+ console.warn(
25
+ `[envlock] Failed to load ${candidate}: ${err instanceof Error ? err.message : String(err)}`
26
+ );
27
+ }
28
+ }
29
+ if (process.env["ENVLOCK_OP_ENV_ID"]) {
30
+ const id = process.env["ENVLOCK_OP_ENV_ID"];
31
+ validateOnePasswordEnvId(id);
32
+ return { onePasswordEnvId: id };
33
+ }
34
+ throw new Error(
35
+ "[envlock] Could not find configuration.\nAdd withEnvlock() to your next.config.js:\n\n import { withEnvlock } from 'envlock-next';\n export default withEnvlock({}, { onePasswordEnvId: 'your-env-id' });\n\nOr set the ENVLOCK_OP_ENV_ID environment variable."
36
+ );
37
+ }
38
+
39
+ // src/cli/index.ts
40
+ var DEFAULT_ENV_FILES = {
41
+ development: ".env.development",
42
+ staging: ".env.staging",
43
+ production: ".env.production"
44
+ };
45
+ async function runNextCommand(subcommand, environment, passthroughArgs) {
46
+ const config = await resolveConfig(process.cwd());
47
+ const envFile = config.envFiles?.[environment] ?? DEFAULT_ENV_FILES[environment];
48
+ validateEnvFilePath(envFile, process.cwd());
49
+ runWithSecrets({
50
+ envFile,
51
+ environment,
52
+ onePasswordEnvId: config.onePasswordEnvId,
53
+ command: "next",
54
+ args: [subcommand, ...passthroughArgs]
55
+ });
56
+ }
57
+ function addEnvFlags(cmd) {
58
+ return cmd.option("--staging", "use staging environment").option("--production", "use production environment").allowUnknownOption(true);
59
+ }
60
+ function getEnvironment(opts) {
61
+ if (opts.production) return "production";
62
+ if (opts.staging) return "staging";
63
+ return "development";
64
+ }
65
+ var program = new Command("envlock");
66
+ program.name("envlock").description("Run Next.js commands with 1Password + dotenvx secret injection").version("0.1.0");
67
+ var devCmd = new Command("dev").description("Start Next.js development server").allowUnknownOption(true);
68
+ addEnvFlags(devCmd).action(async (opts) => {
69
+ const passthrough = devCmd.args.filter(
70
+ (a) => a !== "--staging" && a !== "--production"
71
+ );
72
+ await runNextCommand("dev", getEnvironment(opts), passthrough);
73
+ });
74
+ var buildCmd = new Command("build").description("Build Next.js application").allowUnknownOption(true);
75
+ addEnvFlags(buildCmd).action(async (opts) => {
76
+ const passthrough = buildCmd.args.filter(
77
+ (a) => a !== "--staging" && a !== "--production"
78
+ );
79
+ await runNextCommand("build", getEnvironment(opts), passthrough);
80
+ });
81
+ var startCmd = new Command("start").description("Start Next.js production server").allowUnknownOption(true);
82
+ addEnvFlags(startCmd).action(async (opts) => {
83
+ const passthrough = startCmd.args.filter(
84
+ (a) => a !== "--staging" && a !== "--production"
85
+ );
86
+ await runNextCommand("start", getEnvironment(opts), passthrough);
87
+ });
88
+ program.addCommand(devCmd);
89
+ program.addCommand(buildCmd);
90
+ program.addCommand(startCmd);
91
+ program.parse(process.argv);
@@ -0,0 +1,14 @@
1
+ import { NextConfig } from 'next';
2
+ import { EnvlockOptions } from 'envlock-core';
3
+ export { EnvlockOptions } from 'envlock-core';
4
+ import { createEnv as createEnv$1 } from '@t3-oss/env-nextjs';
5
+
6
+ type EnvlockNextConfig = NextConfig & {
7
+ __envlock: EnvlockOptions;
8
+ };
9
+ declare function withEnvlock(nextConfig: NextConfig, options?: EnvlockOptions): EnvlockNextConfig;
10
+
11
+ type CreateEnvArgs = Parameters<typeof createEnv$1>[0];
12
+ declare function createEnv(options: CreateEnvArgs): never;
13
+
14
+ export { type EnvlockNextConfig, createEnv, withEnvlock };
package/dist/index.js ADDED
@@ -0,0 +1,29 @@
1
+ // src/plugin.ts
2
+ import { validateOnePasswordEnvId } from "envlock-core";
3
+ function withEnvlock(nextConfig, options) {
4
+ if (!options?.onePasswordEnvId) {
5
+ console.warn(
6
+ "[envlock] No onePasswordEnvId provided to withEnvlock(). Set it to your 1Password Environment ID for automatic secret injection. Alternatively, set ENVLOCK_OP_ENV_ID in your environment."
7
+ );
8
+ } else {
9
+ validateOnePasswordEnvId(options.onePasswordEnvId);
10
+ }
11
+ return {
12
+ ...nextConfig,
13
+ __envlock: options ?? { onePasswordEnvId: "" }
14
+ };
15
+ }
16
+
17
+ // src/env/index.ts
18
+ import { createEnv as t3CreateEnv } from "@t3-oss/env-nextjs";
19
+ function createEnv(options) {
20
+ return t3CreateEnv({
21
+ skipValidation: !!process.env["SKIP_ENV_VALIDATION"],
22
+ emptyStringAsUndefined: true,
23
+ ...options
24
+ });
25
+ }
26
+ export {
27
+ createEnv,
28
+ withEnvlock
29
+ };
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "envlock-next",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Next.js plugin, createEnv wrapper, and CLI for envlock",
6
+ "license": "MIT",
7
+ "author": "Benjamin Davies",
8
+ "homepage": "https://github.com/BenDavies1218/envlock#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/BenDavies1218/envlock.git",
12
+ "directory": "packages/next"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/BenDavies1218/envlock/issues"
16
+ },
17
+ "engines": {
18
+ "node": ">=18"
19
+ },
20
+ "bin": {
21
+ "envlock": "./dist/cli/index.js"
22
+ },
23
+ "exports": {
24
+ ".": {
25
+ "import": "./dist/index.js",
26
+ "types": "./dist/index.d.ts"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "dependencies": {
33
+ "commander": "^12.0.0",
34
+ "envlock-core": "0.1.0"
35
+ },
36
+ "peerDependencies": {
37
+ "@t3-oss/env-nextjs": ">=0.12.0",
38
+ "next": ">=14.0.0",
39
+ "zod": ">=3.0.0"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "@t3-oss/env-nextjs": {
43
+ "optional": true
44
+ },
45
+ "zod": {
46
+ "optional": true
47
+ }
48
+ },
49
+ "devDependencies": {
50
+ "@t3-oss/env-nextjs": "^0.12.0",
51
+ "@types/node": "^20.14.10",
52
+ "next": "^15.2.3",
53
+ "tsup": "^8.0.0",
54
+ "typescript": "^5.8.2",
55
+ "vitest": "^3.0.0",
56
+ "zod": "^3.24.2",
57
+ "envlock-core": "0.1.0"
58
+ },
59
+ "scripts": {
60
+ "build": "tsup",
61
+ "dev": "tsup --watch",
62
+ "test": "vitest run",
63
+ "test:watch": "vitest"
64
+ }
65
+ }