Skip to content

cli: add support to bin scripts #58172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

IlyasShabi
Copy link
Contributor

@IlyasShabi IlyasShabi commented May 4, 2025

Description

This PR makes node --run <cmd> run commands from the package’s bin section as well. If the command is in both scripts and bin, the version in scripts still wins.

#53845

TODO

  • Add documentation

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels May 4, 2025
@IlyasShabi IlyasShabi changed the title cli: Add support to bin scripts cli: add support to bin scripts May 4, 2025
@IlyasShabi IlyasShabi force-pushed the ishabi/bin-scripts branch from e8fd4dc to 959e66f Compare May 4, 2025 20:52
@juanarbol
Copy link
Member

I'm -1 on this.

Node.js already bundles npm, which has built-in support for this. I'm not 100% sure, but surely other package managers supports it as well. This is a feature that simply works out of the box.

Refs:
https://docs.npmjs.com/cli/v11/configuring-npm/package-json#bin

Copy link
Member

@anonrig anonrig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change needs a documentation change, before others can review. I have almost no idea what this pull-request is, and had to lookup package.json spec to understand. The code should be self-explanatory. If the solution requires complexity, than we need comments everywhere

Comment on lines +237 to +241
assert.match(child.stderr, /Unknown script or bin entry "tmp"/);
assert.match(child.stderr, /Available scripts:\n/);
assert.match(child.stderr, /ada: ada\n/);
assert.match(child.stderr, /Available bins:\n/);
assert.match(child.stderr, /bin-test: \.\/test\.js\n/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you convert this into a snapshot test? It's extremely hard to understand this assertions.

ProcessRunner runner(result,
path,
command_id,
exec_cmd,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this string? You can just pass exec_cmd here?

@@ -291,57 +291,180 @@ void RunTask(const std::shared_ptr<InitializationResultImpl>& result,
return;
}

// If package_json object doesn't have "scripts" field, throw an error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some comments :-)

}
}

// Try "bin"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you be more explicit? What are we trying?

std::string_view pkg_name;
if (!main_object["name"].get_string().get(pkg_name) &&
pkg_name == command_id) {
exec_rel.assign(rel);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're confusing std::string and std::string_view. These lines are not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants