Skip to content

Commit

Permalink
feature: add ability to handle errors while copying (coderaiser/cloud…
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 9, 2022
1 parent df4da36 commit 9c00210
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
}
}],
"extends": [
"plugin:node/recommended",
"plugin:n/recommended",
"plugin:putout/recommended"
],
"plugins": [
"putout",
"node"
"n"
]
}
8 changes: 4 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Node CI
'on':
on:
- push
- pull_request
jobs:
Expand All @@ -8,12 +8,12 @@ jobs:
strategy:
matrix:
node-version:
- 14.x
- 16.x
- 18.x
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install Redrun
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ yarn-error.log

*.swp
coverage
.idea
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Move Files [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]
# Move Files [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]

Move files with emitter and `zip` archives support. Try to `rename` files first, and only if fail copy them to destination and then remove from source.

Expand Down Expand Up @@ -75,10 +75,8 @@ MIT
[NPMIMGURL]: https://img.shields.io/npm/v/@cloudcmd/move-files.svg?style=flat
[BuildStatusURL]: https://github.com/cloudcmd/move-files/actions
[BuildStatusIMGURL]: https://github.com/cloudcmd/move-files/workflows/CI/badge.svg
[DependencyStatusIMGURL]: https://img.shields.io/david/cloudcmd/move-files.svg?style=flat
[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat
[CoverageIMGURL]: https://coveralls.io/repos/cloudcmd/move-files/badge.svg?branch=master&service=github
[NPMURL]: https://npmjs.org/package/@cloudcmd/move-files "npm"
[DependencyStatusURL]: https://david-dm.org/cloudcmd/move-files "Dependency Status"
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"
[CoverageURL]: https://coveralls.io/github/cloudcmd/move-files?branch=master
11 changes: 9 additions & 2 deletions lib/move-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ const moveFiles = wraptile((move, from, to, names) => {
move.pause = copy.pause.bind(copy);
});

const removeAll = wraptile(({move, from, names, progress}) => {
const removeAll = wraptile(({move, from, names, progress}, {errors}) => {
const doProgress = emitProgress(move, progress);

if (errors.length) {
progress.now(100);
return doProgress();
}

const options = {
force: true,
recursive: true,
Expand All @@ -78,7 +85,7 @@ const removeAll = wraptile(({move, from, names, progress}) => {
progress.now(TIME_TO_REMOVE);

Promise.all(removers)
.then(emitProgress(move, progress))
.then(doProgress)
.catch(emitError(move));
});

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "@cloudcmd/move-files",
"version": "5.0.0",
"type": "commonjs",
"description": "move files",
"main": "lib/move-files.js",
"commitType": "colon",
Expand Down
78 changes: 70 additions & 8 deletions test/move-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {stopAll, reRequire} = mockRequire;

test('move-files: no args', (t) => {
const [error] = tryCatch(moveFiles);

t.equal(error.message, 'from should be a string!', 'should throw when no from');
t.end();
});
Expand All @@ -48,11 +49,15 @@ test('move-files: error', async (t) => {
const to = '/a';
const names = ['README'];
const vol = Volume.fromJSON(FIXTURE, '/');

vol.rename = vol.rename.bind(vol);
mockRequire('fs', vol);

const moveFiles = reRequire('..');
const mv = moveFiles(from, to, names);
const [e] = await once(mv, 'error');

stopAll();
t.equal(e.code, 'ENOENT', 'should be error');
mockRequire.stop('fs');
t.end();
Expand All @@ -63,6 +68,7 @@ test('move-files: error: abort: end', async (t) => {
const to = '/a';
const names = ['README'];
const vol = Volume.fromJSON(FIXTURE, '/');

vol.rename = vol.rename.bind(vol);
mockRequire('fs', vol);

Expand All @@ -79,6 +85,8 @@ test('move-files: error: abort: end', async (t) => {

mockRequire.stop('fs');

stopAll();

t.pass('should emit end');
t.end();
});
Expand All @@ -87,12 +95,15 @@ test('move-files: rename: success', async (t) => {
const from = '/b';
const to = '/a';
const names = ['README'];
const renameFiles = async () => {};
const renameFiles = stub().resolves();

mockRequire('@cloudcmd/rename-files', renameFiles);
const moveFiles = reRequire('..');
const mv = moveFiles(from, to, names);
await once(mv, 'end');
mockRequire.stop('@cloudcmd/rename-files');
stopAll();

t.pass('should rename files');
t.end();
});
Expand All @@ -114,10 +125,61 @@ test('move-files: emit end', async (t) => {
'LICENSE',
];

const renameFiles = async () => {
throw Error('hello');
const renameFiles = stub().rejects(Error('hello'));

const remove = stub().resolves();
mockRequire('redzip', {
remove,
});

mockRequire('@cloudcmd/rename-files', renameFiles);
mockRequire('copymitter', copymitter);

const moveFiles = reRequire('..');

const mv = moveFiles(from, to, names);
await wait(TIME, stub());

const emitCP = () => {
cp.emit('progress', 50);
cp.emit('file', 'README');
cp.emit('progress', 100);
cp.emit('file', 'LICENSE');
cp.emit('end', {
errors: [],
});
};

await Promise.all([
once(mv, 'end'),
emitCP(),
]);

stopAll();

t.pass('should emit file');
t.end();
});

test('move-files: emit end: errors', async (t) => {
const TIME = 10;
const cp = new EventEmitter();

cp.continue = stub();
cp.abort = stub();
cp.pause = stub();

const copymitter = () => cp;

const from = '/b';
const to = '/a';
const names = [
'README',
'LICENSE',
];

const renameFiles = stub().rejects(Error('hello'));

const remove = stub().resolves();
mockRequire('redzip', {
remove,
Expand All @@ -136,7 +198,9 @@ test('move-files: emit end', async (t) => {
cp.emit('file', 'README');
cp.emit('progress', 100);
cp.emit('file', 'LICENSE');
cp.emit('end');
cp.emit('end', {
errors: [1],
});
};

await Promise.all([
Expand Down Expand Up @@ -167,9 +231,7 @@ test('move-files: emit progress', async (t) => {
'LICENSE',
];

const renameFiles = async () => {
throw Error('hello');
};
const renameFiles = stub().rejects(Error('hello'));

const remove = stub().resolves();

Expand Down Expand Up @@ -200,5 +262,5 @@ test('move-files: emit progress', async (t) => {
t.pass('should emit file');
t.equal(n, 50, 'should emit progress');
t.end();
});
}, {checkAssertionsCount: false});

0 comments on commit 9c00210

Please sign in to comment.