Skip to content

Commit

Permalink
Merge pull request #17 from Arcanemagus/directory-argument
Browse files Browse the repository at this point in the history
Add directory argument
  • Loading branch information
Arcanemagus authored Dec 27, 2017
2 parents efbf789 + 65fc1fb commit 6b2154e
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions check-peer-deps.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
const { exec: execCP } = require('child_process');
const { promisify } = require('util');
const { readFile: readFileFS } = require('fs');
const semver = require('semver');
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');

const exec = promisify(execCP);
const readFile = promisify(readFileFS);

const optionDefinitions = [
{
Expand Down Expand Up @@ -33,6 +35,14 @@ const optionDefinitions = [
typeLabel: '[underline]{retries}',
defaultValue: 2,
},
{
name: 'directory',
description: 'The directory to check peerDependencies within. Defaults ' +
'to the current directory.',
type: String,
typeLabel: '[underline]{directory}',
defaultValue: process.cwd(),
},
];

const usageSections = [
Expand Down Expand Up @@ -61,12 +71,26 @@ const log = (value) => {
};

const addDeps = (dependencies) => {
if (!dependencies) {
return;
}
Object.entries(dependencies).forEach((entry) => {
const [name, range] = entry;
deps.set(name, range);
});
};

const readPackageConfig = async (path) => {
let packageConfig = {};
try {
const contents = await readFile(path, { encoding: 'utf8' });
packageConfig = JSON.parse(contents);
} catch (e) {
console.error(e.message);
}
return packageConfig;
};

const npmView = async (name, keys) => {
const opts = ['view', '--json', name].concat(keys);
const command = ['npm'].concat(opts).join(' ');
Expand Down Expand Up @@ -140,14 +164,9 @@ const getNpmPeerDep = async (range, name) => {

const getPeerDep = async (range, name) => {
log(`Getting peerDependencies for ${name}`);
let packageInfo;
try {
// Hacktown, USA.
// eslint-disable-next-line import/no-dynamic-require
packageInfo = require(`${process.cwd()}/node_modules/${name}/package.json`);
} catch (e) {
return;
}
// Hacktown, USA.
const packagePath = `${options.directory}/node_modules/${name}/package.json`;
const packageInfo = await readPackageConfig(packagePath);
if (!packageInfo.peerDependencies) {
return;
}
Expand Down Expand Up @@ -216,11 +235,7 @@ async function checkPeerDeps() {
process.exit(0);
}

// eslint-disable-next-line import/no-dynamic-require
const packageConfig = require(`${process.cwd()}/package.json`);
if (!packageConfig.dependencies) {
console.error('No dependencies in the current pacakge!');
}
const packageConfig = await readPackageConfig(`${options.directory}/package.json`);

// Get the dependencies to process
addDeps(packageConfig.dependencies);
Expand All @@ -229,6 +244,11 @@ async function checkPeerDeps() {
addDeps(packageConfig.devDependencies);
}

if (deps.size < 1) {
console.error('No dependencies in the current package!');
process.exit(0);
}

log('Dependencies:');
deps.forEach((range, name) => { log(`${name}: ${range}`); });

Expand Down

0 comments on commit 6b2154e

Please sign in to comment.