@@ -904,11 +904,7 @@ function putPackage() {
904
904
}
905
905
906
906
function initGitRepo ( ) {
907
- // Cache the current working directory and change
908
- // working directory to the theme path.
909
- const cwd = process . cwd ( ) ;
910
- process . chdir ( themePath ) ;
911
- // Initialize repository.
907
+ // Initialize git repository.
912
908
const stdoutInit = execSync ( `git init -b main` , { stdio : 'pipe' } ) ;
913
909
logInfo ( {
914
910
title : 'Repo initialized' ,
@@ -939,13 +935,20 @@ function initGitRepo() {
939
935
dataLabel : 'Commit response' ,
940
936
data : stdoutCommit . toString ( ) . trim ( ) ,
941
937
} ) ;
942
- // Return the process back to the original working directory.
943
- process . chdir ( cwd ) ;
944
938
}
945
939
946
940
function initRepo ( ) {
941
+ // Cache the current working directory and change
942
+ // working directory to the theme path. At the end of repo
943
+ // initialization, or on error, the process will be moved
944
+ // back to the cached current working directory (cwd).
945
+ const cwd = process . cwd ( ) ;
946
+ process . chdir ( themePath ) ;
947
947
try {
948
948
// Switch on the repo type and initialize a repo with remote origin.
949
+ // Note that by this time, we have moved the process from the current
950
+ // working directory into the theme, and the theme has already been
951
+ // moved to its final location.
949
952
switch ( options . themeRepoType ) {
950
953
case 'git' : {
951
954
initGitRepo ( ) ;
@@ -960,7 +963,12 @@ function initRepo() {
960
963
break ;
961
964
}
962
965
}
966
+ // Return the process back to the cached working directory.
967
+ process . chdir ( cwd ) ;
963
968
} catch ( error ) {
969
+ // Return the process back to the cached working directory. Make sure
970
+ // this happens before the error is thrown to ensure it returns no matter what.
971
+ process . chdir ( cwd ) ;
964
972
// If `failExternals` option is true, throw an error. If it's false, we don't
965
973
// want repo init errors to exit the process, so don't throw. Instead, catch them
966
974
// and log them so the user is aware, while allowing the process to continue.
0 commit comments