-
Notifications
You must be signed in to change notification settings - Fork 1
feat: auto-configure .npmrc for private beta packages #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "@thatopen/services": minor | ||
| --- | ||
|
|
||
| CLI: auto-configure `.npmrc` for private beta packages. | ||
|
|
||
| `thatopen create --beta` (and `thatopen login` inside a beta project) now fetch | ||
| read-only npm credentials from the platform and write a project `.npmrc`, so | ||
| `npm install` of the private `@thatopen-platform/*-beta` packages just works for | ||
| Founding members — no manual token setup. Adds | ||
| `EngineServicesClient.getNpmCredentials()` and exports the `NpmCredentials` type. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { describe, it, expect, beforeEach, afterEach } from 'vitest'; | ||
| import { | ||
| mkdtempSync, | ||
| rmSync, | ||
| existsSync, | ||
| readFileSync, | ||
| writeFileSync, | ||
| } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { setupNpmrc } from './npmrc'; | ||
| import { RequestError } from '../../core/request-error'; | ||
| import type { EngineServicesClient } from '../../core/client'; | ||
|
|
||
| function fakeClient(getNpmCredentials: () => Promise<unknown>): EngineServicesClient { | ||
| return { getNpmCredentials } as unknown as EngineServicesClient; | ||
| } | ||
|
|
||
| describe('setupNpmrc', () => { | ||
| let dir: string; | ||
|
|
||
| beforeEach(() => { | ||
| dir = mkdtempSync(join(tmpdir(), 'npmrc-test-')); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('writes .npmrc and returns written on success', async () => { | ||
| const npmrc = | ||
| '@thatopen-platform:registry=https://registry.npmjs.org/\n' + | ||
| '//registry.npmjs.org/:_authToken=npm_ro\n'; | ||
| const client = fakeClient(async () => ({ | ||
| registry: 'https://registry.npmjs.org/', | ||
| scope: '@thatopen-platform', | ||
| token: 'npm_ro', | ||
| npmrc, | ||
| })); | ||
|
|
||
| const result = await setupNpmrc(client, dir); | ||
|
|
||
| expect(result).toEqual({ status: 'written', scope: '@thatopen-platform' }); | ||
| expect(readFileSync(join(dir, '.npmrc'), 'utf-8')).toBe(npmrc); | ||
| }); | ||
|
|
||
| it('returns forbidden and writes nothing on a 403', async () => { | ||
| const client = fakeClient(async () => { | ||
| throw new RequestError( | ||
| 403, | ||
| 'Forbidden', | ||
| JSON.stringify({ message: 'Community membership required' }), | ||
| ); | ||
| }); | ||
|
|
||
| const result = await setupNpmrc(client, dir); | ||
|
|
||
| expect(result).toEqual({ status: 'forbidden' }); | ||
| expect(existsSync(join(dir, '.npmrc'))).toBe(false); | ||
| }); | ||
|
|
||
| it('returns error (and writes nothing) on any other failure', async () => { | ||
| const client = fakeClient(async () => { | ||
| throw new Error('network down'); | ||
| }); | ||
|
|
||
| const result = await setupNpmrc(client, dir); | ||
|
|
||
| expect(result.status).toBe('error'); | ||
| expect(existsSync(join(dir, '.npmrc'))).toBe(false); | ||
| }); | ||
|
|
||
| describe('.gitignore protection (Sergio review #19)', () => { | ||
| const okClient = () => | ||
| fakeClient(async () => ({ | ||
| registry: 'https://registry.npmjs.org/', | ||
| scope: '@thatopen-platform', | ||
| token: 'npm_ro', | ||
| npmrc: '//registry.npmjs.org/:_authToken=npm_ro\n', | ||
| })); | ||
|
|
||
| const gitignore = () => | ||
| readFileSync(join(dir, '.gitignore'), 'utf-8'); | ||
|
|
||
| it('creates .gitignore ignoring .npmrc when none exists', async () => { | ||
| await setupNpmrc(okClient(), dir); | ||
| expect(gitignore()).toBe('.npmrc\n'); | ||
| }); | ||
|
|
||
| it('appends .npmrc to an existing .gitignore that lacks it', async () => { | ||
| writeFileSync(join(dir, '.gitignore'), 'node_modules\ndist\n'); | ||
| await setupNpmrc(okClient(), dir); | ||
| expect(gitignore()).toBe('node_modules\ndist\n.npmrc\n'); | ||
| }); | ||
|
|
||
| it('adds a newline before appending when the file has no trailing newline', async () => { | ||
| writeFileSync(join(dir, '.gitignore'), 'node_modules'); | ||
| await setupNpmrc(okClient(), dir); | ||
| expect(gitignore()).toBe('node_modules\n.npmrc\n'); | ||
| }); | ||
|
|
||
| it('does not duplicate .npmrc when already ignored', async () => { | ||
| writeFileSync(join(dir, '.gitignore'), 'node_modules\n.npmrc\ndist\n'); | ||
| await setupNpmrc(okClient(), dir); | ||
| expect(gitignore()).toBe('node_modules\n.npmrc\ndist\n'); | ||
| }); | ||
|
|
||
| it('does not write .gitignore when the account is forbidden', async () => { | ||
| const client = fakeClient(async () => { | ||
| throw new RequestError(403, 'Forbidden', '{}'); | ||
| }); | ||
| await setupNpmrc(client, dir); | ||
| expect(existsSync(join(dir, '.gitignore'))).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { | ||
| appendFileSync, | ||
| existsSync, | ||
| readFileSync, | ||
| writeFileSync, | ||
| } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { EngineServicesClient } from '../../core/client'; | ||
| import { RequestError } from '../../core/request-error'; | ||
| import { resolveConfig } from './config'; | ||
|
|
||
| export type NpmrcResult = | ||
| | { status: 'written'; scope: string } | ||
| | { status: 'forbidden' } | ||
| | { status: 'error'; message: string }; | ||
|
|
||
| /** | ||
| * Make sure `<dir>/.gitignore` ignores `.npmrc` before we write a credential | ||
| * into it. The scaffold template already covers this for `create`, but | ||
| * `swap`/`login` run in existing projects whose `.gitignore` we don't own — so | ||
| * without this the token could be committed. Creates `.gitignore` if absent. | ||
| */ | ||
| function ensureNpmrcIgnored(dir: string): void { | ||
| const gitignorePath = join(dir, '.gitignore'); | ||
| if (!existsSync(gitignorePath)) { | ||
| writeFileSync(gitignorePath, '.npmrc\n'); | ||
| return; | ||
| } | ||
| const content = readFileSync(gitignorePath, 'utf-8'); | ||
| const alreadyIgnored = content | ||
| .split(/\r?\n/) | ||
| .some((line) => line.trim() === '.npmrc'); | ||
| if (alreadyIgnored) return; | ||
| const prefix = content.length === 0 || content.endsWith('\n') ? '' : '\n'; | ||
| appendFileSync(gitignorePath, `${prefix}.npmrc\n`); | ||
| } | ||
|
|
||
| /** | ||
| * Fetches the Founders npm credentials and writes them to `<dir>/.npmrc`, so | ||
| * `npm install` can resolve the private `@thatopen-platform` beta packages. | ||
| * | ||
| * Best-effort by design — it never throws, so scaffolding and login keep | ||
| * flowing: | ||
| * - `forbidden`: the account isn't a FOUNDING member (backend 403); no file. | ||
| * - `error`: any other failure (network, misconfig); no file. | ||
| * - `written`: `.npmrc` created (mode 0600, it carries a credential). | ||
| */ | ||
| export async function setupNpmrc( | ||
| client: EngineServicesClient, | ||
| dir: string, | ||
| ): Promise<NpmrcResult> { | ||
| try { | ||
| const creds = await client.getNpmCredentials(); | ||
| ensureNpmrcIgnored(dir); | ||
| writeFileSync(join(dir, '.npmrc'), creds.npmrc, { mode: 0o600 }); | ||
| return { status: 'written', scope: creds.scope }; | ||
| } catch (err) { | ||
| if (err instanceof RequestError && err.status === 403) { | ||
| return { status: 'forbidden' }; | ||
| } | ||
| const message = err instanceof Error ? err.message : String(err); | ||
| return { status: 'error', message }; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * CLI glue for the `--beta` flows (`create` and `swap`): resolves the logged-in | ||
| * config, writes an authenticated `.npmrc` into `dir`, and prints a | ||
| * human-readable status. Best-effort — never throws, so the install still runs. | ||
| */ | ||
| export async function configureBetaNpmrc(dir: string): Promise<void> { | ||
| const config = resolveConfig(dir); | ||
| if (!config) { | ||
| console.log( | ||
| ' Beta libraries are private. Run `thatopen login --token <token>`,', | ||
| ); | ||
| console.log( | ||
| ' then `npm install`, or add your beta npm token to .npmrc manually.', | ||
| ); | ||
| return; | ||
| } | ||
| const client = new EngineServicesClient(config.accessToken, config.apiUrl); | ||
| const result = await setupNpmrc(client, dir); | ||
| if (result.status === 'written') { | ||
| console.log(` Beta access configured — wrote .npmrc for ${result.scope}.`); | ||
| } else if (result.status === 'forbidden') { | ||
| console.log( | ||
| ' Your account is not a Founding member — beta libraries need Founding', | ||
| ); | ||
| console.log(' access, so the install will fail until you have it.'); | ||
| } else { | ||
| console.log( | ||
| ` Could not fetch beta npm credentials (${result.message}). Set your`, | ||
| ); | ||
| console.log(' token in .npmrc manually if the install fails.'); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ node_modules | |
| dist | ||
| *.zip | ||
| .thatopen | ||
| .npmrc | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make sure that .npmrc is included in .gitignore before entering the token here? In swap/login for existing projects, it might not be included (only the
createcommand covers it by default), and we could end up pushing the credentials to the repo.