Skip to content

Commit ed524b8

Browse files
authored
Fix creaternwapp.cmd and creaternwlib.cmd (#14631)
## Description This PR fixes the `creaternwapp.cmd` and `creaternwlib.cmd` scripts in CI due to new upstream bugs, but also adds a check to make sure the command isn't run within an existing git repo. The commands for new RN projects are opinionated about how they set up new projects, and can override the settings in existing git repos. ### Type of Change - Bug fix (non-breaking change which fixes an issue) ### Why To make sure no one runs these scripts an accidentally messes up their git repo with foreign git hooks, especially *this* repo. Resolves #14632 Resolves #14633 ### What See above. ## Screenshots N/A ## Testing Verified the scripts no longer work for creating new projects within this repo. ## Changelog Should this change be included in the release notes: _no_
1 parent 4f21ac8 commit ed524b8

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

.ado/templates/react-native-init-windows.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ steps:
7373

7474
- ${{ if endsWith(parameters.template, '-app') }}:
7575
- script: |
76-
$(Build.SourcesDirectory)\vnext\Scripts\creaternwapp.cmd /rn $(reactNativeDevDependency) /rnw $(npmVersion) /lt ${{ parameters.template }} /verdaccio testcli
76+
$(Build.SourcesDirectory)\vnext\Scripts\creaternwapp.cmd /rn $(reactNativeDevDependency) /rnw $(npmVersion) /t ${{ parameters.template }} /verdaccio testcli
7777
displayName: Init new app project with creaternwapp.cmd
7878
workingDirectory: $(Agent.BuildDirectory)
7979
env:
8080
YARN_ENABLE_IMMUTABLE_INSTALLS: false
8181
8282
- ${{ if endsWith(parameters.template, '-lib') }}:
8383
- script: |
84-
$(Build.SourcesDirectory)\vnext\Scripts\creaternwlib.cmd /rn $(reactNativeDevDependency) /rnw $(npmVersion) /verdaccio testcli
84+
$(Build.SourcesDirectory)\vnext\Scripts\creaternwlib.cmd /rn $(reactNativeDevDependency) /rnw $(npmVersion) /t ${{ parameters.template }} /verdaccio testcli
8585
displayName: Init new lib project with creaternwlib.cmd
8686
workingDirectory: $(Agent.BuildDirectory)
8787
env:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "Add git repo check to creaternwapp.cmd and creaternwlib.cmd",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/Scripts/creaternwapp.cmd

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ REM name The name of the app to create (default: testapp)
88
REM /r [version] Use react@version (default: latest)
99
REM /rn [version] Use react-native@version (default: latest)
1010
REM /rnw [version] Use react-native-windows@version (default: latest)
11-
REM /lt [template] Use template (default: cpp-app)
11+
REM /t [template] Use template (default: cpp-app)
1212
REM /linkrnw Use your local RNW repo at RNW_ROOT
1313
REM /verdaccio Configure new project to use verdaccio (used in CI)
1414
REM
@@ -17,6 +17,12 @@ REM - You've set the RNW_ROOT environment variable with the path to your clone
1717

1818
setlocal enableextensions enabledelayedexpansion
1919

20+
call git rev-parse --is-inside-work-tree > NUL 2>&1
21+
if %ERRORLEVEL% equ 0 (
22+
@echo creaternwapp.cmd: Unable to create a new project in an existing git repo
23+
exit /b -1
24+
)
25+
2026
if "%RNW_ROOT%"=="" (
2127
@echo creaternwapp.cmd: RNW_ROOT environment variable set to %~dp0..\..
2228
set RNW_ROOT=%~dp0..\..
@@ -50,7 +56,7 @@ if not "%part%"=="" (
5056
) else if "%part%"=="/rnw" (
5157
set RNW_VERSION=%param%
5258
shift
53-
) else if "%part%"=="/lt" (
59+
) else if "%part%"=="/t" (
5460
set RNW_TEMPLATE_TYPE=%param%
5561
shift
5662
) else if "%part:~0,1%"=="/" (
@@ -117,10 +123,9 @@ pushd "%APP_NAME%"
117123

118124
if not "x%RN_VERSION:nightly=%"=="x%RN_VERSION%" (
119125
@echo creaternwapp.cmd Fixing react-native nightly issues
120-
pwsh.exe -Command "(gc package.json) -replace '""nightly""', '""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
121-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli"": "".*""', '""@react-native-community/cli"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
122-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli-platform-android"": "".*""', '""@react-native-community/cli-platform-android"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
123-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli-platform-ios"": "".*""', '""@react-native-community/cli-platform-ios"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
126+
pwsh.exe -Command "(gc package.json) -replace '""react-native"": ""[^\*]*""', '""react-native"": ""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
127+
pwsh.exe -Command "(gc package.json) -replace '""@react-native/(.+-(config|preset))"": "".*""', '""@react-native/$1"": ""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
128+
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli((-platform-)?(ios|android))?"": "".*""', '""@react-native-community/cli$1"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
124129
)
125130

126131
@echo creaternwapp.cmd: Calling yarn install

vnext/Scripts/creaternwlib.cmd

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ REM Creates a RNW lib using the new arch template
44
REM
55
REM Options:
66
REM
7-
REM name The name of the app to create (default: testlib)
7+
REM name The name of the lib to create (default: testlib)
88
REM /r [version] Use react@version (default: latest)
99
REM /rn [version] Use react-native@version (default: latest)
1010
REM /rnw [version] Use react-native-windows@version (default: latest)
11-
REM /lt [template] Use create-react-native-library template (default: turbo-module)
11+
REM /t [template] Use template (default: cpp-lib)
12+
REM /bt [template] Use base create-react-native-library template (default: turbo-module)
1213
REM /linkrnw Use your local RNW repo at RNW_ROOT
1314
REM /verdaccio Configure new project to use verdaccio (used in CI)
1415
REM
@@ -17,6 +18,12 @@ REM - You've set the RNW_ROOT environment variable with the path to your clone
1718

1819
setlocal enableextensions enabledelayedexpansion
1920

21+
call git rev-parse --is-inside-work-tree > NUL 2>&1
22+
if %ERRORLEVEL% equ 0 (
23+
@echo creaternwlib.cmd: Unable to create a new project in an existing git repo
24+
exit /b -1
25+
)
26+
2027
if "%RNW_ROOT%"=="" (
2128
@echo creaternwlib.cmd: RNW_ROOT environment variable set to %~dp0..\..
2229
set RNW_ROOT=%~dp0..\..
@@ -51,7 +58,10 @@ if not "%part%"=="" (
5158
) else if "%part%"=="/rnw" (
5259
set RNW_VERSION=%param%
5360
shift
54-
) else if "%part%"=="/lt" (
61+
) else if "%part%"=="/t" (
62+
set RNW_TEMPLATE_TYPE=%param%
63+
shift
64+
) else if "%part%"=="/bt" (
5565
set RN_TEMPLATE_TYPE=%param%
5666
shift
5767
) else if "%part:~0,1%"=="/" (
@@ -106,8 +116,8 @@ for /f "delims=" %%a in ('npm show react@%R_VERSION% version') do @set R_VERSION
106116

107117
@echo creaternwlib.cmd Creating RNW lib "%LIB_NAME%" with react@%R_VERSION%, react-native@%RN_VERSION%, and react-native-windows@%RNW_VERSION%
108118

109-
@echo creaternwlib.cmd Creating base RN library project with: npx --yes create-react-native-library@latest --slug %LIB_NAME% --description %LIB_NAME% --author-name "React-Native-Windows Bot" --author-email [email protected] --author-url http://example.com --repo-url http://example.com --languages kotlin-objc --type %RN_TEMPLATE_TYPE% --react-native-version %RN_VERSION% --example vanilla %LIB_NAME%
110-
call npx --yes create-react-native-library@latest --slug %LIB_NAME% --description %LIB_NAME% --author-name "React-Native-Windows Bot" --author-email 53619745+rnbot@users.noreply.github.com --author-url http://example.com --repo-url http://example.com --languages kotlin-objc --type %RN_TEMPLATE_TYPE% --react-native-version %RN_VERSION% --example vanilla %LIB_NAME%
119+
@echo creaternwlib.cmd Creating base RN library project with: npx --yes create-react-native-library@0.48.9 --slug %LIB_NAME% --description %LIB_NAME% --author-name "React-Native-Windows Bot" --author-email [email protected] --author-url http://example.com --repo-url http://example.com --languages kotlin-objc --local false --type %RN_TEMPLATE_TYPE% --react-native-version %RN_VERSION% --example vanilla %LIB_NAME%
120+
call npx --yes create-react-native-library@0.48.9 --slug %LIB_NAME% --description %LIB_NAME% --author-name "React-Native-Windows Bot" --author-email 53619745+rnbot@users.noreply.github.com --author-url http://example.com --repo-url http://example.com --languages kotlin-objc --local false --type %RN_TEMPLATE_TYPE% --react-native-version %RN_VERSION% --example vanilla %LIB_NAME%
111121

112122
if %ERRORLEVEL% neq 0 (
113123
@echo creaternwlib.cmd: Unable to create base RN library project
@@ -118,15 +128,13 @@ pushd "%LIB_NAME%"
118128

119129
if not "x%RN_VERSION:nightly=%"=="x%RN_VERSION%" (
120130
@echo creaternwlib.cmd Fixing react-native nightly issue
121-
pwsh.exe -Command "(gc package.json) -replace '""nightly""', '""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
122-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli"": "".*""', '""@react-native-community/cli"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
123-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli-platform-android"": "".*""', '""@react-native-community/cli-platform-android"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
124-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli-platform-ios"": "".*""', '""@react-native-community/cli-platform-ios"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
131+
pwsh.exe -Command "(gc package.json) -replace '""react-native"": ""[^\*]*""', '""react-native"": ""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
132+
pwsh.exe -Command "(gc package.json) -replace '""@react-native/(.+-(config|preset))"": "".*""', '""@react-native/$1"": ""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
133+
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli((-platform-)?(ios|android))?"": "".*""', '""@react-native-community/cli$1"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
125134
pushd example
126-
pwsh.exe -Command "(gc package.json) -replace '""nightly""', '""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
127-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli"": "".*""', '""@react-native-community/cli"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
128-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli-platform-android"": "".*""', '""@react-native-community/cli-platform-android"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
129-
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli-platform-ios"": "".*""', '""@react-native-community/cli-platform-ios"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
135+
pwsh.exe -Command "(gc package.json) -replace '""react-native"": ""[^\*]*""', '""react-native"": ""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
136+
pwsh.exe -Command "(gc package.json) -replace '""@react-native/(.+-(config|preset))"": "".*""', '""@react-native/$1"": ""%RN_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
137+
pwsh.exe -Command "(gc package.json) -replace '""@react-native-community/cli((-platform-)?(ios|android))?"": "".*""', '""@react-native-community/cli$1"": ""%RNCLI_VERSION%""' | Out-File -encoding utf8NoBOM package.json"
130138
popd
131139
)
132140

0 commit comments

Comments
 (0)