UNPKG

3.36 kBJavaScriptView Raw
1const moment = require("moment");
2const tryPath = process.cwd();
3const chalk = require("chalk");
4const prettyMs = require("pretty-ms");
5const spawn = require("cross-spawn");
6const boxen = require("boxen");
7const helpers = require("../helpers");
8
9const runTask = async (name, conf = false) => {
10 let output;
11 let startTime = Date.now();
12 let input = name.slice(-1) === "-";
13 let task = {};
14 let tasks = [];
15 if (conf !== false) {
16 conf.set("tasks." + name + ".lastRan", startTime);
17 await conf.updateConfigFile();
18 tasks = conf.get("tasks");
19 }
20 let command =
21 conf === false ? name : tasks[name].file ? "node " + tasks[name].file : tasks[name].script;
22 console.clear();
23 console.log(
24 input
25 ? chalk.hex("#4b5150")(" Running: ") + chalk.bold.underline.hex("#438b34")(name)
26 : boxen(
27 chalk.hex("#717877")(" Running: ") +
28 chalk.bold.underline.hex("#438b34")(name) +
29 chalk.hex("#717877")(" "),
30 {
31 padding: 0,
32 margin: { left: 2, top: 0, bottom: 0, right: 0 },
33 borderStyle: {
34 topLeft: chalk.hex("#5a596d")("╔"),
35 topRight: chalk.hex("#5a596d")("╗"),
36 bottomLeft: chalk.hex("#5a596d")("╚"),
37 bottomRight: chalk.hex("#5a596d")("╝"),
38 horizontal: chalk.hex("#5a596d")("═"),
39 vertical: chalk.hex("#5a596d")("║")
40 }, //"round",
41 // dimBorder: true,
42 align: "center" //,
43 // float: "center"
44 }
45 )
46 );
47 try {
48 let pars = command.split(" ");
49
50 output = spawn(pars[0], pars.slice(1, pars.length), {
51 cwd: tryPath,
52 env: { ...process.env, ...{ FORCE_COLOR: true } },
53 stdio: input ? "inherit" : "pipe"
54 });
55 if (!input) {
56 output.stdout.on("data", code => {
57 code = code + "";
58 console.log(
59 `${chalk.bgHex("#181c24").hex("#8c91a7")(
60 moment().format("HH:MM:SS") + ":"
61 )} ${code}`.trim()
62 );
63 });
64 output.stderr.on("data", code => {
65 code = code + "";
66 console.log(
67 `${chalk.bgHex("#181c24").hex("#a72e32")(
68 moment().format("HH:MM:SS") + "ERR :"
69 )} ${code}`.trim()
70 );
71 });
72 }
73 return new Promise((resolve, reject) => {
74 output.on("close", code => {
75 if (code === 0) {
76 let elapsed = Date.now() - startTime;
77 console.log(`${chalk.bold.green("Finished in " + prettyMs(elapsed), code)}`);
78 resolve();
79 } else {
80 // console.log(`${chalk.bold.green("Finished in " + prettyMs(elapsed), code)}`);
81 reject(`task exited with code ${code}`);
82 }
83 });
84 });
85 } catch (error) {
86 console.log(`${chalk.red.bold(error)}`);
87 }
88};
89
90module.exports = runTask;