Skip to content

Instantly share code, notes, and snippets.

@crutchcorn
Last active December 1, 2021 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crutchcorn/3f63bd89ef9f85287cdfc2e4b9470eb5 to your computer and use it in GitHub Desktop.
Save crutchcorn/3f63bd89ef9f85287cdfc2e4b9470eb5 to your computer and use it in GitHub Desktop.
I would usually break code like this out, but I was tinkering about and was trying to make a lot of it inline. Had fun writing it though!
// Setup npm packages w/o variables
(({ lstatSync, readdirSync }, { join }, check, chalk, { table }, ora) =>
// Create lambda and immediately call it later
((source, getPkgName, spinner) =>
// Wait for all Promises to finish running. Promise.all returns an array
void Promise.all(
readdirSync(source)
// Give me the fill paths of the items
.map(name => join(source, name))
// Only give me directories
.filter(source => lstatSync(source).isDirectory())
.map(pkgPath =>
// Check for package data
check({ cwd: pkgPath })
.reduce((prev, { get }) =>
// Make a lambda that's immediately called in order to avoid duplication of logic
((pkgList, pkgName) =>
void console.log(`\nGot data for ${pkgName}`) ||
pkgList.length ? [
() => console.log(chalk.bold(pkgName)),
() => console.log('\n' + table(
[
// Add header text and concat with pkglist to display properly in a `table`
[
chalk`{bold Package Name}`,
chalk`{bold Dep Type}`,
chalk`{bold Old Package}`,
chalk`{bold New Package}`
].concat(pkgList)
]))
// Otherwise return `prev` in order to not print anything about this data
] : prev
)(
// Get the `packages` data from the `check` call to return as the first arg
get('packages')
// Only give me packages that have a `major` spec bump
.filter(pkg => pkg.bump && pkg.bump === 'major')
// Map to an array of strings to print later. Made array as we want it to match `table` console call
.map(({ moduleName, latest, devDependency, packageWanted }) => [
chalk.bold(moduleName),
devDependency ? chalk.bgYellow`Dev Dep` : chalk.bgGreen`Main Dep`,
packageWanted,
latest
]),
// Pass `getPkName` as second arg
getPkgName(pkgPath)
), []
)
.catch(
// If there's an error, it means that the data we're looking for doesn't exist.
() =>
// Use lambda to be immediately called with the package name to reduce data duplication
// Use `void console.log ||` to console log and return the array of lambdas in one line
(pkgName => void console.log(`\nGot data for ${pkgName}`) || [
() => console.log(chalk`\n{bold.bgWhite ${pkgName} does not have a package.json}`)
])(getPkgName(pkgPath))
)
)
)
// Once all that is done, for each package, for each function in that package data, run
.then(arr => void arr.forEach(val => val.forEach(a => a())) || spinner.stop())
// If there was an error (somehow?) then handle it and stop the spinner
.catch((e) => console.error(e) || spinner.stop())
// Call top level lambda with
)(
// The string of the current path
process.cwd(),
// A lambda that, given a string, will split my `/` and return the last item
path => path.split(/[\/\\]/).find((_, i, a) => i == a.length - 1),
// Start a process to display a loading spinner
ora('Searching packages').start()
))(
require('fs'),
require('path'),
require('npm-check'),
require('chalk'),
require('table'),
require('ora')
);
@rusher2004
Copy link

Thank you for not actually making it literally a one-liner

@crutchcorn
Copy link
Author

@rusher2004 I gotchu

((m,f,x)=>(({lstatSync:l,readdirSync:r},{join:j},c,h,{table:t},o)=>((s,g,p)=>void Promise.all(r(s)[m](n=>j(s,n))[f](e=>l(e).isDirectory())[m](k=>c({cwd:k}).reduce((h,{get})=>((pl,pn)=>void x(`\nGot data for ${pn}`)||pl.length?[()=>x(h.bold(pn)),()=>x('\n'+t([[h`{bold Package Name}`,h`{bold Dep Type}`,h`{bold Old Package}`,h`{bold New Package}`].concat(pl)]))]:h)(get('packages')[f](k=>k.bump&&k.bump==='major')[m](({moduleName:b,latest:z,devDependency:v,packageWanted:w})=>[h.bold(b),v?h.bgYellow`Dev Dep`:h.bgGreen`Main Dep`,w,z]),g(k)),[]).catch(()=>(u=>void x(`\nGot data for ${u}`)||[()=>x(h`\n{bold.bgWhite ${u} does not have a package.json}`)])(g(k))))).then(arr=>void arr.forEach(val=>val.forEach(a=>a()))||p.stop()).catch((e)=>console.error(e)||p.stop()))(process.cwd(),z=>z.split(/[\/\\]/).find((_,i,a)=>i==a.length-1),o('Searching packages').start()))(require('fs'),require('path'),require('npm-check'),require('chalk'),require('table'),require('ora')))('map','filter',console.log)

Done by hand

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment