From c5c288809767cb64356a52488b14a2c854f1e55b Mon Sep 17 00:00:00 2001 From: Le Goff Vincent Date: Tue, 28 Jan 2020 18:00:11 +0100 Subject: [PATCH 1/2] feat: support package.json config --- README.md | 21 ++++++++++++++++++++- bin/cli.js | 10 +++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9fa64f95..ab9fd4ff 100644 --- a/README.md +++ b/README.md @@ -220,7 +220,9 @@ Property | Type | Default | Description `detectiveOptions` | Object | false | Custom `detective` options for [dependency-tree](https://github.com/dependents/node-dependency-tree) and [precinct](https://github.com/dependents/node-precinct#usage) `dependencyFilter` | Function | false | Function called with a dependency filepath (exclude substree by returning false) -> Note that when running the CLI it's possible to use a runtime configuration file. The config should placed in `.madgerc` in your project or home folder. Look [here](https://github.com/dominictarr/rc#standards) for alternative locations for the file. Here's an example: +You can use configuration file either in `.madgerc` in your project or home folder or directly in `package.json`. Look [here](https://github.com/dominictarr/rc#standards) for alternative locations for the file. + +> .madgerc ```json { @@ -233,6 +235,23 @@ Property | Type | Default | Description } ``` +> package.json +```json +{ + "name": "foo", + "version": "0.0.1", + ... + "madge": { + "fontSize": "10px", + "graphVizOptions": { + "G": { + "rankdir": "LR" + } + } + } +} +``` + # CLI ## Examples diff --git a/bin/cli.js b/bin/cli.js index 8d3f0302..eba97073 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -51,7 +51,7 @@ if (!program.color) { const log = require('../lib/log'); const output = require('../lib/output'); const madge = require('../lib/api'); -const config = Object.assign({}, rc); +const config = Object.assign(rc, getPackageConfig()); program.options.forEach((opt) => { const name = opt.name(); @@ -132,6 +132,14 @@ function dependencyFilter() { }; } +function getPackageConfig() { + try { + return require(path.join(process.cwd(), 'package.json')).madge; + } catch (err) { + return {}; + } +} + new Promise((resolve, reject) => { if (program.stdin) { let buffer = ''; From 0acebb7417ea17a0980630685ee4dfe182fbcdbb Mon Sep 17 00:00:00 2001 From: Le Goff Vincent Date: Wed, 29 Jan 2020 09:40:18 +0100 Subject: [PATCH 2/2] review --- bin/cli.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index eba97073..e87d888f 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -51,7 +51,12 @@ if (!program.color) { const log = require('../lib/log'); const output = require('../lib/output'); const madge = require('../lib/api'); -const config = Object.assign(rc, getPackageConfig()); + +let packageConfig = {}; +try { + packageConfig = require(path.join(process.cwd(), 'package.json')).madge; +} catch (e) { } +const config = Object.assign(rc, packageConfig); program.options.forEach((opt) => { const name = opt.name(); @@ -132,14 +137,6 @@ function dependencyFilter() { }; } -function getPackageConfig() { - try { - return require(path.join(process.cwd(), 'package.json')).madge; - } catch (err) { - return {}; - } -} - new Promise((resolve, reject) => { if (program.stdin) { let buffer = '';