UNPKG

2.66 kBJavaScriptView Raw
1///@ts-check
2"use strict";
3
4const path = require('path');
5const through = require('through2');
6// const stringifyObject = require('stringify-object');
7const chalk = require('ansi-colors');
8const titleColor = require('./color');
9const fancyLog = require('./logger');
10
11const inputStyle = chalk.cyan.bold.underline;
12function outputStyle(str) {
13 if (str) {
14 return chalk.gray(" → ") + chalk.underline.bold.gray(str);
15 }
16 return '';
17}
18
19const home = require('os').homedir();
20
21function tildify(str) {
22 str = path.normalize(str) + path.sep;
23 return (str.indexOf(home) === 0 ? str.replace(home + path.sep, '~' + path.sep) : str).slice(0, -1);
24};
25
26module.exports = options => {
27 options = Object.assign({
28 logger: fancyLog,
29 title: 'mp-build:',
30 minimal: true,
31 showFiles: true,
32 showCount: false
33 }, options);
34
35 if (process.argv.includes('--verbose')) {
36 options.verbose = true;
37 options.minimal = false;
38 options.showFiles = true;
39 options.showCount = true;
40 }
41
42 let count = 0;
43
44 return through.obj((file, enc, cb) => {
45 if (options.showFiles) {
46 let output = chalk.whiteBright('[►]') + ' ';
47 const name = path.relative(file.base, file.path);
48 if (options.minimal) {
49 output += inputStyle(options.srcName || name);
50 } else {
51 output =
52 '\n' +
53 (file.cwd ? 'cwd: ' + inputStyle(tildify(file.cwd)) : '') +
54 (file.base ? '\nbase: ' + inputStyle(tildify(file.base)) : '') +
55 (file.path ? '\npath: ' + inputStyle(tildify(file.path)) : '') +
56 // (file.stat && options.verbose ? '\nstat: ' + prop(stringifyObject(file.stat, { indent: ' ' }).replace(/[{}]/g, '').trim()) : '') +
57 '\n';
58 }
59
60 // let outPath = ''
61 if (options.distName) {
62 output += outputStyle(options.distName);
63 } else if (options.distExt || options.dist) {
64 output += outputStyle(
65 path.join(
66 options.dist || '',
67 name.replace(/\.(.*)$/, options.distExt || '.$1')
68 )
69 );
70 }
71
72 options.logger(titleColor(options.title), output || chalk.gray('…'));
73 }
74
75 count++;
76 cb(null, file);
77 }, cb => {
78 if (options.showCount) {
79 options.logger(titleColor(options.title), chalk.green(count + ' ' + (count > 0 ? 'items' : 'item')));
80 }
81 cb();
82 });
83};