Skip to content

Commit

Permalink
feat: update package.json with npm version
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Nov 9, 2018
1 parent ee787b4 commit e8fe729
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 178 deletions.
24 changes: 18 additions & 6 deletions lib/prepare.js
@@ -1,25 +1,37 @@
const path = require('path');
const {move} = require('fs-extra');
const execa = require('execa');
const updatePackageVersion = require('./update-package-version');

module.exports = async ({tarballDir, pkgRoot}, {cwd, env, stdout, stderr, nextRelease: {version}, logger}) => {
const basePath = pkgRoot ? path.resolve(cwd, pkgRoot) : cwd;
await updatePackageVersion(version, basePath, logger);

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

const versionResult = execa('npm', ['version', version, '--no-git-tag-version'], {cwd: basePath, env});
versionResult.stdout.pipe(
stdout,
{end: false}
);
versionResult.stderr.pipe(
stderr,
{end: false}
);

await versionResult;

if (tarballDir) {
logger.log('Creating npm package version %s', version);
const result = execa('npm', ['pack', basePath], {cwd, env});
result.stdout.pipe(
const packResult = execa('npm', ['pack', basePath], {cwd, env});
packResult.stdout.pipe(
stdout,
{end: false}
);
result.stderr.pipe(
packResult.stderr.pipe(
stderr,
{end: false}
);

const tarball = (await result).stdout.split('\n').pop();
const tarball = (await packResult).stdout.split('\n').pop();
await move(path.resolve(cwd, tarball), path.resolve(cwd, tarballDir.trim(), tarball));
}
};
34 changes: 0 additions & 34 deletions lib/update-package-version.js

This file was deleted.

3 changes: 0 additions & 3 deletions package.json
Expand Up @@ -18,15 +18,12 @@
"dependencies": {
"@semantic-release/error": "^2.2.0",
"aggregate-error": "^1.0.0",
"detect-indent": "^5.0.0",
"detect-newline": "^2.1.0",
"execa": "^1.0.0",
"fs-extra": "^7.0.0",
"lodash": "^4.17.4",
"nerf-dart": "^1.0.0",
"normalize-url": "^4.0.0",
"npm": "^6.3.0",
"parse-json": "^4.0.0",
"rc": "^1.2.8",
"read-pkg": "^4.0.0",
"registry-auth-token": "^3.3.1"
Expand Down
23 changes: 0 additions & 23 deletions test/integration.test.js
Expand Up @@ -494,29 +494,6 @@ test('Prepare the package from a sub-directory', async t => {
t.false(await pathExists(path.resolve(cwd, `${pkg.name}-1.0.0.tgz`)));
});

test('Create the package in prepare step', async t => {
const cwd = tempy.directory();
const env = npmRegistry.authEnv;
const pkg = {name: 'prepare-pkg', version: '0.0.0', publishConfig: {registry: npmRegistry.url}};
await outputJson(path.resolve(cwd, 'package.json'), pkg);

await t.context.m.prepare(
{npmPublish: false, tarballDir: 'tarball'},
{
cwd,
env,
options: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
logger: t.context.logger,
nextRelease: {version: '1.0.0'},
}
);

t.is((await readJson(path.resolve(cwd, 'package.json'))).version, '1.0.0');
t.true(await pathExists(path.resolve(cwd, `tarball/${pkg.name}-1.0.0.tgz`)));
});

test('Throw SemanticReleaseError Array if config option are not valid in prepare', async t => {
const cwd = tempy.directory();
const pkg = {publishConfig: {registry: npmRegistry.url}};
Expand Down
226 changes: 226 additions & 0 deletions test/prepare.test.js
@@ -0,0 +1,226 @@
import path from 'path';
import test from 'ava';
import {outputJson, readJson, outputFile, readFile, pathExists} from 'fs-extra';
import tempy from 'tempy';
import execa from 'execa';
import {stub} from 'sinon';
import {WritableStreamBuffer} from 'stream-buffers';
import prepare from '../lib/prepare';

test.beforeEach(t => {
t.context.log = stub();
t.context.logger = {log: t.context.log};
t.context.stdout = new WritableStreamBuffer();
t.context.stderr = new WritableStreamBuffer();
});

test('Updade package.json', async t => {
const cwd = tempy.directory();
const packagePath = path.resolve(cwd, 'package.json');
await outputJson(packagePath, {version: '0.0.0-dev'});

await prepare(
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

// Verify package.json has been updated
t.is((await readJson(packagePath)).version, '1.0.0');

// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
});

test('Updade package.json and npm-shrinkwrap.json', async t => {
const cwd = tempy.directory();
const packagePath = path.resolve(cwd, 'package.json');
const shrinkwrapPath = path.resolve(cwd, 'npm-shrinkwrap.json');
await outputJson(packagePath, {version: '0.0.0-dev'});
// Create a npm-shrinkwrap.json file
await execa('npm', ['shrinkwrap'], {cwd});

await prepare(
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

// Verify package.json and npm-shrinkwrap.json have been updated
t.is((await readJson(packagePath)).version, '1.0.0');
t.is((await readJson(shrinkwrapPath)).version, '1.0.0');
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
});

test('Updade package.json and package-lock.json', async t => {
const cwd = tempy.directory();
const packagePath = path.resolve(cwd, 'package.json');
const packageLockPath = path.resolve(cwd, 'package-lock.json');
await outputJson(packagePath, {version: '0.0.0-dev'});
// Create a package-lock.json file
await execa('npm', ['install'], {cwd});

await prepare(
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

// Verify package.json and package-lock.json have been updated
t.is((await readJson(packagePath)).version, '1.0.0');
t.is((await readJson(packageLockPath)).version, '1.0.0');
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
});

test('Updade package.json and npm-shrinkwrap.json in a sub-directory', async t => {
const cwd = tempy.directory();
const pkgRoot = 'dist';
const packagePath = path.resolve(cwd, pkgRoot, 'package.json');
const shrinkwrapPath = path.resolve(cwd, pkgRoot, 'npm-shrinkwrap.json');
await outputJson(packagePath, {version: '0.0.0-dev'});
// Create a npm-shrinkwrap.json file
await execa('npm', ['shrinkwrap'], {cwd: path.resolve(cwd, pkgRoot)});

await prepare(
{pkgRoot},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

// Verify package.json and npm-shrinkwrap.json have been updated
t.is((await readJson(packagePath)).version, '1.0.0');
t.is((await readJson(shrinkwrapPath)).version, '1.0.0');
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]);
});

test('Updade package.json and package-lock.json in a sub-directory', async t => {
const cwd = tempy.directory();
const pkgRoot = 'dist';
const packagePath = path.resolve(cwd, pkgRoot, 'package.json');
const packageLockPath = path.resolve(cwd, pkgRoot, 'package-lock.json');
await outputJson(packagePath, {version: '0.0.0-dev'});
// Create a package-lock.json file
await execa('npm', ['install'], {cwd: path.resolve(cwd, pkgRoot)});

await prepare(
{pkgRoot},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

// Verify package.json and package-lock.json have been updated
t.is((await readJson(packagePath)).version, '1.0.0');
t.is((await readJson(packageLockPath)).version, '1.0.0');
// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]);
});

test('Preserve indentation and newline', async t => {
const cwd = tempy.directory();
const packagePath = path.resolve(cwd, 'package.json');
await outputFile(packagePath, `{\r\n "name": "package-name",\r\n "version": "0.0.0-dev"\r\n}\r\n`);

await prepare(
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

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

// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
});

test('Use default indentation and newline if it cannot be detected', async t => {
const cwd = tempy.directory();
const packagePath = path.resolve(cwd, 'package.json');
await outputFile(packagePath, `{"name": "package-name","version": "0.0.0-dev"}`);

await prepare(
{},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

// Verify package.json has been updated
t.is(await readFile(packagePath, 'utf-8'), `{\n "name": "package-name",\n "version": "1.0.0"\n}\n`);

// Verify the logger has been called with the version updated
t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]);
});

test('Create the package in the "tarballDir" directory', async t => {
const cwd = tempy.directory();
const packagePath = path.resolve(cwd, 'package.json');
const pkg = {name: 'my-pkg', version: '0.0.0-dev'};
await outputJson(packagePath, pkg);

await prepare(
{tarballDir: 'tarball'},
{
cwd,
env: {},
stdout: t.context.stdout,
stderr: t.context.stderr,
nextRelease: {version: '1.0.0'},
logger: t.context.logger,
}
);

// Verify package.json has been updated
t.is((await readJson(packagePath)).version, '1.0.0');

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

0 comments on commit e8fe729

Please sign in to comment.