Skip to content

Commit 722539b

Browse files
committed
init
0 parents  commit 722539b

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
.DS_Store*
3+
*.log
4+
*.gz
5+
6+
node_modules
7+
coverage
8+
build

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_js:
2+
- "0.10"
3+
- "0.11"
4+
language: node_js
5+
script: "npm run test-travis"
6+
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014 Jonathan Ong [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# cp
3+
4+
[![NPM version][npm-image]][npm-url]
5+
[![Latest tag][github-tag]][github-url]
6+
[![Build status][travis-image]][travis-url]
7+
[![Test coverage][coveralls-image]][coveralls-url]
8+
[![Dependency Status][david-image]][david-url]
9+
[![License][license-image]][license-url]
10+
[![Downloads][downloads-image]][downloads-url]
11+
[![Gittip][gittip-image]][gittip-url]
12+
13+
Copy files with error handling and promise support.
14+
Specifically, if the process crashes while you're copying a file,
15+
you won't end up with half-written copied files.
16+
It should also avoid race conditions, meaning
17+
you should be able to `cp` the same files at the same time and it'll be fine.
18+
19+
## API
20+
21+
```js
22+
var cp = require('fs-cp')
23+
24+
cp('package.json', 'package2.json').then(function () {
25+
26+
})
27+
```
28+
29+
[npm-image]: https://img.shields.io/npm/v/fs-cp.svg?style=flat-square
30+
[npm-url]: https://npmjs.org/package/fs-cp
31+
[github-tag]: http://img.shields.io/github/tag/fs-utils/cp.svg?style=flat-square
32+
[github-url]: https://github.com/fs-utils/cp/tags
33+
[travis-image]: https://img.shields.io/travis/fs-utils/cp.svg?style=flat-square
34+
[travis-url]: https://travis-ci.org/fs-utils/cp
35+
[coveralls-image]: https://img.shields.io/coveralls/fs-utils/cp.svg?style=flat-square
36+
[coveralls-url]: https://coveralls.io/r/fs-utils/cp?branch=master
37+
[david-image]: http://img.shields.io/david/fs-utils/cp.svg?style=flat-square
38+
[david-url]: https://david-dm.org/fs-utils/cp
39+
[license-image]: http://img.shields.io/npm/l/cp.svg?style=flat-square
40+
[license-url]: LICENSE.md
41+
[downloads-image]: http://img.shields.io/npm/dm/fs-cp.svg?style=flat-square
42+
[downloads-url]: https://npmjs.org/package/fs-cp
43+
[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat-square
44+
[gittip-url]: https://www.gittip.com/jonathanong/

index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
var fs = require('mz/fs')
3+
var path = require('path')
4+
var Promise = require('native-or-bluebird')
5+
6+
module.exports = function (src, dest) {
7+
src = path.resolve(src)
8+
dest = path.resolve(dest)
9+
// where the file will be temporarily copied to
10+
// it'll be saved to the same folder, then renamed.
11+
var tmp = dest + affix()
12+
13+
return new Promise(function (resolve, reject) {
14+
var read = fs.createReadStream(src)
15+
.on('error', onerror)
16+
var write = fs.createWriteStream(tmp)
17+
.on('error', onerror)
18+
.on('close', onclose)
19+
20+
read.pipe(write)
21+
22+
function onerror(err) {
23+
fs.unlink(tmp)
24+
cleanup()
25+
reject(err)
26+
}
27+
28+
function onclose() {
29+
cleanup()
30+
resolve()
31+
}
32+
33+
function cleanup() {
34+
read.removeListener('error', onerror)
35+
write.removeListener('error', onerror)
36+
write.removeListener('close', onclose)
37+
}
38+
}).then(function () {
39+
return fs.rename(tmp, dest)
40+
})
41+
}
42+
43+
// an affix to attach to the temporary file
44+
// to avoid race conditions and
45+
function affix() {
46+
return '.' + random() + '.tmp'
47+
}
48+
49+
function random() {
50+
return Math.random().toString(36).slice(2)
51+
}

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "fs-cp",
3+
"description": "",
4+
"version": "0.0.0",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]",
8+
"url": "http://jongleberry.com",
9+
"twitter": "https://twitter.com/jongleberry"
10+
},
11+
"license": "MIT",
12+
"repository": "fs-utils/cp",
13+
"dependencies": {
14+
"mz": "1",
15+
"native-or-bluebird": "1"
16+
},
17+
"devDependencies": {
18+
"mkdirp": "0",
19+
"rimraf": "2",
20+
"istanbul": "0",
21+
"mocha": "1"
22+
},
23+
"scripts": {
24+
"test": "mocha --reporter spec",
25+
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot",
26+
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot"
27+
},
28+
"keywords": [
29+
"fs",
30+
"cp",
31+
"copy"
32+
],
33+
"files": [
34+
"index.js"
35+
]
36+
}

test/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
var fs = require('mz/fs')
3+
var path = require('path')
4+
5+
var cp = require('..')
6+
7+
var build = path.join(__dirname, '..', 'build')
8+
9+
require('rimraf').sync(build)
10+
require('mkdirp').sync(build)
11+
12+
describe('fs-cp', function () {
13+
it('should copy', function () {
14+
var out = path.join(build, 'test.js')
15+
return cp(__filename, out).then(function () {
16+
return fs.stat(out)
17+
})
18+
})
19+
})

0 commit comments

Comments
 (0)