Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit187b823

Browse files
pvdlggr2m
authored andcommitted
fix: Pass registry URL tonpm CLI with--registry
The environment variable `npm_config_registry` takes precedence to the registry configured in `.npmrc` when running `npm` commands.Yarn set this variable to `https://registry.yarnpkg.com` creating an `EINVALIDNPMTOKEN` error when running `semantic-release` with Yarn.Passing `--registry` with the value retrieved from `.npmrc` or `package.json` override the `npm_config_registry` set by Yarn.
1 parent0f654b1 commit187b823

File tree

7 files changed

+82
-50
lines changed

7 files changed

+82
-50
lines changed

‎index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function publish(pluginConfig, {nextRelease: {version}, logger}) {
2828
awaitverifyNpm(pkg,logger);
2929
verified=true;
3030
}
31-
awaitpublishNpm(version,logger);
31+
awaitpublishNpm(pkg,version,logger);
3232
}
3333

3434
module.exports={verifyConditions, getLastRelease, publish};

‎lib/publish.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
constexeca=require('execa');
2+
constgetRegistry=require('./get-registry');
23
constupdatePackageVersion=require('./update-package-version');
34

4-
module.exports=async(version,logger)=>{
5+
module.exports=async({publishConfig, name},version,logger)=>{
6+
constregistry=awaitgetRegistry(publishConfig,name);
57
awaitupdatePackageVersion(version,logger);
68

79
logger.log('Publishing version %s to npm registry',version);
8-
constshell=awaitexeca('npm',['publish']);
10+
constshell=awaitexeca('npm',['publish','--registry',registry]);
911
process.stdout.write(shell.stdout);
1012
};

‎lib/set-npmrc-auth.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ const {appendFile} = require('fs-extra');
22
constgetAuthToken=require('registry-auth-token');
33
constnerfDart=require('nerf-dart');
44
constSemanticReleaseError=require('@semantic-release/error');
5-
constgetRegistry=require('./get-registry');
65

7-
module.exports=async({publishConfig, name},logger)=>{
8-
constregistry=awaitgetRegistry(publishConfig,name);
6+
module.exports=async(registry,logger)=>{
97
logger.log('Verify authentication for registry %s',registry);
108
const{NPM_TOKEN,NPM_USERNAME,NPM_PASSWORD,NPM_EMAIL}=process.env;
119

‎lib/verify.js‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
constexeca=require('execa');
22
constSemanticReleaseError=require('@semantic-release/error');
3+
constgetRegistry=require('./get-registry');
34
constsetNpmrcAuth=require('./set-npmrc-auth');
45

56
module.exports=async(pkg,logger)=>{
6-
awaitsetNpmrcAuth(pkg,logger);
7+
constregistry=awaitgetRegistry(pkg.publishConfig,pkg.name);
8+
awaitsetNpmrcAuth(registry,logger);
79
try{
8-
awaitexeca('npm',['whoami']);
10+
awaitexeca('npm',['whoami','--registry',registry]);
911
}catch(err){
1012
thrownewSemanticReleaseError('Invalid npm token.','EINVALIDNPMTOKEN');
1113
}

‎test/get-registry.test.js‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
importtestfrom'ava';
2+
import{appendFile}from'fs-extra';
3+
importtempyfrom'tempy';
4+
importgetRegistryfrom'../lib/get-registry';
5+
6+
test.beforeEach(t=>{
7+
// Save the current process.env
8+
t.context.env=Object.assign({},process.env);
9+
// Save the current working diretory
10+
t.context.cwd=process.cwd();
11+
// Change current working directory to a temp directory
12+
process.chdir(tempy.directory());
13+
});
14+
15+
test.afterEach.always(t=>{
16+
// Restore the current working directory
17+
process.chdir(t.context.cwd);
18+
});
19+
20+
test.serial('Get default registry',asynct=>{
21+
constregistry=awaitgetRegistry({},'package-name');
22+
23+
t.is(registry,'https://registry.npmjs.org/');
24+
});
25+
26+
test.serial('Get the registry configured in ".npmrc" and normalize trailing slash',asynct=>{
27+
awaitappendFile('./.npmrc','registry = https://custom1.registry.com');
28+
constregistry=awaitgetRegistry({},'package-name');
29+
30+
t.is(registry,'https://custom1.registry.com/');
31+
});
32+
33+
test.serial('Get the registry configured from "publishConfig"',asynct=>{
34+
constregistry=awaitgetRegistry({registry:'https://custom2.registry.com/'},'package-name');
35+
36+
t.is(registry,'https://custom2.registry.com/');
37+
});
38+
39+
test.serial('Get the registry configured in ".npmrc" for scoped package',asynct=>{
40+
awaitappendFile('./.npmrc','@scope:registry = https://custom3.registry.com');
41+
constregistry=awaitgetRegistry({},'@scope/package-name');
42+
43+
t.is(registry,'https://custom3.registry.com/');
44+
});

‎test/integration.test.js‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,24 @@ test.beforeEach(t => {
3939
});
4040

4141
test.afterEach.always(t=>{
42-
// Clear `rc` from the npm cache as it cache the relative path of .npmrc files, preventing to load a new file after changing current working directory
43-
clearModule('rc');
4442
// Restore process.env
4543
process.env=Object.assign({},t.context.env);
4644
// Restore the current working directory
4745
process.chdir(t.context.cwd);
4846
});
4947

5048
test.after.always(async()=>{
51-
// Stop the local NPM registry
52-
awaitnpmRegistry.stop();
5349
// Restore stdout and stderr
5450
processStderr.restore();
5551
processStdout.restore();
52+
// Stop the local NPM registry
53+
awaitnpmRegistry.stop();
5654
});
5755

5856
test.serial('Throws error if NPM token is invalid',asynct=>{
5957
process.env.NPM_TOKEN='wrong_token';
6058
constpkg={name:'published',version:'1.0.0',publishConfig:{registry:npmRegistry.url}};
6159
awaitwriteJson('./package.json',pkg);
62-
awaitappendFile('./.npmrc',`\nregistry =${npmRegistry.url}`);
6360
consterror=awaitt.throws(t.context.m.verifyConditions({},{logger:t.context.logger}));
6461

6562
t.true(errorinstanceofSemanticReleaseError);
@@ -81,6 +78,18 @@ test.serial('Verify npm auth and package', async t => {
8178
t.regex(npmrc,/email=/);
8279
});
8380

81+
test.serial('Verify npm auth and package with "npm_config_registry" env var set by yarn',asynct=>{
82+
Object.assign(process.env,npmRegistry.authEnv);
83+
process.env.npm_config_registry='https://registry.yarnpkg.com';// eslint-disable-line camelcase
84+
constpkg={name:'valid-token',publishConfig:{registry:npmRegistry.url}};
85+
awaitwriteJson('./package.json',pkg);
86+
awaitt.notThrows(t.context.m.verifyConditions({},{logger:t.context.logger}));
87+
88+
constnpmrc=(awaitreadFile('.npmrc')).toString();
89+
t.regex(npmrc,/_auth=/);
90+
t.regex(npmrc,/email=/);
91+
});
92+
8493
test.serial('Return nothing if no version if published',asynct=>{
8594
Object.assign(process.env,npmRegistry.authEnv);
8695
constpkg={name:'not-published',publishConfig:{registry:npmRegistry.url}};

‎test/set-npmrc-auth.test.js‎

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import {readFile, appendFile} from 'fs-extra';
22
importtestfrom'ava';
33
import{stub}from'sinon';
44
importtempyfrom'tempy';
5-
importclearModulefrom'clear-module';
6-
importSemanticReleaseErrorfrom'@semantic-release/error';
75
importsetNpmrcAuthfrom'../lib/set-npmrc-auth';
86

97
test.beforeEach(t=>{
@@ -18,35 +16,21 @@ test.beforeEach(t => {
1816
t.context.cwd=process.cwd();
1917
// Change current working directory to a temp directory
2018
process.chdir(tempy.directory());
21-
// Prevent to use `.npmrc` from the home directory in case there is a valid token set there
22-
process.env.HOME=process.cwd();
23-
process.env.USERPROFILE=process.cwd();
2419
// Stub the logger
2520
t.context.log=stub();
2621
t.context.logger={log:t.context.log};
2722
});
2823

2924
test.afterEach.always(t=>{
30-
// Clear `rc` from the npm cache as it cache the relative path of .npmrc files, preventing to load a new file after changing current working directory. See https://github.com/dominictarr/rc/issues/101
31-
clearModule('rc');
3225
// Restore process.env
3326
process.env=Object.assign({},t.context.env);
3427
// Restore the current working directory
3528
process.chdir(t.context.cwd);
3629
});
3730

38-
test.serial('Set auth with "NPM_TOKEN" and default registry',asynct=>{
31+
test.serial('Set auth with "NPM_TOKEN"',asynct=>{
3932
process.env.NPM_TOKEN='npm_token';
40-
awaitsetNpmrcAuth({name:'package-name'},t.context.logger);
41-
42-
constnpmrc=(awaitreadFile('.npmrc')).toString();
43-
t.regex(npmrc,/\/\/registry.npmjs.org\/:_authToken=\$\{NPM_TOKEN\}/);
44-
t.true(t.context.log.calledWith('Wrote NPM_TOKEN to .npmrc.'));
45-
});
46-
47-
test.serial('Set auth with "NPM_TOKEN" and custom registry',asynct=>{
48-
process.env.NPM_TOKEN='npm_token';
49-
awaitsetNpmrcAuth({name:'package-name',publishConfig:{registry:'http://custom.registry.com'}},t.context.logger);
33+
awaitsetNpmrcAuth('http://custom.registry.com',t.context.logger);
5034

5135
constnpmrc=(awaitreadFile('.npmrc')).toString();
5236
t.regex(npmrc,/\/\/custom.registry.com\/:_authToken=\$\{NPM_TOKEN\}/);
@@ -58,7 +42,7 @@ test.serial('Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"', asyn
5842
process.env.NPM_PASSWORD='npm_pasword';
5943
process.env.NPM_EMAIL='npm_email';
6044

61-
awaitsetNpmrcAuth({name:'package-name'},t.context.logger);
45+
awaitsetNpmrcAuth('http://custom.registry.com',t.context.logger);
6246

6347
constnpmrc=(awaitreadFile('.npmrc')).toString();
6448
t.regex(
@@ -72,63 +56,56 @@ test.serial('Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"', asyn
7256
});
7357

7458
test.serial('Do not modify ".npmrc" if auth is already configured',asynct=>{
75-
awaitappendFile('./.npmrc',`//registry.npmjs.org/:_authToken = \${NPM_TOKEN}`);
76-
awaitsetNpmrcAuth({name:'package-name'},t.context.logger);
77-
78-
t.true(t.context.log.calledOnce);
79-
});
80-
81-
test.serial('Do not modify ".npmrc" if auth is already configured with custom registry',asynct=>{
8259
awaitappendFile('./.npmrc',`//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
83-
awaitsetNpmrcAuth({name:'package-name',publishConfig:{registry:'http://custom.registry.com'}},t.context.logger);
60+
awaitsetNpmrcAuth('http://custom.registry.com',t.context.logger);
8461

8562
t.true(t.context.log.calledOnce);
8663
});
8764

88-
test.serial('Do not modify ".npmrc"is auth is already configured for a scoped package',asynct=>{
65+
test.serial('Do not modify ".npmrc"if auth is already configured for a scoped package',asynct=>{
8966
awaitappendFile(
9067
'./.npmrc',
9168
`@scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
9269
);
93-
awaitsetNpmrcAuth({name:'@scope/package-name'},t.context.logger);
70+
awaitsetNpmrcAuth('http://custom.registry.com',t.context.logger);
9471

9572
t.true(t.context.log.calledOnce);
9673
});
9774

9875
test.serial('Throw error if "NPM_TOKEN" is missing',asynct=>{
99-
consterror=awaitt.throws(setNpmrcAuth({name:'package-name'},t.context.logger));
76+
consterror=awaitt.throws(setNpmrcAuth('http://custom.registry.com',t.context.logger));
10077

101-
t.true(errorinstanceofSemanticReleaseError);
78+
t.is(error.name,'SemanticReleaseError');
10279
t.is(error.message,'No npm token specified.');
10380
t.is(error.code,'ENONPMTOKEN');
10481
});
10582

10683
test.serial('Throw error if "NPM_USERNAME" is missing',asynct=>{
10784
process.env.NPM_PASSWORD='npm_pasword';
10885
process.env.NPM_EMAIL='npm_email';
109-
consterror=awaitt.throws(setNpmrcAuth({name:'package-name'},t.context.logger));
86+
consterror=awaitt.throws(setNpmrcAuth('http://custom.registry.com',t.context.logger));
11087

111-
t.true(errorinstanceofSemanticReleaseError);
88+
t.is(error.name,'SemanticReleaseError');
11289
t.is(error.message,'No npm token specified.');
11390
t.is(error.code,'ENONPMTOKEN');
11491
});
11592

11693
test.serial('Throw error if "NPM_PASSWORD" is missing',asynct=>{
11794
process.env.NPM_USERNAME='npm_username';
11895
process.env.NPM_EMAIL='npm_email';
119-
consterror=awaitt.throws(setNpmrcAuth({name:'package-name'},t.context.logger));
96+
consterror=awaitt.throws(setNpmrcAuth('http://custom.registry.com',t.context.logger));
12097

121-
t.true(errorinstanceofSemanticReleaseError);
98+
t.is(error.name,'SemanticReleaseError');
12299
t.is(error.message,'No npm token specified.');
123100
t.is(error.code,'ENONPMTOKEN');
124101
});
125102

126103
test.serial('Throw error if "NPM_EMAIL" is missing',asynct=>{
127104
process.env.NPM_USERNAME='npm_username';
128105
process.env.NPM_PASSWORD='npm_password';
129-
consterror=awaitt.throws(setNpmrcAuth({name:'package-name'},t.context.logger));
106+
consterror=awaitt.throws(setNpmrcAuth('http://custom.registry.com',t.context.logger));
130107

131-
t.true(errorinstanceofSemanticReleaseError);
108+
t.is(error.name,'SemanticReleaseError');
132109
t.is(error.message,'No npm token specified.');
133110
t.is(error.code,'ENONPMTOKEN');
134111
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp