Skip to content

Commit 595a9f6

Browse files
committed
[test] Made more recommended changed
1 parent f65104a commit 595a9f6

File tree

4 files changed

+96
-54
lines changed

4 files changed

+96
-54
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ jobs:
5858
- *install_js
5959
- run:
6060
name: Check if yarn prettier was run
61-
command: yarn babel-node ./scripts/prettier.js check-changed
61+
command: yarn prettier check-changed
6262
- run:
6363
name: Lint
6464
command: yarn lint

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"jsonlint:files": "find . -name \"*.json\" | grep -v -f .eslintignore",
3737
"lint": "eslint . --cache && echo \"eslint: no lint errors\"",
3838
"lint:fix": "eslint . --cache --fix && echo \"eslint: no lint errors\"",
39-
"prettier": "babel-node ./scripts/prettier.js write-changed",
39+
"prettier": "babel-node ./scripts/prettier.js",
4040
"prettier:all":"babel-node ./scripts/prettier.js write",
4141
"size": "size-limit",
4242
"size:why": "size-limit --why packages/material-ui/build/index.js",

scripts/listChangedFiles.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,36 @@
22

33
// Based on similar script in React
44
// https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/scripts/shared/listChangedFiles.js
5-
import { execFileSync } from 'child_process';
5+
import { promisify } from 'util';
6+
import { execFile } from 'child_process';
67

7-
const exec = (command, args) => {
8+
const execFileAsync = promisify(execFile);
9+
10+
const exec = async (command, args) => {
811
const options = {
912
cwd: process.cwd(),
1013
env: process.env,
1114
stdio: 'pipe',
1215
encoding: 'utf-8',
1316
};
14-
return execFileSync(command, args, options);
17+
18+
const results = await execFileAsync(command, args, options);
19+
return results.stdout;
1520
};
1621

17-
const execGitCmd = args =>
18-
exec('git', args)
22+
const execGitCmd = async args => {
23+
const gitResults = await exec('git', args);
24+
return gitResults
1925
.trim()
2026
.toString()
2127
.split('\n');
28+
};
2229

23-
const listChangedFiles = () => {
24-
const mergeBase = execGitCmd(['merge-base', 'HEAD', 'master']);
25-
return new Set([
26-
...execGitCmd(['diff', '--name-only', '--diff-filter=ACMRTUB', mergeBase]),
27-
...execGitCmd(['ls-files', '--others', '--exclude-standard']),
28-
]);
30+
const listChangedFiles = async () => {
31+
const mergeBase = await execGitCmd(['rev-parse', 'origin/master']);
32+
const gitDiff = await execGitCmd(['diff', '--name-only'].concat(mergeBase));
33+
const gitLs = await execGitCmd(['ls-files', '--others', '--exclude-standard']);
34+
return new Set([...gitDiff, ...gitLs]);
2935
};
3036

3137
module.exports = listChangedFiles;

scripts/prettier.js

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,62 +6,98 @@
66
import glob from 'glob';
77
import prettier from 'prettier';
88
import fs from 'fs';
9-
import os from 'os';
109
import path from 'path';
1110
import listChangedFiles from './listChangedFiles';
1211

1312
const prettierConfigPath = path.join(__dirname, '../prettier.config.js');
1413

15-
const mode = process.argv[2] || 'check';
14+
const mode = process.argv[2] || 'write-changed';
1615
const shouldWrite = mode === 'write' || mode === 'write-changed';
1716
const onlyChanged = mode === 'check-changed' || mode === 'write-changed';
1817

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 = '';
2222

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+
}
3228

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}`;
3534
}
3635

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+
}
5573
}
74+
} catch (error) {
75+
didError = true;
76+
console.log(`\n\n${error.message}`);
5677
console.log(file);
5778
}
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'));
6288
}
63-
});
6489

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();
67103
}

0 commit comments

Comments
 (0)