Skip to content

Commit 2d8ff15

Browse files
travigr2m
authored andcommittedJan 13, 2023
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

+14-16
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

+6-6
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

+39-17
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

+4-2
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

+5-4
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

+7-7
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

+15-14
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

+9-7
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

+4-4
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

+7-7
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

+2-2
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

+14-14
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+
}

‎lib/verify-auth.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const execa = require('execa');
2-
const normalizeUrl = require('normalize-url');
3-
const AggregateError = require('aggregate-error');
4-
const getError = require('./get-error');
5-
const getRegistry = require('./get-registry');
6-
const setNpmrcAuth = require('./set-npmrc-auth');
1+
import execa from 'execa';
2+
import normalizeUrl from 'normalize-url';
3+
import AggregateError from 'aggregate-error';
4+
import getError from './get-error.js';
5+
import getRegistry from './get-registry.js';
6+
import setNpmrcAuth from './set-npmrc-auth.js';
77

8-
module.exports = async (npmrc, pkg, context) => {
8+
export default async function (npmrc, pkg, context) {
99
const {
1010
cwd,
1111
env: {DEFAULT_NPM_REGISTRY = 'https://registry.npmjs.org/', ...env},
@@ -30,4 +30,4 @@ module.exports = async (npmrc, pkg, context) => {
3030
throw new AggregateError([getError('EINVALIDNPMTOKEN', {registry})]);
3131
}
3232
}
33-
};
33+
}

‎lib/verify-config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const {isString, isNil, isBoolean} = require('lodash');
2-
const getError = require('./get-error');
1+
import { isBoolean, isNil, isString } from 'lodash-es';
2+
import getError from './get-error.js';
33

44
const isNonEmptyString = (value) => isString(value) && value.trim();
55

@@ -9,7 +9,7 @@ const VALIDATORS = {
99
pkgRoot: isNonEmptyString,
1010
};
1111

