three-blocks-login 0.1.11 → 0.1.12

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.
Files changed (3) hide show
  1. package/README.md +75 -51
  2. package/bin/login.js +29 -0
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -16,7 +16,7 @@ npx -y three-blocks-login@latest@latest
16
16
 
17
17
  The CLI supports three modes for storing authentication:
18
18
 
19
- ### 1. Project Mode (Recommended for CI/Vercel)
19
+ ### 1. Project Mode (Default)
20
20
 
21
21
  Writes `.npmrc` to the current directory. Best for project-specific authentication.
22
22
 
@@ -24,15 +24,7 @@ Writes `.npmrc` to the current directory. Best for project-specific authenticati
24
24
  npx -y three-blocks-login@latest --mode project --scope @three-blocks --channel stable
25
25
  ```
26
26
 
27
- **Use this in package.json preinstall scripts:**
28
-
29
- ```json
30
- {
31
- "scripts": {
32
- "preinstall": "npx -y three-blocks-login@latest"
33
- }
34
- }
35
- ```
27
+ **For CI/CD:** Run this command BEFORE `pnpm install`, not as a preinstall hook. See [CI/CD Setup](#cicd-setup) for platform-specific examples.
36
28
 
37
29
  **Note:** Flags are optional - the CLI auto-detects `--mode project` in CI environments, defaults to `--scope @three-blocks`, and `--channel stable`.
38
30
 
@@ -79,64 +71,77 @@ npx -y three-blocks-login@latest --mode user
79
71
  | `THREE_BLOCKS_LOGIN_PRINT_SHELL` | Print shell exports in env mode (`1`) |
80
72
  | `CI` | Enable non-interactive mode (`1`) |
81
73
 
82
- ## CI/CD & Vercel Setup
74
+ ## CI/CD Setup
83
75
 
84
- ### Quick Setup (3 steps)
76
+ > ⚠️ **pnpm users:** The preinstall hook approach is unreliable with pnpm because pnpm resolves packages BEFORE running preinstall scripts. Use the **CI Install Override** approach below instead.
85
77
 
86
- 1. **Commit a base `.npmrc`** to your repository (safe - no secrets):
87
- ```
88
- @three-blocks:registry=https://three-blocks-196905988268.d.codeartifact.ap-northeast-1.amazonaws.com/npm/core/
89
- ```
78
+ ### Recommended: CI Install Override (All CIs)
90
79
 
91
- 2. **Add preinstall script** to package.json:
92
- ```json
93
- {
94
- "scripts": {
95
- "preinstall": "npx -y three-blocks-login@latest"
96
- }
97
- }
98
- ```
80
+ Run the login command BEFORE `pnpm install`, not as a preinstall hook:
99
81
 
100
- 3. **Set environment variable** in Vercel/CI:
101
- - **Name**: `THREE_BLOCKS_SECRET_KEY`
102
- - **Value**: Your license key (starts with `tb_...`)
82
+ #### Vercel
83
+
84
+ ```json
85
+ // vercel.json
86
+ {
87
+ "installCommand": "npx -y three-blocks-login@latest && pnpm install"
88
+ }
89
+ ```
103
90
 
104
- ### How It Works
91
+ Set `THREE_BLOCKS_SECRET_KEY` in Vercel → Project Settings → Environment Variables.
105
92
 
106
- - Committed `.npmrc` tells pnpm **WHERE** to find packages
107
- - Preinstall script adds the **AUTH TOKEN** dynamically
108
- - This solves pnpm's resolution timing issue (it needs to know the registry before running preinstall)
93
+ #### AWS Amplify
109
94
 
110
- ### Vercel Environment Variables
95
+ ```yaml
96
+ # amplify.yml
97
+ version: 1
98
+ frontend:
99
+ phases:
100
+ preBuild:
101
+ commands:
102
+ - npx -y three-blocks-login@latest
103
+ - pnpm install
104
+ build:
105
+ commands:
106
+ - pnpm build
107
+ ```
111
108
 
112
- 1. Go to your Vercel project settings
113
- 2. Navigate to: Settings → Environment Variables
114
- 3. Add `THREE_BLOCKS_SECRET_KEY` environment variable
115
- 4. Set the value to your license key (starts with `tb_...`)
116
- 5. Make sure it's available in all environments (Production, Preview, Development)
109
+ Set `THREE_BLOCKS_SECRET_KEY` in Amplify Console App Settings → Environment Variables.
117
110
 
118
- ### GitHub Actions
111
+ #### GitHub Actions
119
112
 
120
113
  ```yaml
121
- - name: Authenticate to Three Blocks
114
+ - name: Auth three-blocks
122
115
  run: npx -y three-blocks-login@latest
123
116
  env:
124
117
  THREE_BLOCKS_SECRET_KEY: ${{ secrets.THREE_BLOCKS_SECRET_KEY }}
