create-header.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * @module create-header
  3. * @author Toru Nagashima
  4. * @copyright 2016 Toru Nagashima. All rights reserved.
  5. * See LICENSE file in root directory for full license.
  6. */
  7. 'use strict'
  8. // ------------------------------------------------------------------------------
  9. // Public Interface
  10. // ------------------------------------------------------------------------------
  11. /**
  12. * Creates the header text for a given task.
  13. *
  14. * @param {string} nameAndArgs - A task name and arguments.
  15. * @param {object} packageInfo - A package.json's information.
  16. * @param {object} packageInfo.body - A package.json's JSON object.
  17. * @param {string} packageInfo.path - A package.json's file path.
  18. * @param {boolean} isTTY - The flag to color the header.
  19. * @returns {string} The header of a given task.
  20. */
  21. module.exports = function createHeader (nameAndArgs, packageInfo, isTTY, ansiStyles) {
  22. if (!packageInfo) {
  23. return `\n> ${nameAndArgs}\n\n`
  24. }
  25. const index = nameAndArgs.indexOf(' ')
  26. const name = (index === -1) ? nameAndArgs : nameAndArgs.slice(0, index)
  27. const args = (index === -1) ? '' : nameAndArgs.slice(index + 1)
  28. const packageName = packageInfo.body.name
  29. const packageVersion = packageInfo.body.version
  30. const scriptBody = packageInfo.body.scripts[name]
  31. const packagePath = packageInfo.path
  32. const color = isTTY ? ansiStyles.gray : { open: '', close: '' }
  33. return `
  34. ${color.open}> ${packageName}@${packageVersion} ${name} ${packagePath}${color.close}
  35. ${color.open}> ${scriptBody} ${args}${color.close}
  36. `
  37. }