Skip to content

Commit f2e30c7

Browse files
committed
feat: use cwd and env options passed by core
BREAKING CHANGE: require `semantic-release` >= `15.8.0`
1 parent 14f9d11 commit f2e30c7

17 files changed

+453
-420
lines changed

index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ async function verifyConditions(pluginConfig, context) {
2424
const errors = verifyNpmConfig(pluginConfig);
2525

2626
try {
27-
const pkg = await getPkg(pluginConfig.pkgRoot);
27+
const pkg = await getPkg(pluginConfig, context);
2828

2929
// Verify the npm authentication only if `npmPublish` is not false and `pkg.private` is not `true`
3030
if (pluginConfig.npmPublish !== false && pkg.private !== true) {
31-
setLegacyToken();
31+
setLegacyToken(context);
3232
await verifyNpmAuth(pluginConfig, pkg, context);
3333
}
3434
} catch (err) {
@@ -46,9 +46,9 @@ async function prepare(pluginConfig, context) {
4646

4747
try {
4848
// Reload package.json in case a previous external step updated it
49-
pkg = await getPkg(pluginConfig.pkgRoot);
49+
pkg = await getPkg(pluginConfig, context);
5050
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
51-
setLegacyToken();
51+
setLegacyToken(context);
5252
await verifyNpmAuth(pluginConfig, pkg, context);
5353
}
5454
} catch (err) {
@@ -65,11 +65,11 @@ async function publish(pluginConfig, context) {
6565
let pkg;
6666
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
6767

68-
setLegacyToken();
68+
setLegacyToken(context);
6969

7070
try {
7171
// Reload package.json in case a previous external step updated it
72-
pkg = await getPkg(pluginConfig.pkgRoot);
72+
pkg = await getPkg(pluginConfig, context);
7373
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
7474
await verifyNpmAuth(pluginConfig, pkg, context);
7575
}

lib/get-pkg.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
const path = require('path');
12
const readPkg = require('read-pkg');
23
const AggregateError = require('aggregate-error');
34
const getError = require('./get-error');
45

5-
module.exports = async pkgRoot => {
6+
module.exports = async ({pkgRoot}, {cwd}) => {
67
const errors = [];
78
let pkg;
89

910
try {
10-
pkg = await readPkg({cwd: pkgRoot ? String(pkgRoot) : process.cwd()});
11+
pkg = await readPkg({cwd: pkgRoot ? path.resolve(cwd, String(pkgRoot)) : cwd});
1112

1213
if (!pkg.name) {
1314
errors.push(getError('ENOPKGNAME'));

lib/get-registry.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
const path = require('path');
2+
const rc = require('rc');
13
const getRegistryUrl = require('registry-auth-token/registry-url');
24

3-
module.exports = async ({registry} = {}, name) => (registry ? registry : getRegistryUrl(name.split('/')[0]));
5+
module.exports = ({publishConfig: {registry} = {}, name}, {cwd}) =>
6+
registry
7+
? registry
8+
: getRegistryUrl(
9+
name.split('/')[0],
10+
rc('npm', {registry: 'https://registry.npmjs.org/'}, {config: path.resolve(cwd, '.npmrc')})
11+
);

lib/get-release-info.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
const execa = require('execa');
22
const normalizeUrl = require('normalize-url');
33

4-
const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/';
5-
64
module.exports = async (
7-
name,
8-
publishConfig,
9-
registry,
10-
defaultRegistry = process.env.DEFAULT_NPM_REGISTRY || DEFAULT_NPM_REGISTRY
5+
{name, publishConfig: {tag} = {}},
6+
{cwd, env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/', ...env}},
7+
registry
118
) => {
12-
const distTag =
13-
(publishConfig && publishConfig.tag) || (await execa.stdout('npm', ['config', 'get', 'tag'])) || 'latest';
9+
const distTag = tag || (await execa.stdout('npm', ['config', 'get', 'tag'], {cwd, env})) || 'latest';
1410

1511
return {
1612
name: `npm package (@${distTag} dist-tag)`,
17-
url: normalizeUrl(registry) === normalizeUrl(defaultRegistry) ? `https://www.npmjs.com/package/${name}` : undefined,
13+
url:
14+
normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)
15+
? `https://www.npmjs.com/package/${name}`
16+
: undefined,
1817
};
1918
};

lib/prepare.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const {move} = require('fs-extra');
33
const execa = require('execa');
44
const updatePackageVersion = require('./update-package-version');
55

6-
module.exports = async ({tarballDir, pkgRoot}, {nextRelease: {version}, logger}) => {
7-
const basePath = pkgRoot || '.';
6+
module.exports = async ({tarballDir, pkgRoot}, {cwd, nextRelease: {version}, logger}) => {
7+
const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd;
88
await updatePackageVersion(version, basePath, logger);
99

1010
if (tarballDir) {
1111
logger.log('Creating npm package version %s', version);
12-
const tarball = (await execa.stdout('npm', ['pack', `./${basePath}`])).split('\n').pop();
13-
await move(tarball, path.join(tarballDir.trim(), tarball));
12+
const tarball = (await execa.stdout('npm', ['pack', basePath], {cwd})).split('\n').pop();
13+
await move(path.resolve(cwd, tarball), path.resolve(cwd, tarballDir.trim(), tarball));
1414
}
1515
};

lib/publish.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
const path = require('path');
12
const execa = require('execa');
23
const getRegistry = require('./get-registry');
34
const getReleaseInfo = require('./get-release-info');
45

5-
module.exports = async (
6-
{npmPublish, pkgRoot},
7-
{publishConfig, name, private: priv},
8-
{nextRelease: {version}, logger}
9-
) => {
10-
if (npmPublish !== false && priv !== true) {
11-
const basePath = pkgRoot || '.';
12-
const registry = await getRegistry(publishConfig, name);
6+
module.exports = async ({npmPublish, pkgRoot}, pkg, context) => {
7+
const {
8+
cwd,
9+
env,
10+
nextRelease: {version},
11+
logger,
12+
} = context;
13+
14+
if (npmPublish !== false && pkg.private !== true) {
15+
const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd;
16+
const registry = getRegistry(pkg, context);
1317

1418
logger.log('Publishing version %s to npm registry', version);
15-
const shell = await execa('npm', ['publish', `./${basePath}`, '--registry', registry]);
19+
const shell = await execa('npm', ['publish', basePath, '--registry', registry], {cwd, env});
1620

17-
process.stdout.write(shell.stdout);
18-
return getReleaseInfo(name, publishConfig, registry);
21+
logger.stdout(shell.stdout);
22+
return getReleaseInfo(pkg, context, registry);
1923
}
2024
logger.log(
2125
'Skip publishing to npm registry as %s is %s',

lib/set-legacy-token.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
module.exports = () => {
1+
module.exports = ({env}) => {
22
// Set the environment variable `LEGACY_TOKEN` when user use the legacy auth, so it can be resolved by npm CLI
3-
if (process.env.NPM_USERNAME && process.env.NPM_PASSWORD && process.env.NPM_EMAIL) {
4-
process.env.LEGACY_TOKEN = Buffer.from(`${process.env.NPM_USERNAME}:${process.env.NPM_PASSWORD}`, 'utf8').toString(
5-
'base64'
6-
);
3+
if (env.NPM_USERNAME && env.NPM_PASSWORD && env.NPM_EMAIL) {
4+
env.LEGACY_TOKEN = Buffer.from(`${env.NPM_USERNAME}:${env.NPM_PASSWORD}`, 'utf8').toString('base64');
75
}
86
};

lib/set-npmrc-auth.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1+
const path = require('path');
2+
const rc = require('rc');
13
const {appendFile} = require('fs-extra');
24
const getAuthToken = require('registry-auth-token');
35
const nerfDart = require('nerf-dart');
46
const AggregateError = require('aggregate-error');
57
const getError = require('./get-error');
68

7-
module.exports = async (registry, logger) => {
9+
module.exports = async (registry, {cwd, env: {NPM_TOKEN, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL}, logger}) => {
810
logger.log('Verify authentication for registry %s', registry);
9-
const {NPM_TOKEN, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL} = process.env;
10-
11-
if (getAuthToken(registry)) {
11+
const config = path.resolve(cwd, '.npmrc');
12+
if (getAuthToken(registry, {npmrc: rc('npm', {registry: 'https://registry.npmjs.org/'}, {config})})) {
1213
return;
1314
}
1415
if (NPM_USERNAME && NPM_PASSWORD && NPM_EMAIL) {
15-
await appendFile('./.npmrc', `\n_auth = ${Buffer.from(`\${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`)}`);
16-
logger.log('Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to .npmrc.');
16+
await appendFile(config, `\n_auth = ${Buffer.from(`\${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`)}`);
17+
logger.log(`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${config}`);
1718
} else if (NPM_TOKEN) {
18-
await appendFile('./.npmrc', `\n${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`);
19-
logger.log('Wrote NPM_TOKEN to .npmrc.');
19+
await appendFile(config, `\n${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`);
20+
logger.log(`Wrote NPM_TOKEN to ${config}`);
2021
} else {
2122
throw new AggregateError([getError('ENONPMTOKEN', {registry})]);
2223
}

lib/update-package-version.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ const DEFAULT_INDENT = 2;
88
const DEFAULT_NEWLINE = '\n';
99

1010
module.exports = async (version, basePath, logger) => {
11-
const packagePath = path.join(basePath, 'package.json');
12-
const shrinkwrapPath = path.join(basePath, 'npm-shrinkwrap.json');
13-
const packageLockPath = path.join(basePath, 'package-lock.json');
11+
const packagePath = path.resolve(basePath, 'package.json');
12+
const shrinkwrapPath = path.resolve(basePath, 'npm-shrinkwrap.json');
13+
const packageLockPath = path.resolve(basePath, 'package-lock.json');
1414
const pkg = await readFile(packagePath, 'utf8');
1515

1616
await writeJson(packagePath, {...parseJson(pkg), ...{version}}, getWriteOptions(pkg));

lib/verify-auth.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ const getError = require('./get-error');
55
const getRegistry = require('./get-registry');
66
const setNpmrcAuth = require('./set-npmrc-auth');
77

8-
const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/';
8+
module.exports = async (pluginConfig, pkg, context) => {
9+
const {
10+
cwd,
11+
env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/', ...env},
12+
} = context;
13+
const registry = getRegistry(pkg, context);
914

10-
module.exports = async (
11-
pluginConfig,
12-
{publishConfig, name},
13-
{logger},
14-
defaultRegistry = process.env.DEFAULT_NPM_REGISTRY || DEFAULT_NPM_REGISTRY
15-
) => {
16-
const registry = await getRegistry(publishConfig, name);
17-
await setNpmrcAuth(registry, logger);
15+
await setNpmrcAuth(registry, context);
1816

19-
if (normalizeUrl(registry) === normalizeUrl(defaultRegistry)) {
17+
if (normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)) {
2018
try {
21-
await execa('npm', ['whoami', '--registry', registry]);
19+
await execa('npm', ['whoami', '--registry', registry], {cwd, env});
2220
} catch (err) {
2321
throw new AggregateError([getError('EINVALIDNPMTOKEN', {registry})]);
2422
}

0 commit comments

Comments
 (0)