npm.io
2.0.2 • Published 11 months ago

move-unused-path

Licence
MIT
Version
2.0.2
Deps
1
Size
4 kB
Vulns
0
Weekly
0
Stars
2

Move Unused Path

Reliably move to an unused path.

It is basically a wrapper around get-unused-path which doesn't just return an unused path, but also moves to it.

Install

npm install move-unused-path

Usage

import fs from 'fs';
import moveUnusedPath from 'move-unused-path';

// Let's move a file to an unused path

const sourceFilePath = '/x/y/z/bar.txt';

const result = await moveUnusedPath ( sourceFilePath, {
  fileName: 'foo.txt',
  folderPath: '/x/y/z',
  // incrementer: ( name, ext, attempt ) => attempt > 1 ? `${name}-${attempt}${ext}` : `${name}${ext}`,
  // maxTries: 1_000
});

result.folderPath; // => '/x/y/z'
result.filePath; // => '/x/y/z/foo (3).txt'
result.fileName; // => 'foo (3).txt'

// Let's check that the file got actually moved

console.log ( fs.existsSync ( sourceFilePath ) ); // => false
console.log ( fs.existsSync ( result.filePath ) ); // => true

License

MIT Fabio Spampinato

Keywords