-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.ps1
More file actions
1685 lines (1549 loc) · 58.1 KB
/
setup.ps1
File metadata and controls
1685 lines (1549 loc) · 58.1 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# MIT License
# Copyright (c) 2025 fonseware
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
param(
[Parameter(Mandatory = $false)]
[string]$ProjectName,
[Parameter(Mandatory = $false)]
[string]$ProjectLocation
)
function functionDrawLogo2 {
Clear-Host
# https://www.asciiart.eu/text-to-ascii-art
Write-Host @"
_ _ _ _
___| | ___ ___| |_ _ __ ___ _ __ (_)___ ___ ___| |_ _ _ _ __
/ _ \ |/ _ \/ __| __| '__/ _ \| '_ \ | / __| / __|/ _ \ __| | | | '_ \
| __/ | __/ (__| |_| | | (_) | | | |_ | \__ \ \__ \ __/ |_| |_| | |_) |
\___|_|\___|\___|\__|_| \___/|_| |_(_)/ |___/ |___/\___|\__|\__,_| .__/
|__/ |_|
______________________[ WELCOME TO ELECTRON.JS SETUP ]_________________________`n
"@ -ForegroundColor Green
}
function functionDrawLogo {
Clear-Host
# https://www.asciiart.eu/text-to-ascii-art
Write-Host @"
_ _ _ _
___| | ___ ___| |_ _ __ ___ _ __ (_)___ ___ ___| |_ _ _ _ __
/ _ \ |/ _ \/ __| __| '__/ _ \| '_ \ | / __| / __|/ _ \ __| | | | '_ \
| __/ | __/ (__| |_| | | (_) | | | |_ | \__ \ \__ \ __/ |_| |_| | |_) |
\___|_|\___|\___|\__|_| \___/|_| |_(_)/ |___/ |___/\___|\__|\__,_| .__/
|__/ |_| v$localVersion
_______________________________________________________________________________`n
"@ -ForegroundColor Green
}
function functionDrawLine {
Write-Host "_______________________________________________________________________________" -ForegroundColor Yellow
}
function Test-InternetConnection {
try {
$url = "https://www.google.com"
$response = Invoke-WebRequest -Uri $url -Method Head -UseBasicParsing -TimeoutSec 5
$response
return $true
}
catch {
return $false
}
}
function functionShowInfoScreen {
Write-Host @"
This PowerShell script is designed to simplify the process of setting up and
creating new Electron.js projects. Whether you're a beginner or an experienced
developer, this script automates the installation of essential tools,
initializes your project, and generates the necessary files to get you started
quickly. It also provides options to create different types of Electron apps,
including a basic Electron app, a Vite-based app, and a Windows-style app.`n
- The script checks for and installs essential tools like Chocolatey, Node.js,
and Visual Studio Code if they are not already installed on your system.`n
- Automatically creates a new Electron.js project directory, initializes npm,
and installs Electron as a dependency.`n
- Generates essential files such as main.js, index.html, and package.json
with default configurations, saving you time on boilerplate code.`n
- Multiple Project Templates: Basic Electron App, Vite-based Electron App,
Windows-style Electron App`n
- Automatically opens the newly created project in Visual Studio Code, allowing
you to start coding immediately.`n
- Provides error logging and user-friendly prompts to guide you through the
setup process, even if something goes wrong.
"@ -ForegroundColor Cyan
functionDrawLine
}
# Path to the local version.txt file in the repo
$localVersionFilePath = ".\version.txt"
# URL of the remote version.txt file on GitHub
#$remoteVersionUrl = "https://raw.githubusercontent.com/fonseware/electronjs-setup/test/version.txt"
$remoteVersionUrl = "https://raw.githubusercontent.com/fonseware/electronjs-setup/main/version.txt"
# Function to get the local version from version.txt
function Get-LocalVersion {
if (Test-Path $localVersionFilePath) {
return Get-Content $localVersionFilePath | Out-String
}
else {
Clear-Host
functionDrawLogo2
Write-Host "Local version file not found." -ForegroundColor Red
Start-Sleep -Seconds 1
return $null
}
}
# Function to get the remote version from GitHub
function Get-RemoteVersion {
try {
$response = Invoke-WebRequest -Uri $remoteVersionUrl -Method Get
return $response.Content
}
catch {
Clear-Host
functionDrawLogo2
Write-Host "Failed to fetch the remote version from GitHub." -ForegroundColor Red
Start-Sleep -Seconds 1
return $null
}
}
# Compare local and remote versions
function Compare-Versions {
Clear-Host
functionDrawLogo2
Write-Host "Checking for updates..." -ForegroundColor Yellow
start-sleep -Seconds 1
$localVersion = Get-LocalVersion
$remoteVersion = Get-RemoteVersion
if ($null -eq $localVersion -or $null -eq $remoteVersion) {
Write-Host "Could not compare versions. Ensure both files are accessible." -ForegroundColor Red
Start-Sleep -Seconds 1
return
}
$localVersion = $localVersion.Trim()
$remoteVersion = $remoteVersion.Trim()
if ($localVersion -ne $remoteVersion) {
$currentFolder = $PSScriptRoot
functionDrawLogo2
Write-Host "A new version $remoteVersion is available, your currently installed version is $localVersion" -ForegroundColor Yellow
Write-Host @"
`nPlease update the script to the latest version!
You still can use this script without updating, but it is recommended to `nupdate to the latest version for the best experience.
`nPlease delete the files in this folder:`n
"@ -ForegroundColor Yellow
Write-Host " $currentFolder`n" -ForegroundColor Cyan
Write-Host "THEN run this command on Command Prompt:" -ForegroundColor Yellow
Write-Host @"
git clone https://github.com/fonseware/electronjs-setup.git
cd electronjs-setup
powershell -ExecutionPolicy Bypass -File setup.ps1
"@ -ForegroundColor Cyan
functionDrawLine
Pause
Clear-Host
}
else {
functionDrawLogo2
Write-Host "You are using the latest version." -ForegroundColor Green
Start-Sleep -Seconds 1
}
}
function functionCheckForPrerequisites {
functionDrawLogo
# Check internet connection before proceeding
Write-Host "Checking for network connection..." -ForegroundColor Yellow
start-sleep -Seconds 1
if (-not (Test-InternetConnection)) {
Write-Host "No internet connection. Please connect to the internet and try again." -ForegroundColor Red
return
}
functionDrawLogo
Write-Host @"
This script will check for and install the following prerequisites:
- Chocolatey
- Node.js
- Visual Studio Code
By continuing, you agree to install these prerequisites on your system.
- Chocolatey: https://docs.chocolatey.org/en-us/information/legal/
- Visual Studio Code: https://code.visualstudio.com/license
- Node.js: https://github.com/nodejs/node/blob/main/LICENSE
"@ -ForegroundColor Blue
functionDrawLine
Write-Host "`nPress [Enter] to accept and continue or type '98' to go to main menu."
$inputValue = Read-Host "Enter your choice"
if ($inputValue -eq "98") {
return
}
if ($inputValue -eq "") {} else {
Write-Host "`nInvalid input..." -ForegroundColor Red
return
}
functionDrawLogo
Write-Host "Checking for prerequisites..." -ForegroundColor Yellow
Write-Host "[-] Chocolatey"-ForegroundColor Yellow
Write-Host "[ ] Node.js"-ForegroundColor DarkGray
Write-Host "[ ] Visual Studio Code"-ForegroundColor DarkGray
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
Write-Host "`nChocolatey is not installed. Installing Chocolatey..." -ForegroundColor Yellow
Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Start-Sleep -Seconds 5
}
else {
Write-Host "`nChocolatey is already installed." -ForegroundColor Green
}
Start-Sleep -Seconds 1
functionDrawLogo
Write-Host "Checking for prerequisites..." -ForegroundColor Yellow
Write-Host "[*] Chocolatey"-ForegroundColor Green
Write-Host "[-] Node.js"-ForegroundColor Yellow
Write-Host "[ ] Visual Studio Code"-ForegroundColor DarkGray
##
function Install-NvmIfNeeded {
if (-not (Get-Command nvm -ErrorAction SilentlyContinue)) {
Write-Host "NVM is not installed. Installing via Chocolatey..." -ForegroundColor Yellow
choco install nvm -y
# Refresh environment variables
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
}
}
function Install-NodeJsWithOptions {
param([bool]$nodeAlreadyInstalled)
# Prompt if Node.js is already installed
if ($nodeAlreadyInstalled) {
Write-Host "`nNode.js is already installed." -ForegroundColor Green
$installAnother = Read-Host "Do you want to install another version of node.js? (Y/N)"
if ($installAnother -notmatch '^[Yy]') { return }
}
# Installation options menu
functionDrawLine
Write-Host "Choose Node.js installation method:`n" -ForegroundColor Yellow
Write-Host "1. Install latest version via Chocolatey" -ForegroundColor Cyan
Write-Host "2. Install latest version using NVM" -ForegroundColor Cyan
Write-Host "3. Choose specific version with NVM" -ForegroundColor Cyan
Write-Host "4. Skip Node.js installation" -ForegroundColor Cyan
$choice = Read-Host "Enter your choice (1-4)"
switch ($choice) {
'1' {
Write-Host "`nInstalling Node.js via Chocolatey..." -ForegroundColor Yellow
choco install nodejs -y
}
'2' {
Install-NvmIfNeeded
Write-Host "`nInstalling latest Node.js via NVM..." -ForegroundColor Yellow
nvm install latest
nvm use latest
}
'3' {
Install-NvmIfNeeded
$version = Read-Host "`nEnter the Node.js version (e.g., 20.13.1)"
Write-Host "`,Installing Node.js $version via NVM..." -ForegroundColor Yellow
nvm install $version
nvm use $version
}
'4' {
Write-Host "`nSkipping installation." -ForegroundColor Yellow
return
}
default {
Write-Host "`nInvalid choice. Skipping." -ForegroundColor Red
return
}
}
# Verify installation
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
Write-Host "`nNode.js installation failed!" -ForegroundColor Red
}
}
# Check if Node.js is already installed
$nodeInstalled = [bool](Get-Command node -ErrorAction SilentlyContinue)
# Run installation menu
Install-NodeJsWithOptions -nodeAlreadyInstalled $nodeInstalled
# Final check
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
Write-Host "`nNode.js is still missing. Some features may not work." -ForegroundColor Red
Start-Sleep -Seconds 2
}
##
Start-Sleep -Seconds 1
functionDrawLogo
Write-Host "Checking for prerequisites..." -ForegroundColor Yellow
Write-Host "[*] Chocolatey"-ForegroundColor Green
Write-Host "[*] Node.js"-ForegroundColor Green
Write-Host "[-] Visual Studio Code"-ForegroundColor Yellow
$vsCodePath = Get-Command 'code' -ErrorAction SilentlyContinue
if (-not $vsCodePath) {
Write-Host "`nVisual Studio Code is not installed. Installing VS Code..." -ForegroundColor Yellow
choco install vscode -y
}
else {
Write-Host "`nVisual Studio Code is already installed." -ForegroundColor Green
}
Start-Sleep -Seconds 1
functionDrawLogo
Write-Host "Checking for prerequisites..." -ForegroundColor Yellow
Write-Host "[*] Chocolatey"-ForegroundColor Green
Write-Host "[*] Node.js"-ForegroundColor Green
Write-Host "[*] Visual Studio Code"-ForegroundColor Green
Write-Host "`nAll prerequisites are installed." -ForegroundColor Green
}
function functionCreateElectronAppDefault {
functionDrawLogo
# Check internet connection before proceeding
Write-Host "Checking for network connection..." -ForegroundColor Yellow
start-sleep -Seconds 1
if (-not (Test-InternetConnection)) {
Write-Host "No internet connection. Please connect to the internet and try again." -ForegroundColor Red
return
}
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[-] Step 1: Name of the project"-ForegroundColor Yellow
Write-Host "[ ] Step 2: Creating project directory and initialise npm"-ForegroundColor DarkGray
Write-Host "[ ] Step 3: Installing Electron to project"-ForegroundColor DarkGray
Write-Host "[ ] Step 4: Creating main.js file"-ForegroundColor DarkGray
Write-Host "[ ] Step 5: Creating index.html file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 8: Building Electron app"-ForegroundColor DarkGray
functionDrawLine
do {
$projectName = Read-Host "Enter the name of your Electron project (use dashes instead of spaces)"
if (-not [string]::IsNullOrWhiteSpace($projectName)) {
$projectName = $projectName -replace ' ', '-'
}
else {
Write-Host "Error: Project name cannot be empty. Please try again." -ForegroundColor Red
return
}
} while ([string]::IsNullOrWhiteSpace($projectName))
do {
$defaultLocation = "C:\Users\$env:USERNAME\source\repos"
$projectLocation = Read-Host "Enter the location (press [Enter] to use default: $defaultLocation\)"
if ([string]::IsNullOrWhiteSpace($projectLocation)) {
$projectLocation = $defaultLocation
Write-Host "No input provided. The project will be saved in: $projectLocation\" -ForegroundColor Yellow
}
if (Test-Path "$projectLocation\$projectName") {
Write-Host "Error: A folder already exists at this location. Please enter a different path." -ForegroundColor Red
}
else {
break
}
} while ($true)
functionDrawLine
Write-Host "Review your project settings..." -ForegroundColor Yellow
Write-Host "Project Name: $projectName"
Write-Host "Save Location: $projectLocation\"
Write-Host "Project Location: $projectLocation\$projectName\" -ForegroundColor Yellow
functionDrawLine
Write-Host "`nPress [Enter] to continue or type '98' to go to main menu, to start over..."
$inputValue = Read-Host "Enter your choice"
if ($inputValue -eq "98") {
return
}
if ($inputValue -eq "") {} else {
Write-Host "`nInvalid input..." -ForegroundColor Red
return
}
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[-] Step 2: Creating project directory and initialise npm"-ForegroundColor Yellow
Write-Host "[ ] Step 3: Installing Electron to project"-ForegroundColor DarkGray
Write-Host "[ ] Step 4: Creating main.js file"-ForegroundColor DarkGray
Write-Host "[ ] Step 5: Creating index.html file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 8: Building Electron app"-ForegroundColor DarkGray
$fullPath = Join-Path -Path $projectLocation -ChildPath $projectName
mkdir $fullPath | Out-Null
Set-Location -Path $fullPath
npm init -y > $null 2>&1
Start-Sleep -Seconds 2
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[-] Step 3: Installing Electron to project"-ForegroundColor Yellow
Write-Host "[ ] Step 4: Creating main.js file"-ForegroundColor DarkGray
Write-Host "[ ] Step 5: Creating index.html file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 8: Building Electron app"-ForegroundColor DarkGray
functionDrawLine
Write-Host "This process may take some time, please wait...`n"
Write-Host "`nFor errors in this process, check the project logs for more info." -ForegroundColor Yellow
Write-host "Located: $projectLocation\$projectName\error.log" -ForegroundColor Yellow
npm install --save-dev electron > $null 2> error.log
Start-Sleep -Seconds 2
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[*] Step 3: Installing Electron to project"-ForegroundColor Green
Write-Host "[-] Step 4: Creating main.js file"-ForegroundColor Yellow
Write-Host "[ ] Step 5: Creating index.html file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 8: Building Electron app"-ForegroundColor DarkGray
$mainJsContent = @"
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
win.loadFile('index.html');
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {`
if (process.platform !== 'darwin') app.quit();
});
"@
Set-Content -Path (Join-Path -Path $fullPath -ChildPath "main.js") -Value $mainJsContent
Start-Sleep -Seconds 1
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[*] Step 3: Installing Electron to project"-ForegroundColor Green
Write-Host "[*] Step 4: Creating main.js file"-ForegroundColor Green
Write-Host "[-] Step 5: Creating index.html file"-ForegroundColor Yellow
Write-Host "[ ] Step 6: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 8: Building Electron app"-ForegroundColor DarkGray
$htmlContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Electron Starter App</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 40px;
background-color: #f0f0f0;
}
.container {
max-width: 800px;
margin: 0 auto;
}
h1 {
color: #2e3c4d;
font-size: 2.5em;
margin-bottom: 20px;
}
h2 {
color: #3a506b;
margin-bottom: 30px;
}
p {
color: #555;
line-height: 1.6;
margin: 15px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 Welcome to Electron!</h1>
<h2>You've successfully set up your Electron application</h2>
<p>
Start building your application by modifying the
<code>index.html</code> file and adding your custom functionality.
</p>
<p>
This basic template includes everything you need to begin developing
your cross-platform desktop app.
</p>
<p>
Check out the Electron documentation to explore all the powerful
features available!
</p>
</div>
</body>
</html>
"@
Set-Content -Path (Join-Path -Path $fullPath -ChildPath "index.html") -Value $htmlContent
Start-Sleep -Seconds 1
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[*] Step 3: Installing Electron to project"-ForegroundColor Green
Write-Host "[*] Step 4: Creating main.js file"-ForegroundColor Green
Write-Host "[*] Step 5: Creating index.html file"-ForegroundColor Green
Write-Host "[-] Step 6: Updating package.json file"-ForegroundColor Yellow
Write-Host "[ ] Step 7: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 8: Building Electron app"-ForegroundColor DarkGray
$jsonPath = Join-Path -Path $fullPath -ChildPath "package.json"
$jsonContent = Get-Content -Path $jsonPath -Raw | ConvertFrom-Json
$jsonContent.main = "main.js"
$jsonContent.scripts = @{ "start" = "electron ." }
$jsonContent | ConvertTo-Json -Compress | Set-Content -Path $jsonPath
Start-Sleep -Seconds 1
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[*] Step 3: Installing Electron to project"-ForegroundColor Green
Write-Host "[*] Step 4: Creating main.js file"-ForegroundColor Green
Write-Host "[*] Step 5: Creating index.html file"-ForegroundColor Green
Write-Host "[*] Step 6: Updating package.json file"-ForegroundColor Green
Write-Host "[-] Step 7: Opening project in Visual Studio Code"-ForegroundColor Yellow
Write-Host "[ ] Step 8: Building Electron app"-ForegroundColor DarkGray
Set-Location -Path $fullPath
code .
Start-Sleep -Seconds 2
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[*] Step 3: Installing Electron to project"-ForegroundColor Green
Write-Host "[*] Step 4: Creating main.js file"-ForegroundColor Green
Write-Host "[*] Step 5: Creating index.html file"-ForegroundColor Green
Write-Host "[*] Step 6: Updating package.json file"-ForegroundColor Green
Write-Host "[*] Step 7: Opening project in Visual Studio Code"-ForegroundColor Green
Write-Host "[-] Step 8: Building Electron app"-ForegroundColor Yellow
functionDrawLine
Write-Host "| To run your Electron app, open a new terminal in VS Code and run the following command:" -ForegroundColor Yellow
Write-Host "| npm start" -ForegroundColor Cyan
#Write-Host "`n`tnpm start`n" -ForegroundColor Cyan
npm start > $null 2>&1
Start-Sleep -Seconds 2
functionDrawLogo
Write-Host "Creating a new Electron.js app..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[*] Step 3: Installing Electron to project"-ForegroundColor Green
Write-Host "[*] Step 4: Creating main.js file"-ForegroundColor Green
Write-Host "[*] Step 5: Creating index.html file"-ForegroundColor Green
Write-Host "[*] Step 6: Updating package.json file"-ForegroundColor Green
Write-Host "[*] Step 7: Opening project in Visual Studio Code"-ForegroundColor Green
Write-Host "[*] Step 8: Building Electron app"-ForegroundColor Green
write-host "`nYour Electron app has been created successfully!" -ForegroundColor Green
write-host "`nThank you for using the Electron.js Setup script." -ForegroundColor Green
}
function functionDrawCmdLogo {
Clear-Host
Write-Host @"
Electron.js Setup v$localVersion (via command line)
------------------------------------------------------------
"@ -ForegroundColor Green
}
function functionDrawCmdLogo2 {
Write-Host @"
------------------------------------------------------------
fonseware
"@ -ForegroundColor Green
}
function functionCreateElectronAppDefault2 {
param(
[Parameter(Mandatory = $true)]
[string]$projectName,
[Parameter(Mandatory = $false)]
[string]$projectLocation
)
functionDrawCmdLogo
try {
if (-not [string]::IsNullOrWhiteSpace($projectName)) {
$projectName = $projectName -replace ' ', '-'
}
else {
Write-Host "Project name cannot be empty. Please try again." -ForegroundColor Red
return
}
$defaultLocation = "C:\Users\$env:USERNAME\source\repos"
if ([string]::IsNullOrWhiteSpace($projectLocation)) {
$projectLocation = $defaultLocation
Write-Host """ProjectLocation"" param not specified.`nThe project will be saved in default location: $projectLocation\" -ForegroundColor Yellow
}
if (Test-Path "$projectLocation\$projectName") {
Write-Host "A folder already exists at this location. Please enter a different path." -ForegroundColor Red
return
}
Write-Host "Your project save location: $projectLocation\$projectName\" -ForegroundColor Yellow
$fullPath = Join-Path -Path $projectLocation -ChildPath $projectName
mkdir $fullPath | Out-Null
Set-Location -Path $fullPath
npm init -y > $null 2>&1
Write-Host "[*] Project directory created and initialised npm."-ForegroundColor Green
#Write-host "Error log saved at: $projectLocation\$projectName\error.log" -ForegroundColor Yellow
npm install --save-dev electron > $null 2> error.log
Write-Host "[*] Installed Electron to project."-ForegroundColor Green
$mainJsContent = @"
const { app, BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
});
win.loadFile('index.html');
}
app.whenReady().then(() => {
createWindow();
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
});
app.on('window-all-closed', () => {`
if (process.platform !== 'darwin') app.quit();
});
"@
Set-Content -Path (Join-Path -Path $fullPath -ChildPath "main.js") -Value $mainJsContent
Write-Host "[*] Created main.js file."-ForegroundColor Green
$htmlContent = @"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Electron Starter App</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 40px;
background-color: #f0f0f0;
}
.container {
max-width: 800px;
margin: 0 auto;
}
h1 {
color: #2e3c4d;
font-size: 2.5em;
margin-bottom: 20px;
}
h2 {
color: #3a506b;
margin-bottom: 30px;
}
p {
color: #555;
line-height: 1.6;
margin: 15px 0;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 Welcome to Electron!</h1>
<h2>You've successfully set up your Electron application</h2>
<p>
Start building your application by modifying the
<code>index.html</code> file and adding your custom functionality.
</p>
<p>
This basic template includes everything you need to begin developing
your cross-platform desktop app.
</p>
<p>
Check out the Electron documentation to explore all the powerful
features available!
</p>
</div>
</body>
</html>
"@
Set-Content -Path (Join-Path -Path $fullPath -ChildPath "index.html") -Value $htmlContent
Write-Host "[*] Created index.html file."-ForegroundColor Green
$jsonPath = Join-Path -Path $fullPath -ChildPath "package.json"
$jsonContent = Get-Content -Path $jsonPath -Raw | ConvertFrom-Json
$jsonContent.main = "main.js"
$jsonContent.scripts = @{ "start" = "electron ." }
$jsonContent | ConvertTo-Json -Compress | Set-Content -Path $jsonPath
Write-Host "[*] Updated package.json file."-ForegroundColor Green
Set-Location -Path $fullPath
code .
Write-Host "[*] Opening project in Visual Studio Code."-ForegroundColor Green
npm start > $null 2>&1
Write-Host "[*] Electron app built."-ForegroundColor Green
write-host "Your Electron app has been created successfully!" -ForegroundColor Green
write-host "Thank you for using the Electron.js Setup script." -ForegroundColor Green
}
catch {
<#Do this if a terminating exception happens#>
Write-Host "An error occurred while creating the project!" -ForegroundColor Red
}
}
function functionCreateElectronAppVite {
functionDrawLogo
Write-Host "You are about to run a script, outside the scope of this setup.`n" -ForegroundColor Yellow
Write-Host " npm create @quick-start/electron@latest`n" -ForegroundColor Cyan
Write-Host "If you want to exit while running the above script, press [Escape]." -ForegroundColor Yellow
functionDrawLine
Write-Host "Press [Enter] to start the script or type '98' to go to main menu."
$inputValue = Read-Host "Enter your choice"
if ($inputValue -eq "98") {
return
}
if ($inputValue -eq "") {} else {
Write-Host "`nInvalid input..." -ForegroundColor Red
return
}
Clear-Host
functionDrawLogo
Write-Host "running command ""npm create @quick-start/electron@latest""" -ForegroundColor Cyan
functionDrawLine
npm create @quick-start/electron@latest
}
function functionCreateElectronAppWindowsStyle {
functionDrawLogo
# Check internet connection before proceeding
Write-Host "Checking for network connection..." -ForegroundColor Yellow
start-sleep -Seconds 1
if (-not (Test-InternetConnection)) {
Write-Host "No internet connection. Please connect to the internet and try again." -ForegroundColor Red
return
}
functionDrawLogo
Write-Host "Creating a new Electron.js app with Windows Style..." -ForegroundColor Yellow
Write-Host "[-] Step 1: Name of the project"-ForegroundColor Yellow
Write-Host "[ ] Step 2: Creating project directory and initialise npm"-ForegroundColor DarkGray
Write-Host "[ ] Step 3: Installing Electron to project"-ForegroundColor DarkGray
Write-Host "[ ] Step 4: Creating all files"-ForegroundColor DarkGray
Write-Host "[ ] Step 5: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Building Electron app"-ForegroundColor DarkGray
functionDrawLine
do {
$projectName = Read-Host "Enter the name of your Electron project (use dashes instead of spaces)"
if (-not [string]::IsNullOrWhiteSpace($projectName)) {
$projectName = $projectName -replace ' ', '-'
}
else {
Write-Host "Error: Project name cannot be empty. Please try again." -ForegroundColor Red
return
}
} while ([string]::IsNullOrWhiteSpace($projectName))
do {
$defaultLocation = "C:\Users\$env:USERNAME\source\repos"
$projectLocation = Read-Host "Enter the location (press [Enter] to use default: $defaultLocation\)"
if ([string]::IsNullOrWhiteSpace($projectLocation)) {
$projectLocation = $defaultLocation
Write-Host "No input provided. The project will be saved in: $projectLocation\" -ForegroundColor Yellow
}
if (Test-Path "$projectLocation\$projectName") {
Write-Host "Error: A folder already exists at this location. Please enter a different path." -ForegroundColor Red
}
else {
break
}
} while ($true)
functionDrawLine
Write-Host "Review your project settings..." -ForegroundColor Yellow
Write-Host "Project Name: $projectName"
Write-Host "Save Location: $projectLocation\"
Write-Host "Project Location: $projectLocation\$projectName\" -ForegroundColor Yellow
functionDrawLine
Write-Host "`nPress [Enter] to continue or type '98' to go to main menu, to start over..."
$inputValue = Read-Host "Enter your choice"
if ($inputValue -eq "98") {
return
}
if ($inputValue -eq "") {} else {
Write-Host "`nInvalid input..." -ForegroundColor Red
return
}
functionDrawLogo
Write-Host "Creating a new Electron.js app with Windows Style..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[-] Step 2: Creating project directory and initialise npm"-ForegroundColor Yellow
Write-Host "[ ] Step 3: Installing Electron to project"-ForegroundColor DarkGray
Write-Host "[ ] Step 4: Creating all files"-ForegroundColor DarkGray
Write-Host "[ ] Step 5: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Building Electron app"-ForegroundColor DarkGray
$fullPath = Join-Path -Path $projectLocation -ChildPath $projectName
mkdir $fullPath | Out-Null
Set-Location -Path $fullPath
npm init -y > $null 2>&1
Start-Sleep -Seconds 2
functionDrawLogo
Write-Host "Creating a new Electron.js app with Windows Style..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[-] Step 3: Installing Electron to project"-ForegroundColor Yellow
Write-Host "[ ] Step 4: Creating all files"-ForegroundColor DarkGray
Write-Host "[ ] Step 5: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Building Electron app"-ForegroundColor DarkGray
functionDrawLine
Write-Host "This process may take some time, please wait...`n"
Write-Host "`nFor errors in this process, check the project logs for more info." -ForegroundColor Yellow
Write-host "Located: $projectLocation\$projectName\error.log" -ForegroundColor Yellow
npm install --save-dev electron > $null 2> error.log
Start-Sleep -Seconds 2
functionDrawLogo
Write-Host "Creating a new Electron.js app with Windows Style..." -ForegroundColor Yellow
Write-Host "[*] Step 1: Name of the project"-ForegroundColor Green
Write-Host "[*] Step 2: Creating project directory and initialise npm"-ForegroundColor Green
Write-Host "[*] Step 3: Installing Electron to project"-ForegroundColor Green
Write-Host "[-] Step 4: Creating all files"-ForegroundColor Yellow
Write-Host "[ ] Step 5: Updating package.json file"-ForegroundColor DarkGray
Write-Host "[ ] Step 6: Opening project in Visual Studio Code"-ForegroundColor DarkGray
Write-Host "[ ] Step 7: Building Electron app"-ForegroundColor DarkGray
functionDrawLine
Write-Host "This process may take some time, please wait...`n"
$mainJsContent = @"
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
frame: false,
backgroundColor: '#FFF',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
nodeIntegration: false,
},
});
mainWindow.loadFile('index.html');
// Open DevTools (optional)
// mainWindow.webContents.openDevTools();
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// IPC handlers for window controls
ipcMain.on('minimize-window', () => {
mainWindow.minimize();
});
ipcMain.on('maximize-window', () => {
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
} else {
mainWindow.maximize();
}
});
ipcMain.on('close-window', () => {
mainWindow.close();
});
"@
Set-Content -Path (Join-Path -Path $fullPath -ChildPath "main.js") -Value $mainJsContent
Start-Sleep -Seconds 1
$htmlContent = @"
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Electron Modern Home</title>
<link rel="stylesheet" href="style.css" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
/>
</head>
<body>
<header id="titlebar">
<div id="drag-region">
<div id="window-title">
<span>Electron Modern Home</span>
</div>
<div id="window-controls">
<div class="button" id="min-button">
<img
class="icon"
srcset="
icons/min-w-10.png 1x,
icons/min-w-12.png 1.25x,
icons/min-w-15.png 1.5x,
icons/min-w-15.png 1.75x,
icons/min-w-20.png 2x,
icons/min-w-20.png 2.25x,
icons/min-w-24.png 2.5x,
icons/min-w-30.png 3x,
icons/min-w-30.png 3.5x
"
draggable="false"
/>
</div>
<div class="button" id="max-button">
<img
class="icon"
srcset="
icons/max-w-10.png 1x,
icons/max-w-12.png 1.25x,
icons/max-w-15.png 1.5x,
icons/max-w-15.png 1.75x,
icons/max-w-20.png 2x,
icons/max-w-20.png 2.25x,
icons/max-w-24.png 2.5x,
icons/max-w-30.png 3x,
icons/max-w-30.png 3.5x
"
draggable="false"
/>
</div>
<div class="button" id="restore-button">
<img
class="icon"
srcset="
icons/restore-w-10.png 1x,
icons/restore-w-12.png 1.25x,
icons/restore-w-15.png 1.5x,
icons/restore-w-15.png 1.75x,
icons/restore-w-20.png 2x,
icons/restore-w-20.png 2.25x,
icons/restore-w-24.png 2.5x,
icons/restore-w-30.png 3x,
icons/restore-w-30.png 3.5x
"
draggable="false"
/>
</div>
<div class="button" id="close-button">
<img
class="icon"
srcset="
icons/close-w-10.png 1x,
icons/close-w-12.png 1.25x,
icons/close-w-15.png 1.5x,
icons/close-w-15.png 1.75x,