Kako prijeći od nule do Create-React-App na sustavu Windows

Upućeni vodič za postavljanje web razvojnog okruženja na Windows 10

Profesionalni sam web programer od ožujka 2018. i u to sam vrijeme koristio i MacO i Windows. Moj preferirani OS koji koristim je Windows, ni iz kojeg drugog razloga, jer više volim raspored tipkovnice.

U sustavu Windows / Linux volim imati tipku Ctrl kao tipku za kopiranje i lijepljenje, a umjesto palca mogu koristiti lijevu ružičastu boju. U svakom slučaju, ostavimo slabo obrazloženje jer to nije svrha ovog posta!

Ako nemate sustav Windows, ovaj post vjerojatno nije za vas, ako se razlikuje od onoga što upotrebljavate, to ga neće učiniti lošim.

Pogledajmo što Ken o tome ima reći:

Dakle, oko 4 mjeseca koristim Windows na poslu puno radno vrijeme, a tbh to uopće nije loše.

- PATAGUCCI MANE (@ken_wheeler) 20. prosinca 2018

Ovo je osvježavanje mog vodiča s početka godine o postavljanju okruženja za web razvoj na Windows računalu.

Ovaj će vodič pokrivati ​​instaliranje Ubuntua, ali možete koristiti neke druge okuse Linuxa dostupne u Windows trgovini, postavke Debiana bit će vrlo slične Ubuntuu.

Cilj ovog posta

Da biste prešli sa svježe instance Windows 10 na nešto s čime možete razvijati web aplikacije.

Ove se upute odnose na Windows 10 Fall Creators Update i novije verzije.

Što ćemo pokriti:

  • Instalirajte WSL
  • Omogućite WSL na vašem računalu
  • ažurirati, nadograditi i automatski ukloniti
  • Instaliraj čvor (sa n)
  • Instalirajte Visual Studio Code
  • Instalirajte Windows Git
  • Instaliranje terminala (Hyper)
  • Instalirajte Fish Shell!
  • Instaliraj Oh My Fish
  • Riblje teme s OMF-om
  • Konfigurirati
  • Git config
  • Izradite aplikaciju React
  • Koristite SSH s GitHubom
  • WSL SSH konfiguracija
  • Windows Git Bash SSH konfiguracija
  • Promijenite WSL verziju
  • Zamotati!

Instalirajte WSL

Ubuntu možete instalirati iz Microsoftove trgovine, što će biti prva polovica instalacije, druga će biti kada otvorite aplikaciju.

Omogućite WSL na vašem računalu

Službene smjernice to vrlo dobro pokrivaju, ali ovdje ću dodati tipke prečaca ako ne želite klikati mišem.

Prije toga, ako niste odabrali PowerShell kao zadani naredbeni redak, možete je odabrati na stranici postavki programske trake koja se nalazi u odjeljku Personalizacija u Postavkama, predlažem da to učinite sada:

Iako smo u tome, trebali bismo također omogućiti skrivene mape i dodati nastavke datoteka za poznate vrste datoteka u programu Windows File Explorer.

Otvorite Windows File Explorer pomoću tipke Windows + e, a zatim na traci odaberite View. Zatim kliknite gumb Opcije krajnje desno, pojavit će se dijaloški okvir Mogućnosti mape, odakle želimo odabrati karticu Pogled i odabrati radio gumb za skrivene datoteke i mape s "Prikaži skrivene datoteke, mape i pogone", poništite i opciju "Sakrij nastavke za poznate vrste datoteka". Kliknite Primijeni i U redu.

Razlog zašto ovo radimo je zato što možemo vidjeti .gitmapu u strukturama projekata koja je također potrebna za datoteke poput .envdatoteka koje se koriste za konfiguraciju okoline.

