Skip to content

Commit

Permalink
fix(project): adjust badges replacement
Browse files Browse the repository at this point in the history
The replace match was incorrect

#2
  • Loading branch information
olavoparno committed Dec 8, 2020
1 parent 96fd15f commit 6b60566
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
17 changes: 8 additions & 9 deletions src/editor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import { readmePathConst, coveragePathConst, hashesConst, coverageUrlConst } from './constants';
import { THashes, TReport } from './types';
import { TColors, THashes, TReport } from './types';

export const getReadmeHashes = (readmeFile: string) => {
export const getReadmeHashes = (readmeFile: string): THashes[] => {
console.log('- Getting readme hashes...');

const readmeHashes = hashesConst.coverage.map((hash) => {
Expand All @@ -18,7 +18,7 @@ export const getReadmeHashes = (readmeFile: string) => {
return (filteredHashes as unknown) as THashes[];
};

export const getCoverageColor = (coverage: number) => {
export const getCoverageColor = (coverage: number): TColors => {
if (coverage < 80) {
return 'red';
}
Expand All @@ -29,7 +29,7 @@ export const getCoverageColor = (coverage: number) => {
return 'brightgreen';
};

export const getCoverageBadge = (coverageFile: string, hashKey: string) => {
export const getCoverageBadge = (coverageFile: string, hashKey: string): string | boolean => {
console.log(`- Getting coverage badge url for ${hashKey}...`);

try {
Expand Down Expand Up @@ -64,23 +64,22 @@ export const getNewReadme = (readmeFile: string, coverageFile: string) => (
}

const pattern = `![${hash.value}]`;
const enpatterned = (value: string) => `${pattern}(${value})`;

const startIndex = newReadmeFile.indexOf(pattern);
const valueToChangeStart = newReadmeFile.slice(startIndex + pattern.length);

const valueToChangeIndex = valueToChangeStart.indexOf(')');
const valueToChangeFinal = valueToChangeStart.substring(1, valueToChangeIndex);

const newUrl = `${coverageBadge}`;

newReadmeFile = newReadmeFile.replace(valueToChangeFinal, newUrl);
newReadmeFile = newReadmeFile.replace(enpatterned(valueToChangeFinal), enpatterned(coverageBadge as string));
});

resolve(newReadmeFile);
});
};

export const writeNewReadme = (readmePath: string) => (newReadmeData: string) => {
export const writeNewReadme = (readmePath: string) => (newReadmeData: string): boolean | void => {
console.log('- Writing new readme data...');

try {
Expand All @@ -90,7 +89,7 @@ export const writeNewReadme = (readmePath: string) => (newReadmeData: string) =>
}
};

export const editReadme = () => {
export const editReadme = (): Promise<void> => {
console.log('Info: 2. Editor process started');

const readmeFile = fs.readFileSync(readmePathConst, 'utf-8');
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import { checkConfig } from './validate';
import { editReadme } from './editor';

const badger = () => {
const badger = (): Promise<void> => {
console.log('Info: 0. Istanbul Badges Readme process started');

return Promise.resolve(checkConfig())
return checkConfig()
.then(() => editReadme())
.catch((error) => {
console.log(`Error: ${error}`);
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export type THashes = {
};

export type TReport = {
[total: string]: any;
[total: string]: Record<string, Record<string, number>>;
};

export type TColors = 'red' | 'yellow' | 'brightgreen';
6 changes: 3 additions & 3 deletions src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hashesConst, readmePathConst, coveragePathConst } from './constants';

export const getCoveragePath = (path: string): string => {
let coveragePath: string = path;
let argPath: string = '';
let argPath = '';

const args = process.argv
.filter((item) => {
Expand Down Expand Up @@ -70,10 +70,10 @@ export const doestReadmeHashExist = (readmePath: string): Promise<boolean | stri
});
};

export const checkConfig = () => {
export const checkConfig = (): Promise<void> => {
console.log('Info: 1. Config check process started');

return Promise.resolve(doesReadmeFileExist(readmePathConst))
return doesReadmeFileExist(readmePathConst)
.then(() => {
console.log('- Readme file exists... ✔️.');
})
Expand Down

0 comments on commit 6b60566

Please sign in to comment.