|
| 1 | +import { writeFile } from "fs/promises"; |
| 2 | +import { isTestMode } from "../tests/test-utils"; |
| 3 | +import { |
| 4 | + ACTION_RULESET_ADD, |
| 5 | + ACTION_TOKEN_ADD, |
| 6 | + CODIGA_CONFIG_FILE, |
| 7 | +} from "../utils/constants"; |
| 8 | +import { readFile, parseYamlFile } from "../utils/file"; |
| 9 | +import { getGitDirectory } from "../utils/git"; |
| 10 | +import { |
| 11 | + printCommandSuggestion, |
| 12 | + printEmptyLine, |
| 13 | + printFailure, |
| 14 | + printInfo, |
| 15 | + printSuggestion, |
| 16 | +} from "../utils/print"; |
| 17 | +import { convertRulesetsToString, getRuleset } from "../utils/ruleset"; |
| 18 | + |
| 19 | +/** |
| 20 | + * Creates a codiga.yml file's content with the rulesets given |
| 21 | + * Note: because this overrides the file, you need to pass any old rulesets that should remain |
| 22 | + * @param {string[]} rulesets |
| 23 | + */ |
| 24 | +export async function createCodigaYml(codigaFileLocation, rulesets) { |
| 25 | + if (isTestMode) return; |
| 26 | + try { |
| 27 | + await writeFile(codigaFileLocation, convertRulesetsToString(rulesets), { |
| 28 | + encoding: "utf-8", |
| 29 | + }); |
| 30 | + } catch (err) { |
| 31 | + // console.debug(err); |
| 32 | + printEmptyLine(); |
| 33 | + printFailure(`We were unable to write to: ${codigaFileLocation}`); |
| 34 | + printSuggestion( |
| 35 | + " ↳ Please try again and contact us, if the issue persists:", |
| 36 | + "https://app.codiga.io/support" |
| 37 | + ); |
| 38 | + printEmptyLine(); |
| 39 | + process.exit(1); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Handles adding a ruleset to a codiga.yml file |
| 45 | + * @param {string[]} rulesetNames |
| 46 | + */ |
| 47 | +export async function addRuleset(rulesetNames) { |
| 48 | + // TODO - change to a prompt when no rulesets are given |
| 49 | + const rulesetName = rulesetNames[0]; |
| 50 | + if (!rulesetName) { |
| 51 | + printEmptyLine(); |
| 52 | + printFailure("You need to specify a ruleset to add"); |
| 53 | + printSuggestion( |
| 54 | + " ↳ You can search for rulesets here:", |
| 55 | + "https://app.codiga.io/hub/rulesets" |
| 56 | + ); |
| 57 | + printCommandSuggestion( |
| 58 | + " ↳ Then follow this command structure:", |
| 59 | + `${ACTION_RULESET_ADD} <ruleset-name>` |
| 60 | + ); |
| 61 | + printEmptyLine(); |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | + |
| 65 | + // Check if the ruleset exists before continuing onwards |
| 66 | + const ruleset = await getRuleset({ name: rulesetName }); |
| 67 | + if (!ruleset) { |
| 68 | + printEmptyLine(); |
| 69 | + printFailure( |
| 70 | + "That ruleset either doesn't exist or you lack the permissions to access it" |
| 71 | + ); |
| 72 | + printCommandSuggestion( |
| 73 | + " ↳ Ensure you have a Codiga API token set with one of the following commands:", |
| 74 | + ACTION_TOKEN_ADD |
| 75 | + ); |
| 76 | + printSuggestion( |
| 77 | + " ↳ You can find more rulesets here:", |
| 78 | + "https://app.codiga.io/hub/rulesets" |
| 79 | + ); |
| 80 | + printEmptyLine(); |
| 81 | + process.exit(1); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the `codiga.yml` file location and content. |
| 86 | + * We'll look in the git directory, if it exists. |
| 87 | + * Otherwise, we'll look in the current directory |
| 88 | + */ |
| 89 | + const gitDirectory = getGitDirectory(); |
| 90 | + const dir = gitDirectory || process.cwd(); |
| 91 | + const codigaFileLocation = `${dir}/${CODIGA_CONFIG_FILE}`; |
| 92 | + const codigaFileContent = readFile(codigaFileLocation); |
| 93 | + |
| 94 | + /** |
| 95 | + * If we found a `codiga.yml` file, add the rule to it |
| 96 | + * If we don't find a `codiga.yml` file, create the file and add the rule to it |
| 97 | + */ |
| 98 | + if (codigaFileContent) { |
| 99 | + const parsedFile = parseYamlFile(codigaFileContent, codigaFileLocation); |
| 100 | + const codigaRulesets = parsedFile.rulesets; |
| 101 | + if (codigaRulesets.includes(rulesetName)) { |
| 102 | + printEmptyLine(); |
| 103 | + printInfo( |
| 104 | + `The ruleset (${rulesetName}) already exists in your \`codiga.yml\`` |
| 105 | + ); |
| 106 | + printSuggestion; |
| 107 | + printEmptyLine(); |
| 108 | + process.exit(1); |
| 109 | + } else { |
| 110 | + // adding the new ruleset to the file |
| 111 | + await createCodigaYml(codigaFileLocation, [ |
| 112 | + ...codigaRulesets, |
| 113 | + rulesetName, |
| 114 | + ]); |
| 115 | + printSuggestion( |
| 116 | + `We added ${rulesetName} to your codiga.yml file:`, |
| 117 | + codigaFileLocation |
| 118 | + ); |
| 119 | + printSuggestion( |
| 120 | + " ↳ Find more rulesets to add here:", |
| 121 | + "https://app.codiga.io/hub/rulesets" |
| 122 | + ); |
| 123 | + } |
| 124 | + } else { |
| 125 | + // creating a new codiga.yml with the ruleset here |
| 126 | + await createCodigaYml(codigaFileLocation, [rulesetName]); |
| 127 | + printSuggestion( |
| 128 | + `No codiga.yml file found, so we created one and added ${rulesetName} to it:`, |
| 129 | + codigaFileLocation |
| 130 | + ); |
| 131 | + printSuggestion( |
| 132 | + " ↳ Find more rulesets to add here:", |
| 133 | + "https://app.codiga.io/hub/rulesets" |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + process.exit(0); |
| 138 | +} |
0 commit comments