Skip to content

Not possible to check the failure of cd? #247

@jakub-g

Description

@jakub-g

Platform: Windows 7, node 0.12.7

It seems that there's no easy way to check for error of shelljs' cd. When trying to cd to non-existent folder:

  • it doesn't throw
  • the return value of the command is null (it seems it's undefined for successful cd, but the distiction is accidental I guess, nothing to rely on reliably)
  • it only displays an error to stdout (not stderr) and that's it.

It seems that I could check if cd succeeded, by getting process.cwd() and comparing it to desired location (and using path.normalize() to account for relative paths, Windows paths scheme etc), but it seems verbose.
Probably it would make sense to write your own wrapper which does shelljs.cd, verifies, and does something (abort script?) when it failed.

What is your preferred way of checking for errors of cd?
Do you think it would be useful to do some change in shelljs to better handle this?

This is my script:

#!/usr/bin/env node

var path = require('path');

try {
  var desiredTarget = 'd:/gh/doesNotExist';
  // var desiredTarget = 'd:/gh/test-shelljs';
  var out = require('shelljs').cd(desiredTarget);

  console.log("OUT:                  " + out);
  console.log("CWD:                  " + path.resolve(process.cwd()));
  console.log("path.resolve(target): " + path.resolve(desiredTarget));
} catch (e) {
  console.log("ERROR" + e);
}

and tests:

    me@mymachine /d/gh/test-shelljs   # desiredTarget = 'd:/gh/test-shelljs';
    $ ./test.js
    OUT:                  undefined
    CWD:                  d:\gh\test-shelljs
    path.resolve(target): d:\gh\test-shelljs

    me@mymachine /d/gh/test-shelljs   # desiredTarget = 'd:/gh/doesNotExist';
    $ ./test.js
    cd: no such file or directory: d:/gh/doesNotExist

    OUT:                  null
    CWD:                  d:\gh\test-shelljs
    path.resolve(target): d:\gh\doesNotExist

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions