npm

Join us for the upcoming webinar, "Enterprise JavaScript done right: the tools you love, the security you need."Sign up here »

base-path-converter

1.0.1 • Public • Published

Base Directory Path Converter

This package converts file paths to starting from a desired "base" directory.

For example:

If the sourcePath is parentDirectory/childDirectory/sourceDirectory

and the provided filePath is parentDirectory/childDirectory/sourceDirectory/example/test.file

then the returned filepath will be sourceDirectory/example/test.file

Installation

npm install --save base-path-converter

Usage

Params
  • sourcePath - The path for the original "base" directory being read from
  • filePath - A filepath that for a file that was read from that source directory

Response

A trimmed version of the filepath that starts from the "base" directory

Example Code

Here's an example of the package being utilized to prepare a form-data object to be send to an API.

In this example, we want to overwrite the filepath we're sending for each file, as we don't want the API receiving the file to care about the "base" folder's parents.

const recursive = require('recursive-fs');
const FormData = require('form-data');
const basePathConverter = require('base-path-converter');
 
const src = './../builds/React-Builds/myBuild/'
recursive.readdirr(src, function (err, dirs, files) {
    const body = new FormData();
    files.forEach((file) => {
        const filepath = path.normalize(file);
        body.append(`file`, fs.createReadStream(file), {
            filepath: basePathConverter(src, file)
        })
    });
});

install

npm i base-path-converter

Downloadsweekly downloads

128

version

1.0.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability