11import { log } from '@clack/prompts'
2- import { join } from 'node:path'
3- import { ensureTargetPath } from './ensure-target-path'
42import { GetArgsResult } from './get-args-result'
5- import { deleteInitScript , getInitScript , InitScript } from './get-init-script'
6- import { getPackageJson } from './get-package-json'
3+ import { getInitScript } from './get-init-script'
4+ import { initScriptDelete } from './init-script-delete'
5+ import { initScriptInstructions } from './init-script-instructions'
6+ import { initScriptRename } from './init-script-rename'
77import { initScriptVersion } from './init-script-version'
8- import { searchAndReplace } from './search-and-replace'
98import { Task , taskFail } from './vendor/clack-tasks'
10- import { namesValues } from './vendor/names'
119
1210export function createAppTaskRunInitScript ( args : GetArgsResult ) : Task {
1311 return {
@@ -27,62 +25,20 @@ export function createAppTaskRunInitScript(args: GetArgsResult): Task {
2725 if ( args . verbose ) {
2826 log . warn ( `initCheckVersion done` )
2927 }
30- await initRename ( args , init , args . verbose )
28+ await initScriptRename ( args , init . rename )
3129 if ( args . verbose ) {
3230 log . warn ( `initRename done` )
3331 }
3432
35- const instructions : string [ ] = ( initInstructions ( init ) ?? [ ] )
33+ const instructions : string [ ] = initScriptInstructions ( init . instructions , args . verbose )
3634 ?. filter ( Boolean )
3735 . map ( ( msg ) => msg . replace ( '{pm}' , args . packageManager ) )
3836
39- if ( args . verbose ) {
40- log . warn ( `initInstructions done` )
41- }
42- deleteInitScript ( args . targetDirectory )
43- if ( args . verbose ) {
44- log . warn ( `deleteInitScript done` )
45- }
37+ initScriptDelete ( args )
4638 return result ( { message : 'Executed init script' , instructions } )
4739 } catch ( error ) {
4840 taskFail ( `init: Error running init script: ${ error } ` )
4941 }
5042 } ,
5143 }
5244}
53-
54- async function initRename ( args : GetArgsResult , init : InitScript , verbose : boolean ) {
55- const { contents } = getPackageJson ( args . targetDirectory )
56- // Rename template from package.json to project name throughout the whole project
57- if ( contents . name ) {
58- await searchAndReplace ( args . targetDirectory , [ contents . name ] , [ args . name ] , false , verbose )
59- }
60-
61- // Return early if there are no renames defined in the init script
62- if ( ! init ?. rename ) {
63- return
64- }
65-
66- // Loop through each word in the rename object
67- for ( const from of Object . keys ( init . rename ) ) {
68- // Get the 'to' property from the rename object
69- const to = init . rename [ from ] . to . replace ( '{{name}}' , args . name . replace ( / - / g, '' ) )
70-
71- // Get the name matrix for the 'from' and the 'to' value
72- const fromNames = namesValues ( from )
73- const toNames = namesValues ( to )
74-
75- for ( const path of init . rename [ from ] . paths ) {
76- const targetPath = join ( args . targetDirectory , path )
77- if ( ! ( await ensureTargetPath ( targetPath ) ) ) {
78- console . error ( `init-script.rename: target does not exist ${ targetPath } ` )
79- continue
80- }
81- await searchAndReplace ( join ( args . targetDirectory , path ) , fromNames , toNames , args . dryRun )
82- }
83- }
84- }
85-
86- function initInstructions ( init : InitScript ) {
87- return init ?. instructions ?. length === 0 ? [ ] : init ?. instructions
88- }
0 commit comments