Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use async and batch stage commands #151

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
19 changes: 7 additions & 12 deletions __mocks__/execa.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
const mockStream = () => ({
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this isn't actually used anymore? I think execa is mocked in every test using jest.mock anyways

once: jest.fn(),
on: jest.fn(),
removeListener: jest.fn(),
pipe: jest.fn(),
});

const mockExeca = jest.fn().mockReturnValue({
stdout: mockStream(),
stderr: mockStream(),
kill: () => {},
});
const mockExeca = jest.fn().mockReturnValue(
Promise.resolve({
stdout: '',
stderr: '',
kill: () => {},
}),
);

const mockExecaSync = jest.fn().mockReturnValue({
stdout: '',
Expand Down
39 changes: 26 additions & 13 deletions __mocks__/prettier.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
const path = require('path');

const resolveConfigMock = jest.fn().mockImplementation((file) =>
Promise.resolve({
file,
}),
);
resolveConfigMock.sync = jest.fn().mockImplementation((file) => ({ file }));

const getFileInfoMock = jest.fn().mockImplementation((file) => {
const ext = path.extname(file);
if (ext === '.js' || ext === '.md') {
return Promise.resolve({ ignored: false, inferredParser: 'babel' });
} else {
return Promise.resolve({ ignored: false, inferredParser: null });
}
});
getFileInfoMock.sync = jest.fn().mockImplementation((file) => {
const ext = path.extname(file);
if (ext === '.js' || ext === '.md') {
return { ignored: false, inferredParser: 'babel' };
} else {
return { ignored: false, inferredParser: null };
}
});

const prettierMock = {
format: jest.fn().mockImplementation((input) => 'formatted:' + input),
resolveConfig: {
sync: jest.fn().mockImplementation((file) => ({ file })),
},
getFileInfo: {
sync: jest.fn().mockImplementation((file) => {
const ext = path.extname(file);
if (ext === '.js' || ext === '.md') {
return { ignored: false, inferredParser: 'babel' };
} else {
return { ignored: false, inferredParser: null };
}
}),
},
resolveConfig: resolveConfigMock,
getFileInfo: getFileInfoMock,
};

module.exports = prettierMock;
117 changes: 64 additions & 53 deletions bin/pretty-quick.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,74 @@ const args = mri(process.argv.slice(2), {
},
});

const prettyQuickResult = prettyQuick(
process.cwd(),
Object.assign({}, args, {
onFoundSinceRevision: (scm, revision) => {
console.log(
`🔍 Finding changed files since ${chalk.bold(
scm,
)} revision ${chalk.bold(revision)}.`,
);
},
(async () => {
const prettyQuickResult = await prettyQuick(
process.cwd(),
Object.assign({}, args, {
onFoundSinceRevision: (scm, revision) => {
console.log(
`🔍 Finding changed files since ${chalk.bold(
scm,
)} revision ${chalk.bold(revision)}.`,
);
},

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

onPartiallyStagedFile: (file) => {
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`);
},
onPartiallyStagedFile: (file) => {
console.log(`✗ Found ${chalk.bold('partially')} staged file ${file}.`);
},

onWriteFile: (file) => {
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
},
onWriteFile: (file) => {
console.log(`✍️ Fixing up ${chalk.bold(file)}.`);
},

onCheckFile: (file, isFormatted) => {
if (!isFormatted) {
console.log(`⛔️ Check failed: ${chalk.bold(file)}`);
}
},
onCheckFile: (file, isFormatted) => {
if (!isFormatted) {
console.log(`⛔️ Check failed: ${chalk.bold(file)}`);
}
},

onExamineFile: (file) => {
console.log(`🔍 Examining ${chalk.bold(file)}.`);
},
}),
);
onExamineFile: (file) => {
console.log(`🔍 Examining ${chalk.bold(file)}.`);
},

if (prettyQuickResult.success) {
console.log('✅ Everything is awesome!');
} else {
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`,
);
}
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
console.log(
'✗ File had to be prettified and prettyQuick was set to bail mode.',
);
}
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
console.log(
'✗ Code style issues found in the above file(s). Forgot to run Prettier?',
);
onStageFiles: () => {
console.log(`🏗️ Staging changed files.`);
},
}),
);

if (prettyQuickResult.success) {
console.log('✅ Everything is awesome!');
} else {
if (prettyQuickResult.errors.indexOf('PARTIALLY_STAGED_FILE') !== -1) {
console.log(
'✗ Partially staged files were fixed up.' +
` ${chalk.bold('Please update stage before committing')}.`,
);
}
if (prettyQuickResult.errors.indexOf('BAIL_ON_WRITE') !== -1) {
console.log(
'✗ File had to be prettified and prettyQuick was set to bail mode.',
);
}
if (prettyQuickResult.errors.indexOf('CHECK_FAILED') !== -1) {
console.log(
'✗ Code style issues found in the above file(s). Forgot to run Prettier?',
);
}
if (prettyQuickResult.errors.indexOf('STAGE_FAILED') !== -1) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New error state due to the introduction of the "staging" step

console.log(
'✗ Failed to stage some or all of the above file(s). Please stage changes made by Prettier before committing.',
);
}
process.exit(1); // ensure git hooks abort
}
process.exit(1); // ensure git hooks abort
}
})();
4 changes: 2 additions & 2 deletions src/__tests__/pretty-quick.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ jest.mock('execa');

afterEach(() => mock.restore());

test('throws an error when no vcs is found', () => {
test('throws an error when no vcs is found', async () => {
mock({
'root/README.md': '',
});

expect(() => prettyQuick('root')).toThrow(
await expect(prettyQuick('root')).rejects.toThrow(
'Unable to detect a source control manager.',
);
});