1 | 'use strict';
|
2 | const format = require('date-fns/format'),
|
3 | _ = require('lodash'),
|
4 | chalk = require('chalk'),
|
5 | fs = require('fs-extra'),
|
6 | path = require('path'),
|
7 | amphoraFs = require('amphora-fs'),
|
8 | configFile = require('./config-file-helpers');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function time(t2, t1) {
|
17 | const diff = t2 - t1;
|
18 |
|
19 | if (diff > 1000 * 60) {
|
20 |
|
21 | return format(new Date(diff), 'm[m] s.SS[s]');
|
22 | } else {
|
23 |
|
24 | return format(new Date(diff), 's.SS[s]');
|
25 | }
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function watcher(e, filepath) {
|
35 | if (!_.includes(filepath, '.DS_Store')) {
|
36 | console.log(chalk.green('✓ ') + chalk.grey(filepath.replace(process.cwd(), '')));
|
37 | }
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function bucket(name) {
|
48 | if (name.match(/^[a-d]/i)) {
|
49 | return 'a-d';
|
50 | } else if (name.match(/^[e-h]/i)) {
|
51 | return 'e-h';
|
52 | } else if (name.match(/^[i-l]/i)) {
|
53 | return 'i-l';
|
54 | } else if (name.match(/^[m-p]/i)) {
|
55 | return 'm-p';
|
56 | } else if (name.match(/^[q-t]/i)) {
|
57 | return 'q-t';
|
58 | } else {
|
59 | return 'u-z';
|
60 | }
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | function unbucket(name) {
|
69 | return _.find(['a-d', 'e-h', 'i-l', 'm-p', 'q-t', 'u-z'], (matcher) => _.includes(name, matcher));
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 | function generateBundles(prefix, ext) {
|
79 | return _.reduce(['a-d', 'e-h', 'i-l', 'm-p', 'q-t', 'u-z'], (bundles, matcher) => {
|
80 | bundles[`${prefix}-${matcher}.${ext}`] = `**/[${matcher}]*.${ext}`;
|
81 | return bundles;
|
82 | }, {});
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function hasChanged(stream, sourceFile, targetPath) {
|
95 | return fs.stat(targetPath).then((targetStat) => {
|
96 | if (sourceFile.stat && sourceFile.stat.ctime > targetStat.ctime) {
|
97 | stream.push(sourceFile);
|
98 | }
|
99 | }).catch(() => {
|
100 |
|
101 | stream.push(sourceFile);
|
102 | });
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function transformPath(prefix, destPath, shouldMinify) {
|
113 | return (filepath) => {
|
114 | if (shouldMinify) {
|
115 |
|
116 | const name = _.head(path.basename(filepath).toLowerCase().split('.'));
|
117 |
|
118 | return path.join(destPath, `${prefix}-${bucket(name)}.js`);
|
119 | } else {
|
120 |
|
121 | return filepath;
|
122 | }
|
123 | };
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function determinePostCSSPlugins(argv) {
|
135 | const configPlugins = configFile.getConfigValue('plugins');
|
136 |
|
137 | if (configPlugins) {
|
138 | if (!Array.isArray(configPlugins)) {
|
139 | console.error(`${chalk.red('Error: Plugins supplied in config file is not an array')}`);
|
140 | }
|
141 |
|
142 |
|
143 | return configPlugins;
|
144 | } else {
|
145 | return _.map(argv.plugins, (pluginName) => {
|
146 | const plugin = amphoraFs.tryRequire(pluginName);
|
147 |
|
148 |
|
149 | if (!plugin) throw new Error(`${chalk.red(`Error: Cannot find plugin "${pluginName}"`)}`);
|
150 |
|
151 | try {
|
152 | return plugin();
|
153 | } catch (e) {
|
154 | console.error(`${chalk.red(`Error: Cannot init plugin "${pluginName}"`)}\n${chalk.grey(e.message)}`);
|
155 | }
|
156 | });
|
157 | }
|
158 | }
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | function getConfigFileOrBrowsersList(key) {
|
168 | const configFileValue = configFile.getConfigValue(key);
|
169 |
|
170 | return configFileValue ? configFileValue : module.exports.browserslist;
|
171 | }
|
172 |
|
173 | module.exports.time = time;
|
174 | module.exports.debouncedWatcher = _.debounce(watcher, 200);
|
175 | module.exports.bucket = bucket;
|
176 | module.exports.unbucket = unbucket;
|
177 | module.exports.generateBundles = generateBundles;
|
178 | module.exports.hasChanged = hasChanged;
|
179 | module.exports.transformPath = transformPath;
|
180 | module.exports.browserslist = { browsers: ['> 3%', 'not and_uc > 0'] };
|
181 | module.exports.determinePostCSSPlugins = determinePostCSSPlugins;
|
182 | module.exports.getConfigFileOrBrowsersList = getConfigFileOrBrowsersList;
|
183 |
|
184 |
|
185 | module.exports.watcher = watcher;
|