Koristite tipku Windows + x, to je isto kao da desnom tipkom miša kliknete ikonu prozora na radnoj površini, otvorit će se izbornik brze poveznice. S ovog mjesta morate odabrati Windows PowerShell (Admin) opciju, možete to učiniti tako da pritisnete na tipkovnici. Dakle, tipka Windows + x pa a otvorit će upit za kontrolu korisničkog računa (Admin), pretpostavljajući da imate administratorska prava na svom računalu da biste za nastavak morali kliknuti da.

Kopirajte kod ovdje i zalijepite ga u prozor PowerShell, Ctrl + v će raditi u PowerShell-u, pritisnite enter da biste izvršili naredbu.

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Nakon toga zatražit će se ponovno pokretanje stroja, što biste i trebali učiniti.

Na poveznici potražite cjelovit popis Windows tipkovnih prečaca.

Nakon ponovnog pokretanja možete otvoriti program Ubuntu s izbornika Start i trebala bi se dogoditi druga instalacija (WSL-a na vašem sustavu). Pričekajte da se ovo završi, a od vas će se zatražiti da stvorite korisnika i lozinku za račun. Morat ćete upamtiti lozinku stvorenu za korisnika jer će se od vas tražiti da koristi sudoprivilegije.

ažurirati, nadograditi i automatski ukloniti

U vrijeme pisanja ovog teksta, verzija Ubuntua koju sam povezao je 18.04.1

Povezana aplikacija instalirat će najnovije stabilno izdanje Ubuntu-a na Windows.

Možete provjeriti s kojom verzijom Ubuntu-a ste instalirali:

lsb_release -a

Ako želite koristiti određenu LTS verziju Ubuntu-a, možete ih preuzeti iz Windows trgovine ovdje:

  • Ubuntu 16.04 LTS
  • Ubuntu 18.04 LTS

Sada ćemo nastaviti s ažuriranjem i nadogradnjom svih stvari, tri naredbe ovdje će ažurirati i nadograditi sav predinstalirani softver koji dolazi s Ubuntu instalacijom.

Kôd u nastavku su tri naredbe spojene zajedno sa &&. Oznaka -yje da automatski prihvati promjene koje će se izvršiti s nadogradnjama. Automatsko uklanjanje uklonit će neiskorištene pakete iz nadogradnji.

Kopirajte naredbe u nastavku u svoj Ubuntu terminal i pokrenite naredbu:

sudo apt update && sudo apt -y upgrade && sudo apt autoremove
Da biste išli od 16.04 do 18.04, pokušajte do-release-upgradeu terminalu. imajte na umu da ovo traje znatno duže od pukog brisanja trenutne instalacije Ubuntua i ponovnog pokretanja.

Sada kada je baza instalirana i ažurirana, moramo instalirati paket koji je bitan za izgradnju kako bismo kompajlirali i izgradili druge pakete, trebat će ga sljedeći paketi koje ćemo instalirati.

sudo apt install -y build-essential

Od ovog trenutka u predložio bih upotrebu dva bash prozora, jedan sa sudo omogućio drugi sa standardnim dozvolama.

tj. otvorite terminal i ne ulazite sudotamo, već koristite terminal kojem je odobreno to odobrenje.

Razlog tome je taj što sam otkrio da ako čvor instalirate kao sudo, svaki put kada želite pokrenuti npm installnaredbu trebat ćete dodijeliti sudo dozvole i sve to može postati pomalo zamorno. I stvarno ne biste trebali instalirati npm pakete sa sudo dozvolama.

Instaliraj čvor (sa n)

Let’s get our run-time on! If you use Node.js you will eventually have situations where you need to be able to switch Node versions, for this you may have heard to nvm (Node Version Manager) which by the way you can still use in WSL.

The reason for me opting for n over nvm is that in the past I experienced slow bash startup times whilst using nvm. Take a look at this WSL GitHub issue detailing it and the specific comment for the pros of using n.

Ok let’s install n, with n-install, the command doesn’t start with sudo so use it in the bash window you have with no sudo privileges.?

curl -L //git.io/n-install | bash

This will install the latest version of Node for us, follow the prompt on the terminal screen to reload bash:

# mine looks like this. /home/scott/.bashrc

