forked from mysticatea/npm-run-all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-header.js
48 lines (41 loc) · 1.72 KB
/
create-header.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @module create-header
* @author Toru Nagashima
* @copyright 2016 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const ansiStyles = require("ansi-styles")
//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
/**
* Creates the header text for a given task.
*
* @param {string} nameAndArgs - A task name and arguments.
* @param {object} packageInfo - A package.json's information.
* @param {object} packageInfo.body - A package.json's JSON object.
* @param {string} packageInfo.path - A package.json's file path.
* @param {boolean} isTTY - The flag to color the header.
* @returns {string} The header of a given task.
*/
module.exports = function createHeader(nameAndArgs, packageInfo, isTTY) {
if (!packageInfo) {
return `\n> ${nameAndArgs}\n\n`
}
const index = nameAndArgs.indexOf(" ")
const name = (index === -1) ? nameAndArgs : nameAndArgs.slice(0, index)
const args = (index === -1) ? "" : nameAndArgs.slice(index + 1)
const packageName = packageInfo.body.name
const packageVersion = packageInfo.body.version
const scriptBody = packageInfo.body.scripts[name]
const packagePath = packageInfo.path
const color = isTTY ? ansiStyles.gray : { open: "", close: "" }
return `
${color.open}> ${packageName}@${packageVersion} ${name} ${packagePath}${color.close}
${color.open}> ${scriptBody} ${args}${color.close}
`
}