118
+
119
+ - name: Install dependencies
120
+ run: pnpm install
125
121
  ```
126
122
 
127
- **Or better yet**, just rely on the preinstall script - GitHub Actions will run it automatically during `npm install` or `pnpm install`.
123
+ #### Netlify
128
124
 
129
- ## Troubleshooting
125
+ ```toml
126
+ # netlify.toml
127
+ [build]
128
+ command = "npx -y three-blocks-login@latest && pnpm install && pnpm build"
129
+ ```
130
130
 
131
- ### 401 Unauthorized on Vercel
131
+ Set `THREE_BLOCKS_SECRET_KEY` in Netlify → Site Settings → Environment Variables.
132
132
 
133
- **Problem:** Getting `ERR_PNPM_FETCH_401` or npm 401 errors on Vercel.
133
+ #### Generic CI
134
134
 
135
- **Root Cause:** pnpm resolves package locations BEFORE running preinstall scripts.
135
+ Always run login before install:
136
+ ```bash
137
+ npx -y three-blocks-login@latest && pnpm install
138
+ ```
139
+
140
+ ### Alternative: Preinstall Hook (npm only)
136
141
 
137
- **Solution (3 steps):**
142
+ > ⚠️ This approach only works reliably with **npm**, not pnpm. For pnpm, use CI Install Override above.
138
143
 
139
- 1. **Commit a base `.npmrc`** with just the registry URL (no auth token):
144
+ 1. **Commit a base `.npmrc`** (no auth token):
140
145
  ```
141
146
  @three-blocks:registry=https://three-blocks-196905988268.d.codeartifact.ap-northeast-1.amazonaws.com/npm/core/
142
147
  ```
@@ -150,12 +155,31 @@ npx -y three-blocks-login@latest --mode user
150
155
  }
151
156
  ```
152
157
 
153
- 3. **Verify `THREE_BLOCKS_SECRET_KEY`** is set in Vercel environment variables
158
+ 3. **Set `THREE_BLOCKS_SECRET_KEY`** in CI environment variables.
159
+
160
+ ## Troubleshooting
161
+
162
+ ### 401 Unauthorized on Vercel/Amplify/CI
163
+
164
+ **Problem:** Getting `ERR_PNPM_FETCH_401` or 401 errors in CI.
165
+
166
+ **Root Cause:** pnpm resolves packages BEFORE running preinstall scripts. If you're using a preinstall hook with pnpm, it won't work reliably.
167
+
168
+ **Solution:** Use CI Install Override instead of preinstall hooks.
169
+
170
+ For Vercel, add to `vercel.json`:
171
+ ```json
172
+ {
173
+ "installCommand": "npx -y three-blocks-login@latest && pnpm install"
174
+ }
175
+ ```
176
+
177
+ For other CIs, see the [CI/CD Setup](#cicd-setup) section above.
154
178
 
155
- **Why this works:**
156
- - Committed `.npmrc` tells pnpm WHERE to look for packages
157
- - Preinstall script adds the auth token dynamically
158
- - pnpm can now resolve packages AND authenticate when downloading
179
+ **Also check:**
180
+ - `THREE_BLOCKS_SECRET_KEY` is set in CI environment variables
181
+ - `.npmrc` does NOT contain an `_authToken` line (tokens expire in 12h)
182
+ - Remove any preinstall hook from `package.json` if using CI override
159
183
 
160
184
  ### License Key Format
161
185
 
package/bin/login.js CHANGED
@@ -241,6 +241,32 @@ if ( SKIP_LOGIN ) {
241
241
 
242
242
  }
243
243
 
244
+ // Detect pnpm + preinstall timing issue
245
+ const isPnpmPreinstall = () => {
246
+
247
+ const lifecycle = String( process.env.npm_lifecycle_event || '' ).toLowerCase();
248
+ const userAgent = String( process.env.npm_config_user_agent || '' ).toLowerCase();
249
+ return lifecycle === 'preinstall' && userAgent.includes( 'pnpm' );
250
+
251
+ };
252
+
253
+ const warnPnpmPreinstall = () => {
254
+
255
+ if ( ! isPnpmPreinstall() || QUIET ) return;
256
+ console.error( '' );
257
+ console.error( cyan( '⚠' ) + ' ' + bold( 'pnpm preinstall timing warning' ) );
258
+ console.error( dim( ' pnpm resolves packages BEFORE running preinstall hooks.' ) );
259
+ console.error( dim( ' If you see 401 errors, use CI install command override instead:' ) );
260
+ console.error( '' );
261
+ console.error( dim( ' Vercel: ' ) + cyan( '"installCommand": "npx -y three-blocks-login@latest && pnpm install"' ) );
262
+ console.error( dim( ' Amplify: ' ) + cyan( 'preBuild: npx -y three-blocks-login@latest && pnpm install' ) );
263
+ console.error( dim( ' Generic: ' ) + cyan( 'npx -y three-blocks-login@latest && pnpm install' ) );
264
+ console.error( '' );
265
+ console.error( dim( ' Docs: https://threejs-blocks.com/auth-guide' ) );
266
+ console.error( '' );
267
+
268
+ };
269
+
244
270
  // Load .env from current working directory (no deps)
245
271
  loadEnvFromDotfile( process.cwd() );
246
272
 
@@ -722,6 +748,9 @@ const log = {
722
748
 
723
749
  ( async () => {
724
750
 
751
+ // Warn about pnpm preinstall timing issue early
752
+ warnPnpmPreinstall();
753
+
725
754
  try {
726
755
 
727
756
  let LICENSE = args.license || process.env.THREE_BLOCKS_SECRET_KEY;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-blocks-login",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "Fetch a short-lived token from the three-blocks broker and configure npm for the current context.",
5
5
  "type": "module",
6
6
  "bin": {