forked from prettier/pretty-quick
/
index.js
68 lines (59 loc) · 1.72 KB
/
index.js
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
import scms from './scms';
import formatFiles from './formatFiles';
import createIgnorer from './createIgnorer';
import isSupportedExtension from './isSupportedExtension';
export default (
currentDirectory,
{
config,
since,
staged,
restage = true,
branch,
verbose,
onFoundSinceRevision,
onFoundChangedFiles,
onPartiallyStagedFile,
onWriteFile,
onExamineFile,
} = {}
) => {
const scm = scms(currentDirectory);
if (!scm) {
throw new Error('Unable to detect a source control manager.');
}
const directory = scm.rootDirectory;
const revision = since || scm.getSinceRevision(directory, { staged, branch });
onFoundSinceRevision && onFoundSinceRevision(scm.name, revision);
const rootIgnorer = createIgnorer(directory);
const cwdIgnorer =
process.cwd() !== directory ? createIgnorer(process.cwd()) : () => true;
const changedFiles = scm
.getChangedFiles(directory, revision, staged)
.filter(isSupportedExtension)
.filter(rootIgnorer)
.filter(cwdIgnorer);
const unstagedFiles = staged
? scm
.getUnstagedChangedFiles(directory, revision)
.filter(isSupportedExtension)
.filter(rootIgnorer)
.filter(cwdIgnorer)
: [];
const wasFullyStaged = f => unstagedFiles.indexOf(f) < 0;
onFoundChangedFiles && onFoundChangedFiles(changedFiles);
formatFiles(directory, changedFiles, {
config,
onWriteFile: file => {
onWriteFile && onWriteFile(file);
if (staged && restage) {
if (wasFullyStaged(file)) {
scm.stageFile(directory, file);
} else {
onPartiallyStagedFile && onPartiallyStagedFile(file);
}
}
},
onExamineFile: verbose && onExamineFile,
});
};