Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit e20730a

Browse files
feat: updated utils for better reporting/reusability
1 parent dce4830 commit e20730a

5 files changed

Lines changed: 55 additions & 14 deletions

File tree

src/checkPush.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
executeGitCommand,
33
findClosestSha,
4-
getRootDirectory,
4+
getGitDirectoryRequired,
55
} from "../utils/git";
66
import {
77
getRulesetsFromCodigaFile,
@@ -100,7 +100,7 @@ export async function checkPush(remoteShaArg, localShaArg) {
100100
}
101101

102102
// ensure that there's a git directory to continue
103-
getRootDirectory();
103+
getGitDirectoryRequired();
104104

105105
// check and verify the SHA args
106106
const { remoteSha, localSha } = checkSHAs(remoteShaArg, localShaArg);

utils/file.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,37 @@ import { ROSIE_SUPPORTED_SUFFIX_TO_LANGUAGE } from "./constants";
55
import { printEmptyLine, printFailure, printSuggestion } from "./print";
66

77
/**
8-
* read a file contents
8+
* Used to read a file
9+
* If the file doesn't exist, we'll return null
910
* @param {string} path
1011
* @returns
1112
*/
1213
export function readFile(path) {
14+
try {
15+
return fs.readFileSync(path, "utf8");
16+
} catch (err) {
17+
return null;
18+
}
19+
}
20+
21+
/**
22+
* Used to read a file
23+
* If the file doesn't exist, we'll exit
24+
* @param {string} path
25+
* @returns
26+
*/
27+
export function readFileRequired(path) {
1328
try {
1429
const file = fs.readFileSync(path, "utf8");
1530
return file;
1631
} catch (err) {
32+
printEmptyLine();
1733
printFailure(`Unable to read file: ${path}`);
34+
printSuggestion(
35+
" ↳ Please try again and contact us, if the issue persists:",
36+
"https://app.codiga.io/support"
37+
);
38+
printEmptyLine();
1839
process.exit(1);
1940
}
2041
}

utils/git.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import child_process from "child_process";
2-
import { printFailure } from "./print";
2+
import { printEmptyLine, printFailure, printSuggestion } from "./print";
33
import { isTestMode } from "../tests/test-utils";
44

55
/**
@@ -20,9 +20,9 @@ export function executeGitCommand(args) {
2020

2121
/**
2222
* Gets the git directory
23-
* @returns the directory string or exits if there isn't one
23+
* @returns the directory location or null if there isn't one
2424
*/
25-
export function getRootDirectory() {
25+
export function getGitDirectory() {
2626
// if we're in test mode we'll skip this check
2727
if (isTestMode) return "./tests/fixtures";
2828

@@ -31,9 +31,28 @@ export function getRootDirectory() {
3131
if (rootDirectory) {
3232
return rootDirectory.split("\n").join("");
3333
} else {
34+
return null;
35+
}
36+
}
37+
38+
/**
39+
* Gets the git directory
40+
* @returns the directory location or exits if there isn't one
41+
*/
42+
export function getGitDirectoryRequired() {
43+
// if we're in test mode we'll skip this check
44+
if (isTestMode) return "./tests/fixtures";
45+
46+
// now look for a git repository
47+
const rootDirectory = executeGitCommand(["rev-parse", "--show-toplevel"]);
48+
if (rootDirectory) {
49+
return rootDirectory.split("\n").join("");
50+
} else {
51+
printEmptyLine();
3452
printFailure(
3553
"Unable to execute a git command because you're not in a git repository"
3654
);
55+
printEmptyLine();
3756
process.exit(1);
3857
}
3958
}
@@ -98,8 +117,9 @@ export function findClosestSha() {
98117

99118
if (output) {
100119
closestSha = output.replace(/\n/g, "").trim();
101-
console.debug(
102-
`Closest SHA found between ${currentBranch} and ${mainBranch}: ${closestSha}`
120+
printSuggestion(
121+
`Closest SHA found between ${currentBranch} and ${mainBranch}`,
122+
closestSha
103123
);
104124
}
105125

utils/rosie.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Listr } from "listr2";
22
import { encodeToBase64 } from "../utils/encoding";
33
import { rosieApiFetch } from "./api";
4-
import { getLanguageForFile, readFile } from "./file";
4+
import { getLanguageForFile, readFileRequired } from "./file";
55
import { getRulesForRosiePerLanguage } from "./rules";
66
import { printEmptyLine, printFailure, printInfo, printSubItem } from "./print";
77

@@ -62,7 +62,7 @@ export async function analyzeFiles(paths, rules) {
6262

6363
detectedLanguages.forEach((language) => {
6464
files[language].forEach((file) => {
65-
const fileContent = readFile(file);
65+
const fileContent = readFileRequired(file);
6666
const body = {
6767
filename: file,
6868
language: language.toLowerCase(),

utils/rulesets.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { readFile, parseYamlFile } from "../utils/file";
1+
import { readFileRequired, parseYamlFile } from "../utils/file";
22
import { codigaApiFetch } from "./api";
33
import { ACTION_TOKEN_ADD, CODIGA_CONFIG_FILE } from "./constants";
4-
import { getRootDirectory } from "./git";
4+
import { getGitDirectoryRequired } from "./git";
55
import { GET_RULESETS_FOR_CLIENT } from "../graphql/queries";
66
import { printCommandSuggestion, printFailure, printSuggestion } from "./print";
77
import { getToken } from "./store";
@@ -35,10 +35,10 @@ export async function getRulesetsWithRules(names) {
3535
* @returns
3636
*/
3737
export function getRulesetsFromCodigaFile() {
38-
const rootDir = getRootDirectory();
38+
const rootDir = getGitDirectoryRequired();
3939

4040
const codigaFileLocation = `${rootDir}/${CODIGA_CONFIG_FILE}`;
41-
const file = readFile(codigaFileLocation);
41+
const file = readFileRequired(codigaFileLocation);
4242
const parsedFile = parseYamlFile(file, codigaFileLocation);
4343

4444
// if there isn't a rulesets value in the codiga.yml file, throw an error

0 commit comments

Comments
 (0)