Now check the versions of Node and npm we have installed with node -v && npm -v in the terminal.

Install Visual Studio Code

Install VSCode? What? This post is to set up WSL? Ok, so we’re going to presume that this is going from nothing to being able to web dev, so we’re going to need a text editor, and there’s nothing out there at the moment that comes close to VSCode, if you already have a text editor installed then skip to the next part.

Install the Windows version of VSCode from the Download section once installed we can enable WSL in the settings, the quickest way to do this is to open the integrated terminal in VSCode with the shortcut keys Ctrl+’ ? that’s and apostrophe. You will be prompted to select your default terminal, select WSL Bash.

Install Windows Git

There’s a bit of stumbling block getting Git support for VSCode, it’s well documented in the various issues mentioned in the linked issue.

There are workarounds as well with things like WSLGit which has its own set of issues, this all stems from users (me included) not wanting to have to install another binary for Git.

I have tried several variants when it comes to using Git with VSCode and the path of least resistance was biting the bullet and installing that additional binary, there’s an overhead with having to maintain and configure SSH keys for both Windows and WSL Git but it’s a one time setup.

Until the VSCode team incorporate WSLGit into VSCode I think this is the best option.

Install from git-scm.com the link will start downloading the install binary you can then open it and go through the install, you can keep clicking next through the install, I have selected a few options that I would like enabled.

For now, that is all we need to do, when it comes to authenticating with with GitHub using SSH we will use the Git Bash command line to configure the Windows side of things.

Install a Terminal (Hyper)

Now we have bash on Windows it’s time to install a nice Terminal app, because, let’s face it, the standard one is a bit basic.

Enter Hyper an electron based terminal app that is super themeable and configurable.

Download and install hyper for Windows, this will be the bare-bones version, it’ll look something like this:

You may notice that, this is the Windows Command prompt too — don’t worry, we’re going to configure that right now.

Open up the .hyper.js file located in the root of your user folder, or from Hyper itself use the shortcut key Ctrl+, to pop open the settings.

If the settings file (.hyper.js) opens in Notepad then you can set the default to be VSCode. In File Explorer (Use Windows key+e to open it) navigate to the file, it'll be in your User folder, right-click the file and select Properties, then Change for 'Opens with:' and select VSCode from the list.

Now we can set WSL as the shell path in Hyper, open the .hyper.jssettings file and search (Ctrl+f) for bash, we want to add the path to the WSL shell into the shell property defined in the file.

// amend shell path// WSL Bashshell: 'C:\\Windows\\System32\\bash.exe',

We can also change the appearance of Hyper from here by specifying font size and font family and also predefined themes, let’s quickly add in the hyper-adventure-time theme into the plugins section.

Open another Hyper tab with Ctrl+Shift+t, this will show the bash terminal for WSL now.

Quick orientation with the Hyper terminal keyboard shortcuts:

  • New tab = Ctrl+Shift+t
  • Close current tab = Ctrl+Shift+w
  • Cycle through tabs right = Ctrl+Shift+right arrow
  • Cycle through tabs left = Ctrl+Shift+left arrow

Now I’m going to configure some additional properties for Hyper, and change the theme to something a bit more subtle.

I purchased Dank Mono, if you want a similar font as OSS check out FiraCode.

Here’s what I changed:

module.exports = { config: { fontSize: 18, fontFamily: '"Dank Mono",...', cursorShape: 'BEAM', cursorBlink: true, copyOnSelect: true, plugins: ['hyper-altair'] }}

Install Fish Shell!

Ok, time to install the Fish Shell! This is a completely optional step, I like to use fish for the nice intellisense you get when navigating file structures, there are also some neat themes you can get with Oh My Fish

sudo apt -y install fish

Install Oh My Fish

Now we can install Oh My Fish (OMF) and get a nice terminal theme going on too, remember we talked about running commands with the correct permissions? Well this is one of those occasions, open a new tab first then paste in the following:

curl -L //get.oh-my.fish | fish

