Skip to content

Commit 8565d9d

Browse files
committed
feat: Read package.json file instead using the one passed by semantic-release
1 parent 2058e9e commit 8565d9d

File tree

10 files changed

+93
-42
lines changed

10 files changed

+93
-42
lines changed

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
const getPkg = require('./lib/get-pkg');
12
const verifyNpm = require('./lib/verify');
23
const publishNpm = require('./lib/publish');
34
const getLastReleaseNpm = require('./lib/get-last-release');
45

56
let verified;
67

7-
async function verifyConditions(pluginConfig, {pkg, logger}) {
8+
async function verifyConditions(pluginConfig, {logger}) {
9+
const pkg = await getPkg();
810
await verifyNpm(pkg, logger);
911
verified = true;
1012
}
1113

12-
async function getLastRelease(pluginConfig, {pkg, logger}) {
14+
async function getLastRelease(pluginConfig, {logger}) {
15+
// Reload package.json in case a previous external step updated it
16+
const pkg = await getPkg();
1317
if (!verified) {
1418
await verifyNpm(pkg, logger);
1519
verified = true;
1620
}
1721
return getLastReleaseNpm(pkg, logger);
1822
}
1923

20-
async function publish(pluginConfig, {pkg, nextRelease: {version}, logger}) {
24+
async function publish(pluginConfig, {nextRelease: {version}, logger}) {
25+
// Reload package.json in case a previous external step updated it
26+
const pkg = await getPkg();
2127
if (!verified) {
2228
await verifyNpm(pkg, logger);
2329
verified = true;

lib/get-pkg.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const readPkgUp = require('read-pkg-up');
2+
const SemanticReleaseError = require('@semantic-release/error');
3+
4+
module.exports = async () => {
5+
const {pkg} = await readPkgUp();
6+
7+
if (!pkg) {
8+
throw new SemanticReleaseError('A package.json file is required to release on npm.', 'ENOPKG');
9+
}
10+
11+
if (!pkg.name) {
12+
throw new SemanticReleaseError('No "name" found in package.json.', 'ENOPKGNAME');
13+
}
14+
15+
return pkg;
16+
};

lib/verify-pkg.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

lib/verify.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
const execa = require('execa');
22
const SemanticReleaseError = require('@semantic-release/error');
3-
const verifyPkg = require('./verify-pkg');
43
const setNpmrcAuth = require('./set-npmrc-auth');
54

65
module.exports = async (pkg, logger) => {
7-
verifyPkg(pkg);
86
await setNpmrcAuth(pkg, logger);
97
try {
108
await execa('npm', ['whoami']);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"nerf-dart": "^1.0.0",
2323
"npm-conf": "^1.1.3",
2424
"npm-registry-client": "^8.5.0",
25+
"read-pkg-up": "^3.0.0",
2526
"registry-auth-token": "^3.3.1"
2627
},
2728
"devDependencies": {
@@ -39,7 +40,7 @@
3940
"nock": "^9.1.0",
4041
"nyc": "^11.2.1",
4142
"prettier": "~1.8.2",
42-
"semantic-release": "^9.1.1",
43+
"semantic-release": "^10.0.0",
4344
"sinon": "^4.1.2",
4445
"tempy": "^0.2.1",
4546
"xo": "^0.18.2"

test/get-pkg.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import test from 'ava';
2+
import {writeJson, writeFile} from 'fs-extra';
3+
import tempy from 'tempy';
4+
import getPkg from '../lib/get-pkg';
5+
6+
test.beforeEach(t => {
7+
// Save the current working diretory
8+
t.context.cwd = process.cwd();
9+
// Change current working directory to a temp directory
10+
process.chdir(tempy.directory());
11+
});
12+
13+
test.afterEach.always(t => {
14+
// Restore the current working directory
15+
process.chdir(t.context.cwd);
16+
});
17+
18+
test.serial('Verify name and return parsed package.json', async t => {
19+
const pkg = {name: 'package', version: '0.0.0'};
20+
await writeJson('./package.json', pkg);
21+
22+
const result = await getPkg();
23+
t.is(pkg.name, result.name);
24+
t.is(pkg.version, result.version);
25+
});
26+
27+
test.serial('Throw error if missing package.json', async t => {
28+
const error = await t.throws(getPkg());
29+
30+
t.is(error.name, 'SemanticReleaseError');
31+
t.is(error.code, 'ENOPKG');
32+
});
33+
34+
test.serial('Throw error if missing package name', async t => {
35+
await writeJson('./package.json', {version: '0.0.0'});
36+
37+
const error = await t.throws(getPkg());
38+
39+
t.is(error.name, 'SemanticReleaseError');
40+
t.is(error.code, 'ENOPKGNAME');
41+
});
42+
43+
test.serial('Throw error if package.json is malformed', async t => {
44+
await writeFile('./package.json', "{name: 'package',}");
45+
46+
const error = await t.throws(getPkg());
47+
48+
t.is(error.name, 'JSONError');
49+
});

test/integration.test.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ test.after.always(async () => {
6666

6767
test.serial('Throws error if NPM token is invalid', async t => {
6868
process.env.NPM_TOKEN = 'wrong_token';
69-
const error = await t.throws(
70-
t.context.m.verifyConditions({}, {pkg: {name: 'invalid-token'}, logger: t.context.logger})
71-
);
69+
const pkg = {name: 'published', version: '1.0.0', publishConfig: {registry: npmRegistry.url}};
70+
await writeJson('./package.json', pkg);
71+
const error = await t.throws(t.context.m.verifyConditions({}, {logger: t.context.logger}));
7272

7373
t.true(error instanceof SemanticReleaseError);
7474
t.is(error.code, 'EINVALIDNPMTOKEN');
@@ -81,7 +81,8 @@ test.serial('Throws error if NPM token is invalid', async t => {
8181
test.serial('Verify npm auth and package', async t => {
8282
Object.assign(process.env, npmRegistry.authEnv);
8383
const pkg = {name: 'valid-token', publishConfig: {registry: npmRegistry.url}};
84-
await t.notThrows(t.context.m.verifyConditions({}, {pkg, logger: t.context.logger}));
84+
await writeJson('./package.json', pkg);
85+
await t.notThrows(t.context.m.verifyConditions({}, {logger: t.context.logger}));
8586

8687
const npmrc = (await readFile('.npmrc')).toString();
8788
t.regex(npmrc, /_auth =/);
@@ -91,7 +92,8 @@ test.serial('Verify npm auth and package', async t => {
9192
test.serial('Return nothing if no version if published', async t => {
9293
Object.assign(process.env, npmRegistry.authEnv);
9394
const pkg = {name: 'not-published', publishConfig: {registry: npmRegistry.url}};
94-
const nextRelease = await t.context.m.getLastRelease({}, {pkg, logger: t.context.logger});
95+
await writeJson('./package.json', pkg);
96+
const nextRelease = await t.context.m.getLastRelease({}, {logger: t.context.logger});
9597

9698
t.deepEqual(nextRelease, {});
9799
});
@@ -110,7 +112,7 @@ test.serial('Return last version published', async t => {
110112

111113
await execa('npm', ['publish']);
112114

113-
const nextRelease = await t.context.m.getLastRelease({}, {pkg, logger: t.context.logger});
115+
const nextRelease = await t.context.m.getLastRelease({}, {logger: t.context.logger});
114116
t.is(nextRelease.version, '1.0.0');
115117
});
116118

@@ -133,7 +135,7 @@ test.serial('Return last version published on a dist-tag', async t => {
133135
// Publish version 1.1.0 on next
134136
await execa('npm', ['publish', '--tag=next']);
135137

136-
const nextRelease = await t.context.m.getLastRelease({}, {pkg, logger: t.context.logger});
138+
const nextRelease = await t.context.m.getLastRelease({}, {logger: t.context.logger});
137139
t.is(nextRelease.version, '1.1.0');
138140
});
139141

@@ -152,7 +154,7 @@ test.serial('Return nothing for an unpublished package', async t => {
152154
await execa('npm', ['publish']);
153155
await execa('npm', ['unpublish', 'unpublished', '--force']);
154156

155-
const nextRelease = await t.context.m.getLastRelease({}, {pkg, logger: t.context.logger});
157+
const nextRelease = await t.context.m.getLastRelease({}, {logger: t.context.logger});
156158
t.deepEqual(nextRelease, {});
157159
});
158160

@@ -161,7 +163,7 @@ test.serial('Publish a package', async t => {
161163
const pkg = {name: 'publish', version: '1.0.0', publishConfig: {registry: npmRegistry.url}};
162164
await writeJson('./package.json', pkg);
163165

164-
await t.context.m.publish({}, {pkg, logger: t.context.logger, nextRelease: {version: '1.0.0'}});
166+
await t.context.m.publish({}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
165167

166168
t.is((await execa('npm', ['view', 'publish', 'version'])).stdout, '1.0.0');
167169
});
@@ -171,13 +173,13 @@ test.serial('Verify token and set up auth only on the fist call', async t => {
171173
const pkg = {name: 'test-module', version: '0.0.0-dev', publishConfig: {registry: npmRegistry.url}};
172174
await writeJson('./package.json', pkg);
173175

174-
await t.notThrows(t.context.m.verifyConditions({}, {pkg, logger: t.context.logger}));
176+
await t.notThrows(t.context.m.verifyConditions({}, {logger: t.context.logger}));
175177

176-
let nextRelease = await t.context.m.getLastRelease({}, {pkg, logger: t.context.logger});
178+
let nextRelease = await t.context.m.getLastRelease({}, {logger: t.context.logger});
177179
t.deepEqual(nextRelease, {});
178180

179-
await t.context.m.publish({}, {pkg, logger: t.context.logger, nextRelease: {version: '1.0.0'}});
181+
await t.context.m.publish({}, {logger: t.context.logger, nextRelease: {version: '1.0.0'}});
180182

181-
nextRelease = await t.context.m.getLastRelease({}, {pkg, logger: t.context.logger});
183+
nextRelease = await t.context.m.getLastRelease({}, {logger: t.context.logger});
182184
t.is(nextRelease.version, '1.0.0');
183185
});

test/set-npmrc-auth.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test.beforeEach(t => {
2727
});
2828

2929
test.afterEach.always(t => {
30-
// Clear `rc` from the npm cache as it cache the relative path of .npmrc files, preventing to load a new file after changing current working directory
30+
// Clear `rc` from the npm cache as it cache the relative path of .npmrc files, preventing to load a new file after changing current working directory. See https://github.com/dominictarr/rc/issues/101
3131
clearModule('rc');
3232
// Restore process.env
3333
process.env = Object.assign({}, t.context.env);

test/update-package-version.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {writeJson, readJson} from 'fs-extra';
21
import test from 'ava';
2+
import {writeJson, readJson} from 'fs-extra';
33
import tempy from 'tempy';
44
import execa from 'execa';
55
import {stub} from 'sinon';

test/verify-pkg.test.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)