@@ -12,31 +12,86 @@ const execPromise = util.promisify(childProcess.exec)
1212 * Filtre les icônes d'une collection en fonction d'une liste de noms.
1313 * @function
1414 *
15- * @param {string } sourcePath - Fichier source
16- * @param {string } targetPath - Fichier destination
15+ * @param {string } sourcePath - Chemin vers le fichier source
16+ * @param {string } targetPath - Chemin vers le fichier destination
17+ *
18+ * @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
1719 *
1820 */
1921export async function createCustomCollectionFile ( sourcePath , targetPath ) {
20- /**
21- * @type {[import('@iconify/vue').IconifyJSON, string[]][] }
22- */
23- const collectionsToFilter = await import ( sourcePath ) . then ( ( { collectionsToFilter } ) => collectionsToFilter )
22+ const [ error , collectionsToFilter ] = await getCollectionsToFilter ( sourcePath )
23+
24+ if ( error ) {
25+ return {
26+ status : 'COULD_NOT_GET_COLLECTIONS_ERROR' ,
27+ error,
28+ }
29+ }
2430
2531 const collections = collectionsToFilter . map ( tuple => filterIcons ( ...tuple ) )
2632
2733 const code = `import type { IconifyJSON } from '@iconify/vue'
2834const collections: IconifyJSON[] = ${ JSON . stringify ( collections ) }
2935export default collections`
3036
31- await fs . writeFile ( targetPath , code )
37+ try {
38+ await fs . writeFile ( targetPath , code )
39+ } catch ( error ) {
40+ console . error ( error )
41+ if ( error instanceof Error ) {
42+ return {
43+ status : 'COULD_NOT_WRITE_FILE_ERROR' ,
44+ error,
45+ }
46+ }
47+ return {
48+ status : 'COULD_NOT_WRITE_FILE_ERROR' ,
49+ error : new Error ( `Erreur inconnue : ${ error } ` ) ,
50+ }
51+ }
3252
33- await runShellCommand ( `npx eslint ${ path . resolve ( process . cwd ( ) , targetPath ) } --fix` )
53+ try {
54+ await runShellCommand ( `npx eslint ${ path . resolve ( process . cwd ( ) , targetPath ) } --fix` )
55+ } catch ( error ) {
56+ if ( error instanceof Error ) {
57+ return {
58+ status : 'COULD_NOT_LINT_FILE_ERROR' ,
59+ error,
60+ }
61+ }
62+ return {
63+ status : 'COULD_NOT_LINT_FILE_ERROR' ,
64+ error : new Error ( `Erreur inconnue : ${ error } ` ) ,
65+ }
66+ }
3467}
3568
3669/**
3770 * Fonctions utilitaires
3871 */
3972
73+ /**
74+ * @function
75+ *
76+ * @param {string } sourcePath - Chemin vers le fichier source
77+ *
78+ * @returns {Promise<[Error] | [null, [import('@iconify/vue').IconifyJSON, string[]][]]> }
79+ */
80+ async function getCollectionsToFilter ( sourcePath ) {
81+ try {
82+ /**
83+ * @type {[import('@iconify/vue').IconifyJSON, string[]][] }
84+ */
85+ const collectionsToFilter = await import ( sourcePath ) . then ( ( { collectionsToFilter } ) => collectionsToFilter )
86+ return [ null , collectionsToFilter ]
87+ } catch ( error ) {
88+ if ( error instanceof Error ) {
89+ return [ error ]
90+ }
91+ return [ new Error ( `Erreur inconnue : ${ error } ` ) ]
92+ }
93+ }
94+
4095/**
4196 * Filtre les icônes d'une collection en fonction d'une liste de noms.
4297 * @function
0 commit comments