Skip to content

Commit

Permalink
Refactor as per new linter rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jennyEckstein committed Feb 25, 2021
1 parent c699750 commit 57b93d8
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
}
}
],
"parserOptions": {
"ecmaVersion": 2020
},
"rules": {
"global-require": "off",
"import/no-dynamic-require": "off",
Expand Down
4 changes: 3 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"endOfLine": "auto",
"printWidth": 100,
"singleQuote": true
"singleQuote": true,
"trailingComma": "none"
}
1 change: 1 addition & 0 deletions bin/lifion-verify-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'use strict';

const argv = require('minimist')(process.argv.slice(2));

const verifyDeps = require('../lib');
const { name } = require('../package.json');

Expand Down
16 changes: 9 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

'use strict';

const { blue, bold, green, red } = require('chalk');
const chalk = require('chalk');
const path = require('path');
const semver = require('semver');
const { exec } = require('child_process');
const { promisify } = require('util');

const { blue, bold, green, red } = chalk;
const execAsync = promisify(exec);

async function getLatestVersions(name) {
Expand All @@ -28,10 +29,11 @@ async function getLatestVersion(name, wanted) {
return applicableVersions[0];
}

function getInstalledVersion(currentDir, name) {
function getInstalledVersion(currentDir, name, logger) {
try {
return require(path.join(currentDir, 'node_modules', name, 'package.json')).version;
} catch (err) {
logger.info(`Error getting a list of installed modules from package.json - ${err}`);
return null;
}
}
Expand All @@ -40,7 +42,7 @@ function pushPkgs({ dir, logger, deps = {}, type, pkgs }) {
return Object.keys(deps).map(async name => {
let wanted = deps[name];
if (!wanted.startsWith('^')) wanted = `^${wanted}`;
const installed = getInstalledVersion(dir, name);
const installed = getInstalledVersion(dir, name, logger);
const latest = await getLatestVersion(name, wanted);
const wantedFixed = wanted.slice(1);
const shouldBeInstalled =
Expand All @@ -66,10 +68,10 @@ function getPkgIds(filteredPkgs) {
* Verifies the dependencies listed in the package.json of the given directory.
*
* @alias module:lifion-verify-deps
* @param {object} [options] - Optional parameters.
* @param {Object} [options] - Optional parameters.
* @param {boolean} [options.autoUpgrade=false] - Automatically upgrade all suggested dependencies.
* @param {string} [options.dir] - The path where to look for the package.json file.
* @param {object} [options.logger] - A logger instance, with a similar API as the console object.
* @param {Object} [options.logger] - A logger instance, with a similar API as the console object.
*/
async function verifyDeps({ autoUpgrade = false, dir, logger = console } = {}) {
const { dependencies, devDependencies } = require(path.join(dir, 'package.json'));
Expand All @@ -84,11 +86,11 @@ async function verifyDeps({ autoUpgrade = false, dir, logger = console } = {}) {
const prodPkgs = toInstall.filter(({ type }) => type === 'prod');
let upgradePackages = '';
if (prodPkgs.length > 0) {
upgradePackages = upgradePackages.concat(`npm i ${getPkgIds(prodPkgs)} `);
upgradePackages += `npm i ${getPkgIds(prodPkgs)} `;
}
const devPkgs = toInstall.filter(({ type }) => type === 'dev');
if (devPkgs.length > 0) {
upgradePackages = upgradePackages.concat(`\nnpm i -D ${getPkgIds(devPkgs)} `);
upgradePackages += `\nnpm i -D ${getPkgIds(devPkgs)} `;
}

if (autoUpgrade) {
Expand Down
5 changes: 4 additions & 1 deletion lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

const Chance = require('chance');
const { promisify: getMockExecAsync } = require('util');
const { join: mockJoin } = require('path');
const path = require('path');

const verifyDeps = require('.');

const { join: mockJoin } = path;

jest.mock('path', () => ({ join: jest.fn() }));
jest.mock('child_process', () => ({}));
jest.mock('util', () => {
Expand Down
Loading

0 comments on commit 57b93d8

Please sign in to comment.