Skip to content

Commit

Permalink
Needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
LauJosefsen committed Sep 22, 2023
1 parent e0bc96d commit 25510f0
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 200 deletions.
10 changes: 6 additions & 4 deletions bin/cmds/toggle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { loadConfig } from "../../src/config_loader";
import { Argv } from "yargs";
import { errorHandler } from "../../src/error_handler";
import { resetDisabledProjects, logDisabledProjects, toggleProjectDisable } from "../../src/disable_projects";
import {
logProjectStatus, resetToggledProjects, toggleProject
} from "../../src/toggle_projects";
import { tabCompleteToggle } from "../../src/tab_completion";

// noinspection JSUnusedGlobalSymbols
Expand All @@ -19,14 +21,14 @@ export async function handler(argv: any) {
switch (argv.project) {
case "status":
// give a status of disabled projects
logDisabledProjects(config);
logProjectStatus(config);
break;
case "reset":
// set disabled projects to projects which defaultDisabled
resetDisabledProjects(config);
resetToggledProjects(config);
break;
default:
toggleProjectDisable(config, argv.project);
toggleProject(config, argv.project);
}
} catch (e) {
errorHandler(e);
Expand Down
36 changes: 24 additions & 12 deletions src/config_loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config } from "./types/config";
import {Config, Project} from "./types/config";

Check warning on line 1 in src/config_loader.ts

View workflow job for this annotation

GitHub Actions / eslint

'Project' is defined but never used
import * as utils from "./utils";
import path from "path";
import assert, { AssertionError } from "assert";
Expand All @@ -7,16 +7,15 @@ import { validateYaml } from "./validate_yaml";
import fs from "fs-extra";
import yaml from "js-yaml";
import * as _ from "lodash";
import { getDisabledProjects, getPreviouslySeenProjectsFromCache } from "./disable_projects";
import { Cache } from "./cache";
import { createActionGraphs } from "./graph";
import {getToggledProjects} from "./toggle_projects";

