Skip to content

Commit 755356b

Browse files
pvdlggr2m
authored andcommitted
feat: return release informations from publish hook
1 parent c2c59ab commit 755356b

File tree

5 files changed

+122
-6
lines changed

5 files changed

+122
-6
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function publish(pluginConfig, {nextRelease: {version}, logger}) {
4444
}
4545
verified = true;
4646
}
47-
await publishNpm(pluginConfig, pkg, version, logger);
47+
return publishNpm(pluginConfig, pkg, version, logger);
4848
}
4949

5050
module.exports = {verifyConditions, publish};

lib/get-release-info.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const execa = require('execa');
2+
const normalizeUrl = require('normalize-url');
3+
4+
const DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/';
5+
6+
module.exports = async (
7+
name,
8+
publishConfig,
9+
registry,
10+
defaultRegistry = process.env.DEFAULT_NPM_REGISTRY || DEFAULT_NPM_REGISTRY
11+
) => {
12+
const distTag =
13+
(publishConfig && publishConfig.tag) || (await execa.stdout('npm', ['config', 'get', 'tag'])) || 'latest';
14+
15+
return {
16+
name: `npm package (@${distTag} dist-tag)`,
17+
url: normalizeUrl(registry) === normalizeUrl(defaultRegistry) ? `https://www.npmjs.com/package/${name}` : undefined,
18+
};
19+
};

lib/publish.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {move} = require('fs-extra');
33
const execa = require('execa');
44
const getRegistry = require('./get-registry');
55
const updatePackageVersion = require('./update-package-version');
6+
const getReleaseInfo = require('./get-release-info.js');
67

78
module.exports = async ({npmPublish, tarballDir, pkgRoot}, {publishConfig, name}, version, logger) => {
89
const basePath = pkgRoot || '.';
@@ -20,4 +21,6 @@ module.exports = async ({npmPublish, tarballDir, pkgRoot}, {publishConfig, name}
2021
const shell = await execa('npm', ['publish', `./${basePath}`, '--registry', registry]);
2122
process.stdout.write(shell.stdout);
2223
}
24+
25+
return getReleaseInfo(name, publishConfig, registry);
2326
};

test/get-release-info.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {writeFile} from 'fs-extra';
2+
import test from 'ava';
3+
import tempy from 'tempy';
4+
import getReleaseInfo from '../lib/get-release-info';
5+
6+
// Save the current process.env
7+
const envBackup = Object.assign({}, process.env);
8+
// Save the current working diretory
9+
const cwd = process.cwd();
10+
11+
test.beforeEach(() => {
12+
// Change current working directory to a temp directory
13+
process.chdir(tempy.directory());
14+
// Delete all `npm_config` environment variable set by CI as they take precedence over the `.npmrc` because the process that runs the tests is started before the `.npmrc` is created
15+
for (let i = 0, keys = Object.keys(process.env); i < keys.length; i++) {
16+
if (keys[i].startsWith('npm_')) {
17+
delete process.env[keys[i]];
18+
}
19+
}
20+
});
21+
22+
test.afterEach.always(() => {
23+
// Restore process.env
24+
process.env = envBackup;
25+
// Restore the current working directory
26+
process.chdir(cwd);
27+
});
28+
29+
test.serial('Default registry and tag', async t => {
30+
t.deepEqual(await getReleaseInfo('module', null, 'https://registry.npmjs.org/'), {
31+
name: 'npm package (@latest dist-tag)',
32+
url: 'https://www.npmjs.com/package/module',
33+
});
34+
});
35+
36+
test.serial('Default registry, tag and scoped module', async t => {
37+
t.deepEqual(await getReleaseInfo('@scope/module', null, 'https://registry.npmjs.org/'), {
38+
name: 'npm package (@latest dist-tag)',
39+
url: 'https://www.npmjs.com/package/@scope/module',
40+
});
41+
});
42+
43+
test.serial('Custom registry, tag and scoped module', async t => {
44+
t.deepEqual(await getReleaseInfo('@scope/module', null, 'https://custom.registry.org/'), {
45+
name: 'npm package (@latest dist-tag)',
46+
url: undefined,
47+
});
48+
});
49+
50+
test.serial('Default registry and tag from .npmrc', async t => {
51+
await writeFile('./.npmrc', 'tag=npmrc');
52+
t.deepEqual(await getReleaseInfo('module', {}, 'https://registry.npmjs.org/'), {
53+
name: 'npm package (@npmrc dist-tag)',
54+
url: 'https://www.npmjs.com/package/module',
55+
});
56+
});
57+
58+
test.serial('Default registry and tag from package.json', async t => {
59+
await writeFile('./.npmrc', 'tag=npmrc');
60+
t.deepEqual(await getReleaseInfo('module', {tag: 'pkg'}, 'https://registry.npmjs.org/'), {
61+
name: 'npm package (@pkg dist-tag)',
62+
url: 'https://www.npmjs.com/package/module',
63+
});
64+
});
65+
66+
test.serial('Default tag', async t => {
67+
await writeFile('./.npmrc', 'tag=');
68+
t.deepEqual(await getReleaseInfo('module', {}, 'https://registry.npmjs.org/'), {
69+
name: 'npm package (@latest dist-tag)',
70+
url: 'https://www.npmjs.com/package/module',
71+
});
72+
});

