Skip to content

Instantly share code, notes, and snippets.

@gusbemacbe
Last active June 7, 2023 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gusbemacbe/dfc36c101c9bb7e4ea16b254784476f2 to your computer and use it in GitHub Desktop.
Save gusbemacbe/dfc36c101c9bb7e4ea16b254784476f2 to your computer and use it in GitHub Desktop.
Customising CSS Jest coverage files
import fs from 'fs';
import path from 'path';
const sourceFolder = 'assets/css/dark/';
const destinationFolders = ['app', 'pages'];
const baseDirectory = '../coverage';
function copyDirectory(source: string, destination: string) {
if (!fs.existsSync(destination)) {
fs.mkdirSync(destination, { recursive: true });
}
const files = fs.readdirSync(source);
for (const file of files) {
const sourcePath = path.join(source, file);
const destinationPath = path.join(destination, file);
if (fs.lstatSync(sourcePath).isDirectory()) {
copyDirectory(sourcePath, destinationPath);
} else {
fs.copyFileSync(sourcePath, destinationPath);
}
}
}
function findDestinationFolders(basePath: string) {
const folders: string[] = [];
function searchFolders(currentPath: string) {
const items = fs.readdirSync(currentPath);
for (const item of items) {
const itemPath = path.join(currentPath, item);
if (fs.lstatSync(itemPath).isDirectory()) {
if (destinationFolders.includes(item)) {
folders.push(itemPath);
} else {
searchFolders(itemPath);
}
}
}
}
searchFolders(basePath);
return folders;
}
function copyToDestinationFolders() {
const currentDir = process.cwd();
const baseFolderPath = path.join(currentDir, baseDirectory);
if (!fs.existsSync(baseFolderPath) || !fs.lstatSync(baseFolderPath).isDirectory()) {
console.log(`Base folder "${baseFolderPath}" does not exist.`);
return;
}
const destinationPaths = findDestinationFolders(baseFolderPath);
if (destinationPaths.length === 0) {
console.log('Destination folders not found.');
return;
}
for (const destinationPath of destinationPaths) {
const sourcePath = path.join(currentDir, sourceFolder);
copyDirectory(sourcePath, destinationPath);
console.log(`Folder copied to ${path.relative(baseFolderPath, destinationPath)} successfully!`);
}
}
copyToDestinationFolders();
const fs = require('fs');
const os = require('os');
const path = require('path');
const sourceFolder = 'assets/css/dark/';
const destinationFolders = ['app', 'pages'];
const baseDirectory = 'coverage';
function copyDirectory(source, destination)
{
if (!fs.existsSync(destination))
{
fs.mkdirSync(destination, { recursive: true });
}
const files = fs.readdirSync(source);
for (const file of files)
{
const sourcePath = path.join(source, file);
const destinationPath = path.join(destination, file);
if (fs.lstatSync(sourcePath).isDirectory()) {
copyDirectory(sourcePath, destinationPath);
}
else
{
fs.copyFileSync(sourcePath, destinationPath);
}
}
}
function findDestinationFolders(basePath)
{
const folders = [];
function searchFolders(currentPath)
{
const items = fs.readdirSync(currentPath);
for (const item of items)
{
const itemPath = path.join(currentPath, item);
if (fs.lstatSync(itemPath).isDirectory())
{
if (destinationFolders.includes(item))
{
folders.push(itemPath);
}
else
{
searchFolders(itemPath);
}
}
}
}
searchFolders(basePath);
return folders;
}
function copyToDestinationFolders() {
const currentDir = process.cwd();
const baseFolderPath = path.join(currentDir, baseDirectory);
if (!fs.existsSync(baseFolderPath) || !fs.lstatSync(baseFolderPath).isDirectory())
{
// Detecting the OS system language
const language = os.env.LANG || 'en';
// Extract the language code
const languageCode = language.split('-')[0];
// Setting the error message according to the OS system language
const errorMessage = languageCode === 'pt' ? `Pasta de base «${baseFolderPath}» não encontrada.` : `Base folder “${baseFolderPath}” not found.`;
// Printing the error message
console.log(errorMessage);
return;
}
const destinationPaths = findDestinationFolders(baseFolderPath);
if (destinationPaths.length === 0)
{
// Detecting the OS system language
const language = os.env.LANG || 'en';
// Extract the language code
const languageCode = language.split('-')[0];
// Setting the error message according to the OS system language
const errorMessage = languageCode === 'pt' ? 'Pastas de destino não encontradas.' : 'Destination folders not found.';
// Printing the error message
console.log(errorMessage);
return;
}
for (const destinationPath of destinationPaths)
{
const sourcePath = path.join(currentDir, sourceFolder);
copyDirectory(sourcePath, destinationPath);
// Detetcing the OS system
const osSystem = os.type();
if (osSystem === 'Linux' || osSystem === 'Darwin' || osSystem === 'mysys' || osSystem === '*microsoft*')
{
// Detecting the OS system language
const language = process.env.LANG?.split('_')[0] || 'en';
// Extract the language code
const languageCode = language.split('-')[0];
// Setting the success message according to the OS system language
const successMessage = languageCode === 'pt' ? `Pasta «${sourceFolder}» copiada para «${destinationPath}» com sucesso.` : `Folder “${sourceFolder}” copied to “${destinationPath}” successfully.`;
// Printing the success message
console.log(successMessage);
}
else
{
// Detecting the OS system language
const language = os.env.LANG || 'en';
// Extract the language code
const languageCode = language.split('-')[0];
// Setting the error message according to the OS system language
const successMessage = languageCode === 'pt' ? `Pasta copiada com ficheiros para «${path.relative(baseFolderPath, destinationPath)}» com sucesso!` : `Folder copied to “${path.relative(baseFolderPath, destinationPath)} successfully!”`;
// Printing the success message
console.log(successMessage);
}
}
}
copyToDestinationFolders();
import fs from 'fs';
import path from 'path';
const os = require('os');
const sourceFolder = 'assets/css/dark/';
const destinationFolders = ['app', 'pages'];
const baseDirectory = '../coverage';
function copyDirectory(source: string, destination: string)
{
if (!fs.existsSync(destination))
{
fs.mkdirSync(destination, { recursive: true });
}
const files = fs.readdirSync(source);
for (const file of files)
{
const sourcePath = path.join(source, file);
const destinationPath = path.join(destination, file);
if (fs.lstatSync(sourcePath).isDirectory()) {
copyDirectory(sourcePath, destinationPath);
}
else
{
fs.copyFileSync(sourcePath, destinationPath);
}
}
}
function findDestinationFolders(basePath: string)
{
const folders: string[] = [];
function searchFolders(currentPath: string)
{
const items = fs.readdirSync(currentPath);
for (const item of items)
{
const itemPath = path.join(currentPath, item);
if (fs.lstatSync(itemPath).isDirectory())
{
if (destinationFolders.includes(item))
{
folders.push(itemPath);
}
else
{
searchFolders(itemPath);
}
}
}
}
searchFolders(basePath);
return folders;
}
function copyToDestinationFolders() {
const currentDir = process.cwd();
const baseFolderPath = path.join(currentDir, baseDirectory);
if (!fs.existsSync(baseFolderPath) || !fs.lstatSync(baseFolderPath).isDirectory())
{
// Detecting the OS system language
const language = os.env.LANG || 'en';
// Extract the language code
const languageCode = language.split('-')[0];
// Setting the error message according to the OS system language
const errorMessage = languageCode === 'pt' ? `Pasta de base «${baseFolderPath}» não encontrada.` : `Base folder “${baseFolderPath}” not found.`;
// Printing the error message
console.log(errorMessage);
return;
}
const destinationPaths = findDestinationFolders(baseFolderPath);
if (destinationPaths.length === 0)
{
// Detecting the OS system language
const language = os.env.LANG || 'en';
// Extract the language code
const languageCode = language.split('-')[0];
// Setting the error message according to the OS system language
const errorMessage = languageCode === 'pt' ? 'Pastas de destino não encontradas.' : 'Destination folders not found.';
// Printing the error message
console.log(errorMessage);
return;
}
for (const destinationPath of destinationPaths)
{
const sourcePath = path.join(currentDir, sourceFolder);
copyDirectory(sourcePath, destinationPath);
// Detetcing the OS system
const osSystem = os.type();
if (osSystem === 'Linux' || osSystem === 'Darwin' || osSystem === 'mysys' || osSystem === '*microsoft*')
{
// Detecting the OS system language
const language = process.env.LANG?.split('_')[0] || 'en';
// Setting the error message according to the OS system language
const successMessage = language === 'pt' ? `Pasta copiada com ficheiros para «${path.relative(baseFolderPath, destinationPath)}» com sucesso!` : `Folder copied to “${path.relative(baseFolderPath, destinationPath)} successfully!”`;
// Printing the success message
console.log(successMessage);
}
else
{
// Detecting the OS system language
const language = os.env.LANG || 'en';
// Extract the language code
const languageCode = language.split('-')[0];
// Setting the error message according to the OS system language
const successMessage = languageCode === 'pt' ? `Pasta copiada com ficheiros para «${path.relative(baseFolderPath, destinationPath)}» com sucesso!` : `Folder copied to “${path.relative(baseFolderPath, destinationPath)} successfully!”`;
// Printing the success message
console.log(successMessage);
}
}
}
copyToDestinationFolders();
module.exports = {
// Other Jest configuration options...
coverageReporters: [
'custom-reporter', // Specify your custom reporter here
'lcov',
'clover'
],
};
{
"scripts":
{
"test": "ts-node custom-reporters-02.ts",
},
"devDependencies":
{
"@types/node": "^20.2.5",
"intl": "^1.2.5",
"ts-node": "^10.9.1",
"typescript": "^5.1.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment