Created
May 21, 2024 03:21
-
-
Save andirkh/5c3c7c580f6ca6d4c69281a40853c385 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
// Define the directory containing the SVG files | |
const svgDirectory = './flags'; | |
function dashedToCamelCase(input) { | |
return input.replace(/-([a-z])/g, function(match, letter) { | |
return letter.toUpperCase(); | |
}); | |
} | |
// Read the contents of the directory | |
fs.readdir(svgDirectory, (err, files) => { | |
if (err) { | |
console.error('Error reading directory:', err); | |
return; | |
} | |
// Filter out SVG files | |
const svgFiles = files.filter((file) => path.extname(file).toLowerCase() === '.svg'); | |
// Generate import statements | |
const importStatements = svgFiles.map((file) => { | |
const flagName = dashedToCamelCase(path.basename(file, '.svg')); | |
return `import ${flagName} from './${file}';`; | |
}).join('\n'); | |
// Generate export statement | |
const exportStatement = `export { ${svgFiles.map((file) => dashedToCamelCase(path.basename(file, '.svg'))).join(', ')} };`; | |
// Generate the content of the index.ts file | |
const indexFileContent = `${importStatements}\n\n${exportStatement}`; | |
// Write the content to index.ts file | |
const indexPath = path.join(svgDirectory, 'index.ts'); | |
fs.writeFile(indexPath, indexFileContent, (err) => { | |
if (err) { | |
console.error('Error writing index.ts file:', err); | |
return; | |
} | |
console.log('index.ts file created successfully!'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment