/
utils.ts
485 lines (409 loc) Β· 13.1 KB
/
utils.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import archiver from 'archiver';
import chalk from 'chalk';
import { ProgressData } from 'cpy';
import figures from 'figures';
import findUp from 'find-up';
import gradient from 'gradient-string';
import logSymbols from 'log-symbols';
import path from 'path';
import PrettyError from 'pretty-error';
import webpack from 'webpack';
import { WpackioError } from '../errors/WpackioError';
import { ArchiveResolve } from '../scripts/Pack';
const pkgPath = path.resolve(__dirname, '../../package.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const pkg = require(pkgPath);
export const donationLink = chalk.bold.blue.underline(
'https://opencollective.com/wpackio'
);
let isYarnCache: boolean | null = null;
export const wpackLogo = `${chalk.bold(gradient.instagram(`__ __`))}
${chalk.bold(gradient.instagram(`\\ \\ /\\ / /`))} ${chalk.bold(
gradient.instagram('PACK.IO')
)}
${chalk.bold(gradient.instagram(` \\ V V /`))} ${chalk.bold.yellowBright(
'JavaScript'
)} tooling for ${chalk.bold.blueBright('WordPress')}
${chalk.bold(gradient.instagram(` \\_/\\_/`))} ${chalk.magenta(
'v'
)}${chalk.bold.magenta(`${pkg.version}`)}`;
export const wpackLogoSmall = gradient.instagram('wpack.io');
export const donationSentence = `If ${wpackLogoSmall} is worthy, π consider donating.
${donationLink}
Every amount counts.`;
export function addTimeStampToLog(log: string): string {
const time = new Date().toTimeString().split(' ')[0];
return `${chalk.dim(`ο½’${wpackLogoSmall} ${time}ο½£`)} ${log}`;
}
export const watchSymbol = `${logSymbols.info}`;
export const watchEllipsis = chalk.dim(figures.ellipsis);
export function printWatchingMessage() {
console.log(
addTimeStampToLog(`${watchSymbol} watching for changes${watchEllipsis}`)
);
}
export function printCompilingMessage() {
console.log(
addTimeStampToLog(
`${logSymbols.info} compiling changes${watchEllipsis}`
)
);
}
export function printCompileTimeMessage(stat: any, lastStat: any | null) {
// safety
if (!stat || typeof stat !== 'object' || !stat.builtAt) {
return;
}
if (
lastStat === null ||
typeof lastStat !== 'object' ||
!lastStat.builtAt ||
stat.builtAt !== lastStat.builtAt
) {
// get bundle name
let name = '';
if (stat.name) {
const entryName = stat.name;
name = chalk.green(entryName);
if (stat.errors.length) {
name = chalk.red(entryName);
} else if (stat.warnings.length) {
name = chalk.yellow(entryName);
}
name = `${name} `;
}
// get log symbol
let symbol = logSymbols.success;
if (stat.errors.length) {
symbol = logSymbols.error;
} else if (stat.warnings.length) {
symbol = logSymbols.warning;
}
// get log message
let msg = 'was rebuilt in';
if (lastStat === null) {
msg = 'was built in';
}
console.log(
addTimeStampToLog(
`${symbol} bundle ${name}${msg} ${chalk.magenta(
`${stat.time}ms`
)}.`
)
);
}
}
export const webpackStatToJsonOptions: webpack.Stats.ToJsonOptions = {
timings: true,
assets: false,
builtAt: true,
cached: false,
cachedAssets: false,
children: true,
chunkModules: false,
chunkOrigins: false,
chunks: false,
entrypoints: false,
env: false,
errors: true,
warnings: true,
modules: false,
publicPath: false,
depth: false,
};
export function printCompileTimeMessages(
stats: webpack.Stats,
lastStatsJson: any | null
) {
let statsJson: any | null = null;
if (stats) {
statsJson = stats.toJson(webpackStatToJsonOptions);
}
if (
statsJson !== null &&
!statsJson.time &&
statsJson.children &&
Array.isArray(statsJson.children)
) {
// if it is multicompiler
statsJson.children.forEach((sj: any, index: number) => {
if (lastStatsJson === null) {
printCompileTimeMessage(sj, null);
} else if (
lastStatsJson !== null &&
lastStatsJson.children &&
Array.isArray(lastStatsJson.children)
) {
printCompileTimeMessage(sj, lastStatsJson.children[index]);
}
});
} else if (statsJson.time) {
// if it is single compiler
if (lastStatsJson === null) {
printCompileTimeMessage(statsJson, null);
} else {
printCompileTimeMessage(statsJson, lastStatsJson);
}
}
}
export function printSuccessfullyCompiledMessage() {
console.log(
addTimeStampToLog(`${logSymbols.success} compiled successfully`)
);
}
export function printCompiledWithWarnMessage() {
console.log(
addTimeStampToLog(
`${logSymbols.warning} ${chalk.dim('compiled with warnings...')}`
)
);
}
export function printFailedCompileMEssage() {
console.error(
addTimeStampToLog(
`${logSymbols.error} ${chalk.dim('failed to compile')}`
)
);
}
export function printGeneralInfoMessage(msg: string, symbol?: string) {
if (typeof symbol === 'undefined') {
// eslint-disable-next-line no-param-reassign
symbol = logSymbols.info;
}
console.info(addTimeStampToLog(`${symbol} ${msg}`));
}
export function printErrorHeading(msg: string = 'ERROR'): void {
console.log('');
console.log(`${chalk.bgRed.black(` ${msg} `)} please review`);
console.log('');
}
export function printWarningHeading(msg: string = 'WARNING'): void {
console.log('');
console.log(`${chalk.bgYellow.black(` ${msg} `)} please review`);
console.log('');
}
export function printSuccessHeading(msg: string = 'OUTPUT'): void {
console.log('');
console.log(
`${chalk.bgGreenBright(chalk.bold.hex('#000000')(` ${msg} `))}`
);
console.log('');
}
export const bulletSymbol = chalk.magenta(figures.pointer);
export const wpackLink = `${chalk.blue.underline('https://wpack.io')}`;
export const wpackIntro = `${wpackLogo}\n`;
export function isYarn(): boolean {
const cwd = process.cwd();
if (isYarnCache !== null) {
return isYarnCache;
}
try {
isYarnCache = findUp.sync('yarn.lock', { cwd }) != null;
return isYarnCache;
} catch (_) {
isYarnCache = false;
return isYarnCache;
}
}
export const contextHelp: string = `Path to context or project root directory. Defaults to current working directory. It is recommended to use absolute path, else it is calculated from current working directory. The path you mention here should be what the URL 'localhost/wp-content/<themes|plugins>/<slug>/' map to. In most cases, you should leave it, because calling the program from npm or yarn script should automatically set it.`;
export function printIntro(): void {
console.log(wpackIntro);
}
/**
* Resolve `cwd`, a.k.a, current working directory or context from user input.
* It takes into account the `--context [path]` option from CLI and uses process
* cwd, if not provided.
*
* @param options Options as received from CLI
*/
export function resolveCWD(
options: { context?: string | undefined } | undefined
): string {
let cwd = process.cwd();
// If user has provided cwd, then use that instead
if (options && options.context) {
const { context } = options;
if (path.isAbsolute(options.context)) {
cwd = context;
} else {
cwd = path.resolve(cwd, context);
}
}
return cwd;
}
export function serverInfo(url: string, uiUrl: string | boolean): void {
const msg = `${logSymbols.success} ${wpackLogoSmall} server is running at.
${bulletSymbol} ${chalk.blue.underline(url)}
and BrowserSync UI running at
${bulletSymbol} ${
typeof uiUrl === 'string'
? chalk.blue.underline(uiUrl)
: chalk.red('N/A')
}
Press ${chalk.yellow('r')} to recompile and ${chalk.yellow('q')} to force quit.
To create production build, run
${bulletSymbol} ${chalk.yellow(isYarn() ? 'yarn build' : 'npm run build')}
${chalk.dim('No files are written on disk during development mode.')}`;
console.log(msg);
}
export function endServeInfo(): void {
const msg = `${
logSymbols.success
} ${wpackLogoSmall} server has been ${chalk.redBright('stopped')}.
To create production build, run
${bulletSymbol} ${chalk.yellow(isYarn() ? 'yarn build' : 'npm run build')}.
Thank you for using ${wpackLink}.
To spread the ${chalk.red(figures.heart)} please ${chalk.yellowBright(
figures.star
)} our repo and tweet.
${donationSentence}`;
console.log(msg);
}
export function endBuildInfo(): void {
const msg = `${wpackLogoSmall} production build was ${chalk.green(
'successful'
)}.
All files were written to disk and you can visit your local server.
If your filesize is too large, remember you can use advanced
dynamic import and multiple entry-points easily with ${wpackLogoSmall}.
${bulletSymbol} For more info, visit: ${wpackLink}.
To spread the ${chalk.red(figures.heart)} please ${chalk.yellowBright(
figures.star
)} our repo and tweet.
${donationSentence}`;
console.log(msg);
}
export function endBootstrapInfo(): void {
const msg = `${wpackLogoSmall} was ${chalk.green(
'successfully'
)} integrated within your project.
If this is your first run edit your ${chalk.bold.magenta(
'wpackio.project.js'
)} file and put
entrypoints. You will find examples within the file itself.
You should keep ${chalk.bold.yellow(
'wpackio.server.js'
)} outside your VCS tracking
as it will most likely differ for different users.
You can run ${chalk.dim('bootstrap')} command again and it will create the
${chalk.bold.yellow('wpackio.server.js')} file if not present.
${bulletSymbol} Start Development: ${chalk.yellow(
isYarn() ? 'yarn start' : 'npm start'
)}.
${bulletSymbol} Production Build: ${chalk.yellow(
isYarn() ? 'yarn build' : 'npm run build'
)}.
${bulletSymbol} Create local server config: ${chalk.yellow(
isYarn() ? 'yarn bootstrap' : 'npm run bootstrap'
)}.
${bulletSymbol} Create distributable zip: ${chalk.yellow(
isYarn() ? 'yarn archive' : 'npm run archive'
)}.
${bulletSymbol} For more info, visit: ${wpackLink}.
To enqueue the assets within your plugin or theme, make sure you
have ${chalk.yellow('wpackio/enqueue')} package from packagist.org/composer
and follow the intructions from documentation. To install now, run
${bulletSymbol} ${chalk.yellow('composer require wpackio/enqueue')}.
To spread the ${chalk.red(figures.heart)} please ${chalk.yellowBright(
figures.star
)} our repo and tweet.`;
console.log(msg);
}
export function prettyPrintError(
e: Error | WpackioError,
errorMsg: string
): void {
const errorPrefix = ` ${chalk.dim.red(figures.pointer)} `;
console.log(chalk.dim('='.repeat(errorMsg.length + 2)));
console.log(`${logSymbols.error} ${errorMsg}`);
console.log(chalk.dim('='.repeat(errorMsg.length + 2)));
console.log('');
if (e instanceof WpackioError) {
console.log(chalk.bgRed.black(' please review the following errors '));
console.log('');
console.error(
errorPrefix +
e.message
.split('\n')
.reduce((acc, line) => `${acc}\n${errorPrefix}${line}`)
);
} else {
const pe = new PrettyError();
console.error(pe.render(e));
}
console.log('\n\n\n');
}
export function getProgressBar(done: number): string {
if (Number.isNaN(done) || done === Infinity || done === -Infinity) {
// eslint-disable-next-line no-param-reassign
done = 0;
}
const pbDoneLength = Math.floor((done / 100) * 20);
let gFunc = gradient('red', 'red');
if (pbDoneLength >= 5) {
gFunc = gradient('red', 'red', 'yellow');
}
if (pbDoneLength >= 10) {
gFunc = gradient('red', 'red', 'yellow', 'yellow');
}
if (pbDoneLength >= 15) {
gFunc = gradient('red', 'red', 'yellow', 'yellow', 'green');
}
const pbDone = gFunc('='.repeat(pbDoneLength));
const pbDoing = chalk.gray('-'.repeat(20 - pbDoneLength));
return `[${pbDone}${pbDoing}] ${chalk.yellow(done.toFixed(2))}%`;
}
export function getFileCopyProgress(progress?: ProgressData): string {
let done = 0;
let totalFiles = 0;
let filesDone = 0;
let size = 0;
if (progress) {
done = Math.round(
(progress.completedFiles / progress.totalFiles) * 100
);
// eslint-disable-next-line prefer-destructuring
totalFiles = progress.totalFiles;
filesDone = progress.completedFiles;
size = progress.completedSize;
}
return `copying files ${getProgressBar(done)} ${chalk.magenta(
filesDone.toString()
)}${chalk.dim('/')}${chalk.cyan(totalFiles.toString())} Files ${chalk.blue(
(size / 1024).toFixed(2)
)} KB`;
}
export function getZipProgress(data?: archiver.ProgressData): string {
let entriesTotal = 0;
let entriesProcessed = 0;
let bytesTotal = 0;
let bytesProcessed = 0;
if (data) {
entriesTotal = data.entries.total;
entriesProcessed = data.entries.processed;
bytesTotal = data.fs.totalBytes;
bytesProcessed = data.fs.processedBytes;
}
const done = Math.round((entriesProcessed / entriesTotal) * 100);
return `creating zip ${getProgressBar(done)} ${chalk.magenta(
entriesProcessed.toString()
)}${chalk.dim('/')}${chalk.cyan(
entriesTotal.toString()
)} Files ${chalk.blue((bytesProcessed / 1024).toFixed(2))}${chalk.dim(
'/'
)}${chalk.cyan((bytesTotal / 1024).toFixed(2))} KB`;
}
export function endPackInfo(results: ArchiveResolve): void {
const msg = `${chalk.bgGreenBright(chalk.bold.hex('#000000')(' OUTPUT '))}
${bulletSymbol} Zip Location: ${chalk.blue(results.relPath)}.
${bulletSymbol} File Size: ${chalk.blue(
(results.size / 1024).toFixed(2)
)} KB.
${wpackLogoSmall} package and archive was ${chalk.green('successful')}.
Thank you for using ${wpackLink}.
To spread the ${chalk.red(figures.heart)} please ${chalk.yellowBright(
figures.star
)} our repo and tweet.
${donationSentence}`;
console.log(msg);
}