Skip to content

Commit

Permalink
chore: use async fs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrl committed Aug 30, 2023
1 parent e1a2901 commit f62994a
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions bin/generate-responsive-icons.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ts = require('typescript')
const fs = require('fs')
const fs = require('fs').promises
const path = require('path')

const ICON_COMPONENTS_DIRECTORY = 'packages/picasso/src/Icon'
Expand Down Expand Up @@ -38,10 +38,10 @@ const removeSize = name => name.replace(/(16|24)/, '')
const removePrefix = name => name.replace(/\.\//, '')

// Get all exports from index.ts using the TypeScript compiler API
const getExportsInFile = file => {
const getExportsInFile = async file => {
const sourceFile = ts.createSourceFile(
'index.ts',
fs.readFileSync(file, 'utf8'),
await fs.readFile(file, 'utf8'),
ts.ScriptTarget.Latest
)

Expand All @@ -64,26 +64,22 @@ const getExportsInFile = file => {
]
}

const generateResponsiveIconFiles = iconNames => {
iconNames.forEach(name => {
fs.writeFileSync(
const generateResponsiveIconFiles = async iconNames => {
iconNames.forEach(async name => {
await fs.writeFile(
`${ICONS_DIRECTORY_PATH}/${name}Responsive.tsx`,
template(name)
)
})
}

const insertExports = (iconNames, indexFilePath) => {
const insertExports = async (iconNames, indexFilePath) => {
const exportLines = iconNames.map(
item => `export { default as ${item}Responsive } from './${item}Responsive'`
)

fs.readFile(indexFilePath, 'utf-8', (err, data) => {
if (err) {
console.error('Error reading file:', err)

return
}
try {
const data = await fs.readFile(indexFilePath, 'utf-8')

const newExportLines = exportLines.filter(line => !data.includes(line))

Expand All @@ -95,18 +91,19 @@ const insertExports = (iconNames, indexFilePath) => {

const updatedContent = data + '\n' + newExportLines.join('\n')

fs.writeFile(indexFilePath, updatedContent, 'utf-8', writeErr => {
if (writeErr) {
console.error('Error writing file:', writeErr)
} else {
console.log('New responsive exports added successfully.')
}
})
})
await fs.writeFile(indexFilePath, updatedContent, 'utf-8')
console.log('New responsive exports added successfully.')
} catch (err) {
console.error('Error writing file:', err)
}
}

const iconNames = getExportsInFile(INDEX_FILE_PATH)
;(async () => {
const iconNames = await getExportsInFile(INDEX_FILE_PATH)

generateResponsiveIconFiles(iconNames)
await generateResponsiveIconFiles(iconNames)

insertExports(iconNames, INDEX_FILE_PATH)
await insertExports(iconNames, INDEX_FILE_PATH)
})().catch(err => {
console.error(err)
})

0 comments on commit f62994a

Please sign in to comment.