11#!/usr/bin/env node
2+ import { fileURLToPath , URL } from 'url'
23import { readFile , writeFile } from 'fs/promises'
34import inquirer from 'inquirer'
45import chalk from 'chalk'
@@ -7,22 +8,25 @@ import path from 'path'
78
89const isCI = process . argv . includes ( '--ci' )
910
10- const __dirname = path . dirname ( new URL ( import . meta. url ) . pathname )
11+ const getNormalizedDir = ( relativeDir ) => fileURLToPath ( new URL ( relativeDir , import . meta. url ) )
1112
12- const sfcs = await globby ( [ path . resolve ( __dirname , '../src/components/**/*.vue' ) ] )
13- const componentsDir = path . resolve ( __dirname , '../ src/components')
13+ // const sfcs = await globby(fileURLToPath(new URL( '../src/components/**/*.vue', import.meta.url)) )
14+ const sfcs = await globby ( ' src/components/**/*.vue ')
1415
15- const projectFn = component => 'export { default as ' + path . basename ( component , '.vue' ) + ' } from \'' + component . replace ( componentsDir , '.' ) + '\''
16+ const projectFn = component => 'export { default as ' + path . basename ( component , '.vue' ) + ' } from \'' + component . replace ( 'src/components' , '.' ) + '\''
1617
1718const correctComponentList = sfcs . map ( projectFn ) . sort ( )
1819const correctString = correctComponentList . join ( '\n' ) + '\n'
1920
20- const index = await readFile ( path . resolve ( __dirname , '../src/components/index.js' ) )
21+ const srcIndexFullpath = getNormalizedDir ( '../src/components' ) + path . sep + 'index.js'
22+ const typesIndexFullpath = getNormalizedDir ( '../types/components' ) + path . sep + 'index.d.ts'
23+
24+ const index = await readFile ( getNormalizedDir ( '../src/components' ) + '/index.js' )
2125const currentFileContent = index . toString ( )
2226
2327if ( currentFileContent !== correctString ) {
2428 if ( process . argv . includes ( '--fix' ) ) {
25- await writeFile ( path . resolve ( __dirname , '../src/components/index.js' ) , correctString )
29+ await writeFile ( srcIndexFullpath , correctString )
2630 console . log ( 'Fixed' )
2731 process . exit ( 0 )
2832 }
@@ -51,14 +55,14 @@ if (currentFileContent !== correctString) {
5155 }
5256
5357 if ( onlyInCorrectList . length || onlyInCurrentFileList . length ) {
54- console . log ( 'dans ' + chalk . yellow . bold ( path . resolve ( __dirname , '../src/components/index.js' ) ) )
58+ console . log ( 'dans ' + chalk . yellow . bold ( srcIndexFullpath ) )
5559 }
5660
5761 if ( ! isCI ) {
5862 await inquirer . prompt ( questions ) . then ( async answers => {
5963 if ( answers . fix . toLocaleLowerCase ( ) === 'y' ) {
60- await writeFile ( path . resolve ( __dirname , '../src/components/index.js' ) , correctString )
61- await writeFile ( path . resolve ( __dirname , '../types/components/index.d.ts' ) , correctString )
64+ await writeFile ( srcIndexFullpath , correctString )
65+ await writeFile ( typesIndexFullpath , correctString )
6266 console . log ( chalk . green . bold ( 'Fichier corrigé !' ) )
6367 process . exit ( 0 )
6468 }
0 commit comments