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'
63import { getPackageJson } from './get-package-json'
4+ import { initScriptDelete } from './init-script-delete'
5+ import { initScriptInstructions } from './init-script-instructions'
6+ import { initScriptKey } from './init-script-key'
7+ import { initScriptRename } from './init-script-rename'
78import { initScriptVersion } from './init-script-version'
8- import { searchAndReplace } from './search-and-replace'
99import { Task , taskFail } from './vendor/clack-tasks'
10- import { namesValues } from './vendor/names'
1110
1211export function createAppTaskRunInitScript ( args : GetArgsResult ) : Task {
1312 return {
1413 enabled : ! args . skipInit ,
1514 title : 'Running init script' ,
1615 task : async ( result ) => {
1716 try {
18- const init = getInitScript ( args . targetDirectory )
17+ const { contents : packageJson } = getPackageJson ( args . targetDirectory )
18+ const init = packageJson [ initScriptKey ]
1919 if ( ! init ) {
2020 return result ( { message : 'Repository does not have an init script' } )
2121 }
@@ -27,62 +27,20 @@ export function createAppTaskRunInitScript(args: GetArgsResult): Task {
2727 if ( args . verbose ) {
2828 log . warn ( `initCheckVersion done` )
2929 }
30- await initRename ( args , init , args . verbose )
30+ await initScriptRename ( args , init . rename , args . verbose )
3131 if ( args . verbose ) {
3232 log . warn ( `initRename done` )
3333 }
3434
35- const instructions : string [ ] = ( initInstructions ( init ) ?? [ ] )
35+ const instructions : string [ ] = initScriptInstructions ( init . instructions , args . verbose )
3636 ?. filter ( Boolean )
3737 . map ( ( msg ) => msg . replace ( '{pm}' , args . packageManager ) )
3838
39- if ( args . verbose ) {
40- log . warn ( `initInstructions done` )
41- }
42- deleteInitScript ( args . targetDirectory )
43- if ( args . verbose ) {
44- log . warn ( `deleteInitScript done` )
45- }
39+ initScriptDelete ( args )
4640 return result ( { message : 'Executed init script' , instructions } )
4741 } catch ( error ) {
4842 taskFail ( `init: Error running init script: ${ error } ` )
4943 }
5044 } ,
5145 }
5246}
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