https://github.com/gobwas/safely
Tool, that creates filenames for safe rewriting of target file
https://github.com/gobwas/safely
Last synced: about 1 month ago
JSON representation
Tool, that creates filenames for safe rewriting of target file
- Host: GitHub
- URL: https://github.com/gobwas/safely
- Owner: gobwas
- Created: 2014-08-14T08:58:20.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-08-14T10:02:25.000Z (over 10 years ago)
- Last Synced: 2025-02-25T01:43:11.962Z (2 months ago)
- Language: JavaScript
- Size: 129 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Safely
> Tool, that creates file names for safe rewriting of target file
## Getting started
Install with [npm](https://npmjs.org/package/safely)
```
npm install --save safely
```## Example
```js
safely("path/to/my/file.ext", function(err, filepath) {
// if given file is exists,
// filepath will be path/to/my/file(1).ext
});
```## API
### safely(filepath, [options], callback)
#### filepath
Type: `String`
Full path to the target file.
This is needed to be able to compare the current existing file (if any) with the destination file.#### options
Type: `Object`
Then options object.
Property | Necessary | Type | Plugin default value
-------------|-----------|------------|---------------------
[format] | no | `String` | `"%s(%d)%s"`
[version] | no | `Function` | `null`#### options.format
Type: `String`
Default value: `"%s(%d)%s"`The sprintf format of generated file name. Always have three placeholders - name, version, extension. Be careful with it, cause if you will do not use `%s` pattern inside it, your plugin can fall in infinite loop.
#### options.version
Type: `Function`
Return type: `Function`Currying generator of file version. Return initialized generator, that will be called in each time, when file is already exists. Initialized generator takes one argument, that is callback, that resolved in standard node way - `callback(err, version)`, where version is a `String`:
```jssafely("path/to/my/file", {
version: function() {
return function(done) {
getSomeAsyncStuffWith(file)
.then(function(result) {
done(null, result);
})
.catch(done);
}
}
}, done);```