-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsrebuild.ps1
267 lines (235 loc) · 9.09 KB
/
psrebuild.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
param(
[Parameter(Mandatory)]
[string]$oldAppName=$(throw "Old App's Name/Directory as a sibling to this rebuildamplify folder"),
[Parameter(Mandatory)]
[string]$newAppName=$(throw "New App's Name/Directory as a sibling to this rebuildamplify folder"),
[Parameter(Mandatory)]
[string]$profileName=$(throw "Profile from your ~/.aws/config file"),
$parent="master"
)
$currentFolder = Split-Path -Path (Get-Location) -Leaf
if ($currentFolder -ne "rebuildamplify")
{
throw "You must run this from the rebuildamplify folder which should be a siblings folder to your project app folder!!!!"
}
Set-Location ..
$directories = Get-ChildItem . -Directory
Write-Host "directories: $directories"
$exists = $directories.Name -like $newAppName
if ($exists) {
Write-Host "NEXT.JS PROJECT ALREADY EXISTS"
} else {
$nodeRebuildVersion = "v20.3.1"
Write-Host "`n
rebuild was written with the latest node version = $nodeRebuildVersion"
$currentNodeVersion = node -v
Write-Host "The current node version on your machine is = $currentNodeVersion"
$npmRebuildVersion = "9.6.7"
Write-Host "rebuild was written with the latest npm version = $npmRebuildVersion"
$currentNpmVersion = npm -v
Write-Host "The current node version on your machine is = $currentNpmVersion"
$amplifyRebuildVersion = "12.5.0"
Write-Host "rebuild was written with the latest amplify version = $amplifyRebuildVersion"
$currentAmplifyVersion = amplify -v
Write-Host "The current node version on your machine is = $currentAmplifyVersion"
Set-Location ./$oldAppName
$nextRebuildVersion = "13.5.3"
Write-Host "rebuild was written with the latest amplify version = $nextRebuildVersion"
$currentNextVersion = npm list next
Write-Host "The current node version on your machine is = $currentNextVersion"
Write-Host "This rebuild will install the latest so update the variables within the script to run a different version..."
Write-Host "npm outdated start (should be nothing if latest):"
npm outdated
Write-Host "npm outdated end:"
#***********************NEXT.JS***********************************
Set-Location ..
Write-Host "CREATE NEXT.JS APP..."
npx create-next-app@$nextRebuildVersion $newAppName --app --js --tailwind --eslint --src-dir --import-alias "@/*"
if (-not $?) {throw "FAILED TO CREATE NEXT.JS APP"}
}
#*************************INIT*********************************
Set-Location .\$newAppName
$directories = Get-ChildItem . -Directory
Write-Host "directories: $directories"
$exists = $directories.Name -like "amplify"
if ($exists) {
Write-Host "AMPLIFY FOLDER ALREADY EXISTS"
} else {
# Define Amplify JSON as a PowerShell hashtable
$amplifyJson = @{
projectName = $newAppName
envName = "dev"
defaultEditor = "code"
}
# Convert the Amplify JSON hashtable to a valid JSON string
$amplify = $amplifyJson | ConvertTo-Json
# Define Frontend JSON as a PowerShell hashtable
$frontendJson = @{
frontend = "javascript"
framework = "react"
config = @{
SourceDir = "src"
DistributionDir = "build"
BuildCommand = "npm run-script build"
StartCommand = "npm run-script start"
}
}
# Convert the Frontend JSON hashtable to a valid JSON string
$frontend = $frontendJson | ConvertTo-Json
# Define Providers JSON as a PowerShell hashtable
$providersJson = @{
awscloudformation = @{
configLevel = "project"
useProfile = $true
profileName = $profileName
}
}
# Convert the Providers JSON hashtable to a valid JSON string
$providers = $providersJson | ConvertTo-Json
# Output the JSON strings for verification
Write-Host "amplifyjson = $amplify"
Write-Host "frontend = $frontend"
Write-Host "providers = $providers"
# Run amplify init with the JSON strings as parameters
Write-Host "INIT AMPLIFY..."
amplify init --amplify $amplify --frontend $frontend --providers $providers --yes
if (-not $?) {throw "FAILED TO AMPLIFY INIT"}
}
#************************AUTH**********************************
#The Interface\/ \/ \/
#https://github.com/aws-amplify/amplify-cli/blob/main/packages/amplify-headless-interface/src/interface/auth/add.ts
$directories = Get-ChildItem .\amplify\backend\ -Directory
Write-Host "directories: $directories"
$exists = $directories.Name -like "auth"
if ($exists) {
Write-Host "AUTH FOLDER ALREADY EXISTS"
} else {
# Construct the JSON structure manually
$resourceName = $newAppName+"Cognito"
#$apiName = $newAppName+"API"
$userPoolName = $newAppName+"UserPool"
$addAuth = @"
{
"version": 2,
"resourceName": "$resourceName",
"serviceConfiguration": {
"serviceName": "Cognito",
"includeIdentityPool": true,
"userPoolConfiguration": {
"signinMethod": "EMAIL",
"requiredSignupAttributes": ["EMAIL"],
"userPoolName": "$userPoolName",
"userPoolGroups": [
{
"groupName": "internal"
}
]
}
}
}
"@
Write-Host $addAuth
# Run amplify add auth with the JSON string as a parameter
$addAuth | jq -c | amplify add auth --headless
if (-not $?) {throw "FAILED TO AMPLIFY ADD AUTH"}
}
#**********************Update AUTH************************************
$directories = Get-ChildItem .\amplify\backend\ -Directory
Write-Host "directories: $directories"
$exists = $directories.Name -like "function"
if ($exists) {
Write-Host "FUNCTION FOLDER ALREADY EXISTS"
} else {
Read-Host -Prompt "Hit your F5 macro to update amplify Auth with Cognito triggers. . ."
Read-Host -Prompt "or hit enter to manually add your Cognito triggers. . ."
# F5 equals these key strokes e\/\/eeee\/eeeeeee""eee\/ee\/\/\/\/se\/sene
# \/ = down arrow
# s = space
# e = enter
# n = n
# "" = string of text "Your Verification Code {####}" and "365" rspectively
# Run amplify add auth with the JSON string as a parameter
amplify update auth
if (-not $?) {throw "FAILED TO AMPLIFY ADD AUTH"}
Write-Host "AMPLIFY PUSH --yes..."
amplify push --yes
}
Read-Host -Prompt "continue to create api. . ."
#************************API**********************************
#The Interface\/ \/ \/
#https://github.com/aws-amplify/amplify-cli/blob/main/packages/amplify-headless-interface/src/interface/api/add.ts
$directories = Get-ChildItem .\amplify\backend\ -Directory
Write-Host "directories: $directories"
$exists = $directories.Name -like "api"
if ($exists) {
Write-Host "API FOLDER ALREADY EXISTS"
} else {
$authPoolId = "auth"+$newAppName+"Cognito"
#Expiry Date
$year = Get-Date -Format "yyyy"
$month = Get-Date -Format "MM"
$day = Get-Date -Format "dd"
$oldYear = [int]$year
$newYear = $oldYear+1
$year = $newYear.ToString()
$exipryDate = $year+"-"+$month+"-"+$day+"T23:59:59.000Z"
# Construct the JSON structure manually
$addApi = @"
{
"version": 1,
"serviceConfiguration": {
"apiName": "$newAppName",
"transformSchema": "type Todo @model {\r\n id: ID!\r\n name: String!\r\n description: String\r\n}",
"serviceName": "AppSync",
"defaultAuthType": {
"mode": "AMAZON_COGNITO_USER_POOLS",
"cognitoUserPoolId": "$authPoolId"
},
"conflictResolution": {},
"additionalAuthTypes": [
{
"mode": "AWS_IAM"
},
{
"mode": "API_KEY",
"expirationTime": 365,
"apiKeyExpirationDate": "$exipryDate",
"keyDescription": "api key description"
}
]
}
}
"@
# Read the contents of the JSON file and format it as a JSON string
Write-Host "Add API..."
# Run amplify add api with the JSON string as a parameter
$addApi | jq -c | amplify add api --headless
if (-not $?) {throw "FAILED TO AMPLIFY INIT"}
Read-Host -Prompt "Press any key to continue to copy across schema and postconfirmation function. . ."
#COPY IN SCHEMA
Write-Host "COPY ACROSS SCHEMA..."
Copy-Item "..\$oldAppName\amplify\backend\api\$oldAppName\schema.graphql" -Destination ".\amplify\backend\api\$newAppName\"
Write-Host "COPY ACROSS LAMBDA FUNCTION..."
#TODO:
#Copy-Item "..\$oldAppName\amplify\backend\function\$oldAppName+CognitoPostConfirmation\src\custom.js" -Destination ".\amplify\backend\function\$newAppName+CognitoPostAuthentication\src\"
Copy-Item "..\$oldAppName\amplify\backend\function\airmv7ced9d447ced9d44PostAuthentication\src\custom.js" -Destination ".\amplify\backend\function\$newAppName+CognitoPostAuthentication\src\"
Write-Host "AMPLIFY PUSH..."
amplify push --codegen --yes --force api
if (-not $?) {throw "FAILED TO AMPLIFY PUSH"}
}
Read-Host -Prompt "Press any key to continue to copy across frontend. . ."
# Copy contents of src\app\
Copy-Item -Path "..\$oldAppName\src\app\*" -Destination "..\$newAppName\src\app\" -Recurse
# Delete contents from public
Remove-Item -Path "..\$newAppName\public\*" -Recurse -Force
# Copy contents of oldAppName\public to newAppName\public
Copy-Item -Path "..\$oldAppName\public\*" -Destination "..\$newAppName\public\" -Recurse
# Copy directory and contents of oldAppName\src\components\ to newAppName\src
Copy-Item -Path "..\$oldAppName\src\components\" -Destination "..\$newAppName\src\" -Recurse
#TODO:
#push backend
#commmit front end
Write-Host "npm outdated:"
npm outdated
Set-Location ..\$currentFolder
#throw "end of script"