Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. const {lstatSync, readdirSync} = require("fs"),
  2. {join} = require("path"),
  3. check = require("npm-check"),
  4. chalk = require("chalk"),
  5. {table} = require("table"),
  6. ora = require("ora");
  7.  
  8. // Create lambda and immediately call it later
  9. ((source, getPkgName, spinner) =>
  10. // Wait for all Promises to finish running. Promise.all returns an array
  11. void Promise.all(
  12. readdirSync(source)
  13. // Give me the fill paths of the items
  14. .map(name => join(source, name))
  15. // Only give me directories
  16. .filter(source => lstatSync(source).isDirectory())
  17. .map(pkgPath =>
  18. // Check for package data
  19. check({cwd: pkgPath})
  20. .reduce((prev, {get}) =>
  21. // Make a lambda that's immediately called in order to avoid duplication of logic
  22. ((pkgList, pkgName) =>
  23. void console.log(`\nGot data for ${pkgName}`) ||
  24. pkgList.length ? [
  25. () => console.log(chalk.bold(pkgName)),
  26. () => console.log("\n" + table(
  27. [
  28. // Add header text and concat with pkglist to display properly in a `table`
  29. [
  30. chalk`{bold Package Name}`,
  31. chalk`{bold Dep Type}`,
  32. chalk`{bold Old Package}`,
  33. chalk`{bold New Package}`
  34. ].concat(pkgList)
  35. ]))
  36. // Otherwise return `prev` in order to not print anything about this data
  37. ] : prev
  38. )(
  39. // Get the `packages` data from the `check` call to return as the first arg
  40. get("packages")
  41. // Only give me packages that have a `major` spec bump
  42. .filter(pkg => pkg.bump && pkg.bump === "major")
  43. // Map to an array of strings to print later. Made array as we want it to match `table` console call
  44. .map(({moduleName, latest, devDependency, packageWanted}) => [
  45. chalk.bold(moduleName),
  46. devDependency ? chalk.bgYellow`Dev Dep` : chalk.bgGreen`Main Dep`,
  47. packageWanted,
  48. latest
  49. ]),
  50. // Pass `getPkName` as second arg
  51. getPkgName(pkgPath)
  52. ), []
  53. )
  54. .catch(
  55. // If there's an error, it means that the data we're looking for doesn't exist.
  56. () =>
  57. // Use lambda to be immediately called with the package name to reduce data duplication
  58. // Use `void console.log ||` to console log and return the array of lambdas in one line
  59. (pkgName => void console.log(`\nGot data for ${pkgName}`) || [
  60. () => console.log(chalk`\n{bold.bgWhite ${pkgName} does not have a package.json}`)
  61. ])(getPkgName(pkgPath))
  62. )
  63. )
  64. )
  65. // Once all that is done, for each package, for each function in that package data, run
  66. .then(arr => void arr.forEach(val => val.forEach(a => a())) || spinner.stop())
  67. // If there was an error (somehow?) then handle it and stop the spinner
  68. .catch((e) => console.error(e) || spinner.stop())
  69. // Call top level lambda with
  70. )(
  71. // The string of the current path
  72. process.cwd(),
  73. // A lambda that, given a string, will split my `/` and return the last item
  74. path => path.split(/[\/\\]/).find((_, i, a) => i == a.length - 1),
  75. // Start a process to display a loading spinner
  76. ora("Searching packages").start()
  77. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement