Skip to content

Commit

Permalink
feature(putout) add --staged
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed May 3, 2020
1 parent 0319f81 commit ebf0ecb
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 129 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ Options:
-h, --help display this help and exit
-v, --version output version information and exit
-f, --format use a specific output format - default: dump
-m, --modified add "modified" files when in git repository
-u, --untracked add "untracked" files when in git repository
-a, --added add "added" files when in git repository
-r, --renamed add "renamed" files when in git repository
-s, --staged add staged files when in git repository
--fix aply found fixes of errors to code
--fix-count count of fixes rounds (defaults to 10)
--rulesdir specify custom rulesdir
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
"husky": {
"hooks": {
"pre-commit": "putout -ar"
"pre-commit": "putout --staged"
}
},
"nyc": {
Expand Down
5 changes: 1 addition & 4 deletions packages/putout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ Options:
-h, --help display this help and exit
-v, --version output version information and exit
-f, --format use a specific output format - default: dump
-m, --modified add "modified" files when in git repository
-u, --untracked add "untracked" files when in git repository
-a, --added add "added" files when in git repository
-r, --renamed add "renamed" files when in git repository
-s, --staged add staged files when in git repository
--fix aply found fixes of errors to code
--fix-count count of fixes rounds (defaults to 10)
--rulesdir specify custom rulesdir
Expand Down
5 changes: 1 addition & 4 deletions packages/putout/help.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
"-h, --help ": "display this help and exit",
"-v, --version ": "output version information and exit",
"-f, --format ": "use a specific output format - default: dump",
"-m, --modified ": "add \"modified\" files when in git repository",
"-u, --untracked ": "add \"untracked\" files when in git repository",
"-a, --added ": "add \"added\" files when in git repository",
"-r, --renamed ": "add \"renamed\" files when in git repository",
"-s, --staged" : "add staged files when in git repository",
"--fix ": "apply fixes of errors to code",
"--fix-count ": "count of fixes rounds (defaults to 10)",
"--rulesdir ": "specify custom rulesdir",
Expand Down
30 changes: 0 additions & 30 deletions packages/putout/lib/cli/get-git-names.js

This file was deleted.

54 changes: 0 additions & 54 deletions packages/putout/lib/cli/get-git-names.spec.js

This file was deleted.

38 changes: 14 additions & 24 deletions packages/putout/lib/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const isString = (a) => typeof a === 'string';
const isStringAll = (...a) => a.filter(isString).length;
const isRuler = (a) => a.disableAll || a.enableAll || isStringAll(a.disable, a.enable);

module.exports = ({argv, halt, log, write, logError}) => {
module.exports = async ({argv, halt, log, write, logError}) => {
const args = yargsParser(argv, {
boolean: [
'cache',
Expand All @@ -26,11 +26,8 @@ module.exports = ({argv, halt, log, write, logError}) => {
'disable-all',
'jsx',
'flow',
'added',
'renamed',
'modified',
'untracked',
'options',
'staged',
],
number: [
'fixCount',
Expand All @@ -47,10 +44,7 @@ module.exports = ({argv, halt, log, write, logError}) => {
h: 'help',
c: 'config',
f: 'format',
a: 'added',
r: 'renamed',
m: 'modified',
u: 'untracked',
s: 'staged',
},
default: {
fix: false,
Expand All @@ -73,10 +67,7 @@ module.exports = ({argv, halt, log, write, logError}) => {
disableAll,
enable,
enableAll,
added,
renamed,
modified,
untracked,
staged,
updateCache,
removeCache,
} = args;
Expand All @@ -87,17 +78,11 @@ module.exports = ({argv, halt, log, write, logError}) => {
logError,
});

let gitNames = [];
let stagedNames = [];

if (untracked || added || modified || renamed) {
const getGitNames = require('./get-git-names');

gitNames = getGitNames({
untracked,
added,
modified,
renamed,
});
if (staged) {
const {get} = require('./staged');
stagedNames = await get();
}

if (args.version) {
Expand All @@ -112,7 +97,7 @@ module.exports = ({argv, halt, log, write, logError}) => {
}

const globFiles = [
...gitNames,
...stagedNames,
...args._.map(String),
];

Expand Down Expand Up @@ -174,6 +159,11 @@ module.exports = ({argv, halt, log, write, logError}) => {
return exit();
}

if (fix && staged) {
const {set} = require('./staged');
await set();
}

if (mergedPlaces.length)
return exit(1);
};
Expand Down
64 changes: 57 additions & 7 deletions packages/putout/lib/cli/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,64 @@ test('putout: cli: no files', (t) => {
t.end();
});

test('putout: cli: get git names', (t) => {
test('putout: cli: --fix --staged', async (t) => {
const name = './xxx.js';
const logError = stub();
const getGitNames = stub().returns(['./xxx.js']);
const get = stub().returns([
name,
]);
const set = stub();
const argv = [
'--untracked',
'--staged',
'--fix',
];

mockRequire('./get-git-names', getGitNames);
const getFiles = stub().returns([null, [
name,
]]);

const process = stub().returns([]);
const processFile = stub().returns(process);

mockRequire('./get-files', getFiles);
mockRequire('./process-file', processFile);

mockRequire('./staged', {
get,
set,
});

const cli = reRequire('.');

runCli({
await runCli({
cli,
argv,
logError,
});

stopAll();

t.ok(set.calledWith());
t.end();
});

test('putout: cli: --staged --fix', async (t) => {
const logError = stub();
const get = stub().returns(['./xxx.js']);
const set = stub();
const argv = [
'--staged',
'--fix',
];

mockRequire('./staged', {
get,
set,
});

const cli = reRequire('.');

await runCli({
cli,
argv,
logError,
Expand All @@ -145,6 +191,8 @@ test('putout: cli: get git names', (t) => {
const output = stripAnsi(arg);
const message = 'No files matching the pattern "./xxx.js" were found';

stopAll();

t.equal(output, message, 'should equal');
t.end();
});
Expand All @@ -168,11 +216,13 @@ test('putout: cli: ruler processor', (t) => {
logError,
});

stopAll();

t.ok(rullerProcessor.called);
t.end();
});

function runCli(options) {
async function runCli(options) {
const {
halt = stub(),
log = stub(),
Expand All @@ -182,7 +232,7 @@ function runCli(options) {
cli = _cli,
} = options;

cli({
await cli({
write,
halt,
log,
Expand Down
Loading

0 comments on commit ebf0ecb

Please sign in to comment.