Fish themes with OMF

After installing OMF you can pick a theme, here you may notice that the text doesn’t fit in the screen on Hyper, a quick way to reset it is to maximise the window then back again, I did this with the Windows key+arrow up to maximise the Windows key+arrow down to restore it.

After installing omf I chose the theme agnoster, installed with omf install agnoster you can list out what is available and what you have already installed by selecting omf theme let's change it once more to the one theme, adjust the size of the window first as things are getting a bit cramped.

Have a play around there’s loads there, I prefer the one theme because you can see what version of node you’re running on, over on the far right. Here you can also see the intellisense for fish where it shows agnoster as a previous selection, if I wanted to select agnoster I could tab to complete the rest of the command.

Configure

So, now we have a swanky looking new terminal and a bare-bones VSCode install it’s time to get things moving.

I have a cheat-sheets repo detailing a lot of the configuration we’re about to go through here, if you find something useful along the way please drop a PR with the suggestion.

First up, in place of typing out fish each time I open a new terminal, and not replacing bash which you can do, but I prefer to use an alias. So what we're going to do is open up the .bashrc file and edit it.

From a new terminal type in nano ~/.bashrc, nano is a bash text editor. Scroll (or Page) down to the bottom of the file and add in an alias for fish, alias f=fish then to exit nano Ctrl+x and confirm changes with a y. Reload the .bashrc file like we did after using n to install Node . /home/scott/.bashrc but your username in place of mine ?, now we can use f instead of fish! Well done you saved yourself typing three extra characters!!!

Git config

Before we crack on with spinning up a Create React App instance we’re going to need to configure Git, via the .gitconfig file.

We’re going to use nano to add some settings for Git, both in WSL and Windows, this is the extra config I mentioned earlier, apologies!

First up we’ll config our bash environment and then move onto Git bash on Windows.

# open up the .gitconfig file in WSL bash# nano will create one if it doesn't existnano ~/.gitconfig

It’s nearly identical config for both environments apart from Windows uses the credential manager helper and WSL will need the cache helper.

# needed in Git Bash for Windows[credential] helper = manager# needed in WSL[credential] helper = cache[color] ui = true
[user] name = yourGitHubUserName email = [email protected]
[core] editor = code --wait

Make your changes then Ctrl+x to exit and confirm with y, that’s the first part of the Git config done.

In Git Bash for Windows do the same in the terminal, use nano to create a .gitconfig file and add the setting to it.

Create React App

Ok, we shall get create react app going so we can get Git configured with GitHub using SSH and start making changes to commit to a GitHub repo.

We’re going to use npx to make a starter React project with Create React App. If you want to have your projects somewhere else now is the time to navigate to that directory.

From the terminal enter:

# this will create a starter react project# called create-react-appnpx create-react-app create-react-app

This is creating the project in my home (~) folder, I've had to speed the gif up to 20x faster, it does take a while.?

Wheeeeeeeeeeeeeeeeeeeee!

So, now that we have spun up a React app to work on we should make a change and then add it to GitHub.

Navigate to the create-react-app folder in the terminal and open the project. You can open VSCode with the code . command, the . denotes the current folder in the terminal.

This will open VSCode at the root of the create-react-app project, make sure everything is working as expected by running the start script in npm.

npm run start

If you want to see what scripts are available in the current project you can also use npm run which will list defined npm commands from the package.json file.

Otvorit će se prozor koji prikazuje osnovnu odredišnu stranicu Create React App, App.jsmodulu ćemo dodati h1, a zatim ga urediti, putem izbornika Source Control u VSCode.

Imajte na umu da je ovo aktualan vodič za razvojno okruženje, neću detaljno opisivati ​​razvoj u Reactu

Dok je terminal zauzet pokretanjem stranice za stvaranje-reagiranje, možemo otvoriti drugu karticu u Hyperu pomoću Ctrl + Shift + t i otvoriti VSCode code .naredbom, prijeći na App.jskomponentu i dodati joj <oznaku h1> samo above the t; označi sa

