Skip to content

Commit

Permalink
Refactor to exclude only command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyEckstein committed Dec 9, 2021
1 parent 5814b9a commit 7e41f3b
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 186 deletions.
20 changes: 8 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@

const chalk = require('chalk');
const semver = require('semver');
const path = require('path');
const semverPrerelease = require('semver/functions/prerelease');
const validatePackageName = require('validate-npm-package-name');

const getInstalledVersionHelper = require('./utils/get-installed-versions');
const getDependenciesHelper = require('./utils/get-dependencies');
const getLatestVersionsHelper = require('./utils/get-latest-versions');
const getLatestTagHelper = require('./utils/get-latest-tag');
const installPackagesHelper = require('./utils/install-packages');
const installDevPackagesHelper = require('./utils/install-dev-packages');
const { execp, requireModule } = require('./utils');

const { blue, bold, green, red } = chalk;

Expand Down Expand Up @@ -42,7 +38,7 @@ function isValidNpmPackageName(name) {
async function getLatestVersions(name) {
isValidNpmPackageName(name);
try {
const { stdout } = await getLatestVersionsHelper(name);
const { stdout } = await execp(`npm view ${name} versions --json`);
const versions = JSON.parse(stdout);
return Array.isArray(versions) ? versions : [versions];
} catch (err) {
Expand All @@ -64,7 +60,7 @@ async function getLatestVersions(name) {
async function getLatestTag(name) {
isValidNpmPackageName(name);
try {
const { stdout } = await getLatestTagHelper(name);
const { stdout } = await execp(`npm view ${name} dist-tags --json`);
const { latest } = JSON.parse(stdout);
return latest;
} catch (err) {
Expand Down Expand Up @@ -125,7 +121,7 @@ async function getLatestVersion(name, wanted) {
*/
function getInstalledVersion(currentDir, name, logger) {
try {
const { version } = getInstalledVersionHelper(currentDir, name);
const { version } = requireModule(path.join(currentDir, 'node_modules', name, 'package.json'));
return version;
} catch (err) {
logger.info(`Error getting a list of installed modules from package.json - ${err}`);
Expand Down Expand Up @@ -203,7 +199,7 @@ function removeLockedDependencies(deps) {
* @param {Logger} [options.logger] - A logger instance, with a similar API as the console object.
*/
async function verifyDeps({ autoUpgrade = false, dir = '', logger = console } = {}) {
const { dependencies = {}, devDependencies = {} } = getDependenciesHelper({ dir });
const { dependencies = {}, devDependencies = {} } = requireModule(path.join(dir, 'package.json'));
logger.info(blue('Verifying dependencies…\n'));

const pkgs = await Promise.all([
Expand All @@ -225,8 +221,8 @@ async function verifyDeps({ autoUpgrade = false, dir = '', logger = console } =
if (autoUpgrade) {
logger.info('UPGRADING…');
logger.info(upgradePackages);
const prodResult = await installPackagesHelper(getPkgIds(prodPkgs));
const devResult = await installDevPackagesHelper(getPkgIds(devPkgs));
const prodResult = await execp(`npm i ${getPkgIds(prodPkgs)}`);
const devResult = await execp(`npm i -D ${getPkgIds(devPkgs)}`);
logger.info(`${green(`${bold('Upgraded dependencies:\n')}${prodResult.stdout}`)}`);
logger.info(`${green(`${bold('Upgraded development dependencies:\n')}${devResult.stdout}`)}`);
} else {
Expand Down
47 changes: 19 additions & 28 deletions lib/index.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
'use strict';

jest.mock('./utils/get-installed-versions.js');
jest.mock('./utils/get-dependencies.js');
jest.mock('./utils/get-latest-versions.js');
jest.mock('./utils/get-latest-tag.js');
jest.mock('./utils/install-packages.js');
jest.mock('./utils/install-dev-packages.js');

const getInstalledVersion = require('./utils/get-installed-versions');
const getDependencies = require('./utils/get-dependencies');
const getLatestVersions = require('./utils/get-latest-versions');
const getLatestTag = require('./utils/get-latest-tag');
const installPackages = require('./utils/install-packages');
const installDevPackages = require('./utils/install-dev-packages');
jest.mock('./utils');

const utils = require('./utils');
const verifyDeps = require('.');

jest.mock('chalk', () => ({
Expand All @@ -33,27 +23,25 @@ describe('lib/index', () => {
});

function mockDependencies(obj) {
getDependencies.mockImplementationOnce(() => obj);
utils.requireModule.mockImplementationOnce(() => obj);
}
function mockInstalledVersion(version) {
getInstalledVersion.mockImplementationOnce(() => ({ version }));
utils.requireModule.mockImplementationOnce(() => ({ version }));
}
function mockLatestVersions(arr) {
getLatestVersions.mockImplementationOnce(() =>
Promise.resolve({ stdout: JSON.stringify(arr) })
);
utils.execp.mockImplementationOnce(() => Promise.resolve({ stdout: JSON.stringify(arr) }));
}
function mockLatestTag(latest) {
getLatestTag.mockImplementationOnce(() =>
utils.execp.mockImplementationOnce(() =>
Promise.resolve({ stdout: JSON.stringify({ latest }) })
);
}

function mockInstallPackages(stdout) {
installPackages.mockImplementationOnce(() => Promise.resolve({ stdout }));
utils.execp.mockImplementationOnce(() => Promise.resolve({ stdout }));
}
function mockInstallDevPackages(stdout) {
installDevPackages.mockImplementationOnce(() => Promise.resolve({ stdout }));
utils.execp.mockImplementationOnce(() => Promise.resolve({ stdout }));
}

test('should upgrade upto patch versions when versions prefixed with ~', async () => {
Expand Down Expand Up @@ -108,7 +96,7 @@ describe('lib/index', () => {

test('when node module is not installed', async () => {
mockDependencies({ dependencies: {}, devDependencies: { mi: '^1.0.0' } });
getInstalledVersion.mockImplementationOnce(() => {
utils.requireModule.mockImplementationOnce(() => {
throw new Error('foobar');
});
mockLatestVersions(['1.0.0', '1.0.1', '2.0.0']);
Expand Down Expand Up @@ -201,7 +189,7 @@ describe('lib/index', () => {
} catch (err) {
syntaxError = err;
}
getLatestVersions.mockImplementationOnce(() => {
utils.execp.mockImplementationOnce(() => {
throw syntaxError;
});
await expect(verifyDeps({ dir, logger })).rejects.toThrow(
Expand All @@ -220,7 +208,7 @@ describe('lib/index', () => {
} catch (err) {
syntaxError = err;
}
getLatestTag.mockImplementationOnce(() => {
utils.execp.mockImplementationOnce(() => {
throw syntaxError;
});
await expect(verifyDeps({ dir, logger })).rejects.toThrow(
Expand All @@ -231,7 +219,7 @@ describe('lib/index', () => {
test('throw error when getting latest versions fails', async () => {
mockDependencies({ dependencies: { tans: '^1.0.0' } });
mockInstalledVersion('1.0.0');
getLatestVersions.mockImplementation(() => {
utils.execp.mockImplementation(() => {
throw new Error('foo');
});
await expect(verifyDeps({ dir, logger })).rejects.toThrow(
Expand All @@ -243,7 +231,7 @@ describe('lib/index', () => {
mockDependencies({ dependencies: { utex: '^1.0.0' } });
mockInstalledVersion('1.0.0');
mockLatestVersions(['1.0.0', '1.0.1']);
getLatestTag.mockImplementationOnce(() => {
utils.execp.mockImplementationOnce(() => {
throw new Error('boo');
});
await expect(verifyDeps({ dir, logger })).rejects.toThrow(
Expand Down Expand Up @@ -316,7 +304,10 @@ describe('lib/index', () => {
});

test('should update to version aliased as latest when aliased latest is less that most recent published version', async () => {
mockDependencies({ dependencies: { foo1: '^1.2.3' }, devDependencies: { fooDev1: '^1.2.3' } });
mockDependencies({
dependencies: { foo1: '^1.2.3' },
devDependencies: { fooDev1: '^1.2.3' }
});
mockInstalledVersion('1.2.3');
mockInstalledVersion('1.2.3');
mockLatestVersions(['1.2.4', '1.2.5']);
Expand Down Expand Up @@ -436,7 +427,7 @@ describe('lib/index', () => {
test('throw error when npm module name is invalid', async () => {
mockDependencies({ dependencies: { 'bad name Dependency': '^1.2.3' }, devDependencies: {} });
mockInstalledVersion('1.2.3');
getLatestVersions.mockImplementationOnce(() => {
utils.execp.mockImplementationOnce(() => {
throw new Error('name can only contain URL-friendly characters');
});
await expect(verifyDeps({ autoUpgrade: true, dir, logger })).rejects.toThrow(
Expand Down
16 changes: 16 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const { exec } = require('child_process');
const { promisify } = require('util');

const execPromise = promisify(exec);

const execp = (cmd = '') => {
return execPromise(cmd);
};

const requireModule = (path = '') => {
return require(path);
};

module.exports = { execp, requireModule };
23 changes: 0 additions & 23 deletions lib/utils/get-dependencies.js

This file was deleted.

22 changes: 0 additions & 22 deletions lib/utils/get-installed-versions.js

This file was deleted.

24 changes: 0 additions & 24 deletions lib/utils/get-latest-tag.js

This file was deleted.

25 changes: 0 additions & 25 deletions lib/utils/get-latest-versions.js

This file was deleted.

25 changes: 0 additions & 25 deletions lib/utils/install-dev-packages.js

This file was deleted.

25 changes: 0 additions & 25 deletions lib/utils/install-packages.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"files": [
"bin/lifion-verify-deps.js",
"lib/index.js",
"lib/utils"
"lib/utils.js"
],
"keywords": [
"check",
Expand Down Expand Up @@ -83,7 +83,7 @@
"test": {
"collectCoverageFrom": [
"**/*.js",
"!**/utils/*.js"
"!**/utils.js"
]
}
}
Expand Down

0 comments on commit 7e41f3b

Please sign in to comment.