1 |
|
2 | "use strict";
|
3 |
|
4 | const path = require('path');
|
5 | const through = require('through2');
|
6 |
|
7 | const chalk = require('ansi-colors');
|
8 | const titleColor = require('./color');
|
9 | const fancyLog = require('./logger');
|
10 |
|
11 | const inputStyle = chalk.cyan.bold.underline;
|
12 | function outputStyle(str) {
|
13 | if (str) {
|
14 | return chalk.gray(" → ") + chalk.underline.bold.gray(str);
|
15 | }
|
16 | return '';
|
17 | }
|
18 |
|
19 | const home = require('os').homedir();
|
20 |
|
21 | function 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 |
|
26 | module.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 |
|
57 | '\n';
|
58 | }
|
59 |
|
60 |
|
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 | };
|