export async function loadConfig(cwd: string, needs = true, shouldDisableProjects = true): Promise<Config> {
const cnfPath = path.join(cwd, `.gitte.yml`);
const dotenvPath = path.join(cwd, `.gitte-env`);
const overridePath = path.join(cwd, ".gitte-override.yml");
const cachePath = path.join(cwd, ".gitte-cache.json");
const projectsDisablePath = path.join(cwd, ".gitte-projects-disable");

let fileContent;

Expand Down Expand Up @@ -60,8 +59,6 @@ export async function loadConfig(cwd: string, needs = true, shouldDisableProject
yml = _.merge(yml, overrideYml);
}

const seenProjects = getPreviouslySeenProjectsFromCache(cachePath);

// Write .gitte-cache.json
const cache: Cache = {
version: 1,
Expand All @@ -70,19 +67,34 @@ export async function loadConfig(cwd: string, needs = true, shouldDisableProject
};
fs.writeJsonSync(cachePath, cache, { spaces: 4 });

const disabledProjects = getDisabledProjects(seenProjects, projectsDisablePath, yml);
if (shouldDisableProjects) {
disabledProjects.forEach((projectName) => {
_.unset(yml.projects, projectName);
assert(validateYaml(yml), "Invalid .gitte.yml file");

const toggledProjects = getToggledProjects({...yml, cwd});

const disabledProjects = shouldDisableProjects ? Object.entries(yml.projects).reduce((acc, [projectName, project]) => {
const toggledState: boolean | undefined = toggledProjects[projectName];

Check warning on line 75 in src/config_loader.ts

View workflow job for this annotation

GitHub Actions / eslint

'toggledState' is assigned a value but never used

if ((project.defaultDisabled && toggledProjects[projectName] !== true) || toggledProjects[projectName] === false) {
acc.push(projectName);
}
return acc;
}, [] as string[]) : [];


// Unset default disabled projects unless they are toggled
if(shouldDisableProjects) {
Object.entries(yml.projects).forEach(([projectName, project]) => {

Check warning on line 86 in src/config_loader.ts

View workflow job for this annotation

GitHub Actions / eslint

'project' is defined but never used
if (disabledProjects.includes(projectName)) {
_.unset(yml.projects, projectName);
}
});
}

assert(validateYaml(yml), "Invalid .gitte.yml file");

// For any action, replace needs with an empty array if undefined.
Object.entries(yml.projects).forEach(([, project]) => {
Object.entries(project.actions).forEach(([, action]) => {
action.needs = action.needs?.filter((need) => !disabledProjects.includes(need)) ?? [];
action.needs = action.needs || [];
action.needs = action.needs.filter((need) => !disabledProjects.includes(need));
action.priority = action.priority || null;
action.searchFor = action.searchFor || [];
});
Expand Down
108 changes: 0 additions & 108 deletions src/disable_projects.ts

This file was deleted.

90 changes: 90 additions & 0 deletions src/toggle_projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {Config} from "./types/config";
import path from "path";
import assert from "assert";
import fs from "fs-extra";
import chalk from "chalk";
import {printHeader} from "./utils";

const projectsToggleFileName = ".gitte-projects-toggled"

export function getToggledProjects(cfg: Config): {[key: string]: boolean} {
const toggledProjectsFilePath = path.join(cfg.cwd, projectsToggleFileName);

if (!fs.pathExistsSync(toggledProjectsFilePath)) {
fs.writeFileSync(toggledProjectsFilePath, "", "utf8");
}

return fs.readFileSync(toggledProjectsFilePath, "utf8").toString()
.split("\n")
.filter((x) => x.length > 0)
.map((x) => x.split(":"))
.reduce((carry, [projectName, enabled]) => {
carry[projectName] = enabled === "true";
return carry;
}, {} as { [key: string]: boolean });
}

export function logProjectStatus(cfg: Config): void {
const toggledProjects = getToggledProjects(cfg);

Object.entries(cfg.projects).forEach(([projectName, project]) => {
let enabled = !project.defaultDisabled
if(toggledProjects[projectName]) {
enabled = toggledProjects[projectName]
}

if (enabled) {
console.log(chalk`{bold ${projectName}:} {green enabled}`);
} else {
console.log(chalk`{bold ${projectName}:} {red disabled}`);
}
});
}

export function resetToggledProjects(cfg: Config): void {
const toggledProjectsFilePath = path.join(cfg.cwd, projectsToggleFileName);
fs.writeFileSync(toggledProjectsFilePath, "", "utf8");

printHeader("Toggled projects have been cleaned.", "SUCCESS");

logProjectStatus(cfg);
}

function saveToggledProjects(cfg: Config, toggledProjects: {[key: string]: boolean}): void {
const toggledProjectsFilePath = path.join(cfg.cwd, projectsToggleFileName);
fs.writeFileSync(toggledProjectsFilePath, Object.entries(toggledProjects)
.map(([projectName, enabled]) => `${projectName}:${enabled}`)
.join("\n"), "utf8");
}

export function toggleProject(cfg: Config, projectName: string): void {

// assert the project exists in config
assert(
cfg.projects[projectName],
`Project "${projectName}" does not exist. (See "gitte list" to see available projects.)`,
);

const customToggles = getToggledProjects(cfg);
const defaultState = !cfg.projects[projectName].defaultDisabled;
const currentState = customToggles[projectName] ?? defaultState;
const desiredState = !currentState;

if(desiredState === defaultState) {
// Delete the project from the custom toggles
delete customToggles[projectName];
}
else {
// Add or change the project in the custom toggles
customToggles[projectName] = desiredState;
}

// Write the custom toggles to file
saveToggledProjects(cfg, customToggles);

if (desiredState) {
printHeader(`${projectName} has been enabled.`, "SUCCESS");
} else {
printHeader(`${projectName} has been disabled.`, "SUCCESS");
}
}
Loading

0 comments on commit 25510f0

Please sign in to comment.