Skip to content

Commit

Permalink
feat: add addChannel plugin step
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Nov 29, 2018
1 parent b8deba7 commit fb96126
Show file tree
Hide file tree
Showing 9 changed files with 304 additions and 20 deletions.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -9,11 +9,12 @@
[![npm latest version](https://img.shields.io/npm/v/@semantic-release/npm/latest.svg)](https://www.npmjs.com/package/@semantic-release/npm)
[![npm next version](https://img.shields.io/npm/v/@semantic-release/npm/next.svg)](https://www.npmjs.com/package/@semantic-release/npm)

| Step | Description |
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|
| `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. |
| `prepare` | Update the `package.json` version and [create](https://docs.npmjs.com/cli/pack) the npm package tarball. |
| `publish` | [Publish the npm package](https://docs.npmjs.com/cli/publish) to the registry. |
| Step | Description | |
|--------------------|-----------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| `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. | |
| `prepare` | Update the `package.json` version and [create](https://docs.npmjs.com/cli/pack) the npm package tarball. | |
| `addChannel` | | [Add a release to a dist-tag](https://docs.npmjs.com/cli/dist-tag). |
| `publish` | [Publish the npm package](https://docs.npmjs.com/cli/publish) to the registry. | |

## Install

Expand Down
26 changes: 25 additions & 1 deletion index.js
Expand Up @@ -4,6 +4,7 @@ const setLegacyToken = require('./lib/set-legacy-token');
const getPkg = require('./lib/get-pkg');
const verifyNpmConfig = require('./lib/verify-config');
const verifyNpmAuth = require('./lib/verify-auth');
const addChannelNpm = require('./lib/add-channel');
const prepareNpm = require('./lib/prepare');
const publishNpm = require('./lib/publish');

Expand Down Expand Up @@ -87,4 +88,27 @@ async function publish(pluginConfig, context) {
return publishNpm(pluginConfig, pkg, context);
}

module.exports = {verifyConditions, prepare, publish};
async function addChannel(pluginConfig, context) {
let pkg;
const errors = verified ? [] : verifyNpmConfig(pluginConfig);

setLegacyToken(context);

try {
// Reload package.json in case a previous external step updated it
pkg = await getPkg(pluginConfig, context);
if (!verified && pluginConfig.npmPublish !== false && pkg.private !== true) {
await verifyNpmAuth(pluginConfig, pkg, context);
}
} catch (error) {
errors.push(...error);
}

if (errors.length > 0) {
throw new AggregateError(errors);
}

return addChannelNpm(pluginConfig, pkg, context);
}

module.exports = {verifyConditions, prepare, publish, addChannel};
45 changes: 45 additions & 0 deletions lib/add-channel.js
@@ -0,0 +1,45 @@
const execa = require('execa');
const getRegistry = require('./get-registry');
const getChannel = require('./get-channel');
const getReleaseInfo = require('./get-release-info');

module.exports = async ({npmPublish}, pkg, context) => {
const {
cwd,
env,
stdout,
stderr,
nextRelease: {version, channel},
logger,
} = context;

if (npmPublish !== false && pkg.private !== true) {
const registry = getRegistry(pkg, context);
const distTag = getChannel(channel);

logger.log(`Adding version ${version} to npm registry on dist-tag ${distTag}`);
const result = execa('npm', ['dist-tag', 'add', `${pkg.name}@${version}`, distTag, '--registry', registry], {
cwd,
env,
});
result.stdout.pipe(
stdout,
{end: false}
);
result.stderr.pipe(
stderr,
{end: false}
);
await result;

logger.log(`Published ${pkg.name}@${version} on ${registry}`);

return getReleaseInfo(pkg, context, distTag, registry);
}

logger.log(
`Skip adding to npm channel as ${
npmPublish === false ? 'npmPublish' : "package.json's private property"
} is ${npmPublish !== false}`
);
};
3 changes: 2 additions & 1 deletion lib/get-release-info.js
@@ -1,7 +1,8 @@
const normalizeUrl = require('normalize-url');

module.exports = async ({name}, {env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/'}}, distTag, registry) => ({
module.exports = ({name}, {env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/'}}, distTag, registry) => ({
name: `npm package (@${distTag} dist-tag)`,
url:
normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY) ? `https://www.npmjs.com/package/${name}` : undefined,
channel: distTag,
});
5 changes: 4 additions & 1 deletion lib/prepare.js
Expand Up @@ -7,7 +7,10 @@ module.exports = async ({tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRe

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

const versionResult = execa('npm', ['version', version, '--no-git-tag-version'], {cwd: basePath, env});
const versionResult = execa('npm', ['version', version, '--no-git-tag-version', '--allow-same-version'], {
cwd: basePath,
env,
});
versionResult.stdout.pipe(
stdout,
{end: false}
Expand Down
11 changes: 7 additions & 4 deletions lib/publish.js
Expand Up @@ -19,7 +19,7 @@ module.exports = async ({npmPublish, pkgRoot}, pkg, context) => {
const registry = getRegistry(pkg, context);
const distTag = getChannel(channel);

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

logger.log(`Published ${pkg.name}@${pkg.version} on ${registry}`);
logger.log(`Published ${pkg.name}@${version} to ${registry}`);

return getReleaseInfo(pkg, context, distTag, registry);
}

logger.log(
'Skip publishing to npm registry as %s is %s',
...(npmPublish === false ? ['npmPublish', false] : ["package.json's private property", true])
`Skip publishing to npm registry as ${
npmPublish === false ? 'npmPublish' : "package.json's private property"
} is ${npmPublish !== false}`
);
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -79,7 +79,7 @@
"all": true
},
"peerDependencies": {
"semantic-release": ">=15.9.0 <16.0.0"
"semantic-release": ">=15.9.0 <17.0.0"
},
"prettier": {
"printWidth": 120,
Expand Down
2 changes: 2 additions & 0 deletions test/get-release-info.test.js
Expand Up @@ -5,12 +5,14 @@ test('Default registry and scoped module', async t => {
t.deepEqual(await getReleaseInfo({name: '@scope/module'}, {env: {}}, 'latest', 'https://registry.npmjs.org/'), {
name: 'npm package (@latest dist-tag)',
url: 'https://www.npmjs.com/package/@scope/module',
channel: 'latest',
});
});

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

0 comments on commit fb96126

Please sign in to comment.