From 1a77e032aa7a15e899584b1b553a7dc6420ce36e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanislas=20Ormi=C3=A8res?= Date: Tue, 14 Jan 2025 13:08:46 +0100 Subject: [PATCH] =?UTF-8?q?chore(meta):=20=F0=9F=90=9B=20corrige=20le=20co?= =?UTF-8?q?de=20asynchrone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- meta/custom-icon-collections-creator-bin.js | 32 ++++++++++-- meta/custom-icon-collections-creator.js | 56 ++++++++++++++++++--- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/meta/custom-icon-collections-creator-bin.js b/meta/custom-icon-collections-creator-bin.js index 7e09eb71..7d367456 100755 --- a/meta/custom-icon-collections-creator-bin.js +++ b/meta/custom-icon-collections-creator-bin.js @@ -1,5 +1,5 @@ #!/usr/bin/env node - +/* eslint-disable no-console */ import path from 'node:path' import process from 'node:process' @@ -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) diff --git a/meta/custom-icon-collections-creator.js b/meta/custom-icon-collections-creator.js index 9f99f236..17563b20 100644 --- a/meta/custom-icon-collections-creator.js +++ b/meta/custom-icon-collections-creator.js @@ -12,15 +12,21 @@ 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)) @@ -28,15 +34,49 @@ export async function createCustomCollectionFile (sourcePath, targetPath) { 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) + return { + status: 'COULD_NOT_WRITE_FILE_ERROR', + error, + } + } - await runShellCommand(`npx eslint ${path.resolve(process.cwd(), targetPath)} --fix`) + try { + await runShellCommand(`npx eslint ${path.resolve(process.cwd(), targetPath)} --fix`) + } catch (error) { + return { + status: 'COULD_NOT_LINT_FILE_ERROR', + 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) { + return [error] + } +} + /** * Filtre les icônes d'une collection en fonction d'une liste de noms. * @function