gperdomor / nx-tools

Nx Workspaces builders and tools
MIT License
337 stars 45 forks source link

Feature Request: Support `{absWorkspaceRoot}` and `{absProjectRoot}` token replacement for file paths #980

Open JakeDern opened 3 months ago

JakeDern commented 3 months ago

I'll open a PR for this myself if you agree with the idea and will review it.

context and file settings seem to be relative to where you run the command from. This is a bit annoying because if you're jumping around in the terminal then all of the sudden your image builds will fail. Currently the repos for my team are set up to have everyone just run everything from the root always.

I've implemented a handful of executors and have started supporting replacement of {absWorkspaceRoot} and {absProjectRoot} tokens for any file paths. It's very simple to do, you can get the necessary information from the executor context. It's also consistent with how nx does it for executor inputs and outputs. Here's an example:

// You can get the root of any project like this and the context provides what the current project is
// Workspace root is available directly from `ctx.root`
function getProjectRoot(ctx: ExecutorContext): string {
    const projectRoot = ctx.projectsConfigurations?.projects[String(ctx.projectName)].root
    if (!projectRoot) {
        throw new Error(`Could not find project root for ${ctx.projectName}`)
    }

    return projectRoot;
}

Then you can just replace the tokens:

const finalContextPath = options.context.replaceAll('{absProjectRoot}', path.join(ctx.root, getProjectRoot(ctx))

Again I'm happy to do the work, just want to know if you agree with the idea first.

JakeDern commented 3 months ago

Updated to clarify I'm looking for support for absolute paths rather than the default nx behavior of giving relative paths from root.

coracuity commented 1 month ago

Seconding that this is valuable!

gperdomor commented 1 month ago

@JakeDern can you send the PR for this?