Skip to content

Commit

Permalink
feat: add --staged flag
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Jan 8, 2018
1 parent 34995ed commit 695c6a5
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,25 @@ yarn add --dev husky
In `package.json`'s `"scripts"` section, add:

```
"precommit": "pretty-quick --since HEAD"
"precommit": "pretty-quick --staged"
```

## CLI Flags

### `--staged`

## CLI Flags
Pre-commit mode. Under this flag only staged files will be formatted, and they will be re-staged after formatting.

<!-- Undocumented = Unsupported :D
### `--add`
Restage files that are formatted.
### `--since`
A SCM revision such as a git commit hash or ref.
For example `pretty-quick --since HEAD` will format only staged files.
-->
4 changes: 3 additions & 1 deletion bin/pretty-quick.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ prettyQuick(
console.log(
`🔍 Finding changed files since ${chalk.bold(
scm
)} revision ${chalk.bold(revision)}`
)} revision ${chalk.bold(revision)}.`
);
},

onFoundChangedFiles: changedFiles => {
console.log(
`🎯 Found ${changedFiles.length} ${
changedFiles.length === 1 ? 'file' : 'files'
} changed.`
);
},

onWriteFile: file => {
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test": "jest",
"lint": "eslint . --ignore-path=.gitignore",
"semantic-release": "semantic-release",
"precommit": "pretty-quick --since HEAD"
"precommit": "./bin/pretty-quick.js --staged"
},
"peerDependencies": {
"prettier": ">=1.8.0"
Expand Down
2 changes: 1 addition & 1 deletion src/formatFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default (files, { config, onWriteFile }) => {
);

if (output !== input) {
onWriteFile && onWriteFile(file);
writeFileSync(file, output);
onWriteFile && onWriteFile(file);
}
}
};
19 changes: 16 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ import isSupportedExtension from './isSupportedExtension';

export default (
directory,
{ since, config, onFoundSinceRevision, onFoundChangedFiles, onWriteFile }
{
config,
since,
staged,
onFoundSinceRevision,
onFoundChangedFiles,
onWriteFile,
}
) => {
const scm = scms(directory);
if (!scm) {
throw new Error('Unable to detect a source control manager.');
}

const revision = since || scm.getSinceRevision(directory);
const revision = since || scm.getSinceRevision(directory, { staged });

onFoundSinceRevision && onFoundSinceRevision(scm.name, revision);

Expand All @@ -26,5 +33,11 @@ export default (

onFoundChangedFiles && onFoundChangedFiles(changedFiles);

formatFiles(changedFiles, { config, onWriteFile });
formatFiles(changedFiles, {
config,
onWriteFile: file => {
onWriteFile(file);
scm.stageFile(directory, file);
},
});
};
9 changes: 8 additions & 1 deletion src/scms/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ const runGit = (directory, args) =>

const getLines = execaResult => execaResult.stdout.split('\n');

export const getSinceRevision = directory => {
export const getSinceRevision = (directory, { staged }) => {
if (staged) {
return 'HEAD';
}
return runGit(directory, ['merge-base', 'HEAD', 'master']).stdout.trim();
};

Expand All @@ -38,3 +41,7 @@ export const getChangedFiles = (directory, revision) => {
),
].filter(Boolean);
};

export const stageFile = (directory, file) => {
runGit(directory, ['add', file]);
};

0 comments on commit 695c6a5

Please sign in to comment.