@@ -9,10 +9,10 @@ import ora from 'ora';
99import prompts from 'prompts' ;
1010import { createMatchPath , loadConfig } from 'tsconfig-paths' ;
1111
12- import type { AddOptionsSchema , Registry } from '@/utils/types' ;
12+ import type { AddOptionsSchema , ConfigSchema , Registry } from '@/utils/types' ;
1313
1414import { APP_PATH , REPO_URLS } from '@/utils/constants' ;
15- import { getConfig , getPackageManager } from '@/utils/helpers' ;
15+ import { getConfig , getPackageManager , toCase } from '@/utils/helpers' ;
1616import { addOptionsSchema } from '@/utils/types' ;
1717
1818type FileType = 'hook' | 'package' | 'util' ;
@@ -45,13 +45,29 @@ const resolveDependencies = (registry: Registry, hooks: string[]): Map<string, F
4545 return files ;
4646} ;
4747
48- const updateImports = async ( filePath : string , utilsPath : string ) => {
49- const fileContent = await fs . readFileSync ( filePath , 'utf-8' ) ;
50- const utilsImportRegex = / i m p o r t \s + \{ ( [ ^ } ] + ) \} \s + f r o m \s + [ ' " ] ( @ \/ u t i l s [ ^ ' " ] * ) [ ' " ] / g;
48+ interface UpdateImportsRules {
49+ regex : RegExp ;
50+ replacer : ( ...args : string [ ] ) => string ;
51+ }
52+
53+ const updateImports = async ( filePath : string , config : ConfigSchema ) => {
54+ const fileContent = fs . readFileSync ( filePath , 'utf-8' ) ;
55+
56+ const rules : UpdateImportsRules [ ] = [
57+ {
58+ regex : / i m p o r t \s + \{ ( [ ^ } ] + ) \} \s + f r o m \s + [ ' " ] ( @ \/ u t i l s [ ^ ' " ] * ) [ ' " ] / g,
59+ replacer : ( _ , imports ) => `import {${ imports } } from '${ config . aliases . utils } '`
60+ } ,
61+ {
62+ regex : / i m p o r t \s + (?: t y p e \s + ) ? \{ ( [ ^ } ] + ) \} \s + f r o m \s + [ ' " ] ( \. [ ^ ' " ] * ) [ ' " ] / g,
63+ replacer : ( _ , imports , internalPath ) =>
64+ `import {${ imports } } from '${ toCase ( internalPath , config . case ) } '`
65+ }
66+ ] ;
5167
52- const updatedContent = fileContent . replace (
53- utilsImportRegex ,
54- ( _ , imports ) => `import { ${ imports } } from ' ${ utilsPath } '`
68+ const updatedContent = rules . reduce (
69+ ( acc , { regex , replacer } ) => acc . replace ( regex , replacer ) ,
70+ fileContent
5571 ) ;
5672
5773 fs . writeFileSync ( filePath , updatedContent ) ;
@@ -165,9 +181,11 @@ export const add = {
165181 const files = Array . from ( dependencies . values ( ) )
166182 . map ( ( dependency ) => {
167183 if ( dependency . type === 'hook' ) {
168- const filePath = `${ dependency . name } /${ dependency . name } ` ;
184+ const filePath = toCase ( `${ dependency . name } /${ dependency . name } ` , config . case ) ;
169185 const directoryPath = `${ pathToLoadHooks } /${ filePath } .${ language } ` ;
170- const registryPath = `${ REPO_URLS [ language . toUpperCase ( ) as keyof typeof REPO_URLS ] } /hooks/${ filePath } .${ language } ` ;
186+ const registryPath = `${
187+ REPO_URLS [ language . toUpperCase ( ) as keyof typeof REPO_URLS ]
188+ } /hooks/${ dependency . name } /${ dependency . name } .${ language } `;
171189 const indexPath = `${ pathToLoadHooks } /index.${ language } ` ;
172190 return {
173191 name : dependency . name ,
@@ -180,9 +198,11 @@ export const add = {
180198 }
181199
182200 if ( dependency . type === 'util' ) {
183- const filePath = `${ dependency . name } ` ;
201+ const filePath = toCase ( `${ dependency . name } ` , config . case ) ;
184202 const directoryPath = `${ pathToLoadUtils } /${ filePath } .${ language } ` ;
185- const registryPath = `${ REPO_URLS [ language . toUpperCase ( ) as keyof typeof REPO_URLS ] } /utils/helpers/${ filePath } .${ language } ` ;
203+ const registryPath = `${
204+ REPO_URLS [ language . toUpperCase ( ) as keyof typeof REPO_URLS ]
205+ } /utils/helpers/${ dependency . name } .${ language } `;
186206 const indexPath = `${ pathToLoadUtils } /index.${ language } ` ;
187207 return {
188208 name : dependency . name ,
@@ -233,9 +253,7 @@ export const add = {
233253
234254 const fileResponse = await fetches . get < Buffer > ( registryPath ) ;
235255 await fs . writeFileSync ( directoryPath , fileResponse . data ) ;
236- if ( type === 'hook' ) {
237- await updateImports ( directoryPath , config . aliases . utils ) ;
238- }
256+ await updateImports ( directoryPath , config ) ;
239257
240258 const exportStatement = `export * from './${ filePath } ';\n` ;
241259
@@ -247,7 +265,9 @@ export const add = {
247265
248266 const packageManager = await getPackageManager ( options . cwd ) ;
249267
250- spinner . text = `Installing packages ${ chalk . bold ( packages . join ( ', ' ) ) } with ${ chalk . cyan ( packageManager ) } ` ;
268+ spinner . text = `Installing packages ${ chalk . bold ( packages . join ( ', ' ) ) } with ${ chalk . cyan (
269+ packageManager
270+ ) } `;
251271 if ( packages . length ) {
252272 await execa ( packageManager , [ packageManager === 'npm' ? 'install' : 'add' , ...packages ] , {
253273 cwd : options . cwd
0 commit comments