Skip to content

Commit fb96126

Browse files
committed
feat: add addChannel plugin step
1 parent b8deba7 commit fb96126

File tree

9 files changed

+304
-20
lines changed

9 files changed

+304
-20
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
[![npm latest version](https://img.shields.io/npm/v/@semantic-release/npm/latest.svg)](https://www.npmjs.com/package/@semantic-release/npm)
1010
[![npm next version](https://img.shields.io/npm/v/@semantic-release/npm/next.svg)](https://www.npmjs.com/package/@semantic-release/npm)
1111

12-
| Step | Description |
13-
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
14-
| `verifyConditions` | Verify the presence of the `NPM_TOKEN` environment variable, create or update the `.npmrc` file with the token and verify the token is valid. |
15-
| `prepare` | Update the `package.json` version and [create](https://docs.npmjs.com/cli/pack) the npm package tarball. |
16-
| `publish` | [Publish the npm package](https://docs.npmjs.com/cli/publish) to the registry. |
12+
| Step | Description | |
13+
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
14+
| `verifyConditions` | Verify the presence of the `NPM_TOKEN` environment variable, create or update the `.npmrc` file with the token and verify the token is valid. | |
15+
| `prepare` | Update the `package.json` version and [create](https://docs.npmjs.com/cli/pack) the npm package tarball. | |
16+
| `addChannel` | | [Add a release to a dist-tag](https://docs.npmjs.com/cli/dist-tag). |
17+
| `publish` | [Publish the npm package](https://docs.npmjs.com/cli/publish) to the registry. | |
1718

1819
## Install
1920

index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const setLegacyToken = require('./lib/set-legacy-token');
44
const getPkg = require('./lib/get-pkg');
55
const verifyNpmConfig = require('./lib/verify-config');
66
const verifyNpmAuth = require('./lib/verify-auth');
7+
const addChannelNpm = require('./lib/add-channel');
78
const prepareNpm = require('./lib/prepare');
89
const publishNpm = require('./lib/publish');
910

@@ -87,4 +88,27 @@ async function publish(pluginConfig, context) {
8788
return publishNpm(pluginConfig, pkg, context);
8889
}
8990

90-
module.exports = {verifyConditions, prepare, publish};
91+
async function addChannel(pluginConfig, context) {
92+
let pkg;
93+
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
94+
95+
setLegacyToken(context);
96+
97+
try {
98+
// Reload package.json in case a previous external step updated it
99+
pkg = await getPkg(pluginConfig, context);
100+
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
101+
await verifyNpmAuth(pluginConfig, pkg, context);
102+
}
103+
} catch (error) {
104+
errors.push(...error);
105+
}
106+
107+
if (errors.length > 0) {
108+
throw new AggregateError(errors);
109+
}
110+
111+
return addChannelNpm(pluginConfig, pkg, context);
112+
}
113+
114+
module.exports = {verifyConditions, prepare, publish, addChannel};

lib/add-channel.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const execa = require('execa');
2+
const getRegistry = require('./get-registry');
3+
const getChannel = require('./get-channel');
4+
const getReleaseInfo = require('./get-release-info');
5+
6+
module.exports = async ({npmPublish}, pkg, context) => {
7+
const {
8+
cwd,
9+
env,
10+
stdout,
11+
stderr,
12+
nextRelease: {version, channel},
13+
logger,
14+
} = context;
15+
16+
if (npmPublish !== false && pkg.private !== true) {
17+
const registry = getRegistry(pkg, context);
18+
const distTag = getChannel(channel);
19+
20+
logger.log(`Adding version ${version} to npm registry on dist-tag ${distTag}`);
21+
const result = execa('npm', ['dist-tag', 'add', `${pkg.name}@${version}`, distTag, '--registry', registry], {
22+
cwd,
23+
env,
24+
});
25+
result.stdout.pipe(
26+
stdout,
27+
{end: false}
28+
);
29+
result.stderr.pipe(
30+
stderr,
31+
{end: false}
32+
);
33+
await result;
34+
35+
logger.log(`Published ${pkg.name}@${version} on ${registry}`);
36+
37+
return getReleaseInfo(pkg, context, distTag, registry);
38+
}
39+
40+
logger.log(
41+
`Skip adding to npm channel as ${
42+
npmPublish === false ? 'npmPublish' : "package.json's private property"
43+
} is ${npmPublish !== false}`
44+
);
45+
};

lib/get-release-info.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const normalizeUrl = require('normalize-url');
22

3-
module.exports = async ({name}, {env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/'}}, distTag, registry) => ({
3+
module.exports = ({name}, {env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/'}}, distTag, registry) => ({
44
name: `npm package (@${distTag} dist-tag)`,
55
url:
66
normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY) ? `https://www.npmjs.com/package/${name}` : undefined,
7+
channel: distTag,
78
});

lib/prepare.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ module.exports = async ({tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRe
77

88
logger.log('Write version %s to package.json in %s', version, basePath);
99

10-
const versionResult = execa('npm', ['version', version, '--no-git-tag-version'], {cwd: basePath, env});
10+
const versionResult = execa('npm', ['version', version, '--no-git-tag-version', '--allow-same-version'], {
11+
cwd: basePath,
12+
env,
13+
});
1114
versionResult.stdout.pipe(
1215
stdout,
1316
{end: false}

lib/publish.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = async ({npmPublish, pkgRoot}, pkg, context) => {
1919
const registry = getRegistry(pkg, context);
2020
const distTag = getChannel(channel);
2121

22-
logger.log('Publishing version %s to npm registry', version);
22+
logger.log(`Publishing version ${version} to npm registry`);
2323
const result = execa('npm', ['publish', basePath, '--tag', distTag, '--registry', registry], {
2424
cwd,
2525
env,
@@ -34,11 +34,14 @@ module.exports = async ({npmPublish, pkgRoot}, pkg, context) => {
3434
);
3535
await result;
3636

37-
logger.log(`Published ${pkg.name}@${pkg.version} on ${registry}`);
37+
logger.log(`Published ${pkg.name}@${version} to ${registry}`);
38+
3839
return getReleaseInfo(pkg, context, distTag, registry);
3940
}
41+
4042
logger.log(
41-
'Skip publishing to npm registry as %s is %s',
42-
...(npmPublish === false ? ['npmPublish', false] : ["package.json's private property", true])
43+
`Skip publishing to npm registry as ${
44+
npmPublish === false ? 'npmPublish' : "package.json's private property"
45+
} is ${npmPublish !== false}`
4346
);
4447
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
"all": true
8080
},
8181
"peerDependencies": {
82-
"semantic-release": ">=15.9.0 <16.0.0"
82+
"semantic-release": ">=15.9.0 <17.0.0"
8383
},
8484
"prettier": {
8585
"printWidth": 120,

test/get-release-info.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ test('Default registry and scoped module', async t => {
55
t.deepEqual(await getReleaseInfo({name: '@scope/module'}, {env: {}}, 'latest', 'https://registry.npmjs.org/'), {
66
name: 'npm package (@latest dist-tag)',
77
url: 'https://www.npmjs.com/package/@scope/module',
8+
channel: 'latest',
89
});
910
});
1011

1112
test('Custom registry and scoped module', async t => {
1213
t.deepEqual(await getReleaseInfo({name: '@scope/module'}, {env: {}}, 'latest', 'https://custom.registry.org/'), {
1314
name: 'npm package (@latest dist-tag)',
1415
url: undefined,
16+
channel: 'latest',
1517
});
1618
});

0 commit comments

Comments
 (0)