It is now called @redwoodjs/structure
/project.ts
: The main API and classes (such as Project, Page, Service, Side, etc)/language-server.ts
: A Language Server Protocol implementation that wraps the project
classes/typescript-language-service-plugin
: A TypeScript language service plugin for Redwood.The most common use-case is getting the diagnostics of a complete redwood project:
import { Project } from "./project";
async function test() {
const project = new Project({ projectRoot: "/foo/bar" });
for (const d of await project.getAllDiagnostics()) {
console.log(d.diagnostic.severity + ": " + d.diagnostic.message);
}
}
// ...
// error: Router must have only one "notfound" page
// error: Duplicate path in router: '/about-us'
// error: Parameter "id" in route '/product/{id}' does not exist on ProductPage
// error: PostsCell is missing the "Success" exported const
// error: Property "emial" does not exist on "User" model
// warning: Unused page AboutUs.js
Note: Gathering all diagnostics is expensive. It will trigger the creation of the complete project graph. You can also traverse the graph to get more specific information.
For example: Iterating over the routes of a redwood project:
import { Project } from "./project";
const project = new Project({ projectRoot: "/foo/bar" });
for (const route of project.web.router.routes) {
console.log(route.path + (route.isPrivate ? " (private)" : ""));
}
// /
// /about
// /product/{id}
// /admin (private)
You can also get nodes by id
. For example:
import { Project } from "./project";
const project = new Project({ projectRoot: "/foo/bar" });
const router = project.findNode("/foo/bar/web/src/Routes.js");
(You can read more about id
s below).
In most cases, if you just want to get the node for a given file, you don't even need to create a project by hand:
import { findNode } from "./project";
findNode("/foo/bar/web/src/Routes.js")?.diagnostics?.length; // 8
The findNode utility method will recursively look for a redwood.toml file to identify where the project root might be.
The Diagnostics API/structures are based on the Language Server Protocol.
id
property.Here are some examples of ids:
"/project/root"
"/project/root/web"
"/project/root/web/src/Routes.js"
"/project/root/web/src/Routes.js /home"
(notice that this id has two elements - it is an "internal" node)An id is "usually" a file or folder.
Anatomy of an id:
import { Project } from "./project";
const project = new Project({ projectRoot: "/foo/bar" });
// lets find the "/home" page and delete it
const home = project.web?.router?.routes?.find((r) => r.path === "/home");
if (home) {
const edits = home.remove();
// returns a list of edits that need to be applied to your project's files
// in this case, some file deletions and some file modifications
}
Some diagnostics provide a "quickFix", which is a list of edits that will "fix" the error.
For example, let's create an empty "page" file and then get its diagnostics:
import { findNode } from "./project";
const pageNode = findNode("/foo/bar/web/src/pages/AboutUs/AboutUs.js");
pageNode.diagnostics[0].message; // this Page is empty
pageNode.diagnostics[0].quickFix.edits; // a list of edits to fix the problem
pageNode.diagnostics[0].quickFix.edits.apply();
You can apply the edits
To allow use cases like dealing with unsaved files in IDEs, some filesystem methods can be overriden via the Host interface.
When possible, the project graph is constructed synchronously. There are only a few exceptions. This simplifies the domain logic and validations, which is the main driver behind the project model itself.