Skip to content

Commit e8fe729

Browse files
committed
feat: update package.json with npm version
1 parent ee787b4 commit e8fe729

File tree

6 files changed

+244
-178
lines changed

6 files changed

+244
-178
lines changed

lib/prepare.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
const path = require('path');
22
const {move} = require('fs-extra');
33
const execa = require('execa');
4-
const updatePackageVersion = require('./update-package-version');
54

65
module.exports = async ({tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRelease: {version}, logger}) => {
76
const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd;
8-
await updatePackageVersion(version, basePath, logger);
7+
8+
logger.log('Write version %s to package.json in %s', version, basePath);
9+
10+
const versionResult = execa('npm', ['version', version, '--no-git-tag-version'], {cwd: basePath, env});
11+
versionResult.stdout.pipe(
12+
stdout,
13+
{end: false}
14+
);
15+
versionResult.stderr.pipe(
16+
stderr,
17+
{end: false}
18+
);
19+
20+
await versionResult;
921

1022
if (tarballDir) {
1123
logger.log('Creating npm package version %s', version);
12-
const result = execa('npm', ['pack', basePath], {cwd, env});
13-
result.stdout.pipe(
24+
const packResult = execa('npm', ['pack', basePath], {cwd, env});
25+
packResult.stdout.pipe(
1426
stdout,
1527
{end: false}
1628
);
17-
result.stderr.pipe(
29+
packResult.stderr.pipe(
1830
stderr,
1931
{end: false}
2032
);
2133

22-
const tarball = (await result).stdout.split('\n').pop();
34+
const tarball = (await packResult).stdout.split('\n').pop();
2335
await move(path.resolve(cwd, tarball), path.resolve(cwd, tarballDir.trim(), tarball));
2436
}
2537
};

lib/update-package-version.js

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

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@
1818
"dependencies": {
1919
"@semantic-release/error": "^2.2.0",
2020
"aggregate-error": "^1.0.0",
21-
"detect-indent": "^5.0.0",
22-
"detect-newline": "^2.1.0",
2321
"execa": "^1.0.0",
2422
"fs-extra": "^7.0.0",
2523
"lodash": "^4.17.4",
2624
"nerf-dart": "^1.0.0",
2725
"normalize-url": "^4.0.0",
2826
"npm": "^6.3.0",
29-
"parse-json": "^4.0.0",
3027
"rc": "^1.2.8",
3128
"read-pkg": "^4.0.0",
3229
"registry-auth-token": "^3.3.1"

test/integration.test.js

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -494,29 +494,6 @@ test('Prepare the package from a sub-directory', async t => {
494494
t.false(await pathExists(path.resolve(cwd, `${pkg.name}-1.0.0.tgz`)));
495495
});
496496

497-
test('Create the package in prepare step', async t => {
498-
const cwd = tempy.directory();
499-
const env = npmRegistry.authEnv;
500-
const pkg = {name: 'prepare-pkg', version: '0.0.0', publishConfig: {registry: npmRegistry.url}};
501-
await outputJson(path.resolve(cwd, 'package.json'), pkg);
502-
503-
await t.context.m.prepare(
504-
{npmPublish: false, tarballDir: 'tarball'},
505-
{
506-
cwd,
507-
env,
508-
options: {},
509-
stdout: t.context.stdout,
510-
stderr: t.context.stderr,
511-
logger: t.context.logger,
512-
nextRelease: {version: '1.0.0'},
513-
}
514-
);
515-
516-
t.is((await readJson(path.resolve(cwd, 'package.json'))).version, '1.0.0');
517-
t.true(await pathExists(path.resolve(cwd, `tarball/${pkg.name}-1.0.0.tgz`)));
518-
});
519-
520497
test('Throw SemanticReleaseError Array if config option are not valid in prepare', async t => {
521498
const cwd = tempy.directory();
522499
const pkg = {publishConfig: {registry: npmRegistry.url}};

test/prepare.test.js

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
import path from 'path';
2+
import test from 'ava';
3+
import {outputJson, readJson, outputFile, readFile, pathExists} 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';
9+
10+
test.beforeEach(t => {
11+
t.context.log = stub();
12+
t.context.logger = {log: t.context.log};
13+
t.context.stdout = new WritableStreamBuffer();
14+
t.context.stderr = new WritableStreamBuffer();
15+
});
16+
17+
test('Updade package.json', async t => {
18+
const cwd = tempy.directory();
19+
const packagePath = path.resolve(cwd, 'package.json');
20+
await outputJson(packagePath, {version: '0.0.0-dev'});
21+
22+
await prepare(
23+
{},
24+
{
25+
cwd,
26+
env: {},
27+
stdout: t.context.stdout,
28+
stderr: t.context.stderr,
29+
nextRelease: {version: '1.0.0'},
30+
logger: t.context.logger,
31+
}
32+
);
33+
34+
// Verify package.json has been updated
35+
t.is((await readJson(packagePath)).version, '1.0.0');
36+
37+
// Verify the logger has been called with the version updated
38+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
39+
});
40+
41+
test('Updade package.json and npm-shrinkwrap.json', async t => {
42+
const cwd = tempy.directory();
43+
const packagePath = path.resolve(cwd, 'package.json');
44+
const shrinkwrapPath = path.resolve(cwd, 'npm-shrinkwrap.json');
45+
await outputJson(packagePath, {version: '0.0.0-dev'});
46+
// Create a npm-shrinkwrap.json file
47+
await execa('npm', ['shrinkwrap'], {cwd});
48+
49+
await prepare(
50+
{},
51+
{
52+
cwd,
53+
env: {},
54+
stdout: t.context.stdout,
55+
stderr: t.context.stderr,
56+
nextRelease: {version: '1.0.0'},
57+
logger: t.context.logger,
58+
}
59+
);
60+
61+
// Verify package.json and npm-shrinkwrap.json have been updated
62+
t.is((await readJson(packagePath)).version, '1.0.0');
63+
t.is((await readJson(shrinkwrapPath)).version, '1.0.0');
64+
// Verify the logger has been called with the version updated
65+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
66+
});
67+
68+
test('Updade package.json and package-lock.json', async t => {
69+
const cwd = tempy.directory();
70+
const packagePath = path.resolve(cwd, 'package.json');
71+
const packageLockPath = path.resolve(cwd, 'package-lock.json');
72+
await outputJson(packagePath, {version: '0.0.0-dev'});
73+
// Create a package-lock.json file
74+
await execa('npm', ['install'], {cwd});
75+
76+
await prepare(
77+
{},
78+
{
79+
cwd,
80+
env: {},
81+
stdout: t.context.stdout,
82+
stderr: t.context.stderr,
83+
nextRelease: {version: '1.0.0'},
84+
logger: t.context.logger,
85+
}
86+
);
87+
88+
// Verify package.json and package-lock.json have been updated
89+
t.is((await readJson(packagePath)).version, '1.0.0');
90+
t.is((await readJson(packageLockPath)).version, '1.0.0');
91+
// Verify the logger has been called with the version updated
92+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
93+
});
94+
95+
test('Updade package.json and npm-shrinkwrap.json in a sub-directory', async t => {
96+
const cwd = tempy.directory();
97+
const pkgRoot = 'dist';
98+
const packagePath = path.resolve(cwd, pkgRoot, 'package.json');
99+
const shrinkwrapPath = path.resolve(cwd, pkgRoot, 'npm-shrinkwrap.json');
100+
await outputJson(packagePath, {version: '0.0.0-dev'});
101+
// Create a npm-shrinkwrap.json file
102+
await execa('npm', ['shrinkwrap'], {cwd: path.resolve(cwd, pkgRoot)});
103+
104+
await prepare(
105+
{pkgRoot},
106+
{
107+
cwd,
108+
env: {},
109+
stdout: t.context.stdout,
110+
stderr: t.context.stderr,
111+
nextRelease: {version: '1.0.0'},
112+
logger: t.context.logger,
113+
}
114+
);
115+
116+
// Verify package.json and npm-shrinkwrap.json have been updated
117+
t.is((await readJson(packagePath)).version, '1.0.0');
118+
t.is((await readJson(shrinkwrapPath)).version, '1.0.0');
119+
// Verify the logger has been called with the version updated
120+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]);
121+
});
122+
123+
test('Updade package.json and package-lock.json in a sub-directory', async t => {
124+
const cwd = tempy.directory();
125+
const pkgRoot = 'dist';
126+
const packagePath = path.resolve(cwd, pkgRoot, 'package.json');
127+
const packageLockPath = path.resolve(cwd, pkgRoot, 'package-lock.json');
128+
await outputJson(packagePath, {version: '0.0.0-dev'});
129+
// Create a package-lock.json file
130+
await execa('npm', ['install'], {cwd: path.resolve(cwd, pkgRoot)});
131+
132+
await prepare(
133+
{pkgRoot},
134+
{
135+
cwd,
136+
env: {},
137+
stdout: t.context.stdout,
138+
stderr: t.context.stderr,
139+
nextRelease: {version: '1.0.0'},
140+
logger: t.context.logger,
141+
}
142+
);
143+
144+
// Verify package.json and package-lock.json have been updated
145+
t.is((await readJson(packagePath)).version, '1.0.0');
146+
t.is((await readJson(packageLockPath)).version, '1.0.0');
147+
// Verify the logger has been called with the version updated
148+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]);
149+
});
150+
151+
test('Preserve indentation and newline', async t => {
152+
const cwd = tempy.directory();
153+
const packagePath = path.resolve(cwd, 'package.json');
154+
await outputFile(packagePath, `{\r\n "name": "package-name",\r\n "version": "0.0.0-dev"\r\n}\r\n`);
155+
156+
await prepare(
157+
{},
158+
{
159+
cwd,
160+
env: {},
161+
stdout: t.context.stdout,
162+
stderr: t.context.stderr,
163+
nextRelease: {version: '1.0.0'},
164+
logger: t.context.logger,
165+
}
166+
);
167+
168+
// Verify package.json has been updated
169+
t.is(
170+
await readFile(packagePath, 'utf-8'),
171+
`{\r\n "name": "package-name",\r\n "version": "1.0.0"\r\n}\r\n`
172+
);
173+
174+
// Verify the logger has been called with the version updated
175+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
176+
});
177+
178+
test('Use default indentation and newline if it cannot be detected', async t => {
179+
const cwd = tempy.directory();
180+
const packagePath = path.resolve(cwd, 'package.json');
181+
await outputFile(packagePath, `{"name": "package-name","version": "0.0.0-dev"}`);
182+
183+
await prepare(
184+
{},
185+
{
186+
cwd,
187+
env: {},
188+
stdout: t.context.stdout,
189+
stderr: t.context.stderr,
190+
nextRelease: {version: '1.0.0'},
191+
logger: t.context.logger,
192+
}
193+
);
194+
195+
// Verify package.json has been updated
196+
t.is(await readFile(packagePath, 'utf-8'), `{\n "name": "package-name",\n "version": "1.0.0"\n}\n`);
197+
198+
// Verify the logger has been called with the version updated
199+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
200+
});
201+
202+
test('Create the package in the "tarballDir" directory', async t => {
203+
const cwd = tempy.directory();
204+
const packagePath = path.resolve(cwd, 'package.json');
205+
const pkg = {name: 'my-pkg', version: '0.0.0-dev'};
206+
await outputJson(packagePath, pkg);
207+
208+
await prepare(
209+
{tarballDir: 'tarball'},
210+
{
211+
cwd,
212+
env: {},
213+
stdout: t.context.stdout,
214+
stderr: t.context.stderr,
215+
nextRelease: {version: '1.0.0'},
216+
logger: t.context.logger,
217+
}
218+
);
219+
220+
// Verify package.json has been updated
221+
t.is((await readJson(packagePath)).version, '1.0.0');
222+
223+
t.true(await pathExists(path.resolve(cwd, `tarball/${pkg.name}-1.0.0.tgz`)));
224+
// Verify the logger has been called with the version updated
225+
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
226+
});

0 commit comments

Comments
 (0)