12-
module.exports = ({npmPublish, tarballDir, pkgRoot}) => {
12+
export default function ({npmPublish, tarballDir, pkgRoot}) {
1313
const errors = Object.entries({npmPublish, tarballDir, pkgRoot}).reduce(
1414
(errors, [option, value]) =>
1515
!isNil(value) && !VALIDATORS[option](value)
@@ -19,4 +19,4 @@ module.exports = ({npmPublish, tarballDir, pkgRoot}) => {
1919
);
2020

2121
return errors;
22-
};
22+
}

‎package-lock.json

+2,042-4,155
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,24 @@
2222
"aggregate-error": "^3.0.0",
2323
"execa": "^5.0.0",
2424
"fs-extra": "^11.0.0",
25-
"lodash": "^4.17.15",
25+
"lodash-es": "^4.17.21",
2626
"nerf-dart": "^1.0.0",
2727
"normalize-url": "^6.0.0",
2828
"npm": "^8.3.0",
2929
"rc": "^1.2.8",
30-
"read-pkg": "^5.0.0",
30+
"read-pkg": "^7.0.0",
3131
"registry-auth-token": "^5.0.0",
3232
"semver": "^7.1.2",
33-
"tempy": "^1.0.0"
33+
"tempy": "^2.0.0"
3434
},
3535
"devDependencies": {
3636
"ava": "5.1.0",
37-
"clear-module": "4.1.2",
37+
"c8": "7.10.0",
3838
"codecov": "3.8.3",
3939
"delay": "5.0.0",
4040
"dockerode": "3.3.4",
4141
"get-stream": "6.0.1",
4242
"got": "11.8.6",
43-
"nyc": "15.1.0",
4443
"p-retry": "4.6.2",
4544
"semantic-release": "20.0.2",
4645
"sinon": "15.0.1",
@@ -64,7 +63,7 @@
6463
],
6564
"license": "MIT",
6665
"main": "index.js",
67-
"nyc": {
66+
"c8": {
6867
"include": [
6968
"lib/**/*.js",
7069
"index.js"
@@ -95,9 +94,10 @@
9594
"lint": "xo",
9695
"pretest": "npm run lint",
9796
"semantic-release": "semantic-release",
98-
"test": "nyc ava -v",
99-
"test:ci": "nyc ava -v"
97+
"test": "c8 ava -v",
98+
"test:ci": "c8 ava -v"
10099
},
100+
"type": "module",
101101
"xo": {
102102
"prettier": true,
103103
"space": true,

‎test/get-channel.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const test = require('ava');
2-
const getChannel = require('../lib/get-channel');
1+
import test from 'ava';
2+
import getChannel from '../lib/get-channel.js';
33

44
test.serial('Get default channel', (t) => {
55
t.is(getChannel(undefined), 'latest');

‎test/get-pkg.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const path = require('path');
2-
const test = require('ava');
3-
const {outputJson, writeFile} = require('fs-extra');
4-
const tempy = require('tempy');
5-
const getPkg = require('../lib/get-pkg');
1+
import path from 'path';
2+
import test from 'ava';
3+
import fs from 'fs-extra';
4+
import tempy from 'tempy';
5+
import getPkg from '../lib/get-pkg.js';
66

77
test('Verify name and version then return parsed package.json', async (t) => {
88
const cwd = tempy.directory();
99
const pkg = {name: 'package', version: '0.0.0'};
10-
await outputJson(path.resolve(cwd, 'package.json'), pkg);
10+
await fs.outputJson(path.resolve(cwd, 'package.json'), pkg);
1111

1212
const result = await getPkg({}, {cwd});
1313
t.is(pkg.name, result.name);
@@ -18,7 +18,7 @@ test('Verify name and version then return parsed package.json from a sub-directo
1818
const cwd = tempy.directory();
1919
const pkgRoot = 'dist';
2020
const pkg = {name: 'package', version: '0.0.0'};
21-
await outputJson(path.resolve(cwd, pkgRoot, 'package.json'), pkg);
21+
await fs.outputJson(path.resolve(cwd, pkgRoot, 'package.json'), pkg);
2222

2323
const result = await getPkg({pkgRoot}, {cwd});
2424
t.is(pkg.name, result.name);
@@ -35,7 +35,7 @@ test('Throw error if missing package.json', async (t) => {
3535

3636
test('Throw error if missing package name', async (t) => {
3737
const cwd = tempy.directory();
38-
await outputJson(path.resolve(cwd, 'package.json'), {version: '0.0.0'});
38+
await fs.outputJson(path.resolve(cwd, 'package.json'), {version: '0.0.0'});
3939

4040
const [error] = await t.throwsAsync(getPkg({}, {cwd}));
4141

@@ -45,7 +45,7 @@ test('Throw error if missing package name', async (t) => {
4545

4646
test('Throw error if package.json is malformed', async (t) => {
4747
const cwd = tempy.directory();
48-
await writeFile(path.resolve(cwd, 'package.json'), "{name: 'package',}");
48+
await fs.writeFile(path.resolve(cwd, 'package.json'), "{name: 'package',}");
4949

5050
const [error] = await t.throwsAsync(getPkg({}, {cwd}));
5151

‎test/get-registry.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const path = require('path');
2-
const test = require('ava');
3-
const {appendFile} = require('fs-extra');
4-
const tempy = require('tempy');
5-
const getRegistry = require('../lib/get-registry');
1+
import path from 'path';
2+
import test from 'ava';
3+
import fs from 'fs-extra';
4+
import tempy from 'tempy';
5+
import getRegistry from '../lib/get-registry.js';
66

77
test('Get default registry', (t) => {
88
const cwd = tempy.directory();
@@ -12,14 +12,14 @@ test('Get default registry', (t) => {
1212

1313
test('Get the registry configured in ".npmrc" and normalize trailing slash', async (t) => {
1414
const cwd = tempy.directory();
15-
await appendFile(path.resolve(cwd, '.npmrc'), 'registry = https://custom1.registry.com');
15+
await fs.appendFile(path.resolve(cwd, '.npmrc'), 'registry = https://custom1.registry.com');
1616

1717
t.is(getRegistry({name: 'package-name'}, {cwd, env: {}}), 'https://custom1.registry.com/');
1818
});
1919

2020
test('Get the registry configured from "publishConfig"', async (t) => {
2121
const cwd = tempy.directory();
22-
await appendFile(path.resolve(cwd, '.npmrc'), 'registry = https://custom2.registry.com');
22+
await fs.appendFile(path.resolve(cwd, '.npmrc'), 'registry = https://custom2.registry.com');
2323

2424
t.is(
2525
getRegistry({name: 'package-name', publishConfig: {registry: 'https://custom3.registry.com/'}}, {cwd, env: {}}),
@@ -38,14 +38,14 @@ test('Get the registry configured in "NPM_CONFIG_REGISTRY"', (t) => {
3838

3939
test('Get the registry configured in ".npmrc" for scoped package', async (t) => {
4040
const cwd = tempy.directory();
41-
await appendFile(path.resolve(cwd, '.npmrc'), '@scope:registry = https://custom3.registry.com');
41+
await fs.appendFile(path.resolve(cwd, '.npmrc'), '@scope:registry = https://custom3.registry.com');
4242

4343
t.is(getRegistry({name: '@scope/package-name'}, {cwd, env: {}}), 'https://custom3.registry.com/');
4444
});
4545

4646
test.serial('Get the registry configured via "NPM_CONFIG_USERCONFIG" for scoped package', async (t) => {
4747
const cwd = tempy.directory();
48-
await appendFile(path.resolve(cwd, '.custom-npmrc'), '@scope:registry = https://custom4.registry.com');
48+
await fs.appendFile(path.resolve(cwd, '.custom-npmrc'), '@scope:registry = https://custom4.registry.com');
4949

5050
t.is(
5151
getRegistry(

‎test/get-release-info.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const test = require('ava');
2-
const getReleaseInfo = require('../lib/get-release-info');
1+
import test from 'ava';
2+
import getReleaseInfo from '../lib/get-release-info.js';
33

44
test('Default registry and scoped module', async (t) => {
55
t.deepEqual(

‎test/helpers/npm-registry.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
const Docker = require('dockerode');
2-
const getStream = require('get-stream');
3-
const got = require('got');
4-
const path = require('path');
5-
const delay = require('delay');
6-
const pRetry = require('p-retry');
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import Docker from 'dockerode';
4+
import getStream from 'get-stream';
5+
import got from 'got';
6+
import path from 'path';
7+
import delay from 'delay';
8+
import pRetry from 'p-retry';
79

810
const IMAGE = 'verdaccio/verdaccio:4';
911
const REGISTRY_PORT = 4873;
@@ -12,12 +14,14 @@ const NPM_USERNAME = 'integration';
1214
const NPM_PASSWORD = 'suchsecure';
1315
const NPM_EMAIL = 'integration@test.com';
1416
const docker = new Docker();
17+
const __dirname = dirname(fileURLToPath(import.meta.url));
18+
1519
let container;
1620

1721
/**
1822
* Download the `npm-registry-docker` Docker image, create a new container and start it.
1923
*/
20-
async function start() {
24+
export async function start() {
2125
await getStream(await docker.pull(IMAGE));
2226

2327
container = await docker.createContainer({
@@ -55,21 +59,19 @@ async function start() {
5559
});
5660
}
5761

58-
const url = `http://${REGISTRY_HOST}:${REGISTRY_PORT}/`;
62+
export const url = `http://${REGISTRY_HOST}:${REGISTRY_PORT}/`;
5963

60-
const authEnv = {
64+
export const authEnv = {
6165
npm_config_registry: url, // eslint-disable-line camelcase
6266
NPM_USERNAME,
6367
NPM_PASSWORD,
6468
NPM_EMAIL,
6569
};
6670

6771
/**
68-
* Stop and remote the `npm-registry-docker` Docker container.
72+
* Stop and remove the `npm-registry-docker` Docker container.
6973
*/
70-
async function stop() {
74+
export async function stop() {
7175
await container.stop();
7276
await container.remove();
7377
}
74-
75-
module.exports = {start, stop, authEnv, url};

‎test/integration.test.js

+57-60
Large diffs are not rendered by default.

‎test/prepare.test.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const path = require('path');
2-
const test = require('ava');
3-
const {outputJson, readJson, outputFile, readFile, pathExists, appendFile} = require('fs-extra');
4-
const tempy = require('tempy');
5-
const execa = require('execa');
6-
const {stub} = require('sinon');
7-
const {WritableStreamBuffer} = require('stream-buffers');
8-
const prepare = require('../lib/prepare');
1+
import path from 'path';
2+
import test from 'ava';
3+
import fs from 'fs-extra';
4+
import tempy from 'tempy';
5+
import execa from 'execa';
6+
import { stub } from 'sinon';
7+
import { WritableStreamBuffer } from 'stream-buffers';
8+
import prepare from '../lib/prepare.js';
99

1010
test.beforeEach((t) => {
1111
t.context.log = stub();
@@ -18,7 +18,7 @@ test('Updade package.json', async (t) => {
1818
const cwd = tempy.directory();
1919
const npmrc = tempy.file({name: '.npmrc'});
2020
const packagePath = path.resolve(cwd, 'package.json');
21-
await outputJson(packagePath, {version: '0.0.0-dev'});
21+
await fs.outputJson(packagePath, {version: '0.0.0-dev'});
2222

2323
await prepare(
2424
npmrc,
@@ -34,7 +34,7 @@ test('Updade package.json', async (t) => {
3434
);
3535

3636
// Verify package.json has been updated
37-
t.is((await readJson(packagePath)).version, '1.0.0');
37+
t.is((await fs.readJson(packagePath)).version, '1.0.0');
3838

3939
// Verify the logger has been called with the version updated
4040
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
@@ -45,7 +45,7 @@ test('Updade package.json and npm-shrinkwrap.json', async (t) => {
4545
const npmrc = tempy.file({name: '.npmrc'});
4646
const packagePath = path.resolve(cwd, 'package.json');
4747
const shrinkwrapPath = path.resolve(cwd, 'npm-shrinkwrap.json');
48-
await outputJson(packagePath, {version: '0.0.0-dev'});
48+
await fs.outputJson(packagePath, {version: '0.0.0-dev'});
4949
// Create a npm-shrinkwrap.json file
5050
await execa('npm', ['shrinkwrap'], {cwd});
5151

@@ -63,8 +63,8 @@ test('Updade package.json and npm-shrinkwrap.json', async (t) => {
6363
);
6464

6565
// Verify package.json and npm-shrinkwrap.json have been updated
66-
t.is((await readJson(packagePath)).version, '1.0.0');
67-
t.is((await readJson(shrinkwrapPath)).version, '1.0.0');
66+
t.is((await fs.readJson(packagePath)).version, '1.0.0');
67+
t.is((await fs.readJson(shrinkwrapPath)).version, '1.0.0');
6868
// Verify the logger has been called with the version updated
6969
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
7070
});
@@ -74,8 +74,8 @@ test('Updade package.json and package-lock.json', async (t) => {
7474
const npmrc = tempy.file({name: '.npmrc'});
7575
const packagePath = path.resolve(cwd, 'package.json');
7676
const packageLockPath = path.resolve(cwd, 'package-lock.json');
77-
await outputJson(packagePath, {version: '0.0.0-dev'});
78-
await appendFile(path.resolve(cwd, '.npmrc'), 'package-lock = true');
77+
await fs.outputJson(packagePath, {version: '0.0.0-dev'});
78+
await fs.appendFile(path.resolve(cwd, '.npmrc'), 'package-lock = true');
7979
// Create a package-lock.json file
8080
await execa('npm', ['install'], {cwd});
8181

@@ -93,8 +93,8 @@ test('Updade package.json and package-lock.json', async (t) => {
9393
);
9494

9595
// Verify package.json and package-lock.json have been updated
96-
t.is((await readJson(packagePath)).version, '1.0.0');
97-
t.is((await readJson(packageLockPath)).version, '1.0.0');
96+
t.is((await fs.readJson(packagePath)).version, '1.0.0');
97+
t.is((await fs.readJson(packageLockPath)).version, '1.0.0');
9898
// Verify the logger has been called with the version updated
9999
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
100100
});
@@ -105,7 +105,7 @@ test('Updade package.json and npm-shrinkwrap.json in a sub-directory', async (t)
105105
const pkgRoot = 'dist';
106106
const packagePath = path.resolve(cwd, pkgRoot, 'package.json');
107107
const shrinkwrapPath = path.resolve(cwd, pkgRoot, 'npm-shrinkwrap.json');
108-
await outputJson(packagePath, {version: '0.0.0-dev'});
108+
await fs.outputJson(packagePath, {version: '0.0.0-dev'});
109109
// Create a npm-shrinkwrap.json file
110110
await execa('npm', ['shrinkwrap'], {cwd: path.resolve(cwd, pkgRoot)});
111111

@@ -123,8 +123,8 @@ test('Updade package.json and npm-shrinkwrap.json in a sub-directory', async (t)
123123
);
124124

125125
// Verify package.json and npm-shrinkwrap.json have been updated
126-
t.is((await readJson(packagePath)).version, '1.0.0');
127-
t.is((await readJson(shrinkwrapPath)).version, '1.0.0');
126+
t.is((await fs.readJson(packagePath)).version, '1.0.0');
127+
t.is((await fs.readJson(shrinkwrapPath)).version, '1.0.0');
128128
// Verify the logger has been called with the version updated
129129
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]);
130130
});
@@ -135,8 +135,8 @@ test('Updade package.json and package-lock.json in a sub-directory', async (t) =
135135
const pkgRoot = 'dist';
136136
const packagePath = path.resolve(cwd, pkgRoot, 'package.json');
137137
const packageLockPath = path.resolve(cwd, pkgRoot, 'package-lock.json');
138-
await outputJson(packagePath, {version: '0.0.0-dev'});
139-
await appendFile(path.resolve(cwd, pkgRoot, '.npmrc'), 'package-lock = true');
138+
await fs.outputJson(packagePath, {version: '0.0.0-dev'});
139+
await fs.appendFile(path.resolve(cwd, pkgRoot, '.npmrc'), 'package-lock = true');
140140
// Create a package-lock.json file
141141
await execa('npm', ['install'], {cwd: path.resolve(cwd, pkgRoot)});
142142

@@ -154,8 +154,8 @@ test('Updade package.json and package-lock.json in a sub-directory', async (t) =
154154
);
155155

156156
// Verify package.json and package-lock.json have been updated
157-
t.is((await readJson(packagePath)).version, '1.0.0');
158-
t.is((await readJson(packageLockPath)).version, '1.0.0');
157+
t.is((await fs.readJson(packagePath)).version, '1.0.0');
158+
t.is((await fs.readJson(packageLockPath)).version, '1.0.0');
159159
// Verify the logger has been called with the version updated
160160
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]);
161161
});
@@ -164,7 +164,7 @@ test('Preserve indentation and newline', async (t) => {
164164
const cwd = tempy.directory();
165165
const npmrc = tempy.file({name: '.npmrc'});
166166
const packagePath = path.resolve(cwd, 'package.json');
167-
await outputFile(packagePath, `{\r\n "name": "package-name",\r\n "version": "0.0.0-dev"\r\n}\r\n`);
167+
await fs.outputFile(packagePath, `{\r\n "name": "package-name",\r\n "version": "0.0.0-dev"\r\n}\r\n`);
168168

169169
await prepare(
170170
npmrc,
@@ -181,7 +181,7 @@ test('Preserve indentation and newline', async (t) => {
181181

182182
// Verify package.json has been updated
183183
t.is(
184-
await readFile(packagePath, 'utf-8'),
184+
await fs.readFile(packagePath, 'utf-8'),
185185
`{\r\n "name": "package-name",\r\n "version": "1.0.0"\r\n}\r\n`
186186
);
187187

@@ -194,7 +194,7 @@ test('Create the package in the "tarballDir" directory', async (t) => {
194194
const npmrc = tempy.file({name: '.npmrc'});
195195
const packagePath = path.resolve(cwd, 'package.json');
196196
const pkg = {name: 'my-pkg', version: '0.0.0-dev'};
197-
await outputJson(packagePath, pkg);
197+
await fs.outputJson(packagePath, pkg);
198198

199199
await prepare(
200200
npmrc,
@@ -210,9 +210,9 @@ test('Create the package in the "tarballDir" directory', async (t) => {
210210
);
211211

212212
// Verify package.json has been updated
213-
t.is((await readJson(packagePath)).version, '1.0.0');
213+
t.is((await fs.readJson(packagePath)).version, '1.0.0');
214214

215-
t.true(await pathExists(path.resolve(cwd, `tarball/${pkg.name}-1.0.0.tgz`)));
215+
t.true(await fs.pathExists(path.resolve(cwd, `tarball/${pkg.name}-1.0.0.tgz`)));
216216
// Verify the logger has been called with the version updated
217217
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
218218
});
@@ -222,7 +222,7 @@ test('Only move the created tarball if the "tarballDir" directory is not the CWD
222222
const npmrc = tempy.file({name: '.npmrc'});
223223
const packagePath = path.resolve(cwd, 'package.json');
224224
const pkg = {name: 'my-pkg', version: '0.0.0-dev'};
225-
await outputJson(packagePath, pkg);
225+
await fs.outputJson(packagePath, pkg);
226226

227227
await prepare(
228228
npmrc,
@@ -238,9 +238,9 @@ test('Only move the created tarball if the "tarballDir" directory is not the CWD
238238
);
239239

240240
// Verify package.json has been updated
241-
t.is((await readJson(packagePath)).version, '1.0.0');
241+
t.is((await fs.readJson(packagePath)).version, '1.0.0');
242242

243-
t.true(await pathExists(path.resolve(cwd, `${pkg.name}-1.0.0.tgz`)));
243+
t.true(await fs.pathExists(path.resolve(cwd, `${pkg.name}-1.0.0.tgz`)));
244244
// Verify the logger has been called with the version updated
245245
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
246246
});

‎test/set-npmrc-auth.test.js

+46-38
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
const path = require('path');
2-
const test = require('ava');
3-
const {readFile, appendFile} = require('fs-extra');
4-
const {stub} = require('sinon');
5-
const tempy = require('tempy');
6-
const clearModule = require('clear-module');
1+
import path from 'path';
2+
import test from 'ava';
3+
import fs from 'fs-extra';
4+
import { stub } from 'sinon';
5+
import tempy from 'tempy';
76

87
const {HOME} = process.env;
98
const cwd = process.cwd();
@@ -12,9 +11,6 @@ test.beforeEach((t) => {
1211
// Stub the logger
1312
t.context.log = stub();
1413
t.context.logger = {log: t.context.log};
15-
16-
clearModule('rc');
17-
clearModule('../lib/set-npmrc-auth');
1814
});
1915

2016
test.afterEach.always(() => {
@@ -29,9 +25,10 @@ test.serial('Set auth with "NPM_TOKEN"', async (t) => {
2925
const npmrc = tempy.file({name: '.npmrc'});
3026
const env = {NPM_TOKEN: 'npm_token'};
3127

32-
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
28+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
29+
await setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
3330

34-
t.regex((await readFile(npmrc)).toString(), /\/\/custom.registry.com\/:_authToken = \${NPM_TOKEN}/);
31+
t.regex((await fs.readFile(npmrc)).toString(), /\/\/custom.registry.com\/:_authToken = \${NPM_TOKEN}/);
3532
t.deepEqual(t.context.log.args[1], [`Wrote NPM_TOKEN to ${npmrc}`]);
3633
});
3734

@@ -42,9 +39,10 @@ test.serial('Set auth with "NPM_USERNAME", "NPM_PASSWORD" and "NPM_EMAIL"', asyn
4239
const npmrc = tempy.file({name: '.npmrc'});
4340
const env = {NPM_USERNAME: 'npm_username', NPM_PASSWORD: 'npm_pasword', NPM_EMAIL: 'npm_email'};
4441

45-
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
42+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
43+
await setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
4644

47-
t.is((await readFile(npmrc)).toString(), `_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`);
45+
t.is((await fs.readFile(npmrc)).toString(), `_auth = \${LEGACY_TOKEN}\nemail = \${NPM_EMAIL}`);
4846
t.deepEqual(t.context.log.args[1], [`Wrote NPM_USERNAME, NPM_PASSWORD and NPM_EMAIL to ${npmrc}`]);
4947
});
5048

@@ -55,11 +53,12 @@ test.serial('Preserve home ".npmrc"', async (t) => {
5553
const npmrc = tempy.file({name: '.npmrc'});
5654
const env = {NPM_TOKEN: 'npm_token'};
5755

58-
await appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
56+
await fs.appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
5957

60-
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
58+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
59+
await setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
6160

62-
t.is((await readFile(npmrc)).toString(), `home_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
61+
t.is((await fs.readFile(npmrc)).toString(), `home_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
6362
t.deepEqual(t.context.log.args[1], [
6463
'Reading npm config from %s',
6564
[path.resolve(process.env.HOME, '.npmrc')].join(', '),
@@ -74,13 +73,14 @@ test.serial('Preserve home and local ".npmrc"', async (t) => {
7473
const npmrc = tempy.file({name: '.npmrc'});
7574
const env = {NPM_TOKEN: 'npm_token'};
7675

77-
await appendFile(path.resolve(cwd, '.npmrc'), 'cwd_config = test');
78-
await appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
76+
await fs.appendFile(path.resolve(cwd, '.npmrc'), 'cwd_config = test');
77+
await fs.appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
7978

80-
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
79+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
80+
await setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger});
8181

8282
t.is(
83-
(await readFile(npmrc)).toString(),
83+
(await fs.readFile(npmrc)).toString(),
8484
`home_config = test\ncwd_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
8585
);
8686
t.deepEqual(t.context.log.args[1], [
@@ -96,12 +96,13 @@ test.serial('Preserve all ".npmrc" if auth is already configured', async (t) =>
9696
process.chdir(cwd);
9797
const npmrc = tempy.file({name: '.npmrc'});
9898

99-
await appendFile(path.resolve(cwd, '.npmrc'), `//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
100-
await appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
99+
await fs.appendFile(path.resolve(cwd, '.npmrc'), `//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
100+
await fs.appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
101101

102-
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env: {}, logger: t.context.logger});
102+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
103+
await setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env: {}, logger: t.context.logger});
103104

104-
t.is((await readFile(npmrc)).toString(), `home_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
105+
t.is((await fs.readFile(npmrc)).toString(), `home_config = test\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
105106
t.deepEqual(t.context.log.args[1], [
106107
'Reading npm config from %s',
107108
[path.resolve(process.env.HOME, '.npmrc'), path.resolve(cwd, '.npmrc')].join(', '),
@@ -114,16 +115,17 @@ test.serial('Preserve ".npmrc" if auth is already configured for a scoped packag
114115
process.chdir(cwd);
115116
const npmrc = tempy.file({name: '.npmrc'});
116117

117-
await appendFile(
118+
await fs.appendFile(
118119
path.resolve(cwd, '.npmrc'),
119120
`@scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
120121
);
121-
await appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
122+
await fs.appendFile(path.resolve(process.env.HOME, '.npmrc'), 'home_config = test');
122123

123-
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env: {}, logger: t.context.logger});
124+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
125+
await setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env: {}, logger: t.context.logger});
124126

125127
t.is(
126-
(await readFile(npmrc)).toString(),
128+
(await fs.readFile(npmrc)).toString(),
127129
`home_config = test\n@scope:registry=http://custom.registry.com\n//custom.registry.com/:_authToken = \${NPM_TOKEN}`
128130
);
129131
t.deepEqual(t.context.log.args[1], [
@@ -138,8 +140,9 @@ test.serial('Throw error if "NPM_TOKEN" is missing', async (t) => {
138140
process.chdir(cwd);
139141
const npmrc = tempy.file({name: '.npmrc'});
140142

143+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
141144
const [error] = await t.throwsAsync(
142-
require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env: {}, logger: t.context.logger})
145+
setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env: {}, logger: t.context.logger})
143146
);
144147

145148
t.is(error.name, 'SemanticReleaseError');
@@ -153,15 +156,16 @@ test.serial('Emulate npm config resolution if "NPM_CONFIG_USERCONFIG" is set', a
153156
process.chdir(cwd);
154157
const npmrc = tempy.file({name: '.npmrc'});
155158

156-
await appendFile(path.resolve(cwd, '.custom-npmrc'), `//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
159+
await fs.appendFile(path.resolve(cwd, '.custom-npmrc'), `//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
157160

158-
await require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {
161+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
162+
await setNpmrcAuth(npmrc, 'http://custom.registry.com', {
159163
cwd,
160164
env: {NPM_CONFIG_USERCONFIG: path.resolve(cwd, '.custom-npmrc')},
161165
logger: t.context.logger,
162166
});
163167

164-
t.is((await readFile(npmrc)).toString(), `//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
168+
t.is((await fs.readFile(npmrc)).toString(), `//custom.registry.com/:_authToken = \${NPM_TOKEN}`);
165169
t.deepEqual(t.context.log.args[1], ['Reading npm config from %s', [path.resolve(cwd, '.custom-npmrc')].join(', ')]);
166170
});
167171

@@ -172,8 +176,9 @@ test.serial('Throw error if "NPM_USERNAME" is missing', async (t) => {
172176
const npmrc = tempy.file({name: '.npmrc'});
173177
const env = {NPM_PASSWORD: 'npm_pasword', NPM_EMAIL: 'npm_email'};
174178

179+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
175180
const [error] = await t.throwsAsync(
176-
require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger})
181+
setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger})
177182
);
178183

179184
t.is(error.name, 'SemanticReleaseError');
@@ -188,8 +193,9 @@ test.serial('Throw error if "NPM_PASSWORD" is missing', async (t) => {
188193
const npmrc = tempy.file({name: '.npmrc'});
189194
const env = {NPM_USERNAME: 'npm_username', NPM_EMAIL: 'npm_email'};
190195

196+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
191197
const [error] = await t.throwsAsync(
192-
require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger})
198+
setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger})
193199
);
194200

195201
t.is(error.name, 'SemanticReleaseError');
@@ -204,8 +210,9 @@ test.serial('Throw error if "NPM_EMAIL" is missing', async (t) => {
204210
const npmrc = tempy.file({name: '.npmrc'});
205211
const env = {NPM_USERNAME: 'npm_username', NPM_PASSWORD: 'npm_password'};
206212

213+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
207214
const [error] = await t.throwsAsync(
208-
require('../lib/set-npmrc-auth')(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger})
215+
setNpmrcAuth(npmrc, 'http://custom.registry.com', {cwd, env, logger: t.context.logger})
209216
);
210217

211218
t.is(error.name, 'SemanticReleaseError');
@@ -221,12 +228,13 @@ test.serial('Prefer .npmrc over environment variables', async (t) => {
221228
// Specify an NPM token environment variable
222229
const env = {NPM_TOKEN: 'env_npm_token'};
223230

224-
await appendFile(path.resolve(cwd, '.npmrc'), '//registry.npmjs.org/:_authToken=npmrc_npm_token');
231+
await fs.appendFile(path.resolve(cwd, '.npmrc'), '//registry.npmjs.org/:_authToken=npmrc_npm_token');
225232

226-
await require('../lib/set-npmrc-auth')(npmrc, 'http://registry.npmjs.org', {cwd, env, logger: t.context.logger});
233+
const setNpmrcAuth = (await import('../lib/set-npmrc-auth.js')).default;
234+
await setNpmrcAuth(npmrc, 'http://registry.npmjs.org', {cwd, env, logger: t.context.logger});
227235

228236
t.is(
229-
(await readFile(npmrc)).toString(),
237+
(await fs.readFile(npmrc)).toString(),
230238
// Assert did not write the token from environment variable
231239
`//registry.npmjs.org/:_authToken=npmrc_npm_token`
232240
);

‎test/verify-config.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const test = require('ava');
2-
const {stub} = require('sinon');
3-
const verify = require('../lib/verify-config');
1+
import test from 'ava';
2+
import { stub } from 'sinon';
3+
import verify from '../lib/verify-config.js';
44

55
test.beforeEach((t) => {
66
// Stub the logger functions

0 commit comments

Comments
 (0)
Please sign in to comment.