shelljs
shelljs copied to clipboard
Having trouble properly implementing ShellJS
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!
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-
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
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.
The callback is only available for
shell.exec(). The other ShellJS methods (likeshell.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
nitpick:
Having trouble properly implementing ShellJS
please rename to "using ShellJS"