Um, the other thing is, uh, you can have any consistent tooling. So, um, typically with the MPM scripts, um, they, they just get named haphazardly and there's a bunch of flags thrown in there. Um, and nobody knows exactly what all the flags do. Um, nobody knows exactly what all the scripts really do. Um, so you've got, you know, different versions of build and test and, and serve and Lint. And, you know, they're, they're, they're set up for different scenarios and different, um, you know, eventualities. But there's, uh, it's really hard unless you're working on that project every day. It's really hard to know what all those scripts actually do. Um, so it'd be nice to have, you know, some consistent rules and, and, um, uh, some, uh, just conventions about what those things should be named and, and how to easily, uh, know what each flag needs in that, uh, in your scripts.
All right. So you need some, some tooling to help you manage that, that complexity. So that's what, what NX can do. NX is a tooling to help you, uh, get all the benefits of a monorepo without the negatives of code colocation. All right. So NX gives you a faster command execution. So, um, executors are basically, um, like, uh, NPM scripts, um, that can help you, um, you run your, um, your build and test and lent and all the actions that you take on your code. Uh, NX affected is the tool that, that lets you run the tests for only the projects that are affected by a, uh, by a code change and not the tests that are unrelated. Um, and then we have local and distributed caching. So anytime you run a command, um, and, uh, if you run that same command again without changing the code, it just uses the, instead of actually running the build again, it just uses the, the cast version of that, um, of that output and, um, without, you know, actually, um, running the build again.
So if, if your bill takes, you know, a minute and a half, um, if you run that build again, instead of taking a minute, a half, it'll take a second or two just to pool that, that data from the cache and to replay the terminal output for you. Um, so that's just locally. That's, that's completely free. That's, that's in the open source NX. Um, then with a NX cloud, you can use the distributed caching. So that, that same caching behavior that you get, um, on your own machine, you can share that cache across your whole organization. So if anybody anywhere has run this command with this same code, you can use their cache and replay it yourself. So this, you know, particularly for PRS, you, you run your code, you push it up. Um, the runs in CI and then somebody else checks that branch out on their machine and they run the same thing. They can, um, just use your cache version instead of running that same, um, you know, computation on their machine again. Um, so basically you, you only run, um, the actions for a particular set of code one time across your whole organization, which is, um, and that's also, it's not just for, um, uh, that that's broken up by projects. So if you have, um, one application that depends on five different, um, libraries, um, that's the, the library build step is cached as well as the application build steps. So say you change just the application, those five libraries, their build output is already cached for you. And then, so you can just use that cache and then only do the last application built step, which can be game changing for large code bases.
Um, and X also gives you controlled code sharing. So you, you set a, an index.ts file at the root of your, um, each project. And you say everything exported from this index.ts file is available for other people to use. Um, but anything that's not exported from the, from that index.ts you're not allowed to reach in there and grab it and start using it. Um, you can set up a tagging system to say, these particular projects can depend on other projects. Um, and it's very flexible. You can set up your own, uh, structure of how those, how those tags work. Um, so the, these, these libraries belong to this team. And these libraries belong to these other team, this other team. And then these libraries are shared, uh, shared, and they can be used by either. Um, so you can set up that whole structure yourself. Uh, you can publish easily published libraries to MPM and, um, you can use the code owners file to require, um, this get feature GitHub feature, um, and or in any of it, like get lab and other, all the other, um, like code management, get repositories will, um, has the same functionality with code earners, where you force, um, a, a review from a certain, um, from a certain group of people, if you change code within that area. So you can control how that code is managed.
Number three is consistent coding practices. So you can, you know, there's, uh, NX gives you some Lint rules to help, um, you know, manage structure. Um, you can, uh, write code generators to automate the steps that people, the developers take over and over again. Um, there are plugins that are, that, uh, that NX creates and plugins created by the community have these, these generators and, uh, executors, um, written for you. Um, so the plugins help you with, um, basically that you can stop worrying about your build system. If you want to, you can have the plugin manager build system and then just make whatever tweaks you need to for your particular application. Um, plugins also help you with, um, automating the update process. So when a, um, when there's a new version of no that comes out, if there's a breaking change or if there's a new version of express or whatever, um, they, there's a migration script that, that can automatically update your code to, to handle any breaking changes that, that happen. Um, and same thing with community plugins, they, they're community plugins that, uh, that cover, uh, use cases that, that NX does not.
Then the other thing is a NX gives you a, um, an accurate architecture diagram of how your projects depend on each other. So this is useful for the NX effective command, but it's also useful for yourself to really see, okay, what is the actual architecture of my code rather than the architecture that I wish my code had, or the architecture that my code had two months ago when somebody drew a diagram? Um, so NX actually understands your code and can draw the connecting dependency lines between your projects based on what's actually there you or some with some architect actually like wished was there, or thought should be there. So, um, yes, this is a typical NX file structure. Um, really your, your file structure can be whatever you want, but basically, um, the, the way we'll set it up was, we'll have an You can set up a gist folder and a libs folder. Um, and then yeah. Dist is where your, um, you know, applications get built to, um, tools is for like scripts and, and helper things. Um, workspace. JSON is not valid anymore. Nx dot JSON is like your root level configuration for NX itself. Um, TS config that sets up a, a, um, um, a script, um, a, a, um, and then you set up kind of like paths, um, for your, um, for your libraries to automatically, so you can reference them to import them into applications. Um, or all the different, um, kind of configuration that NX has, there's, there's workspace level and project level. So the project level configuration will inherit from the workspace level configuration. Same thing, um, if it has a different, uh, TypeScript, uh, setting that it needs, same thing for Jest and Prettier and all the other, uh, tools that, that NX can set up for you.
All right, so that was a lot of talking. Um, we're going to get started with creating a new workspace. Um, so to create a new workspace, you run a NPX create NX workspace, or you can do the same thing with YARN or PMPM, um, and you give it a name. So in this case, the name is sets three different things. It'll set what directory that you're putting your code in. It'll give you the, uh, path alias, um, for importing code, uh, within things. So if you set your workspace name to my org, your directory will be under my dash org, your path alias will be at my org slash whatever project you're importing. And then if you ever, if you ever publish your, um, a library to MPM, the, the MPM scope will be at my org, um, slash, whatever library you have. So each of these three things you can change after the fact. Um, but when you initially set up your workspace, um, whatever you type in after CreateNX Workspace will be, will be set to those, those two things will be set to that name. Okay. So let's get started with lab one. Okay. So, um, this repo here, you do not need to, um, you don't do not need to clone this repo. Um, cause we're going to be creating a whole new repo, um, for the, um, for the workshop. So we're going to go to lab one here, generate an empty work NX workspace for a fictional company called the BoardGameHorde. And we're going to name it BG Dash Horde. All right. So I'm going to run NPX create NX workspace. And we're going to name it BG dash Horde. All right. Uh, let me, let me make this full screen so we can read it.
Comments