test/integration.test.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,23 @@ test.serial('Publish the package', async t => {
181181
const pkg = {name: 'publish', version: '0.0.0', publishConfig: {registry: npmRegistry.url}};
182182
await outputJson('./package.json', pkg);
183183

184-
await t.context.m.publish({}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
184+
const result = await t.context.m.publish({}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
185185

186+
t.deepEqual(result, {name: 'npm package (@latest dist-tag)', url: undefined});
187+
t.is((await readJson('./package.json')).version, '1.0.0');
188+
t.false(await pathExists(`./${pkg.name}-1.0.0.tgz`));
189+
t.is((await execa('npm', ['view', pkg.name, 'version'])).stdout, '1.0.0');
190+
});
191+
192+
test.serial('Publish the package on a dist-tag', async t => {
193+
Object.assign(process.env, npmRegistry.authEnv);
194+
process.env.DEFAULT_NPM_REGISTRY = npmRegistry.url;
195+
const pkg = {name: 'publish-tag', version: '0.0.0', publishConfig: {registry: npmRegistry.url, tag: 'next'}};
196+
await outputJson('./package.json', pkg);
197+
198+
const result = await t.context.m.publish({}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
199+
200+
t.deepEqual(result, {name: 'npm package (@next dist-tag)', url: 'https://www.npmjs.com/package/publish-tag'});
186201
t.is((await readJson('./package.json')).version, '1.0.0');
187202
t.false(await pathExists(`./${pkg.name}-1.0.0.tgz`));
188203
t.is((await execa('npm', ['view', pkg.name, 'version'])).stdout, '1.0.0');
@@ -193,8 +208,12 @@ test.serial('Publish the package from a sub-directory', async t => {
193208
const pkg = {name: 'publish-sub-dir', version: '0.0.0', publishConfig: {registry: npmRegistry.url}};
194209
await outputJson('./dist/package.json', pkg);
195210

196-
await t.context.m.publish({pkgRoot: 'dist'}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
211+
const result = await t.context.m.publish(
212+
{pkgRoot: 'dist'},
213+
{logger: t.context.logger, nextRelease: {version: '1.0.0'}}
214+
);
197215

216+
t.deepEqual(result, {name: 'npm package (@latest dist-tag)', url: undefined});
198217
t.is((await readJson('./dist/package.json')).version, '1.0.0');
199218
t.false(await pathExists(`./${pkg.name}-1.0.0.tgz`));
200219
t.is((await execa('npm', ['view', pkg.name, 'version'])).stdout, '1.0.0');
@@ -211,11 +230,12 @@ test.serial('Create the package and skip publish', async t => {
211230
const pkg = {name: 'skip-publish', version: '0.0.0', publishConfig: {registry: npmRegistry.url}};
212231
await outputJson('./package.json', pkg);
213232

214-
await t.context.m.publish(
233+
const result = await t.context.m.publish(
215234
{npmPublish: false, tarballDir: 'tarball'},
216235
{logger: t.context.logger, nextRelease: {version: '1.0.0'}}
217236
);
218237

238+
t.deepEqual(result, {name: 'npm package (@latest dist-tag)', url: undefined});
219239
t.is((await readJson('./package.json')).version, '1.0.0');
220240
t.true(await pathExists(`./tarball/${pkg.name}-1.0.0.tgz`));
221241
await t.throws(execa('npm', ['view', pkg.name, 'version']));
@@ -226,11 +246,12 @@ test.serial('Create the package and skip publish from a sub-directory', async t
226246
const pkg = {name: 'skip-publish-sub-dir', version: '0.0.0', publishConfig: {registry: npmRegistry.url}};
227247
await outputJson('./dist/package.json', pkg);
228248

229-
await t.context.m.publish(
249+
const result = await t.context.m.publish(
230250
{npmPublish: false, tarballDir: './tarball', pkgRoot: './dist'},
231251
{logger: t.context.logger, nextRelease: {version: '1.0.0'}}
232252
);
233253

254+
t.deepEqual(result, {name: 'npm package (@latest dist-tag)', url: undefined});
234255
t.is((await readJson('./dist/package.json')).version, '1.0.0');
235256
t.true(await pathExists(`./tarball/${pkg.name}-1.0.0.tgz`));
236257
await t.throws(execa('npm', ['view', pkg.name, 'version']));
@@ -243,5 +264,6 @@ test.serial('Verify token and set up auth only on the fist call', async t => {
243264

244265
await t.notThrows(t.context.m.verifyConditions({}, {options: {}, logger: t.context.logger}));
245266

246-
await t.context.m.publish({}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
267+
const result = await t.context.m.publish({}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
268+
t.deepEqual(result, {name: 'npm package (@latest dist-tag)', url: undefined});
247269
});

0 commit comments

Comments
 (0)