UNPKG

3.92 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3'use strict';
4
5/**
6 * CLI output
7 */
8const ora = require('ora');
9const mri = require('mri');
10const glob = require('glob');
11
12const LIBRARY_NAME = require('../package.json').name;
13const main = require('../lib').main;
14
15const config = mri(process.argv.slice(2));
16
17/**
18 * If the user provided one or more glob patterns to match against, ensure that there are
19 * applicable files available
20 */
21let filesWhitelist = null;
22if (config.whitelist) {
23 filesWhitelist = [];
24 if (Array.isArray(config.whitelist)) {
25 config.whitelist.forEach(pattern => {
26 filesWhitelist = [...filesWhitelist, ...glob.sync(config.whitelist)];
27 });
28 } else {
29 filesWhitelist = glob.sync(config.whitelist);
30 }
31 if (!filesWhitelist || !filesWhitelist.length) {
32 console.error(
33 `Error: No files match the glob pattern(s) you provided for --whitelist -> "${
34 config.pattern
35 }"`
36 );
37 return process.exit(1);
38 }
39}
40
41/**
42 * If the user specifies at least one SHA, perform some validation and
43 * apply some defaults
44 */
45if (config.base || config.head) {
46 if (!config.base) {
47 console.error(
48 `Error: When giving a value of --head, you must also give a value for --base`
49 );
50 return process.exit(1);
51 }
52 if (!config.head) {
53 /**
54 * If the user only specified `--base`, set the value of `--head` to be "HEAD"
55 */
56 config.head = 'HEAD';
57 }
58}
59
60const options = {
61 checkOnly: config['check-only'] || false,
62 filesWhitelist,
63 base: config.base || null,
64 head: config.head || null,
65 formatter: config.formatter || 'prettier',
66};
67
68const primarySpinner = ora(` Running ${LIBRARY_NAME}...`);
69const modifiedFilesSpinner = ora(' Detecting modified files from git...');
70const spinnersByFilename = {};
71
72let shouldErrorOut = false;
73
74main(process.cwd(), options, {
75 onInit(workingDirectory) {
76 primarySpinner.start();
77 modifiedFilesSpinner.start();
78 },
79 onModifiedFilesDetected(modifiedFilenames) {
80 if (!modifiedFilenames || !modifiedFilenames.length) {
81 return;
82 }
83 modifiedFilesSpinner.succeed(
84 ` ${LIBRARY_NAME}: ${modifiedFilenames.length} modified file(s) found`
85 );
86 },
87 onBegunProcessingFile(filename, index, totalFiles) {
88 spinnersByFilename[filename] = ora()
89 .start()
90 .succeed(` [${index + 1}/${totalFiles}] Processing file: ${filename}`);
91 },
92 onFinishedProcessingFile(filename, index, status) {
93 const spinner = spinnersByFilename[filename];
94 switch (status) {
95 case 'UPDATED':
96 spinner.succeed(` --> Updated formatting in: ${filename}`);
97 break;
98 case 'NOT_UPDATED':
99 spinner.info(
100 ` --> No formatting changes required in: ${filename}`
101 );
102 break;
103 case 'INVALID_FORMATTING':
104 /**
105 * If --check-only is passed as a CLI argument, the script will error out.
106 */
107 if (options.checkOnly) {
108 shouldErrorOut = true;
109 }
110 spinner.fail(` --> Invalid formatting detected in: ${filename}`);
111 break;
112 }
113 },
114 onError(err) {
115 modifiedFilesSpinner.fail(` ${LIBRARY_NAME}: An Error occurred\n`);
116 console.error(err);
117 console.log('\n');
118 primarySpinner.stop();
119 return process.exit(1);
120 },
121 onComplete(totalFiles) {
122 if (!totalFiles) {
123 modifiedFilesSpinner.info(` ${LIBRARY_NAME}: No matching modified files detected.
124
125 --> If you feel that one or more files should be showing up here, be sure to first check what file extensions prettier supports, and whether or not you have included those files in a .prettierignore file
126
127 `);
128 primarySpinner.stop();
129 return process.exit(shouldErrorOut ? 1 : 0);
130 }
131 if (options.checkOnly) {
132 primarySpinner.succeed(' Checks complete 🎉');
133 } else {
134 primarySpinner.succeed(' Formatting complete 🎉');
135 }
136 return process.exit(shouldErrorOut ? 1 : 0);
137 },
138});