Skip to content

Commit 5b0d3eb

Browse files
authored
🐛 read workspace config correctly (#91)
* 🐞 fix: get config in workspace correctly * Update dependencies in package.json * refactor core method ref * chore: build, dist updated
1 parent 95c12c4 commit 5b0d3eb

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

dist/index.js

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,32 @@
3434
"@jscpd/core": "^3.5.4",
3535
"@jscpd/finder": "^3.5.10",
3636
"@jscpd/tokenizer": "^3.5.4",
37-
"@octokit/rest": "^18.2.0",
37+
"@octokit/rest": "^18.12.0",
3838
"deepmerge": "4.3.1",
3939
"js-yaml": "^4.1.0",
4040
"jscpd": "^3.5.10",
4141
"node-fetch": "^2.7.0"
4242
},
4343
"devDependencies": {
44-
"@babel/cli": "^7.23.0",
45-
"@babel/core": "^7.23.2",
46-
"@babel/preset-env": "^7.23.3",
47-
"@octokit/types": "^12.3.0",
44+
"@babel/cli": "^7.23.4",
45+
"@babel/core": "^7.23.6",
46+
"@babel/preset-env": "^7.23.6",
47+
"@octokit/types": "^12.4.0",
4848
"@types/jest": "^29.5.11",
4949
"@types/js-yaml": "^4.0.9",
50-
"@types/node": "^18.18.8",
51-
"@types/node-fetch": "2.6.6",
52-
"@typescript-eslint/parser": "^6.15.0",
50+
"@types/node": "^18.19.3",
51+
"@types/node-fetch": "2.6.10",
52+
"@typescript-eslint/parser": "^6.16.0",
5353
"@vercel/ncc": "^0.38.1",
5454
"babel-jest": "^29.7.0",
55-
"eslint": "^8.53.0",
56-
"eslint-config-prettier": "^9.0.0",
55+
"eslint": "^8.56.0",
56+
"eslint-config-prettier": "^9.1.0",
5757
"eslint-plugin-github": "^4.10.1",
58-
"eslint-plugin-jest": "^27.4.3",
59-
"eslint-plugin-prettier": "^5.0.1",
58+
"eslint-plugin-jest": "^27.6.0",
59+
"eslint-plugin-prettier": "^5.1.2",
6060
"jest": "^29.7.0",
6161
"js-yaml": "^4.1.0",
62-
"prettier": "3.0.3",
62+
"prettier": "3.1.1",
6363
"ts-jest": "^29.1.1",
6464
"typescript": "^4.9.5"
6565
}

src/duplicated.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { error, info, notice, setFailed, setOutput, warning } from '@actions/core';
1+
import * as core from '@actions/core';
22
import { context } from '@actions/github';
33
import { IClone, IOptions } from '@jscpd/core';
44
import { Octokit } from '@octokit/rest';
@@ -34,12 +34,12 @@ export async function duplicatedCheck(
3434
fs.writeFileSync(markdownReport, message);
3535
await git.UploadReportToArtifacts([markdownReport, jsonReport], REPORT_ARTIFACT_NAME);
3636
const isOverThreshold = checkThreshold(jsonReport, options.threshold || 0);
37-
jscpdCheckAsError && isOverThreshold ? setFailed('❌ DUPLICATED CODE FOUND') : warning('DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
37+
jscpdCheckAsError && isOverThreshold ? core.setFailed('❌ DUPLICATED CODE FOUND') : core.warning('DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
3838
showAnnotation(clones, cwd, jscpdCheckAsError && isOverThreshold);
39-
setOutput('hasDuplicates', `${isOverThreshold}`);
39+
core.setOutput('hasDuplicates', `${isOverThreshold}`);
4040
} else {
41-
setOutput('hasDuplicates', 'false');
42-
notice('✅ NO DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
41+
core.setOutput('hasDuplicates', 'false');
42+
core.notice('✅ NO DUPLICATED CODE FOUND', ANNOTATION_OPTIONS);
4343
}
4444
await execute(`rm -rf ${cwd}/${REPORT_ARTIFACT_NAME}`);
4545
}
@@ -52,19 +52,21 @@ function getOptions(jscpdConfigPath: string, workspace: string, cwd: string): Pa
5252
output: `${cwd}/${REPORT_ARTIFACT_NAME}`
5353
};
5454
const options = { ...configOptions, ...defaultOptions };
55-
info(`loaded options: ${inspect(options)}`);
55+
core.startGroup('🔎 loaded options');
56+
core.info(`${inspect(options)}`);
57+
core.endGroup();
5658
return options;
5759
}
5860

5961
function getReportFiles(cwd: string): string[] {
6062
const files = fs.readdirSync(`${cwd}/${REPORT_ARTIFACT_NAME}`);
6163
const filePaths = files.map(file => `${cwd}/${REPORT_ARTIFACT_NAME}/${file}`);
62-
info(`reportFiles: ${filePaths.join(',')}`);
64+
core.info(`reportFiles: ${filePaths.join(',')}`);
6365
return filePaths;
6466
}
6567

6668
function checkWorkspace(workspace: string): string {
67-
info(`workspace: ${workspace}`);
69+
core.info(`workspace: ${workspace}`);
6870
//check if workspace path is a file
6971
const isFile = fs.existsSync(workspace) && fs.lstatSync(workspace).isFile();
7072
if (isFile) {
@@ -75,7 +77,7 @@ function checkWorkspace(workspace: string): string {
7577
}
7678

7779
function showAnnotation(clones: IClone[], cwd: string, isError: boolean): void {
78-
const show = isError ? error : warning;
80+
const show = isError ? core.error : core.warning;
7981
for (const clone of clones) {
8082
show(
8183
`${clone.duplicationA.sourceId.replace(cwd, '')} (${clone.duplicationA.start.line}-${clone.duplicationA.end.line})
@@ -137,7 +139,7 @@ function checkThreshold(jsonReport: string, threshold: number): boolean {
137139
// read json report
138140
const report = JSON.parse(fs.readFileSync(jsonReport, 'utf8')) as IJsonReport;
139141
if (report.statistics.total.percentage > threshold) {
140-
error(`DUPLICATED CODE FOUND ${report.statistics.total.percentage}% IS OVER THRESHOLD ${threshold}%`, ANNOTATION_OPTIONS);
142+
core.error(`DUPLICATED CODE FOUND ${report.statistics.total.percentage}% IS OVER THRESHOLD ${threshold}%`, ANNOTATION_OPTIONS);
141143
return true;
142144
}
143145
return false;

src/readConfig.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ function arrayMergeDedupe<T>(source: T[], target: T[]): T[] {
3838
return Array.from(new Set([...source, ...target]));
3939
}
4040

41-
export function readConfig<T>(defaultOptions: Partial<T>, configName: string, workspace: string, defaultConfigName: string): Partial<T> {
42-
const configFile = resolve(configName || defaultConfigName);
43-
const workspaceConfig = resolve(workspace, configName || defaultConfigName);
41+
export function readConfig<T>(defaultOptions: Partial<T>, mainConfigPath: string, workspace: string, defaultConfigName: string): Partial<T> {
42+
const configFile = resolve(mainConfigPath || defaultConfigName);
43+
const configFilename = configFile.split('/').pop();
44+
const workspaceConfig = resolve(workspace, configFilename || defaultConfigName);
4445

4546
const configExists = fs.existsSync(configFile);
4647
const workspaceConfigExists = workspaceConfig !== configFile && fs.existsSync(workspaceConfig);

0 commit comments

Comments
 (0)