UNPKG

1.75 kBJavaScriptView Raw
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//------------------------------------------------------------------------------
10// Requirements
11//------------------------------------------------------------------------------
12
13const chalk = require("chalk")
14
15//------------------------------------------------------------------------------
16// Public Interface
17//------------------------------------------------------------------------------
18
19/**
20 * Creates the header text for a given task.
21 *
22 * @param {string} nameAndArgs - A task name and arguments.
23 * @param {object} packageInfo - A package.json's information.
24 * @param {object} packageInfo.body - A package.json's JSON object.
25 * @param {string} packageInfo.path - A package.json's file path.
26 * @param {boolean} isTTY - The flag to color the header.
27 * @returns {string} The header of a given task.
28 */
29module.exports = function createHeader(nameAndArgs, packageInfo, isTTY) {
30 if (!packageInfo) {
31 return `\n> ${nameAndArgs}\n\n`
32 }
33
34 const index = nameAndArgs.indexOf(" ")
35 const name = (index === -1) ? nameAndArgs : nameAndArgs.slice(0, index)
36 const args = (index === -1) ? "" : nameAndArgs.slice(index + 1)
37 const packageName = packageInfo.body.name
38 const packageVersion = packageInfo.body.version
39 const scriptBody = packageInfo.body.scripts[name]
40 const packagePath = packageInfo.path
41 const color = isTTY ? chalk.styles.gray : { open: "", close: "" }
42
43 return `
44${color.open}> ${packageName}@${packageVersion} ${name} ${packagePath}${color.close}
45${color.open}> ${scriptBody} ${args}${color.close}
46
47`
48}