|
1 | 1 | #!/usr/bin/env node |
2 | 2 | import { readFile, writeFile } from 'fs/promises' |
| 3 | +import inquirer from 'inquirer' |
| 4 | +import chalk from 'chalk' |
3 | 5 | import { globby } from 'globby' |
4 | 6 | import path from 'path' |
5 | 7 |
|
| 8 | +const isCI = process.argv.includes('--ci') |
| 9 | + |
6 | 10 | const __dirname = path.dirname(new URL(import.meta.url).pathname) |
7 | 11 |
|
8 | 12 | const sfcs = await globby([path.resolve(__dirname, '../src/components/**/*.vue')]) |
9 | 13 | const componentsDir = path.resolve(__dirname, '../src/components') |
10 | 14 |
|
11 | 15 | const projectFn = component => 'export { default as ' + path.basename(component, '.vue') + ' } from \'' + component.replace(componentsDir, '.') + '\'' |
12 | 16 |
|
13 | | -const finalString = sfcs.map(projectFn).sort().join('\n') + '\n' |
| 17 | +const correctComponentList = sfcs.map(projectFn).sort() |
| 18 | +const correctString = correctComponentList.join('\n') + '\n' |
14 | 19 |
|
15 | 20 | const index = await readFile(path.resolve(__dirname, '../src/components/index.js')) |
| 21 | +const currentFileContent = index.toString() |
16 | 22 |
|
17 | | -if (index.toString() != finalString) { |
| 23 | +if (currentFileContent !== correctString) { |
18 | 24 | if (process.argv.includes('--fix')) { |
19 | | - await writeFile(path.resolve(__dirname, '../src/components/index.js'), finalString) |
| 25 | + await writeFile(path.resolve(__dirname, '../src/components/index.js'), correctString) |
20 | 26 | console.log('Fixed') |
21 | 27 | process.exit(0) |
22 | 28 | } |
23 | | - console.error('Le fichier d’export des composants src/components/index.js n’est pas correct') |
| 29 | + |
| 30 | + const questions = [ |
| 31 | + { |
| 32 | + type: 'input', |
| 33 | + name: 'fix', |
| 34 | + message: 'Le fichier d’export des composants src/components/index.js n’est pas correct\n Est ce que vous voulez corriger (y/N) ?', |
| 35 | + }, |
| 36 | + ] |
| 37 | + |
| 38 | + const missingComponent = '' |
| 39 | + const currentFileContentAsList = currentFileContent.split('\n').slice(0, -1).sort() |
| 40 | + const onlyInCorrectList = correctComponentList.filter(line => !currentFileContentAsList.includes(line)) |
| 41 | + if (onlyInCorrectList.length) { |
| 42 | + console.log(chalk.yellow.bold('\nLes lignes suivantes sont manquantes :')) |
| 43 | + onlyInCorrectList.forEach(line => console.log(chalk.red.bold(line))) |
| 44 | + console.log('') |
| 45 | + } |
| 46 | + |
| 47 | + const onlyInCurrentFileList = currentFileContentAsList.filter(line => !correctComponentList.includes(line)) |
| 48 | + if (onlyInCurrentFileList.length) { |
| 49 | + console.log(chalk.yellow.bold('\nLes lignes suivantes sont en trop :')) |
| 50 | + onlyInCurrentFileList.forEach(line => console.log(chalk.red.bold(line))) |
| 51 | + console.log('') |
| 52 | + } |
| 53 | + |
| 54 | + if (onlyInCorrectList.length || onlyInCurrentFileList.length) { |
| 55 | + console.log('dans ' + chalk.yellow.bold(path.resolve(__dirname, '../src/components/index.js'))) |
| 56 | + } |
| 57 | + |
| 58 | + if (!isCI) { |
| 59 | + await inquirer.prompt(questions).then(async answers => { |
| 60 | + if (answers.fix.toLocaleLowerCase() === 'y') { |
| 61 | + await writeFile(path.resolve(__dirname, '../src/components/index.js'), correctString) |
| 62 | + console.log(chalk.green.bold('Fichier corrigé !')) |
| 63 | + process.exit(0) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
24 | 67 | process.exit(1) |
25 | 68 | } |
0 commit comments