|
6 | 6 | import glob from 'glob'; |
7 | 7 | import prettier from 'prettier'; |
8 | 8 | import fs from 'fs'; |
9 | | -import os from 'os'; |
10 | 9 | import path from 'path'; |
11 | 10 | import listChangedFiles from './listChangedFiles'; |
12 | 11 |
|
13 | 12 | const prettierConfigPath = path.join(__dirname, '../prettier.config.js'); |
14 | 13 |
|
15 | | -const mode = process.argv[2] || 'check'; |
| 14 | +const mode = process.argv[2] || 'write-changed'; |
16 | 15 | const shouldWrite = mode === 'write' || mode === 'write-changed'; |
17 | 16 | const onlyChanged = mode === 'check-changed' || mode === 'write-changed'; |
18 | 17 |
|
19 | | -const changedFiles = onlyChanged ? listChangedFiles() : null; |
20 | | -let didWarn = false; |
21 | | -let didError = false; |
| 18 | +function mapEslintEntriesToGlobs(file) { |
| 19 | + let prefix = ''; |
| 20 | + let hasSlashPrefix = false; |
| 21 | + let suffix = ''; |
22 | 22 |
|
23 | | -const ignoredFiles = fs |
24 | | - .readFileSync('.eslintignore', 'utf-8') |
25 | | - .split(os.EOL) |
26 | | - .filter(notEmpty => notEmpty) |
27 | | - .map(file => `**/${file}/**`); |
28 | | - |
29 | | -const files = glob |
30 | | - .sync('**/*.{js,tsx,d.ts}', { ignore: ['**/node_modules/**', ...ignoredFiles] }) |
31 | | - .filter(f => !onlyChanged || changedFiles.has(f)); |
| 23 | + if (file.charAt(0) === '/') { |
| 24 | + hasSlashPrefix = true; |
| 25 | + } else { |
| 26 | + prefix = '**/'; |
| 27 | + } |
32 | 28 |
|
33 | | -if (!files.length) { |
34 | | - process.exit(0); |
| 29 | + if (!path.extname(file)) { |
| 30 | + suffix = '/**'; |
| 31 | + } |
| 32 | + console.log(`${prefix}${hasSlashPrefix ? file.substring(1) : file}${suffix}`); |
| 33 | + return `${prefix}${hasSlashPrefix ? file.substring(1) : file}${suffix}`; |
35 | 34 | } |
36 | 35 |
|
37 | | -files.forEach(file => { |
38 | | - const options = prettier.resolveConfig.sync(file, { |
39 | | - config: prettierConfigPath, |
40 | | - }); |
41 | | - try { |
42 | | - const input = fs.readFileSync(file, 'utf8'); |
43 | | - if (shouldWrite) { |
44 | | - console.log(`Formatting ${file}`); |
45 | | - const output = prettier.format(input, { ...options, filepath: file }); |
46 | | - if (output !== input) { |
47 | | - fs.writeFileSync(file, output, 'utf8'); |
48 | | - } |
49 | | - } else if (!prettier.check(input, { ...options, filepath: file })) { |
50 | | - if (!didWarn) { |
51 | | - console.log(`\nThis project uses prettier to format all JavaScript code. |
52 | | - Please run 'yarn prettier-all' and add changes to files listed |
53 | | - below to your commit:\n\n`); |
54 | | - didWarn = true; |
| 36 | +function runPrettier(changedFiles) { |
| 37 | + let didWarn = false; |
| 38 | + let didError = false; |
| 39 | + |
| 40 | + const warnedFiles = []; |
| 41 | + const ignoredFiles = fs |
| 42 | + .readFileSync('.eslintignore', 'utf-8') |
| 43 | + .split(/\r*\n/) |
| 44 | + .filter(notEmpty => notEmpty) |
| 45 | + .map(file => mapEslintEntriesToGlobs(file)); |
| 46 | + |
| 47 | + const files = glob |
| 48 | + .sync('**/*.{js,tsx,d.ts}', { ignore: ['**/node_modules/**', ...ignoredFiles] }) |
| 49 | + .filter(f => !changedFiles || changedFiles.has(f)); |
| 50 | + |
| 51 | + if (!files.length) { |
| 52 | + process.exit(0); |
| 53 | + } |
| 54 | + |
| 55 | + files.forEach(file => { |
| 56 | + const options = prettier.resolveConfig.sync(file, { |
| 57 | + config: prettierConfigPath, |
| 58 | + }); |
| 59 | + try { |
| 60 | + const input = fs.readFileSync(file, 'utf8'); |
| 61 | + if (shouldWrite) { |
| 62 | + console.log(`Formatting ${file}`); |
| 63 | + const output = prettier.format(input, { ...options, filepath: file }); |
| 64 | + if (output !== input) { |
| 65 | + fs.writeFileSync(file, output, 'utf8'); |
| 66 | + } |
| 67 | + } else { |
| 68 | + console.log(`Checking ${file}`); |
| 69 | + if (!prettier.check(input, { ...options, filepath: file })) { |
| 70 | + warnedFiles.push(file); |
| 71 | + didWarn = true; |
| 72 | + } |
55 | 73 | } |
| 74 | + } catch (error) { |
| 75 | + didError = true; |
| 76 | + console.log(`\n\n${error.message}`); |
56 | 77 | console.log(file); |
57 | 78 | } |
58 | | - } catch (error) { |
59 | | - didError = true; |
60 | | - console.log(`\n\n${error.message}`); |
61 | | - console.log(file); |
| 79 | + }); |
| 80 | + |
| 81 | + if (didWarn) { |
| 82 | + console.log( |
| 83 | + '\n\nThis project uses prettier to format all JavaScript code.\n' + |
| 84 | + `Please run '${!changedFiles ? 'yarn prettier:all' : 'yarn prettier'}'` + |
| 85 | + 'and commit the changes to the files listed below:\n\n', |
| 86 | + ); |
| 87 | + console.log(warnedFiles.join('\n')); |
62 | 88 | } |
63 | | -}); |
64 | 89 |
|
65 | | -if (didWarn || didError) { |
66 | | - process.exit(1); |
| 90 | + if (didWarn || didError) { |
| 91 | + process.exit(1); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +if (onlyChanged) { |
| 96 | + listChangedFiles() |
| 97 | + .then(changedFiles => { |
| 98 | + runPrettier(changedFiles); |
| 99 | + }) |
| 100 | + .catch(console.error); |
| 101 | +} else { |
| 102 | + runPrettier(); |
67 | 103 | } |
0 commit comments