1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const ora = require('ora');
|
9 | const mri = require('mri');
|
10 | const glob = require('glob');
|
11 |
|
12 | const LIBRARY_NAME = require('../package.json').name;
|
13 | const main = require('../lib').main;
|
14 |
|
15 | const config = mri(process.argv.slice(2));
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | let filesWhitelist = null;
|
22 | if (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 |
|
43 |
|
44 |
|
45 | if (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 |
|
55 |
|
56 | config.head = 'HEAD';
|
57 | }
|
58 | }
|
59 |
|
60 | const 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 |
|
68 | const primarySpinner = ora(` Running ${LIBRARY_NAME}...`);
|
69 | const modifiedFilesSpinner = ora(' Detecting modified files from git...');
|
70 | const spinnersByFilename = {};
|
71 |
|
72 | let shouldErrorOut = false;
|
73 |
|
74 | main(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 |
|
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 | });
|