-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make big improvements and add tests.
Fixes #3 . This package now supports npm v7+. While Node.js v12 and v14 should be supported (with npm updated to v7+), this isn’t easy to test in GitHub Actions CI as actions/setup-node@v2 doesn’t allow the npm version to be configured, see: actions/setup-node#213 .
- Loading branch information
1 parent
1635252
commit 07a350e
Showing
74 changed files
with
2,209 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/test/fixtures/package-json-broken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
.DS_Store | ||
!/test/fixtures/**/node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
package.json | ||
/test/snapshots |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,169 +1,128 @@ | ||
#!/usr/bin/env node | ||
|
||
import { exec } from 'child_process'; | ||
import { promisify } from 'util'; | ||
import chalk from 'chalk'; | ||
import Table from 'cli-table3'; | ||
import moment from 'moment'; | ||
|
||
const asyncExec = promisify(exec); | ||
const startTime = new Date(); | ||
const thresholds = [ | ||
{ | ||
label: 'Day', | ||
count: 0, | ||
ms: 8.64e7, | ||
color: 'green', | ||
}, | ||
{ | ||
label: 'Week', | ||
count: 0, | ||
ms: 6.048e8, | ||
color: 'cyan', | ||
}, | ||
{ | ||
label: 'Month', | ||
count: 0, | ||
ms: 2.628e9, | ||
color: 'magenta', | ||
}, | ||
{ | ||
label: 'Year', | ||
count: 0, | ||
ms: 3.154e10, | ||
color: 'yellow', | ||
}, | ||
{ | ||
label: 'Year+', | ||
count: 0, | ||
ms: Infinity, | ||
color: 'red', | ||
}, | ||
]; | ||
const clearTableChars = { | ||
top: '', | ||
'top-mid': '', | ||
'top-left': '', | ||
'top-right': '', | ||
bottom: '', | ||
'bottom-mid': '', | ||
'bottom-left': '', | ||
'bottom-right': '', | ||
left: '', | ||
'left-mid': '', | ||
mid: '', | ||
'mid-mid': '', | ||
right: '', | ||
'right-mid': '', | ||
middle: '', | ||
}; | ||
import createFormatDuration from 'duration-relativetimeformat'; | ||
import kleur from 'kleur'; | ||
import reportCliError from '../private/reportCliError.mjs'; | ||
import auditAge from '../public/auditAge.mjs'; | ||
|
||
const formatDuration = createFormatDuration('en-US'); | ||
|
||
/** | ||
* Audits the age of installed npm packages. | ||
* @private | ||
* Runs the `audit-age` CLI. | ||
* @kind function | ||
* @name auditAgeCli | ||
* @returns {Promise<void>} Resolves once the operation is done. | ||
* @ignore | ||
*/ | ||
async function auditAge() { | ||
const { stdout: rawTree } = await asyncExec( | ||
'npm ls --prod --only production --json' | ||
); | ||
const tree = JSON.parse(rawTree); | ||
const lookups = []; | ||
|
||
/** | ||
* Recurses dependencies to prepare the report. | ||
* @param {Array<object>} dependencies Dependencies nested at the current level. | ||
* @param {Array<string>} ancestorPath How the dependency is nested. | ||
*/ | ||
const recurse = (dependencies, ancestorPath = []) => { | ||
Object.entries(dependencies).forEach( | ||
([name, { version, dependencies }]) => { | ||
const path = [...ancestorPath, `${name}@${version}`]; | ||
lookups.push( | ||
asyncExec(`npm view ${name} time --json`).then( | ||
({ stdout: rawTimes }) => { | ||
const times = JSON.parse(rawTimes); | ||
const published = moment(times[version]); | ||
const msDiff = moment(startTime).diff(published); | ||
const threshold = thresholds.find(({ ms }) => msDiff < ms); | ||
threshold.count++; | ||
return { | ||
path, | ||
name, | ||
version, | ||
published, | ||
threshold, | ||
}; | ||
} | ||
) | ||
); | ||
if (dependencies) recurse(dependencies, path); | ||
} | ||
); | ||
}; | ||
|
||
recurse(tree.dependencies); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`\nFetching ${lookups.length} package ages...\n`); | ||
async function auditAgeCli() { | ||
try { | ||
console.info('Auditing the age of installed production npm packages…'); | ||
|
||
const list = await Promise.all(lookups); | ||
const sorted = list.sort((a, b) => a.published - b.published); | ||
const packagesTable = new Table({ | ||
chars: { | ||
...clearTableChars, | ||
mid: '─', | ||
'mid-mid': '─', | ||
}, | ||
}); | ||
|
||
sorted.forEach(({ path, published, threshold }) => | ||
packagesTable.push([ | ||
const dateAudit = new Date(); | ||
const audit = await auditAge(); | ||
const unknownCategory = { | ||
label: 'Unknown', | ||
color: 'grey', | ||
count: 0, | ||
}; | ||
const thresholdCategories = [ | ||
{ | ||
vAlign: 'bottom', | ||
content: path.reduce((tree, item, index) => { | ||
if (index > 0) tree += `\n${' '.repeat(index - 1)}└─ `; | ||
return (index === path.length - 1 ? chalk.dim(tree) : tree) + item; | ||
}, ''), | ||
label: 'Day', | ||
ms: 8.64e7, | ||
color: 'green', | ||
count: 0, | ||
}, | ||
{ | ||
hAlign: 'right', | ||
vAlign: 'bottom', | ||
content: `${chalk[threshold.color]( | ||
published.fromNow() | ||
)}\n${published.format('lll')}`, | ||
label: 'Week', | ||
ms: 6.048e8, | ||
color: 'cyan', | ||
count: 0, | ||
}, | ||
]) | ||
); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`${packagesTable.toString()}\n\n`); | ||
|
||
const summaryTable = new Table({ | ||
chars: clearTableChars, | ||
}); | ||
|
||
thresholds.reverse().forEach(({ color, label, count }) => | ||
summaryTable.push([ | ||
{ | ||
hAlign: 'right', | ||
content: chalk[color](label), | ||
label: 'Month', | ||
ms: 2.628e9, | ||
color: 'magenta', | ||
count: 0, | ||
}, | ||
{ | ||
hAlign: 'right', | ||
content: count, | ||
label: 'Year', | ||
ms: 3.154e10, | ||
color: 'yellow', | ||
count: 0, | ||
}, | ||
]) | ||
); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log(`${summaryTable.toString()}\n`); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log( | ||
`Audited ${lookups.length} package ages in ${ | ||
(new Date() - startTime) / 1000 | ||
}s.\n` | ||
); | ||
{ | ||
label: 'Year+', | ||
ms: Infinity, | ||
color: 'red', | ||
count: 0, | ||
}, | ||
]; | ||
|
||
for (const { path, datePublished } of audit) { | ||
let category; | ||
|
||
if (datePublished) { | ||
const msDiff = dateAudit - datePublished; | ||
|
||
category = thresholdCategories.find(({ ms }) => msDiff < ms); | ||
} else category = unknownCategory; | ||
|
||
category.count++; | ||
|
||
let dependencyTree = ''; | ||
|
||
path.forEach(({ name, version }, index) => { | ||
if (index) | ||
dependencyTree += ` | ||
${' '.repeat(index - 1)}└─ `; | ||
|
||
if (index === path.length - 1) | ||
dependencyTree = kleur.dim(dependencyTree); | ||
|
||
dependencyTree += name; | ||
|
||
if (version) dependencyTree += `@${version}`; | ||
}); | ||
|
||
console.info(` | ||
${dependencyTree} | ||
${kleur[category.color]( | ||
`${kleur.dim(datePublished ? datePublished.toISOString() : 'Unavailable')} (${ | ||
datePublished ? formatDuration(datePublished, dateAudit) : 'unknown age' | ||
})` | ||
)}`); | ||
} | ||
|
||
const allCategories = [...thresholdCategories.reverse(), unknownCategory]; | ||
|
||
// This is needed to align the category count column. | ||
const longestCategoryLabelLength = Math.max( | ||
...allCategories.map(({ label }) => label.length) | ||
); | ||
|
||
let outputSummary = ''; | ||
|
||
for (const { label, color, count } of allCategories) | ||
outputSummary += ` | ||
${' '.repeat(longestCategoryLabelLength - label.length)}${kleur[color]( | ||
label | ||
)} ${count}`; | ||
|
||
outputSummary += ` | ||
${kleur.bold( | ||
`Audited the age of ${audit.length} installed production npm package${ | ||
audit.length === 1 ? '' : 's' | ||
}.` | ||
)} | ||
`; | ||
|
||
console.info(outputSummary); | ||
} catch (error) { | ||
reportCliError('audit-age', error); | ||
|
||
process.exitCode = 1; | ||
} | ||
} | ||
|
||
auditAge(); | ||
auditAgeCli(); |
Oops, something went wrong.