Package: aws-prototyping-sdk.nx-monorepo
The nx-monorepo package vends a NxMonorepoProject Projen construct that addsNXmonorepo support and manages your yarn/npm/pnpm workspaces on your behalf. This construct enables polyglot builds (and inter language build dependencies), build caching, dependency visualization and much, much more.The PDK itself uses the nx-monorepo project itself and is a good reference for seeing how a complex, polyglot monorepo can be set up.BREAKING CHANGES(pre-release)v0.13.0:WorkspaceConfig.nxConfigtypeNxConfig => Nx.WorkspaceConfig, andoverrideProjectTargetsremoved in favor ofPDKProject.nxconfig to manage Nx project configurations. SeePR 231.To get started simply run the following command in an empty directory:npxprojennew--from@aws-prototyping-sdk/nx-monorepoThis will bootstrap a new Projen monorepo and contain the following in the .projenrc.ts:import{nx_monorepo}from"aws-prototyping-sdk";constproject=newnx_monorepo.NxMonorepoProject({defaultReleaseBranch:"main",deps:["aws-cdk-lib","constructs","cdk-nag"],devDeps:["aws-prototyping-sdk"],name:"my-package",});project.synth();To add new packages to the monorepo, you can simply add them as a child to the monorepo. To demonstrate, lets add a PDK Pipeline TS Project as a child as follows:import{nx_monorepo}from"aws-prototyping-sdk";constproject=newnx_monorepo.NxMonorepoProject({defaultReleaseBranch:"main",deps:["aws-cdk-lib","constructs","cdk-nag"],devDeps:["aws-prototyping-sdk"],name:"my-package",});newPDKPipelineTsProject({parent:project,outdir:"packages/cicd",defaultReleaseBranch:"mainline",name:"cicd",cdkVersion:"2.1.0"});project.synth();Once added, runnpx projenfrom the root directory. You will now notice a new TS package has been created under the packages/cicd path.Now let's add a python project to the monorepo and add an inter-language build dependency.import{nx_monorepo}from"aws-prototyping-sdk";import{PDKPipelineTsProject}from"aws-prototyping-sdk/pipeline";import{PythonProject}from"projen/lib/python";constproject=newnx_monorepo.NxMonorepoProject({defaultReleaseBranch:"main",deps:["aws-cdk-lib","constructs","cdk-nag"],devDeps:["aws-prototyping-sdk"],name:"test",});constpipelineProject=newPDKPipelineTsProject({parent:project,outdir:"packages/cicd",defaultReleaseBranch:"mainline",name:"cicd",cdkVersion:"2.1.0"});//StandardProjenprojectsalsoworkhereconstpythonlib=newPythonProject({parent:project,outdir:"packages/pythonlib",authorEmail:"",authorName:"",moduleName:"pythonlib",name:"pythonlib",version:"0.0.0"});//Pipelineprojectdependsonpythonlibtobuildfirstproject.addImplicitDependency(pipelineProject,pythonlib);project.synth();Runnpx projenfrom the root directory. You will now notice a new Python package has been created under packages/pythonlib.To visualize our dependency graph, run the following command from the root directory:npx nx graph.Now lets test building our project, from the root directory runnpx nx run-many --target=build --nx-bail. As you can see, the pythonlib was built first followed by the cicd package.This is equivalent to runningyarn build,pnpm build, ornpm run builddepending on your node package manager, and similarlyyarn buildalso accepts the sameNx Run-Many options(eg:yarn build --projects=cicd).The NxMonorepoProject also manages your yarn/pnpm workspaces for you and synthesizes these into your package.json pnpm-workspace.yml respectively.For more information on NX commands, refer to thisdocumentation.Homogenous DependenciesAs well as adding implicit dependencies, you can add dependencies between projects of the same language in order to have one project consume another project's code.TypescriptSince theNxMonorepoProjectmanages a yarn/npm/pnpm workspace, configuring dependencies between Typescript projects is as straightforward as referencing them indeps.Note that dependencies cannot be added in the same project synthesis (npx projen) as when projects are created. This means a two-pass approach is recommended, first to create your new projects, and then to add the dependencies.For example:First add your new projects:newTypeScriptProject({parent:monorepo,outdir:"packages/a",defaultReleaseBranch:"main",name:"project-a"});newTypeScriptProject({parent:monorepo,outdir:"packages/b",defaultReleaseBranch:"main",name:"project-b",});Synthesise, then you can set up your dependency:consta=newTypeScriptProject({parent:monorepo,outdir:"packages/a",defaultReleaseBranch:"main",name:"project-a"});newTypeScriptProject({parent:monorepo,outdir:"packages/b",defaultReleaseBranch:"main",name:"project-b",//BdependsonAdeps:[a.package.packageName],});PythonPoetry (Recommended)The recommended way to configure dependencies between python projects within your monorepo is to use Poetry. Poetry sets up separate virtual environments per project but also supports local file dependencies. You can use the monorepo'saddPythonPoetryDependencymethod:consta=newPythonProject({parent:monorepo,outdir:'packages/a',moduleName:'a',name:'a',authorName:'jack',authorEmail:'me@example.com',version:'1.0.0',poetry:true,});constb=newPythonProject({parent:monorepo,outdir:'packages/b',moduleName:'b',name:'b',authorName:'jack',authorEmail:'me@example.com',version:'1.0.0',poetry:true,});//bdependsonamonorepo.addPythonPoetryDependency(b,a);PipIf you are using pip for your python projects, you can set up a dependency using a single shared virtual environment. You can then install packages you wish to depend on into that environment using pip'sEditable Installs.You will also need to add an implicit dependency to tell the monorepo the correct build order for your packages.For example:constsharedEnv:VenvOptions={envdir:'../../.env',};consta=newPythonProject({parent:monorepo,outdir:'packages/a',moduleName:'a',name:'a',authorName:'jack',authorEmail:'me@example.com',version:'1.0.0',venvOptions:sharedEnv,});//InstallAintothevirtualenvsinceitisconsumedbyBa.tasks.tryFind('install')!.exec('pip install --editable .');constb=newPythonProject({parent:monorepo,outdir:'packages/b',moduleName:'b',name:'b',authorName:'jack',authorEmail:'me@example.com',version:'1.0.0',venvOptions:sharedEnv,//BdependsonAdeps:[a.moduleName],});//AddtheimplicitdependencysothatthemonorepowillbuildAbeforeBmonorepo.addImplicitDependency(b,a);JavaThe recommended way to configure dependencies between java projects within your monorepo is to use shared maven repositories. The default java project build will already create a distributable in the correct format for a maven repository in itsdist/javafolder, so you can use this as a repository. You can use the monorepo'saddJavaDependencymethod:For example:consta=newJavaProject({parent:monorepo,outdir:'packages/a',groupId:'com.mycompany',artifactId:'a',name:'a',version:'1.0.0',});constb=newJavaProject({parent:monorepo,outdir:'packages/b',groupId:'com.mycompany',artifactId:'b',name:'b',version:'1.0.0',});//bdependsonamonorepo.addJavaDependency(b,a);