Skip to content

Commit 741f555

Browse files
committed
Add support for using both the -c and -l flags
1 parent 32c8d78 commit 741f555

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/index.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const {
1111
CI_CONFIG_PATH = '.gitlab-ci.yml',
1212
CI_JOB_NAME,
1313
CI_PROJECT_DIR = cwd(),
14-
PRETTIER_CODE_QUALITY_REPORT,
1514
} = env;
1615

1716
/**
@@ -39,9 +38,14 @@ function getOutputPath() {
3938
*/
4039
function parse(results) {
4140
return results
42-
.toString()
4341
.split('\n')
44-
.filter((line) => line);
42+
.filter(
43+
(line) =>
44+
Boolean(line) &&
45+
!line.startsWith('Checking formatting...') &&
46+
!line.includes('Code style issues found'),
47+
)
48+
.map((line) => line.replace('[warn] ', ''));
4549
}
4650

4751
/**
@@ -50,6 +54,7 @@ function parse(results) {
5054
* @returns {Promise<void>}
5155
*/
5256
export async function prettierFormatterGitLab(results) {
57+
const { PRETTIER_CODE_QUALITY_REPORT } = env;
5358
if (CI_JOB_NAME || PRETTIER_CODE_QUALITY_REPORT) {
5459
const files = parse(results);
5560

test/cli.spec.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { describe, it, expect, afterEach } from 'bun:test';
1+
import { describe, it, expect, afterEach, spyOn } from 'bun:test';
22
import { existsSync, unlinkSync, readFileSync } from 'node:fs';
33
import { execSync } from 'node:child_process';
4+
import { prettierFormatterGitLab } from '../src/index.js';
45

56
const CODE_QUALITY_FILENAME = 'gl-prettier-codequality.json';
67

@@ -24,4 +25,28 @@ describe('prettier-formatter-gitlab cli', () => {
2425
const content = readFileSync(CODE_QUALITY_FILENAME);
2526
expect(content.toString()).toMatchSnapshot();
2627
});
28+
29+
it('should work with the `-c` or `-l` flag', async () => {
30+
const consoleSpy = spyOn(console, 'log');
31+
consoleSpy.mockImplementation(() => null);
32+
process.env.PRETTIER_CODE_QUALITY_REPORT = CODE_QUALITY_FILENAME;
33+
34+
// prettier -c path/to/folder/
35+
const outputCheck = `Checking formatting...
36+
[warn] test/dirty-2.js
37+
[warn] test/dirty.js
38+
[warn] Code style issues found in 3 files. Run Prettier to fix.
39+
`;
40+
41+
// prettier -l path/to/folder/
42+
const outputList = `test/dirty-2.js
43+
test/dirty.js
44+
`;
45+
46+
await prettierFormatterGitLab(outputCheck);
47+
await prettierFormatterGitLab(outputList);
48+
expect(consoleSpy).toHaveBeenCalledTimes(12);
49+
expect(consoleSpy.mock.calls.slice(0, 6)).toEqual(consoleSpy.mock.calls.slice(6, 12));
50+
consoleSpy.mockRestore();
51+
});
2752
});

0 commit comments

Comments
 (0)