Skip to content

Commit e2eec80

Browse files
committed
WIP Fix parsing of !reference tag
Fix: #50
1 parent d11ce0f commit e2eec80

File tree

4 files changed

+69
-34
lines changed

4 files changed

+69
-34
lines changed

package-lock.json

+16-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
"dependencies": {
4949
"chalk": "^5.3.0",
5050
"jest-diff": "^29.7.0",
51-
"js-yaml": "^4.1.0",
52-
"prettier-linter-helpers": "^1.0.0"
51+
"prettier-linter-helpers": "^1.0.0",
52+
"yaml": "2.7.0"
5353
},
5454
"peerDependencies": {
5555
"prettier": "^3.0"

src/index.js

+5-30
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,19 @@
11
#!/usr/bin/env node
2-
import { readFileSync, mkdirSync, writeFileSync } from 'node:fs';
3-
import { cwd, env } from 'node:process';
4-
import { join, resolve, dirname } from 'node:path';
5-
import yaml from 'js-yaml';
2+
import { mkdirSync, writeFileSync } from 'node:fs';
3+
import { env } from 'node:process';
4+
import { dirname } from 'node:path';
65
import { diff } from './formatters/diff.js';
76
import { gitlab } from './formatters/gitlab.js';
87
import { parse } from './utils/parse-prettier-results.js';
9-
10-
const {
11-
// Used as a fallback for local testing.
12-
CI_CONFIG_PATH = '.gitlab-ci.yml',
13-
CI_JOB_NAME,
14-
CI_PROJECT_DIR = cwd(),
15-
} = env;
16-
17-
/**
18-
* Get the output path for the report file.
19-
* @returns {string}
20-
*/
21-
function getOutputPath() {
22-
const jobs = yaml.load(readFileSync(join(CI_PROJECT_DIR, CI_CONFIG_PATH), 'utf-8'));
23-
const { artifacts } = jobs[CI_JOB_NAME];
24-
const location = artifacts && artifacts.reports && artifacts.reports.codequality;
25-
const msg = `Expected ${CI_JOB_NAME}.artifacts.reports.codequality to be one exact path`;
26-
if (!location) {
27-
throw new Error(`${msg}, but no value was found.`);
28-
}
29-
if (Array.isArray(location)) {
30-
throw new Error(`${msg}, but found an array instead.`);
31-
}
32-
return resolve(CI_PROJECT_DIR, location);
33-
}
8+
import { getOutputPath } from './utils/get-output-path.js';
349

3510
/**
3611
* Format Prettier results for GitLab Code Quality Reports.
3712
* @param {string} results
3813
* @returns {Promise<void>}
3914
*/
4015
export async function prettierFormatterGitLab(results) {
41-
const { PRETTIER_CODE_QUALITY_REPORT } = env;
16+
const { CI_JOB_NAME, PRETTIER_CODE_QUALITY_REPORT } = env;
4217
if (CI_JOB_NAME || PRETTIER_CODE_QUALITY_REPORT) {
4318
const files = parse(results);
4419

src/utils/get-output-path.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { readFileSync } from 'node:fs';
2+
import { cwd, env } from 'node:process';
3+
import { join, resolve } from 'node:path';
4+
import { parseDocument } from 'yaml';
5+
6+
/** @type {yaml.CollectionTag} */
7+
const referenceTag = {
8+
tag: '!reference',
9+
collection: 'seq',
10+
default: false,
11+
resolve() {
12+
// We only allow the syntax. We don’t actually resolve the reference.
13+
},
14+
};
15+
16+
/**
17+
* Get the output path for the report file.
18+
* @returns {string}
19+
*/
20+
export function getOutputPath() {
21+
const {
22+
// Used as a fallback for local testing.
23+
CI_CONFIG_PATH = '.gitlab-ci.yml',
24+
CI_JOB_NAME,
25+
CI_PROJECT_DIR = cwd(),
26+
} = env;
27+
28+
const jobs = parseDocument(readFileSync(join(CI_PROJECT_DIR, CI_CONFIG_PATH), 'utf-8'), {
29+
version: '1.1',
30+
customTags: [referenceTag],
31+
});
32+
33+
const { artifacts } = jobs[CI_JOB_NAME];
34+
const location = artifacts && artifacts.reports && artifacts.reports.codequality;
35+
const msg = `Expected ${CI_JOB_NAME}.artifacts.reports.codequality to be one exact path`;
36+
37+
if (!location) {
38+
throw new Error(`${msg}, but no value was found.`);
39+
}
40+
41+
if (Array.isArray(location)) {
42+
throw new Error(`${msg}, but found an array instead.`);
43+
}
44+
45+
return resolve(CI_PROJECT_DIR, location);
46+
}

0 commit comments

Comments
 (0)