Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const {lstatSync, readdirSync} = require("fs"),
- {join} = require("path"),
- check = require("npm-check"),
- chalk = require("chalk"),
- {table} = require("table"),
- ora = require("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()
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement