Skip to content

Commit 2d8ff15

Browse files
travigr2m
authored andcommitted
refactor(esm): converted the package to esm
BREAKING CHANGE: `@semantic-release/npm` is now a native ES Module. It has named exports for each plugin hook (`verifyConditions`, `prepare`, `publish`, `addChannel`)
1 parent 7157d76 commit 2d8ff15

25 files changed

+2363
-4443
lines changed

index.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
const {defaultTo, castArray} = require('lodash');
2-
const AggregateError = require('aggregate-error');
3-
const tempy = require('tempy');
4-
const setLegacyToken = require('./lib/set-legacy-token');
5-
const getPkg = require('./lib/get-pkg');
6-
const verifyNpmConfig = require('./lib/verify-config');
7-
const verifyNpmAuth = require('./lib/verify-auth');
8-
const addChannelNpm = require('./lib/add-channel');
9-
const prepareNpm = require('./lib/prepare');
10-
const publishNpm = require('./lib/publish');
1+
import { castArray, defaultTo } from 'lodash-es';
2+
import AggregateError from 'aggregate-error';
3+
import tempy from 'tempy';
4+
import setLegacyToken from './lib/set-legacy-token.js';
5+
import getPkg from './lib/get-pkg.js';
6+
import verifyNpmConfig from './lib/verify-config.js';
7+
import verifyNpmAuth from './lib/verify-auth.js';
8+
import addChannelNpm from './lib/add-channel.js';
9+
import prepareNpm from './lib/prepare.js';
10+
import publishNpm from './lib/publish.js';
1111

1212
let verified;
1313
let prepared;
1414
const npmrc = tempy.file({name: '.npmrc'});
1515