; Pozdrav Reagiraj!

u tome.

Now the landing page should show the newly added h1, we can confirm that the file is changed in Git by doing a git status from the terminal. Then we can commit it from either the terminal or the IDE (VSCode) my preference is to use VSCode as the visual diffing of files is great.

Commit the changes with Ctrl+Enter if you have not selected the file you want to commit you will get a dialogue telling you there are no staged changes to commit, you can select the files (in this case only the App.jsfile) by clicking the plus next to the file. Hitting Ctrl+Enter again will commit the changes with a message, you can check that there are no changes to commit with git status from the terminal or visually from the Source Control section in VSCode.

Ok, time to add our super sweet code changes to a repository on GitHub for the world to see!

Next up we’re going to go through adding the local changes we’ve made to a GitHub repository. If you haven’t got a GitHub account and are following along now may be a good time to set one up.

Use SSH with GitHub

First up, we’re going to make a repository on GitHub and push the changes we have made to it, here we’re going to click the + button in the top right of the GitHub home page, enter a repository name and GitHub will give you some defaults to choose from. As we have already made change locally we can ignore the first several commands but we do need the command to add the remote:

git remote add origin [email protected]:spences10/cra.git

And to push the changes up to GitHub git push -u origin master but we're going to get an error, as we haven't set up SSH.

WSL SSH Config

Otvorite novu karticu bash u Hyper-u i unesite ls -al ~/.sshprovjerite nema li rsadatoteka prije nego što to učinite.

Primijetit ćete da to radimo u bashu, a ne u Fishu.

Ako tamo nema ničega, generirajte nove ključeve pomoću:

# add your email address ?ssh-keygen -t rsa -b 4096 -C [email protected] 

Sada ls -al ~/.sshće se prikazati dvije dodatne datoteke:

# there should be a private and public key-pairid_rsaid_rsa.pub

Pokrenite SSH agent:

eval "$(ssh-agent -s)"

Dodajte RSA ključ u SSH pomoću:

ssh-add ~/.ssh/id_rsa

Sada je vrijeme da dodamo javni SSH ključ GitHubu, u WSL-u ćemo kopirati-lijepiti javni SSH ključ cat ~/.ssh/id_rsa.pubu Hyper. Tada možemo kopirati s Hypera na novi SSH ključ.

Na kraju, provjerite autentičnost pomoću GitHub:

ssh -T [email protected]

Windows Git Bash SSH konfiguracija

Za Windows Git Bash kopirat ćemo naše SSH ključeve koje smo upravo generirali u WSL-u u Windows Git Bash, postoje neke suptilne razlike prilikom autentifikacije s Windows Git Bashom.

Iz Hypera, kopirajte SSH datoteke s Linuxa na Windows:

cp ~/.ssh/* /c/Users/spenc/.ssh/

Pokrenite SSH agent:

eval `ssh-agent -s`

Dodajte ključ SSH agentu:

ssh-add ~/.ssh/id_rsa

Autentifikacija:

ssh -T [email protected]

Now the additional set up for SSH is done we should be able to go back to Hyper and push our change to GitHub.

Also, you may have noticed that the SSH Key ? token went from black to green on the settings screen there this indicates that you have authenticated.

Change WSL version

If you want to use a different version of Ubuntu, Debian or any of the other available distributions of Linux from the Windows store with Hyper you will need to change the default version from PowerShell:

# list available versionswslconfig /l# set defaultwslconfig /setdefault Debian

Wrap up!

That’s it! We have gone from scratch to a functioning web development environment. There are several other personal things I’m now going to add to my install now which is aliases for both Git and Fish shell. If you are interested in them I have a cheat-sheet for Fish with a list of all my aliases I use, the same for my .gitconfig you can find that in my dotfiles repo

Thanks for reading ?

This was originally posted on my Gatsby blog you can view it here please take a look at my other content if you enjoyed this.

Follow me on Twitter or Ask Me Anything on GitHub.

You can read other articles like this on my blog.