Skip to content
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

feat: Implement ls command #17

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

metcoder95
Copy link

@metcoder95 metcoder95 commented Jul 18, 2023

This PR attempts to introduce the ls command, similar to npm and pnpm versions.
Its initial version aims for simplicity and just provides a tree-like representation of the modules installed based on the package.json manifest with options to omit dev or dependencies accordingly.

Note: I just opened this PR as most likely will require some early feedback, I'll be pushing more changes soon 🙂

crates/cli/src/commands.rs Outdated Show resolved Hide resolved
/// Indicates what dependencies to omit while running
/// ls
#[arg(long = "omit", short = 'O')]
pub omit: String
Copy link
Member

Choose a reason for hiding this comment

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

It seems like pnpm has different characteristics than npm on this. Can you follow pnpm? https://pnpm.io/cli/list

Copy link
Author

Choose a reason for hiding this comment

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

Here I'm quite stuck (sorry for the silly question, still getting used to Rust ownership concept).
I've tried several things but always stopped upon the issue of the lifetime of the strings within the loop.

cc: @anonrig

Comment on lines +79 to +98
for (name, version) in dependencies {
// let label = format!("{} - {}", name, version);
// print!("{}", label);
let mut tree = helper::create_tree(name, version);

if depth > 1 {
let path = format!("{}{}{}", &name, MAIN_SEPARATOR, "package.json");
let pjson = PackageJson::from_path(&node_modules_path.join(path))?;
let subtree = self.list(
&pjson,
DependencyGroup::Default,
node_modules_path,
depth - 1,
)?;

tree.push(subtree);
}

root.push(tree.clone());
}
Copy link
Author

Choose a reason for hiding this comment

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

Basically, here, I'm losing the reference to the name and version, after the scope of the loop ends, which is expected.
The main issue comes at the moment of trying the build the whole tree for a single manifest.
My initial idea was to use a recursive data structure (Tree), read its dependencies, mount it into a three representation; and continue accordingly to the depth passed.

Sadly, the way I initially designed seems to not play well with the borrowed concept 🤔

Another option was to convert it to an iterator and use the utils function to map it and print it back. But wanted to keep it as last resort.

Any thoughts? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants