UNPKG

@codecovevienna/gittt-cli

Version:

Tracking time with CLI into a git repository

310 lines (309 loc) 14.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileHelper = void 0; const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const yaml_1 = __importDefault(require("yaml")); const _1 = require("./"); const gitttFileError_1 = require("../types/errors/gitttFileError"); class FileHelper { constructor(configDir, configFileName, timerFileName, projectDir) { this.jsonWriteOptions = { EOL: "\n", spaces: 4, }; this.createConfigDir = () => __awaiter(this, void 0, void 0, function* () { yield fs_extra_1.default.mkdirs(this.configDir); yield fs_extra_1.default.mkdirs(this.projectDir); }); this.initConfigFile = (gitRepo) => __awaiter(this, void 0, void 0, function* () { const initial = { created: Date.now(), gitRepo, links: [], }; yield this.saveConfigObject(initial); return initial; }); this.initProject = (project) => __awaiter(this, void 0, void 0, function* () { try { const projectPath = yield this.getProjectPath(project); _1.LogHelper.debug(`Ensuring domain directory for ${project.name}`); yield fs_extra_1.default.mkdirs(projectPath); yield this.saveProjectObject(project); return project; } catch (err) { _1.LogHelper.debug("Error writing project file", err); throw new Error("Error initializing project"); } }); this.initTimerFile = () => __awaiter(this, void 0, void 0, function* () { try { const initial = { start: 0, stop: 0, }; yield fs_extra_1.default.writeJson(this.timerFilePath, initial, this.jsonWriteOptions); } catch (err) { _1.LogHelper.debug("Error initializing timer file", err); throw new Error("Error initializing timer file"); } }); this.configDirExists = () => __awaiter(this, void 0, void 0, function* () { try { return yield fs_extra_1.default.pathExists(this.configFilePath); } catch (err) { _1.LogHelper.error("Error checking config file existence"); return false; } }); this.getGitttFile = () => __awaiter(this, void 0, void 0, function* () { try { return yaml_1.default.parse((yield fs_extra_1.default .readFile(".gittt.yml")) .toString()); } catch (err) { _1.LogHelper.debug("Unable to parse .gittt.yml file", err); throw new gitttFileError_1.GitttFileError("Unable to parse .gittt.yml file"); } }); this.getConfigObject = (fromDisk = false) => __awaiter(this, void 0, void 0, function* () { try { if (!this.configObject || fromDisk) { const configObj = yield fs_extra_1.default.readJson(this.configFilePath); this.setConfigObject(configObj); return configObj; } else { return this.configObject; } } catch (err) { _1.LogHelper.debug("Error reading config file", err); throw new Error("Error getting config object"); } }); this.timerFileExists = () => { try { return fs_extra_1.default.existsSync(this.timerFilePath); } catch (err) { _1.LogHelper.error("Error checking timer file existence"); return false; } }; this.saveProjectObject = (project) => __awaiter(this, void 0, void 0, function* () { try { const projectPath = yield this.getProjectPath(project); const projectFilePath = path_1.default.join(projectPath, `${project.name}.json`); _1.LogHelper.debug(`Saving project file to ${projectFilePath}`); yield fs_extra_1.default.writeJson(projectFilePath, project, this.jsonWriteOptions); // TODO update cache } catch (err) { _1.LogHelper.debug("Error writing project file", err); throw new Error("Error writing project file"); } }); this.invalidateCache = () => { this.configObject = undefined; }; this.getTimerObject = () => __awaiter(this, void 0, void 0, function* () { try { const timerObj = yield fs_extra_1.default.readJson(this.timerFilePath); return timerObj; } catch (err) { _1.LogHelper.debug("Error reading timer object", err); throw new Error("Error getting timer object"); } }); this.initReadme = () => __awaiter(this, void 0, void 0, function* () { try { yield fs_extra_1.default.writeFile(path_1.default.join(this.configDir, "README.md"), "# Initially generated gittt README.md file"); } catch (err) { _1.LogHelper.debug("Error writing readme file", err); throw new Error("Error initializing readme file"); } }); this.findProjectByName = (projectName, projectMeta) => __awaiter(this, void 0, void 0, function* () { const allFoundProjects = []; if (projectMeta) { // Use specific domain const domainProjects = yield this.findProjectsForDomain(projectMeta); for (const project of domainProjects) { if (project.name === projectName) { allFoundProjects.push(project); } } } else { // Search in all domains const projectDomains = fs_extra_1.default.readdirSync(this.projectDir); for (const projectDomain of projectDomains) { const tmpStat = fs_extra_1.default.lstatSync(path_1.default.join(this.projectDir, projectDomain)); if (tmpStat.isFile()) { const project = yield fs_extra_1.default.readJson(path_1.default.join(this.projectDir, projectDomain)); if (project.name === projectName) { allFoundProjects.push(project); } } else { const projectFiles = fs_extra_1.default.readdirSync(path_1.default.join(this.projectDir, projectDomain)); for (const projectFile of projectFiles) { const project = yield fs_extra_1.default.readJson(path_1.default.join(this.projectDir, projectDomain, projectFile)); if (project.name === projectName) { allFoundProjects.push(project); } } } } } switch (allFoundProjects.length) { case 0: // No project found return undefined; case 1: // No project found return allFoundProjects[0]; default: // If more than 1 project with the given name gets found, throw error throw new Error(`Found more than 1 project named "${projectName}"`); } }); this.saveTimerObject = (timer) => __awaiter(this, void 0, void 0, function* () { try { yield fs_extra_1.default.writeJson(this.timerFilePath, timer, this.jsonWriteOptions); } catch (err) { _1.LogHelper.debug("Error writing timer file", err); throw new Error("Error writing timer file"); } }); this.findAllProjects = () => __awaiter(this, void 0, void 0, function* () { const allProjects = []; const projectDomains = fs_extra_1.default.readdirSync(this.projectDir); for (const projectDomain of projectDomains) { const tmpStat = fs_extra_1.default.lstatSync(path_1.default.join(this.projectDir, projectDomain)); if (tmpStat.isFile()) { const project = yield fs_extra_1.default.readJson(path_1.default.join(this.projectDir, projectDomain)); allProjects.push(project); } else { const projectFiles = fs_extra_1.default.readdirSync(path_1.default.join(this.projectDir, projectDomain)); for (const projectFile of projectFiles) { const project = yield fs_extra_1.default.readJson(path_1.default.join(this.projectDir, projectDomain, projectFile)); allProjects.push(project); } } } return allProjects; }); this.findProjectsForDomain = (projectMeta) => __awaiter(this, void 0, void 0, function* () { const projects = []; if (!(yield fs_extra_1.default.pathExists(this.projectMetaToPath(projectMeta)))) { return projects; } const projectFiles = fs_extra_1.default.readdirSync(this.projectMetaToPath(projectMeta)); for (const projectFile of projectFiles) { const project = yield fs_extra_1.default.readJson(path_1.default.join(this.projectMetaToPath(projectMeta), projectFile)); projects.push(project); } return projects; }); this.removeDomainDirectory = (projectMeta, force = false) => __awaiter(this, void 0, void 0, function* () { const projectsInDomain = yield this.findProjectsForDomain(projectMeta); if (projectsInDomain.length > 0) { if (force) { yield fs_extra_1.default.remove(this.projectMetaToPath(projectMeta)); } else { throw new Error(`${this.projectMetaToPath(projectMeta)} is not empty`); } } else { yield fs_extra_1.default.remove(this.projectMetaToPath(projectMeta)); } }); this.removeProjectFile = (project) => __awaiter(this, void 0, void 0, function* () { return fs_extra_1.default.remove(_1.ProjectHelper.projectToProjectFilename(project)); }); this.setConfigObject = (config) => { this.configObject = config; }; this.projectMetaToPath = (projectMeta) => { return path_1.default.join(this.projectDir, _1.ProjectHelper.projectMetaToDomain(projectMeta)); }; this.saveConfigObject = (config) => __awaiter(this, void 0, void 0, function* () { try { yield fs_extra_1.default.writeJson(this.configFilePath, config, this.jsonWriteOptions); this.setConfigObject(config); } catch (err) { _1.LogHelper.debug("Error writing config file", err); throw new Error("Error writing config file"); } }); this.configDir = configDir; this.projectDir = path_1.default.join(configDir, projectDir); this.configFilePath = path_1.default.join(configDir, configFileName); this.timerFilePath = path_1.default.join(configDir, timerFileName); } getProjectPath(project) { if (!project.meta) { return path_1.default.join(this.projectDir); } else { return this.projectMetaToPath(project.meta); } } isConfigFileValid() { return __awaiter(this, void 0, void 0, function* () { let config; try { config = yield this.getConfigObject(true); } catch (err) { _1.LogHelper.debug(`Unable to parse config file: ${err.message}`); return false; } try { (0, _1.parseProjectNameFromGitUrl)(config.gitRepo); return true; } catch (err) { _1.LogHelper.debug("Unable to get project name", err); return false; } }); } } exports.FileHelper = FileHelper; FileHelper.getHomeDir = () => { // eslint-disable-next-line @typescript-eslint/no-var-requires const home = require("os").homedir() || process.env.HOME || process.env.HOMEPATH || process.env.USERPROFIL; if (!home) { throw new Error("Unable to determinate home directory"); } return home; };