Giter Site home page Giter Site logo

nx-pwm's Introduction

Nx PWM

nw-pwm (short for NX Packages Workspace Manager) is a plugin/cli that aims to provide tools to manage publishable packages in an NX Workspace.

It works for workspaces with independently versioned libraries, and libraries that follow the same version.

Why nx-pwm exists?

nx-pwm is highly inspired by tooling that exist in the NX-repo itself. As I've wanted to create NX Workspaces for publishable packages, I've looked into how NX itself handles this issue, and replicated it to several workspaces.

This worked, however it was not a scalable solution, as it required copy-pasta from one repo to another (with possible adjustments), and handling it required some more in-depth knowledge of NX, and the reasoning behind the tooling. This is not something I expect a colleague to easily fathom, as most do not have in-depth knowledge of NX.

I wanted to make the tooling accessible to anyone, and make it as easily usable and adjustable. This is what nx-pwm is - it's an NX Plugin (Executors and Generators) and a CLI to make the aforementioned tooling easy to pick up and use, with as little knowledge of NX as possible.

What is the workflow nx-pwm enables?

As mentioned before, nx-pwm comes to replicate (mostly) the workflow in the NX repo. The NX repo is a workspace consisting of many plugin packages, the nx package itself, and @nrwl/devkit, alongside whatever testing machinery needed to test all that code.

Library Versions Management

A primary concern for managing packages, especially in NX itself, is handling package.json dependencies versions, as well as library versions that plugins install and maintain in other NX workspaces.

For that, it employs 2 tools:

1. depcheck (the nx-pwm:depcheck executor)

Using the depcheck library, we ensure that libraries that a plugin uses are declared in package.json. This is especially needed in an NX workspace where dependencies exist in the workspace root, and will be available to projects without declaring them in their package.json.

This also ensures that the versions specified in a project's package.json matches the version specified in the root package.json (if specified) semver-wise.

2. version-check (the nx-pwm:version-check executor)

This utility helps ensure that library versions are both up-to-date, in both package.json, and versions.ts files.

It collects library versions from these files, and uses npm view to find newer available versions.

It also allows you to update the version files with the newer versions found, as well as create/update a migration in a plugin's migration.json.

What are version.ts files?

versions.ts files are a convention in NX plugins to maintain the versions strings to be used when generating or migrating projects. Such files export constants with a clear naming convention: <libName>Version, where <libName> is the library npm name camel-cased, for example:

// packages/my-plugin/src/utils/versions.ts
export const chalkVersion = '0.0.0';
export const nrwlNodeVersion = 'x.y.z'; // @nrwl/node -> nrwlNode

The version-check script scans such version.ts files, and collects the library names and their respective versions.

Given that convention, special casing needs to be done to properly convert a variable name to the correct npm package name, specifically for packages with scopes ("nrwlNode" === "nrwl-node" or "@nrwl/node"?).

Local Registry (the nx-pwm local-registry <cmd> cli)

In some cases, to properly test our packages, we'd need to publish them to a package registry and use them in a test project, for example.

This can be done using a local registry, which you can run on your machine and point your package manager to work with.

The package registry used by the NX repo (and nx-pwm) is verdaccio.

nx-pwm provides CLI commands for the following actions:

nx-pwm local-registry <command>

local registry utilities

Commands:
  nx-pwm local-registry start    start local registry
  nx-pwm local-registry clear    clear local registry cache
  nx-pwm local-registry status   status of package managers registry configuration
  nx-pwm local-registry enable   enable local registry
  nx-pwm local-registry disable  disable local registry

Lock File Check (the nx-pwm check-lock-file cli)

As you use a local registry, references to it might end up in your workspace's package manager lock file (package-lock.json/yarn.lock/pnpm-lock.yaml).

To help prevent from local registry references ending up being pushed to your git remote, nx-pwm provides a command that validates that your lock file does not contain such references.

nx-pwm's People

Contributors

gioragutt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nx-pwm's Issues

Create an init generator

The init generator should

  • setup the .nx-pwm.json file
  • allow to add a depcheck target for projects

Template for the config file:

