From fa103554ac759e038977d36866a1cc97f535a6ab Mon Sep 17 00:00:00 2001 From: Joel Helbling Date: Fri, 27 Oct 2023 11:35:29 +0100 Subject: [PATCH] Add option to only consider prod dependencies --- README.md | 12 ++++++++++++ index.js | 25 ++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a883baf..7dad706 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,18 @@ import npmOutdated from "danger-plugin-npm-outdated"; schedule(npmOutdated()); ``` +## Options + +|Name|Default|Function| +|-|-|-| +|prodOnly|false|Only consider production dependencies| + +Pass options to the `npmOutdated` function + +```ts +scheduled(npmOutdated({ prodOnly: true })) +``` + ## Sample message ![sample message](https://raw.githubusercontent.com/revathskumar/danger-plugin-npm-outdated/master/images/message.png) diff --git a/index.js b/index.js index 1cb3a55..ec22df5 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,14 @@ "use strict"; const exec = require("child_process").exec; -const formatOutdatedPackages = (outdatedPackages = {}, packageNames = []) => { +const formatOutdatedPackages = (outdatedPackages = {}, packageNames = [], options = {}) => { const headers = [ - "| Package | Current | Wanted | latest |", - "|---------|---------|--------|--------|" + `| Package | Current | Wanted | latest |${options.prodOnly ? '' : ' kind |'}`, + `|---------|---------|--------|--------|${options.prodOnly ? '' : '------|'}` ]; const content = packageNames.map(packageName => { - const { current, wanted, latest } = outdatedPackages[packageName]; - return `| ${packageName} | ${current} | ${wanted} | ${latest} |`; + const { current, wanted, latest, type } = outdatedPackages[packageName]; + return `| ${packageName} | ${current} | ${wanted} | ${latest} |${options.prodOnly ? '' : `${type} |`}`; }); return headers.concat(content).join("\n"); }; @@ -27,8 +27,14 @@ const execP = outdatedCommand => { }); }; -export default async function npmOutdated(options = {}) { - let outdatedCommand = "npm outdated --json"; +export default async function npmOutdated(options = { + prodOnly: false, +}) { + let outdatedCommand = "npm outdated --json --long"; + + if (options.prodOnly){ + outdatedCommand += ' --production' + } try { const outdatedPackages = await execP(outdatedCommand); @@ -36,10 +42,11 @@ export default async function npmOutdated(options = {}) { if (packageNames.length) { const packagesTable = formatOutdatedPackages( outdatedPackages, - packageNames + packageNames, + options ); - warn(`You have ${packageNames.length} outdated packages`); + warn(`You have ${packageNames.length} outdated ${options.prodOnly ? 'production' : ''} packages`); markdown(`