Skip to content

Commit 3173a68

Browse files
committed
feat: use cwd and env options passed by core
feat: use `cwd` and `env` options passed by core BREAKING CHANGE: require `semantic-release` >= `15.8.0`
1 parent a172379 commit 3173a68

File tree

6 files changed

+80
-70
lines changed

6 files changed

+80
-70
lines changed

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const prepareChangelog = require('./lib/prepare');
44

55
let verified;
66

7-
async function verifyConditions(pluginConfig, {options}) {
7+
async function verifyConditions(pluginConfig, context) {
8+
const {options} = context;
89
// If the Changelog prepare plugin is used and has `changelogFile` configured, validate them now in order to prevent any release if the configuration is wrong
910
if (options.prepare) {
1011
const preparePlugin =
@@ -16,12 +17,12 @@ async function verifyConditions(pluginConfig, {options}) {
1617
verified = true;
1718
}
1819

19-
async function prepare(pluginConfig, {nextRelease: {notes}, logger}) {
20+
async function prepare(pluginConfig, context) {
2021
if (!verified) {
2122
await verifyChangelog(pluginConfig);
2223
verified = true;
2324
}
24-
await prepareChangelog(pluginConfig, notes, logger);
25+
await prepareChangelog(pluginConfig, context);
2526
}
2627

2728
module.exports = {verifyConditions, prepare};

lib/prepare.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1+
const path = require('path');
12
const {readFile, writeFile, ensureFile} = require('fs-extra');
23
const resolveConfig = require('./resolve-config');
34

4-
module.exports = async (pluginConfig, notes, logger) => {
5+
module.exports = async (pluginConfig, {cwd, nextRelease: {notes}, logger}) => {
56
const {changelogFile, changelogTitle} = resolveConfig(pluginConfig);
7+
const changelogPath = path.resolve(cwd, changelogFile);
68

79
if (notes) {
8-
await ensureFile(changelogFile);
9-
const currentFile = (await readFile(changelogFile)).toString().trim();
10+
await ensureFile(changelogPath);
11+
const currentFile = (await readFile(changelogPath)).toString().trim();
1012

1113
if (currentFile) {
12-
logger.log('Update %s', changelogFile);
14+
logger.log('Update %s', changelogPath);
1315
} else {
14-
logger.log('Create %s', changelogFile);
16+
logger.log('Create %s', changelogPath);
1517
}
1618

1719
const currentContent =
@@ -20,6 +22,6 @@ module.exports = async (pluginConfig, notes, logger) => {
2022
: currentFile;
2123
const content = `${notes.trim()}\n${currentContent ? `\n${currentContent}\n` : ''}`;
2224

23-
await writeFile(changelogFile, changelogTitle ? `${changelogTitle}\n\n${content}` : content);
25+
await writeFile(changelogPath, changelogTitle ? `${changelogTitle}\n\n${content}` : content);
2426
}
2527
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"all": true
6464
},
6565
"peerDependencies": {
66-
"semantic-release": ">=15.0.0 <16.0.0"
66+
"semantic-release": ">=15.8.0 <16.0.0"
6767
},
6868
"prettier": {
6969
"printWidth": 120,

test/integration.test.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1+
import path from 'path';
12
import test from 'ava';
23
import {outputFile, readFile} from 'fs-extra';
34
import {stub} from 'sinon';
45
import clearModule from 'clear-module';
56
import tempy from 'tempy';
67

7-
// Save the current working diretory
8-
const cwd = process.cwd();
9-
108
test.beforeEach(t => {
11-
// Change current working directory to a temp directory
12-
process.chdir(tempy.directory());
139
// Clear npm cache to refresh the module state
1410
clearModule('..');
1511
t.context.m = require('..');
@@ -18,62 +14,66 @@ test.beforeEach(t => {
1814
t.context.logger = {log: t.context.log};
1915
});
2016

21-
test.afterEach.always(() => {
22-
// Restore the current working directory
23-
process.chdir(cwd);
24-
});
25-
2617
test.serial('Verify "changelogFile"', async t => {
27-
const changelogFile = 'docs/changelog.txt';
18+
const cwd = tempy.directory();
2819
const notes = 'Test release note';
20+
const changelogFile = 'docs/changelog.txt';
2921

30-
await t.notThrows(t.context.m.verifyConditions({changelogFile}, {nextRelease: {notes}, options: {}}));
22+
await t.notThrows(t.context.m.verifyConditions({changelogFile}, {cwd, options: {}, nextRelease: {notes}}));
3123
});
3224

3325
test.serial('Create new CHANGELOG.md', async t => {
34-
const changelogFile = 'docs/changelog.txt';
26+
const cwd = tempy.directory();
3527
const notes = 'Test release note';
28+
const changelogFile = 'docs/changelog.txt';
29+
const changelogPath = path.resolve(cwd, changelogFile);
3630

37-
await t.context.m.prepare({changelogFile}, {nextRelease: {notes}, logger: t.context.logger});
31+
await t.context.m.prepare({changelogFile}, {cwd, options: {}, nextRelease: {notes}, logger: t.context.logger});
3832

3933
// Verify the content of the CHANGELOG.md
40-
t.is((await readFile(changelogFile)).toString(), `${notes}\n`);
34+
t.is((await readFile(changelogPath)).toString(), `${notes}\n`);
4135

42-
t.deepEqual(t.context.log.args[0], ['Create %s', changelogFile]);
36+
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
4337
});
4438

4539
test.serial('Skip changelog update if the release is empty', async t => {
46-
await outputFile('CHANGELOG.md', 'Initial CHANGELOG');
40+
const cwd = tempy.directory();
41+
const changelogFile = 'CHANGELOG.txt';
42+
const changelogPath = path.resolve(cwd, changelogFile);
43+
await outputFile(changelogPath, 'Initial CHANGELOG');
4744

48-
await t.context.m.prepare({}, {nextRelease: {}, options: {}, logger: t.context.logger});
45+
await t.context.m.prepare({}, {cwd, options: {}, nextRelease: {}, logger: t.context.logger});
4946

5047
// Verify the content of the CHANGELOG.md
51-
t.is((await readFile('CHANGELOG.md')).toString(), 'Initial CHANGELOG');
48+
t.is((await readFile(changelogPath)).toString(), 'Initial CHANGELOG');
5249
});
5350

5451
test.serial('Verify only on the fist call', async t => {
55-
const changelogFile = 'docs/changelog.txt';
52+
const cwd = tempy.directory();
5653
const notes = 'Test release note';
54+
const changelogFile = 'docs/changelog.txt';
55+
const changelogPath = path.resolve(cwd, changelogFile);
5756

5857
await t.context.m.verifyConditions(
5958
{changelogFile},
6059
{nextRelease: {notes}, options: {prepare: ['@semantic-release/git']}}
6160
);
62-
await t.context.m.prepare({changelogFile}, {nextRelease: {notes}, logger: t.context.logger});
61+
await t.context.m.prepare({changelogFile}, {cwd, nextRelease: {notes}, logger: t.context.logger});
6362

6463
// Verify the content of the CHANGELOG.md
65-
t.is((await readFile(changelogFile)).toString(), `${notes}\n`);
64+
t.is((await readFile(changelogPath)).toString(), `${notes}\n`);
6665

67-
t.deepEqual(t.context.log.args[0], ['Create %s', changelogFile]);
66+
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
6867
});
6968

7069
test('Throw SemanticReleaseError if prepare "changelogFile" option is not a string', async t => {
70+
const cwd = tempy.directory();
7171
const changelogFile = 42;
7272
const errors = [
7373
...(await t.throws(
7474
t.context.m.verifyConditions(
7575
{},
76-
{options: {prepare: ['@semantic-release/git', {path: '@semantic-release/changelog', changelogFile}]}}
76+
{cwd, options: {prepare: ['@semantic-release/git', {path: '@semantic-release/changelog', changelogFile}]}}
7777
)
7878
)),
7979
];

test/prepare.test.js

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,92 @@
1+
import path from 'path';
12
import test from 'ava';
23
import {outputFile, readFile} from 'fs-extra';
34
import {stub} from 'sinon';
45
import tempy from 'tempy';
56
import prepare from '../lib/prepare';
67

7-
// Save the current working diretory
8-
const cwd = process.cwd();
9-
108
test.beforeEach(t => {
11-
// Change current working directory to a temp directory
12-
process.chdir(tempy.directory());
139
// Stub the logger
1410
t.context.log = stub();
1511
t.context.logger = {log: t.context.log};
1612
});
1713

18-
test.afterEach.always(() => {
19-
// Restore the current working directory
20-
process.chdir(cwd);
21-
});
22-
23-
test.serial('Create new CHANGELOG.md', async t => {
14+
test('Create new CHANGELOG.md', async t => {
15+
const cwd = tempy.directory();
2416
const notes = 'Test release note';
17+
const changelogFile = 'CHANGELOG.md';
18+
const changelogPath = path.resolve(cwd, changelogFile);
2519

26-
await prepare({}, notes, t.context.logger);
20+
await prepare({}, {cwd, nextRelease: {notes}, logger: t.context.logger});
2721

2822
// Verify the content of the CHANGELOG.md
29-
t.is((await readFile('CHANGELOG.md')).toString(), `${notes}\n`);
23+
t.is((await readFile(changelogPath)).toString(), `${notes}\n`);
3024

31-
t.deepEqual(t.context.log.args[0], ['Create %s', 'CHANGELOG.md']);
25+
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
3226
});
3327

34-
test.serial('Create new changelog with custom path', async t => {
28+
test('Create new changelog with custom path', async t => {
29+
const cwd = tempy.directory();
3530
const notes = 'Test release note';
3631
const changelogFile = 'docs/changelog.txt';
32+
const changelogPath = path.resolve(cwd, 'docs/changelog.txt');
3733

38-
await prepare({changelogFile}, notes, t.context.logger);
34+
await prepare({changelogFile}, {cwd, nextRelease: {notes}, logger: t.context.logger});
3935

4036
// Verify the content of the CHANGELOG.md
41-
t.is((await readFile(changelogFile)).toString(), `${notes}\n`);
37+
t.is((await readFile(changelogPath)).toString(), `${notes}\n`);
4238

43-
t.deepEqual(t.context.log.args[0], ['Create %s', changelogFile]);
39+
t.deepEqual(t.context.log.args[0], ['Create %s', changelogPath]);
4440
});
4541

46-
test.serial('Prepend the CHANGELOG.md if there is an existing one', async t => {
42+
test('Prepend the CHANGELOG.md if there is an existing one', async t => {
43+
const cwd = tempy.directory();
4744
const notes = 'Test release note';
48-
await outputFile('CHANGELOG.md', 'Initial CHANGELOG');
45+
const changelogFile = 'CHANGELOG.md';
46+
const changelogPath = path.resolve(cwd, changelogFile);
47+
await outputFile(changelogPath, 'Initial CHANGELOG');
4948

50-
await prepare({}, notes, t.context.logger);
49+
await prepare({}, {cwd, nextRelease: {notes}, logger: t.context.logger});
5150

5251
// Verify the content of the CHANGELOG.md
53-
t.is((await readFile('CHANGELOG.md')).toString(), `${notes}\n\nInitial CHANGELOG\n`);
54-
t.deepEqual(t.context.log.args[0], ['Update %s', 'CHANGELOG.md']);
52+
t.is((await readFile(changelogPath)).toString(), `${notes}\n\nInitial CHANGELOG\n`);
53+
t.deepEqual(t.context.log.args[0], ['Update %s', changelogPath]);
5554
});
5655

57-
test.serial('Prepend title in the CHANGELOG.md if there is none', async t => {
56+
test('Prepend title in the CHANGELOG.md if there is none', async t => {
57+
const cwd = tempy.directory();
5858
const notes = 'Test release note';
59-
await outputFile('CHANGELOG.md', 'Initial CHANGELOG');
60-
6159
const changelogTitle = '# My Changelog Title';
62-
await prepare({changelogTitle}, notes, t.context.logger);
60+
const changelogFile = 'CHANGELOG.md';
61+
const changelogPath = path.resolve(cwd, changelogFile);
62+
await outputFile(changelogPath, 'Initial CHANGELOG');
63+
64+
await prepare({changelogTitle}, {cwd, nextRelease: {notes}, logger: t.context.logger});
6365

64-
t.is((await readFile('CHANGELOG.md')).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`);
66+
t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`);
6567
});
6668

67-
test.serial('Keep the title at the top of the CHANGELOG.md', async t => {
69+
test('Keep the title at the top of the CHANGELOG.md', async t => {
70+
const cwd = tempy.directory();
6871
const notes = 'Test release note';
6972
const changelogTitle = '# My Changelog Title';
70-
await outputFile('CHANGELOG.md', `${changelogTitle}\n\nInitial CHANGELOG`);
73+
const changelogFile = 'CHANGELOG.md';
74+
const changelogPath = path.resolve(cwd, changelogFile);
75+
await outputFile(changelogPath, `${changelogTitle}\n\nInitial CHANGELOG`);
7176

72-
await prepare({changelogTitle}, notes, t.context.logger);
77+
await prepare({changelogTitle}, {cwd, nextRelease: {notes}, logger: t.context.logger});
7378

74-
t.is((await readFile('CHANGELOG.md')).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`);
79+
t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n\nInitial CHANGELOG\n`);
7580
});
7681

7782
test.serial('Create new changelog with title if specified', async t => {
83+
const cwd = tempy.directory();
7884
const notes = 'Test release note';
7985
const changelogTitle = '# My Changelog Title';
8086
const changelogFile = 'HISTORY.md';
87+
const changelogPath = path.resolve(cwd, changelogFile);
8188

82-
await prepare({changelogTitle, changelogFile}, notes, t.context.logger);
89+
await prepare({changelogTitle, changelogFile}, {cwd, nextRelease: {notes}, logger: t.context.logger});
8390

84-
t.is((await readFile(changelogFile)).toString(), `${changelogTitle}\n\n${notes}\n`);
91+
t.is((await readFile(changelogPath)).toString(), `${changelogTitle}\n\n${notes}\n`);
8592
});

test/verify.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import test from 'ava';
22
import verify from '../lib/verify';
33

4-
test.serial('Verify String "changelogFile" and "chagngelogTitle"', t => {
4+
test('Verify String "changelogFile" and "chagngelogTitle"', t => {
55
const changelogFile = 'docs/changelog.txt';
66
const changelogTitle = '# My title here';
77
t.notThrows(() => verify({changelogFile, changelogTitle}));
88
});
99

10-
test.serial('Verify undefined "changelogFile" and "chagngelogTitle"', t => {
10+
test('Verify undefined "changelogFile" and "chagngelogTitle"', t => {
1111
t.notThrows(() => verify({}));
1212
});
1313

0 commit comments

Comments
 (0)