Skip to content

Commit b5e2f9f

Browse files
authored
Merge pull request #1021 from dnum-mi/fix/bug-asynchrone-dans-meta
chore(meta): 🐛 corrige le code asynchrone
2 parents fa42356 + 8c02e50 commit b5e2f9f

File tree

2 files changed

+91
-12
lines changed

2 files changed

+91
-12
lines changed

meta/custom-icon-collections-creator-bin.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env node
2-
2+
/* eslint-disable no-console */
33
import path from 'node:path'
44
import process from 'node:process'
55

@@ -17,7 +17,31 @@ program
1717

1818
const options = program.opts()
1919

20-
if (options.source && options.target) {
21-
createCustomCollectionFile(path.resolve(process.cwd(), options.source), path.resolve(process.cwd(), options.target))
22-
console.log(chalk.green('Les icônes ont été générées')) // eslint-disable-line no-console
20+
const resultMessages = {
21+
COULD_NOT_GET_COLLECTIONS_ERROR: 'Impossible de récupérer les collections d’icônes (cf. erreur ci-dessus)',
22+
COULD_NOT_WRITE_FILE_ERROR: 'Impossible d’écrire le fichier cible (cf. erreur ci-dessus)',
23+
COULD_NOT_LINT_FILE_ERROR: 'Impossible de linter le fichier cible (cf. erreur ci-dessus)',
2324
}
25+
26+
;(async (options) => {
27+
if (!options.source || !options.target) {
28+
console.log(chalk.yellow('Veuillez indiquer la source et la cible'))
29+
}
30+
31+
const result = await createCustomCollectionFile(path.resolve(process.cwd(), options.source), path.resolve(process.cwd(), options.target))
32+
33+
if (!result) {
34+
console.log(chalk.green('Les icônes ont été générées'))
35+
return
36+
}
37+
38+
if (result.status === 'COULD_NOT_LINT_FILE_ERROR') {
39+
console.log(chalk.green('Les icônes ont été générées'))
40+
console.log(chalk.yellow(resultMessages[result.status]))
41+
return
42+
}
43+
44+
console.error(result.error)
45+
46+
console.log(chalk.red(resultMessages[result.status]))
47+
})(options)

meta/custom-icon-collections-creator.js

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
1921
export 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'
2834
const collections: IconifyJSON[] = ${JSON.stringify(collections)}
2935
export 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

Comments
 (0)