{
  "$schema": "./node_modules/nx-pwm/src/lib/config-schema.json",
  "versionType": "independent",
  "depcheck": {
    "ignore": {
      "discrepancies": {
        "*": []
      },
      "missing": {
        "*": []
      }
    }
  }
}

TESTS!

I need to test all of this, it's time.

Initial Research

Tooling in each repo

nx-packages and nx-plugins are repos internal to my company. Both are packages workspaces, the plugins repo is synced-versions, and the packages repo is independent.


nx-packages
File Name Purpose Notes
tools/check-lock-files.js Ensures that there’s no lock file but yarn.lock, and ensures that yarn.lock does not reference the local registry  
tools/check-versions.ts Checks library package json files, and dependencies that have newer versions Contains a check to ignore libraries that are part of the repo
tools/depcheck/discrepancies.ts Looks for dependencies in project package.json files that do not satisfy the root package.json (or vice versa)  
tools/depcheck/missing.ts Uses the depcheck module to find missing imports in projects  
tools/depcheck/index.ts CLI entry point for running the discrepancies/missing checks are reporting those Contains a check to ignore libraries that are part of the repo
tools/library-publishing-plugin.js NX Plugin that adds version/publish targets to packages Only relevant for independent-versions libraries
tools/local-registry.sh Manages setting-up/tearing down and configuring package managers to work with verdaccio  
tools/local-registry/config.yml Verdaccio config our configs might be invalid since it still only references @nrwl packages as a special case, something that we might be missing out
tools/package.ts Builds packages and updates the built package.json for local or prod publishing  
tools/scripts/publish.mjs Script generated by NX itself to be a good default script for publishing repositories  
tools/tsconfig.scripts.json tsconfig for .ts scripts  
tools/tsconfig.tools.json tsconfig generated by NX, difference is "rootDir": "*"  
tools/workspace-utils.ts Utility module that exports metadata about projects in the repository for other scripts  

nx-plugins
File Name Purpose Notes
tools/check-lock-files.js Same as in nx-packages  
tools/check-versions.ts Intended for use in plugins which have a lib/utils/versions.ts package. Checks for newer versions of packages specified there. Requires external configuration to differentiate between scoped packages and other packages.
tools/depcheck/discrepancies.ts
tools/depcheck/index.ts
tools/depcheck/missing.ts
Should be the same as in nx-packages. TODO: diff the code
tools/find-affected.js Basically a hack until affected --tags is added Should be a reusable github action
tools/local-registry.sh
tools/local-registry/config.yml
Same as in nx-packages  
tools/nx-release.js Script to release all packages in a synced-version workspace  
tools/package.js Should be more or less the same as in nx-packages, there is definitely a difference tho. CHECKITOUT
tools/publish.sh Utility script of nx-release.js to publish all the packages  
tools/tsconfig.scripts.json
tools/tsconfig.tools.json
Same as in nx-packages

  • Add same summary for the NX repo itself
nrwl/nx
File Name Purpose Notes

What’s the scope of the CLI

Provides tooling for developing, validating, and optionally building & publishing packages in an NX workspace.

  • Differentiation should be done for synced-version and independent-version.

TODO: Check how the semver plugin works for synced-version. Perhaps nx-release.js is deprecated.

  • Take on the local-registry utility, improve it, add a status utility, etc.

  • check-version.ts should be consolidated to a single tool and have it check plugins and normal libraries simultaneously.

  • Perhaps check-lock-files should be adjusted to allow different package managers?

Create the CLI/Plugin project

Requirements

CLI

nx-pwm should be a CLI so that it can be used the following way:

$ yarn add -D nx-pwm
$ yarn pwm check-versions
$ yarn pwm depcheck

Plugin

nx-pwm should also be a plugin so that it can be used the following way:

$ yarn add -D nx-pwm
$ yarn nx g nx-pwm:install

Implementation

Project generation

As far as I'm concerned, nx-pwm can be generated with @nrwl/nx-plugin.
This does limit the ability to extract logic to nx-based libraries.
For now, this is acceptable. If I want to extract certain behaviors to libraries, I might as well make nx-pwm modular and publish the behaviors.

CLI Impl

TBD

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.