Skip to content

Commit c54b2cf

Browse files
authored
[Feat] Populate the code (#2)
1 parent 61263e1 commit c54b2cf

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

src/calculate-values.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import fs from 'node:fs/promises';
2+
import path from 'node:path';
3+
4+
import type { FileExtension } from './types';
5+
6+
const calculateValues = async (
7+
fileNames: string[],
8+
ignoreEmptyLines: boolean,
9+
typescriptExtensions: FileExtension[],
10+
) => {
11+
const totalFilesAll = fileNames.length;
12+
let totalLinesAll = 0;
13+
let totalFilesTS = 0;
14+
let totalLinesTS = 0;
15+
16+
for (const fileName of fileNames) {
17+
const ext = path.extname(fileName) as FileExtension;
18+
const fileContent = await fs.readFile(fileName, 'utf-8');
19+
const allLines = fileContent.split('\n');
20+
const lines = ignoreEmptyLines ? allLines.filter(Boolean) : allLines;
21+
const lineCount = lines.length;
22+
23+
if (typescriptExtensions.includes(ext)) {
24+
totalLinesTS += lineCount;
25+
totalFilesTS += 1;
26+
}
27+
28+
totalLinesAll += lineCount;
29+
}
30+
31+
const migratedPercentage = (totalLinesTS / totalLinesAll) * 100;
32+
return { migratedPercentage, totalFilesAll, totalLinesAll, totalFilesTS, totalLinesTS };
33+
};
34+
35+
export default calculateValues;

src/index.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import chalk from 'chalk';
2+
import fs from 'node:fs/promises';
3+
import { glob } from 'glob';
4+
import process from 'node:process';
5+
6+
import calculateValues from './calculate-values';
7+
import normalizeConfig from './normalize-config';
8+
9+
const configFileName = 'type-coverage.config.json';
10+
11+
try {
12+
const configPath = `${process.cwd()}/${configFileName}`;
13+
14+
let configData = '';
15+
try {
16+
configData = await fs.readFile(configPath, 'utf-8');
17+
} catch (err) {
18+
console.warn(chalk.yellow(`Couldn't find ${configFileName} file, using defaults`));
19+
}
20+
21+
const { include, exclude, ignoreEmptyLines, tsExtensions } = normalizeConfig(configData);
22+
23+
console.log(`Including files from: ${include}`);
24+
console.log(`Excluding files from: ${exclude}`);
25+
26+
const fileNames = await glob(include, { ignore: exclude });
27+
const { migratedPercentage, totalFilesTS, totalFilesAll } = await calculateValues(
28+
fileNames,
29+
ignoreEmptyLines,
30+
tsExtensions,
31+
);
32+
33+
console.log(
34+
chalk.green(
35+
`The state of the TypeScript migration: ${chalk.underline(
36+
`we are currently at ${migratedPercentage.toFixed(2)}%`,
37+
)}`,
38+
),
39+
);
40+
console.log(`Total Typescript files: ${totalFilesTS} (${tsExtensions})`);
41+
console.log(`Total files found: ${totalFilesAll} (all types)`);
42+
} catch (error) {
43+
console.error(chalk.red('Error while executing:', error));
44+
process.exit(1);
45+
}
46+
47+
export type { FileExtension, Settings } from './types';
48+
export { defaultSettings } from './normalize-config';
49+
export { calculateValues, normalizeConfig };

src/normalize-config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { Settings } from './types';
2+
3+
export const defaultSettings: Settings = {
4+
include: 'src/**/*.{ts,tsx,js,jsx}',
5+
exclude: ["**/__tests__/**"],
6+
ignoreEmptyLines: false,
7+
tsExtensions: ['.ts', '.tsx'],
8+
};
9+
10+
const normalizeConfig = (configData: string) => {
11+
try {
12+
const settings: Settings = JSON.parse(configData);
13+
14+
return {
15+
include: settings.include ?? defaultSettings.include,
16+
exclude: settings.exclude ?? defaultSettings.exclude,
17+
ignoreEmptyLines: settings.ignoreEmptyLines ?? defaultSettings.ignoreEmptyLines,
18+
tsExtensions: settings.tsExtensions ?? defaultSettings.tsExtensions,
19+
};
20+
} catch (err) {
21+
return defaultSettings;
22+
}
23+
};
24+
25+
export default normalizeConfig;

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type FileExtension = `.${string}`;
2+
3+
export type Settings = {
4+
include: string | string[];
5+
exclude: string | string[];
6+
ignoreEmptyLines: boolean;
7+
tsExtensions: FileExtension[];
8+
};

0 commit comments

Comments
 (0)