Monday, 29 May 2017

Get chain of parents with fs

I'd like to get an array of parents of current directory. Currently I'm doing this

function parseStructure(drive) {
    let structure = [];
    while (drive.length > 3) {
        structure.unshift(path.basename(drive));
        drive = path.dirname(drive);
    }
    drive = drive.substr(0, 1);
    structure.unshift(drive);

    return structure;
}

because path.dirname('C:/') returns C:/. This will probably work on Windows but most probably won't on Linux. How can I check if current directory is root?

This route C:\Users\user\SO should return array of ['C', 'Users', 'user', 'SO'] and this route /usr/user/SO should return array of ['usr', 'user', 'SO'].



via Dread Boy

No comments:

Post a Comment