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

Having trouble properly implementing ShellJS #1107

Open
Morsmalleo opened this issue Oct 24, 2022 · 5 comments
Open

Having trouble properly implementing ShellJS #1107

Morsmalleo opened this issue Oct 24, 2022 · 5 comments
Labels
question Question from a user (which may not require code/documentation changes to the project)

Comments

@Morsmalleo
Copy link

Morsmalleo commented Oct 24, 2022

G'day guys, I'm so sorry to bother you with such a silly question, I was wondering if anyone could help me out with properly implementing Shelljs into my project, I'm very new to shelljs and a little bit confused on the documentation of how it's implemented, basically all I want to do is allow the Unix find command and it's configuration from my NodeJS app shown below, to work cross platform with Windows... Instead of it just working on Mac & Linux

exec('find -name "' + "file.txt" + '"', { cwd: randomFolder }, (error, stdout, stderr) => {
     var filePath = stdout.substring(stdout.indexOf("./") + 2).trim("\n");
     if (error !== null) {
         console.Log("Cannot Locate the File...");
         console.log();
         return;
     }

but I don't know how to properly implement shelljs here, can anyone please help me out with how this is done, I've been trying at this for a while now!

@nfischer
Copy link
Member

Try using shell.find('...') instead of shell.exec('find ...'). shell.exec() just executes a binary in the default shell; on Windows this would execute the Windows find command (if such a command can be found on your system) which may not understand unix-style arguments. On the other hand, shell.find() is a JavaScript function which behaves the same across all platforms.

You may need to adapt the syntax to work with shell.find(). Check out the documentation: https://github.com/shelljs/shelljs#findpath--path-

@nfischer nfischer added the question Question from a user (which may not require code/documentation changes to the project) label Oct 24, 2022
@Morsmalleo
Copy link
Author

Morsmalleo commented Oct 24, 2022

So something like this???

var path = "C:/Users/someUser"
var file = "file.txt"

shell.find(file, { cwd: path }, (error, stdout, stderr) => {
     var filePath = stdout.substring(stdout.indexOf("./") + 2).trim("\n");
     if (error !== null) {
         console.Log("Cannot Locate the File...");
         console.log();
         return;
     }

Sorry I'm very new to ShellJS

Edit

I've cleaned up the code after reading the npm ShellJS documentation a few times over, hopefully that's close to what I need

@nfischer
Copy link
Member

The callback is only available for shell.exec(). The other ShellJS methods (like shell.find()) return their results synchronously.

I think you want something like:

shell.cd(path);
var results = shell.find(file);
// "results" is like an array, but with some extra ShellString properties.
// Check .stderr or .code to determine success.
if (results.code !== 0) {
  console.log(results.stderr);
} else {
  // Assuming we're searching for an exact match rather than a wildcard pattern,
  // then there should only be 1 element in the array.
  var filePath = results[0];
}

I haven't tested this, so you may need to tweak the code to get it the way you want.

@Morsmalleo
Copy link
Author

The callback is only available for shell.exec(). The other ShellJS methods (like shell.find()) return their results synchronously.

I think you want something like:

shell.cd(path);
var results = shell.find(file);
// "results" is like an array, but with some extra ShellString properties.
// Check .stderr or .code to determine success.
if (results.code !== 0) {
  console.log(results.stderr);
} else {
  // Assuming we're searching for an exact match rather than a wildcard pattern,
  // then there should only be 1 element in the array.
  var filePath = results[0];
}

I haven't tested this, so you may need to tweak the code to get it the way you want.

Thank you I'll check this out and let you know how I go

@milahu
Copy link

milahu commented Dec 2, 2022

nitpick:

Having trouble properly implementing ShellJS

please rename to "using ShellJS"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question from a user (which may not require code/documentation changes to the project)
Projects
None yet
Development

No branches or pull requests

3 participants