16-
async function verifyConditions(pluginConfig, context) {
16+
export async function verifyConditions(pluginConfig, context) {
1717
// If the npm publish plugin is used and has `npmPublish`, `tarballDir` or `pkgRoot` configured, validate them now in order to prevent any release if the configuration is wrong
1818
if (context.options.publish) {
1919
const publishPlugin =
@@ -46,7 +46,7 @@ async function verifyConditions(pluginConfig, context) {
4646
verified = true;
4747
}
4848

49-
async function prepare(pluginConfig, context) {
49+
export async function prepare(pluginConfig, context) {
5050
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
5151

5252
setLegacyToken(context);
@@ -69,7 +69,7 @@ async function prepare(pluginConfig, context) {
6969
prepared = true;
7070
}
7171

72-
async function publish(pluginConfig, context) {
72+
export async function publish(pluginConfig, context) {
7373
let pkg;
7474
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
7575

@@ -96,7 +96,7 @@ async function publish(pluginConfig, context) {
9696
return publishNpm(npmrc, pluginConfig, pkg, context);
9797
}
9898

99-
async function addChannel(pluginConfig, context) {
99+
export async function addChannel(pluginConfig, context) {
100100
let pkg;
101101
const errors = verified ? [] : verifyNpmConfig(pluginConfig);
102102

@@ -118,5 +118,3 @@ async function addChannel(pluginConfig, context) {
118118

119119
return addChannelNpm(npmrc, pluginConfig, pkg, context);
120120
}
121-
122-
module.exports = {verifyConditions, prepare, publish, addChannel};

lib/add-channel.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const execa = require('execa');
2-
const getRegistry = require('./get-registry');
3-
const getChannel = require('./get-channel');
4-
const getReleaseInfo = require('./get-release-info');
1+
import execa from 'execa';
2+
import getRegistry from './get-registry.js';
3+
import getChannel from './get-channel.js';
4+
import getReleaseInfo from './get-release-info.js';
55

6-
module.exports = async (npmrc, {npmPublish}, pkg, context) => {
6+
export default async function (npmrc, {npmPublish}, pkg, context) {
77
const {
88
cwd,
99
env,
@@ -43,4 +43,4 @@ module.exports = async (npmrc, {npmPublish}, pkg, context) => {
4343
);
4444

4545
return false;
46-
};
46+
}

lib/definitions/errors.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
1-
const pkg = require('../../package.json');
1+
import {dirname, resolve} from 'node:path';
2+
import {fileURLToPath} from 'node:url';
3+
import {readPackageSync} from 'read-pkg';
24

5+
const __dirname = dirname(fileURLToPath(import.meta.url));
6+
const pkg = readPackageSync({cwd: resolve(__dirname, '../../')});
37
const [homepage] = pkg.homepage.split('#');
48
const linkify = (file) => `${homepage}/blob/master/${file}`;
59

6-
module.exports = {
7-
EINVALIDNPMPUBLISH: ({npmPublish}) => ({
10+
export function EINVALIDNPMPUBLISH({ npmPublish }) {
11+
return {
812
message: 'Invalid `npmPublish` option.',
913
details: `The [npmPublish option](${linkify('README.md#npmpublish')}) option, if defined, must be a \`Boolean\`.
1014
1115
Your configuration for the \`npmPublish\` option is \`${npmPublish}\`.`,
12-
}),
13-
EINVALIDTARBALLDIR: ({tarballDir}) => ({
16+
};
17+
}
18+
19+
export function EINVALIDTARBALLDIR({ tarballDir }) {
20+
return {
1421
message: 'Invalid `tarballDir` option.',
1522
details: `The [tarballDir option](${linkify('README.md#tarballdir')}) option, if defined, must be a \`String\`.
1623
1724
Your configuration for the \`tarballDir\` option is \`${tarballDir}\`.`,
18-
}),
19-
EINVALIDPKGROOT: ({pkgRoot}) => ({
25+
};
26+
}
27+
28+
export function EINVALIDPKGROOT({ pkgRoot }) {
29+
return {
2030
message: 'Invalid `pkgRoot` option.',
2131
details: `The [pkgRoot option](${linkify('README.md#pkgroot')}) option, if defined, must be a \`String\`.
2232
2333
Your configuration for the \`pkgRoot\` option is \`${pkgRoot}\`.`,
24-
}),
25-
ENONPMTOKEN: ({registry}) => ({
34+
};
35+
}
36+
37+
export function ENONPMTOKEN({ registry }) {
38+
return {
2639
message: 'No npm token specified.',
2740
details: `An [npm token](${linkify(
2841
'README.md#npm-registry-authentication'
2942
)}) must be created and set in the \`NPM_TOKEN\` environment variable on your CI environment.
3043
3144
Please make sure to create an [npm token](https://docs.npmjs.com/getting-started/working_with_tokens#how-to-create-new-tokens) and to set it in the \`NPM_TOKEN\` environment variable on your CI environment. The token must allow to publish to the registry \`${registry}\`.`,
32-
}),
33-
EINVALIDNPMTOKEN: ({registry}) => ({
45+
};
46+
}
47+
48+
export function EINVALIDNPMTOKEN({ registry }) {
49+
return {
3450
message: 'Invalid npm token.',
3551
details: `The [npm token](${linkify(
3652
'README.md#npm-registry-authentication'
@@ -40,17 +56,23 @@ If you are using Two Factor Authentication for your account, set its level to ["
4056
Authorization and writes" level.
4157
4258
Please make sure to set the \`NPM_TOKEN\` environment variable in your CI with the exact value of the npm token.`,
43-
}),
44-
ENOPKGNAME: () => ({
59+
};
60+
}
61+
62+
export function ENOPKGNAME() {
63+
return {
4564
message: 'Missing `name` property in `package.json`.',
4665
details: `The \`package.json\`'s [name](https://docs.npmjs.com/files/package.json#name) property is required in order to publish a package to the npm registry.
4766
4867
Please make sure to add a valid \`name\` for your package in your \`package.json\`.`,
49-
}),
50-
ENOPKG: () => ({
68+
};
69+
}
70+
71+
export function ENOPKG() {
72+
return {
5173
message: 'Missing `package.json` file.',
5274
details: `A [package.json file](https://docs.npmjs.com/files/package.json) at the root of your project is required to release on npm.
5375
5476
Please follow the [npm guideline](https://docs.npmjs.com/getting-started/creating-node-modules) to create a valid \`package.json\` file.`,
55-
}),
56-
};
77+
};
78+
}

lib/get-channel.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
const semver = require('semver');
1+
import semver from 'semver';
22

3-
module.exports = (channel) => (channel ? (semver.validRange(channel) ? `release-${channel}` : channel) : 'latest');
3+
export default function (channel) {
4+
return channel ? (semver.validRange(channel) ? `release-${channel}` : channel) : 'latest';
5+
}

lib/get-error.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const SemanticReleaseError = require('@semantic-release/error');
2-
const ERROR_DEFINITIONS = require('./definitions/errors');
1+
import SemanticReleaseError from '@semantic-release/error';
2+
import * as ERROR_DEFINITIONS from './definitions/errors.js';
33

4-
module.exports = (code, ctx = {}) => {
4+
export default function (code, ctx = {}) {
55
const {message, details} = ERROR_DEFINITIONS[code](ctx);
6+
67
return new SemanticReleaseError(message, code, details);
7-
};
8+
}

lib/get-pkg.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const path = require('path');
2-
const readPkg = require('read-pkg');
3-
const AggregateError = require('aggregate-error');
4-
const getError = require('./get-error');
1+
import path from 'path';
2+
import {readPackage} from 'read-pkg';
3+
import AggregateError from 'aggregate-error';
4+
import getError from './get-error.js';
55

6-
module.exports = async ({pkgRoot}, {cwd}) => {
6+
export default async function ({pkgRoot}, {cwd}) {
77
try {
8-
const pkg = await readPkg({cwd: pkgRoot ? path.resolve(cwd, String(pkgRoot)) : cwd});
8+
const pkg = await readPackage({cwd: pkgRoot ? path.resolve(cwd, String(pkgRoot)) : cwd});
99

1010
if (!pkg.name) {
1111
throw getError('ENOPKGNAME');
@@ -19,4 +19,4 @@ module.exports = async ({pkgRoot}, {cwd}) => {
1919

2020
throw new AggregateError([error]);
2121
}
22-
};
22+
}

lib/get-registry.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
const path = require('path');
2-
const rc = require('rc');
3-
const getRegistryUrl = require('registry-auth-token/registry-url');
1+
import path from 'path';
2+
import rc from 'rc';
3+
import getRegistryUrl from 'registry-auth-token/registry-url.js';
44

5-
module.exports = ({publishConfig: {registry} = {}, name}, {cwd, env}) =>
6-
registry ||
7-
env.NPM_CONFIG_REGISTRY ||
8-
getRegistryUrl(
9-
name.split('/')[0],
10-
rc(
11-
'npm',
12-
{registry: 'https://registry.npmjs.org/'},
13-
{config: env.NPM_CONFIG_USERCONFIG || path.resolve(cwd, '.npmrc')}
14-
)
15-
);
5+
export default function ({ publishConfig: { registry } = {}, name }, { cwd, env }) {
6+
return registry ||
7+
env.NPM_CONFIG_REGISTRY ||
8+
getRegistryUrl(
9+
name.split('/')[0],
10+
rc(
11+
'npm',
12+
{ registry: 'https://registry.npmjs.org/' },
13+
{ config: env.NPM_CONFIG_USERCONFIG || path.resolve(cwd, '.npmrc') }
14+
)
15+
);
16+
}

lib/get-release-info.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
const normalizeUrl = require('normalize-url');
1+
import normalizeUrl from 'normalize-url';
22

3-
module.exports = (
3+
export default function (
44
{name},
55
{env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/'}, nextRelease: {version}},
66
distTag,
77
registry
8-
) => ({
9-
name: `npm package (@${distTag} dist-tag)`,
10-
url:
8+
) {
9+
return {
10+
name: `npm package (@${distTag} dist-tag)`,
11+
url:
1112
normalizeUrl(registry) === normalizeUrl(DEFAULT_NPM_REGISTRY)
1213
? `https://www.npmjs.com/package/${name}/v/${version}`
1314
: undefined,
14-
channel: distTag,
15-
});
15+
channel: distTag,
16+
};
17+
}

lib/prepare.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const path = require('path');
2-
const {move} = require('fs-extra');
3-
const execa = require('execa');
1+
import path from 'path';
2+
import { move } from 'fs-extra';
3+
import execa from 'execa';
44

5-
module.exports = async (npmrc, {tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRelease: {version}, logger}) => {
5+
export default async function (npmrc, {tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRelease: {version}, logger}) {
66
const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd;
77

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

lib/publish.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
const path = require('path');
2-
const execa = require('execa');
3-
const getRegistry = require('./get-registry');
4-
const getChannel = require('./get-channel');
5-
const getReleaseInfo = require('./get-release-info');
1+
import path from 'path';
2+
import execa from 'execa';
3+
import getRegistry from './get-registry.js';
4+
import getChannel from './get-channel.js';
5+
import getReleaseInfo from './get-release-info.js';
66

7-
module.exports = async (npmrc, {npmPublish, pkgRoot}, pkg, context) => {
7+
export default async function (npmrc, {npmPublish, pkgRoot}, pkg, context) {
88
const {
99
cwd,
1010
env,
@@ -41,4 +41,4 @@ module.exports = async (npmrc, {npmPublish, pkgRoot}, pkg, context) => {
4141
);
4242

4343
return false;
44-
};
44+
}

lib/set-legacy-token.js

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

lib/set-npmrc-auth.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const path = require('path');
2-
const rc = require('rc');
3-
const {outputFile, readFile} = require('fs-extra');
4-
const getAuthToken = require('registry-auth-token');
5-
const nerfDart = require('nerf-dart');
6-
const AggregateError = require('aggregate-error');
7-
const getError = require('./get-error');
1+
import path from 'path';
2+
import rc from 'rc';
3+
import fs from 'fs-extra';
4+
import getAuthToken from 'registry-auth-token';
5+
import nerfDart from 'nerf-dart';
6+
import AggregateError from 'aggregate-error';
7+
import getError from './get-error.js';
88

9-
module.exports = async (
9+
export default async function (
1010
npmrc,
1111
registry,
1212
{cwd, env: {NPM_TOKEN, NPM_CONFIG_USERCONFIG, NPM_USERNAME, NPM_PASSWORD, NPM_EMAIL}, logger}
13-
) => {
13+
) {
1414
logger.log('Verify authentication for registry %s', registry);
1515
const {configs, ...rcConfig} = rc(
1616
'npm',
@@ -22,26 +22,26 @@ module.exports = async (
2222
logger.log('Reading npm config from %s', configs.join(', '));
2323
}
2424

25-
const currentConfig = configs ? (await Promise.all(configs.map((config) => readFile(config)))).join('\n') : '';
25+
const currentConfig = configs ? (await Promise.all(configs.map((config) => fs.readFile(config)))).join('\n') : '';
2626

2727
if (getAuthToken(registry, {npmrc: rcConfig})) {
28-
await outputFile(npmrc, currentConfig);
28+
await fs.outputFile(npmrc, currentConfig);
2929
return;
3030
}
3131

3232
if (NPM_USERNAME && NPM_PASSWORD && NPM_EMAIL) {
33-
await outputFile(
33+
await fs.outputFile(
3434
npmrc,
3535
`${currentConfig ? `${currentConfig}\n` : ''}_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`
3636
);
3737
logger.log(`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${npmrc}`);
3838
} else if (NPM_TOKEN) {
39-
await outputFile(
39+
await fs.outputFile(
4040
npmrc,
4141
`${currentConfig ? `${currentConfig}\n` : ''}${nerfDart(registry)}:_authToken = \${NPM_TOKEN}`
4242
);
4343
logger.log(`Wrote NPM_TOKEN to ${npmrc}`);
4444
} else {
4545
throw new AggregateError([getError('ENONPMTOKEN', {registry})]);
4646
}
47-
};
47+
}

0 commit comments

Comments
 (0)