UNPKG

debt-collector

Version:

a nodejs tool to identify, track and mesure technical debt

135 lines (134 loc) 6.32 kB
import { useState, useEffect } from 'react'; import { useValidatedConfig } from '../../lib/config/index.js'; import { useGitUtils } from '../../lib/git/index.js'; import { useTaskList } from './useTaskList.js'; import { getTagListFromConfig } from '../../lib/config/getTagListFromConfig.js'; import { getCommitResult } from './getCommitResult.js'; import { getCachePath } from '../../lib/fileSystem/getCachePath.js'; import path from 'path'; import fs from 'fs'; import open from 'open'; import { execSync, exec } from 'child_process'; import { useDevLogger } from '../hooks/useDevLogger.js'; import { filterNoScoresFilesFromWalkResult } from './filterNoScoresFilesFromWalkResult.js'; export const useWalkState = (walkOptions) => { const { config, openReport } = walkOptions; const logger = useDevLogger(); const [results, setResults] = useState(null); const [currentCommit, setCurrentCommit] = useState({ commit: '', index: 0 }); const [isReady, setIsReady] = useState(false); const [tags, setTags] = useState({}); const [isFinished, setIsFinished] = useState(false); const { isConfigValid, sanitizedConfig, configErrors } = useValidatedConfig(config); const { isGitReady, walkCommits, checkoutTo, currentBranch, revList, isHistoryDirty, } = useGitUtils(sanitizedConfig); const revlength = isConfigValid && sanitizedConfig?.walkConfig?.limit ? sanitizedConfig.walkConfig.limit : '?'; const tasks = useTaskList({ isConfigValid, isReady, isHistoryDirty, isFinished, revlength: Number(revlength), currentCommit, isGitReady, }); useEffect(() => { ; (async () => { if (isConfigValid && isGitReady && isHistoryDirty === false) { setTags(getTagListFromConfig(sanitizedConfig)); const walkResults = await walkCommits(revList?.reverse() ?? [], { onCommitChange: async ({ rev, index, previousResult }) => { setCurrentCommit({ commit: rev.name, index: index || 0 + 1 }); const results = await getCommitResult({ previousResult: previousResult?.results ?? null, sanitizedConfig, include: sanitizedConfig?.include ?? null, previousHash: previousResult?.rev?.hash, }); logger.info(`${rev.name} - ${Object.keys(results).length} files`, results); return results; }, onError: (error) => { console.log(error); }, onEnd: async (results) => { await checkoutTo(currentBranch ?? ''); return results; }, }); setResults(walkResults); setIsReady(true); } })(); }, [isConfigValid, isGitReady, isHistoryDirty]); useEffect(() => { ; (async () => { if (isReady) { const cachePath = getCachePath(); if (sanitizedConfig && results) { const walkReportData = { config: sanitizedConfig, results, }; const filteredResults = filterNoScoresFilesFromWalkResult(walkReportData); fs.mkdir(cachePath, { recursive: true }, (err) => { if (err) throw err; fs.writeFileSync(`${cachePath}/${sanitizedConfig.projectName}-results.json`, JSON.stringify(filteredResults, null, 2)); if (openReport) { try { fs.writeFileSync(`${cachePath}/projects-config.json`, JSON.stringify({ projects: [ { name: sanitizedConfig.projectName, description: 'unknown', publicUrl: 'unknown', repoUrl: 'unknown', resultTag: sanitizedConfig.projectName, }, ], }, null, 2)); const __dirname = import.meta.url; const executionPath = path .dirname(__dirname.replace(`file:${path.sep}${path.sep}`, '')) .split(path.sep); const distPathIndex = executionPath.lastIndexOf('dist'); const dashboardPath = executionPath.slice(0, distPathIndex + 1).join(path.sep) + path.sep + 'dashboard'; execSync(`cp -L -R ${dashboardPath}${path.sep}* ${cachePath}`); const port = 9000; const serve = exec(`npx serve ${cachePath} -p ${port}`); open(`http://localhost:${port}`); serve.stdout?.on('data', (data) => { console.log(`dashboard server: ${data}`); }); } catch (e) { // eslint-disable-next-line no-console console.log('tried to open file but could not... it may be because we are in a virtual env'); console.log(e); } } }); } setIsFinished(true); } })(); }, [isReady]); return { tasks, results, currentCommit, configErrors, isConfigValid, isHistoryDirty, isReady, tags, logs: logger.logs, isFinished, }; };