Coder Social home page Coder Social logo

jsftp-mkdirp's Introduction

jsftp-mkdirp

Recursively create nested directories with jsftp, like mkdirp

FTP can natively create only one directory at the time.

Useful for being able to upload files to deep paths without knowing if the directories exists beforehand.

Install

$ npm install jsftp-mkdirp

Usage

import JsFtp from 'jsftp';
import JsFtpMkdirp from 'jsftp-mkdirp';

// Decorate `JSFtp` with a new method `mkdirp`
JsFtpMkdirp(JSFtp);

const ftp = new JsFtp({
	host: 'myserver.com'
});

const path = 'public_html/deploy/foo/bar';

await ftp.mkdirp(path);
console.log('Created path:', path);

API

JsFtp.mkdirp(path)

Returns a Promise.

path

Type: string

The path of the nested directories you want to create.

jsftp-mkdirp's People

Contributors

afharo avatar artskydj avatar kevva avatar prinzhorn avatar richienb avatar samverschueren avatar sindresorhus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsftp-mkdirp's Issues

mkdirp fails if the full destination directory already exists

Now I started using the new version and the behavior is still a bit odd and not exactly like mkdir -p.

If you run mkdirp on a full directory that already exists, it will throw a 550 error, because (err.code === 550 && dirs.length > 0) will be false, see https://github.com/sindresorhus/jsftp-mkdirp/blob/master/index.js#L28

I think we need multiple ftp commands here to properly map the expected mkdir -p behavior on ftp level. The algorithm should look like this in pseudocode, just to give an idea:

INPUT: path

old_pwd = ftp.raw.pwd();

components = reverse(parentDirs(path));

Promise.mapSeries(components, function(component) {
    if (ftp.cwd(component) == error[550]) {
      return ftp.raw.mkd(component);
    } else {
      return true;
    }
  }).finally(function() {
    ftp.raw.cwd(old_pwd);
  })

So we basically just try to cd into each path component, if that fails at any point... run ftp.raw.mkd on that path component without any error handling. Because if we cannot cd into an ftp directory and cannot create it, then everything is lost.

This will still do the expected thing wrt #4

TypeError: Cannot read property 'bind' of undefined

Happens with node 4, 6, 8 and 9. The tests fail in a clean checked out repo.

git clone [email protected]:sindresorhus/jsftp-mkdirp.git
cd jsftp-mkdirp/
nvm use 9
npm i
npm test

 Rejected promise returned by test. Reason:

  TypeError {
    message: 'Cannot read property \'bind\' of undefined',
  }

The line is

return pify(this.raw.mlst.bind(this.raw))(dir)
                            ^

I want through all commits in jsftp since Nov 28 but couldn't find any occurrence of mlst

mkdirp on relative directories misbehaves

It seems parents misbehaves.

console.log(parents("abc/def"));

yields:

[ '/abc/def', '/abc' ]

ok, wtf. Where did / come from?

None of the given directories exist. The ftp server is supposed to create $(pwd)/abc/def.

insufficient error handling

This hides issues. I don't see how that is correct error handling. On permissions errors, the mkdirp function will just do nothing instead of properly failing.

            .catch(err => {
                if (err.code === 550) {
                    if (dirs.length > 0) {
                        return mkdir(dirs.pop());
                    }

                    return;
                }

                err.message += ` - mkd: ${dir}`;
                throw err;
            });

Path problem on Unix based FTP

General

On Unix based FTP servers using a path that contains '//' returns error code 553 ๐Ÿ˜ฑ While on Windows based FTP servers there is no error ๐Ÿ‘.

Solution

Using the following code to change the path to work with Windows and Unix โœ…

remotePath = remotePath.replace(/\\/g, '/').replace(/\/\//g, '/');

Enjoy ๐ŸŽ‰

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.