Skip to content

Commit

Permalink
chore(meta): 🐛 corrige le code asynchrone
Browse files Browse the repository at this point in the history
  • Loading branch information
laruiss committed Jan 20, 2025
1 parent fa42356 commit 8c02e50
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 12 deletions.
32 changes: 28 additions & 4 deletions meta/custom-icon-collections-creator-bin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env node

/* eslint-disable no-console */
import path from 'node:path'
import process from 'node:process'

Expand All @@ -17,7 +17,31 @@ program

const options = program.opts()

if (options.source && options.target) {
createCustomCollectionFile(path.resolve(process.cwd(), options.source), path.resolve(process.cwd(), options.target))
console.log(chalk.green('Les icônes ont été générées')) // eslint-disable-line no-console
const resultMessages = {
COULD_NOT_GET_COLLECTIONS_ERROR: 'Impossible de récupérer les collections d’icônes (cf. erreur ci-dessus)',
COULD_NOT_WRITE_FILE_ERROR: 'Impossible d’écrire le fichier cible (cf. erreur ci-dessus)',
COULD_NOT_LINT_FILE_ERROR: 'Impossible de linter le fichier cible (cf. erreur ci-dessus)',
}

;(async (options) => {
if (!options.source || !options.target) {
console.log(chalk.yellow('Veuillez indiquer la source et la cible'))
}

const result = await createCustomCollectionFile(path.resolve(process.cwd(), options.source), path.resolve(process.cwd(), options.target))

if (!result) {
console.log(chalk.green('Les icônes ont été générées'))
return
}

if (result.status === 'COULD_NOT_LINT_FILE_ERROR') {
console.log(chalk.green('Les icônes ont été générées'))
console.log(chalk.yellow(resultMessages[result.status]))
return
}

console.error(result.error)

console.log(chalk.red(resultMessages[result.status]))
})(options)
71 changes: 63 additions & 8 deletions meta/custom-icon-collections-creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,86 @@ const execPromise = util.promisify(childProcess.exec)
* Filtre les icônes d'une collection en fonction d'une liste de noms.
* @function
*
* @param {string} sourcePath - Fichier source
* @param {string} targetPath - Fichier destination
* @param {string} sourcePath - Chemin vers le fichier source
* @param {string} targetPath - Chemin vers le fichier destination
*
* @returns {Promise<{ status: 'COULD_NOT_GET_COLLECTIONS_ERROR' | 'COULD_NOT_WRITE_FILE_ERROR' | 'COULD_NOT_LINT_FILE_ERROR', error: Error } | undefined>} Le résultat si une erreur est survenue, undefined sinon
*
*/
export async function createCustomCollectionFile (sourcePath, targetPath) {
/**
* @type {[import('@iconify/vue').IconifyJSON, string[]][]}
*/
const collectionsToFilter = await import(sourcePath).then(({ collectionsToFilter }) => collectionsToFilter)
const [error, collectionsToFilter] = await getCollectionsToFilter(sourcePath)

if (error) {
return {
status: 'COULD_NOT_GET_COLLECTIONS_ERROR',
error,
}
}

const collections = collectionsToFilter.map(tuple => filterIcons(...tuple))

const code = `import type { IconifyJSON } from '@iconify/vue'
const collections: IconifyJSON[] = ${JSON.stringify(collections)}
export default collections`

await fs.writeFile(targetPath, code)
try {
await fs.writeFile(targetPath, code)
} catch (error) {
console.error(error)
if (error instanceof Error) {
return {
status: 'COULD_NOT_WRITE_FILE_ERROR',
error,
}
}
return {
status: 'COULD_NOT_WRITE_FILE_ERROR',
error: new Error(`Erreur inconnue : ${error}`),
}
}

await runShellCommand(`npx eslint ${path.resolve(process.cwd(), targetPath)} --fix`)
try {
await runShellCommand(`npx eslint ${path.resolve(process.cwd(), targetPath)} --fix`)
} catch (error) {
if (error instanceof Error) {
return {
status: 'COULD_NOT_LINT_FILE_ERROR',
error,
}
}
return {
status: 'COULD_NOT_LINT_FILE_ERROR',
error: new Error(`Erreur inconnue : ${error}`),
}
}
}

/**
* Fonctions utilitaires
*/

/**
* @function
*
* @param {string} sourcePath - Chemin vers le fichier source
*
* @returns {Promise<[Error] | [null, [import('@iconify/vue').IconifyJSON, string[]][]]>}
*/
async function getCollectionsToFilter (sourcePath) {
try {
/**
* @type {[import('@iconify/vue').IconifyJSON, string[]][]}
*/
const collectionsToFilter = await import(sourcePath).then(({ collectionsToFilter }) => collectionsToFilter)
return [null, collectionsToFilter]
} catch (error) {
if (error instanceof Error) {
return [error]
}
return [new Error(`Erreur inconnue : ${error}`)]
}
}

/**
* Filtre les icônes d'une collection en fonction d'une liste de noms.
* @function
Expand Down

0 comments on commit 8c02e50

Please sign in to comment.