Skip to content

Commit

Permalink
Merge a5d228b into afc3a56
Browse files Browse the repository at this point in the history
  • Loading branch information
aalykiot committed Feb 24, 2020
2 parents afc3a56 + a5d228b commit 84bc8e0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
6 changes: 4 additions & 2 deletions lib/helpers.js
Expand Up @@ -21,8 +21,10 @@
const jsyaml = require('js-yaml');
const fs = require('fs');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const { promisify } = require('util');

const rmrf = require('./utils/rmrf');

const readdir = promisify(fs.readdir);

// Take yaml as a string that has already been loaded from a file or something.
Expand Down Expand Up @@ -50,7 +52,7 @@ function createDir (directoryToCreate) {
}

function cleanUp (directoryToClean) {
return promisify(rimraf)(directoryToClean);
return rmrf(directoryToClean);
}

function listFiles (dir) {
Expand Down
69 changes: 69 additions & 0 deletions lib/utils/rmrf.js
@@ -0,0 +1,69 @@
/*
*
* Copyright 2016-2017 Red Hat, Inc, and individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

'use strict';

const fs = require('fs');
const path = require('path');
const { promisify } = require('util');

const lstat = promisify(fs.lstat);
const readdir = promisify(fs.readdir);
const unlink = promisify(fs.unlink);
const rmdir = promisify(fs.rmdir);

const MAX_BUSY_TRIES = 20;

const rmrf = async (location, busyTries = 0) => {
try {
const stats = await lstat(location);

if (stats.isFile()) {
return unlink(location);
}

const files = await readdir(location);

await Promise.all(files.map(file => rmrf(path.join(location, file))));

rmdir(location);
} catch (err) {
if (err.code === 'ENOENT') {
// nothing to do here, return gracefully
return;
}

if (
(err.code === 'EBUSY' || err.code === 'EPERM') &&
busyTries < MAX_BUSY_TRIES
) {
// if file busy or perm errors retry a couple of times
await new Promise((resolve, reject) => {
setTimeout(() => {
rmrf(location, ++busyTries)
.then(resolve)
.catch(reject);
}, busyTries * 100);
});
}

throw err;
}
};

module.exports = rmrf;
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -57,7 +57,6 @@
"lodash": "^4.17.4",
"mkdirp": "^1.0.3",
"openshift-rest-client": "~4.0.0",
"rimraf": "^3.0.0",
"tar": "~6.0.1",
"yargs": "^15.0.2"
},
Expand Down
8 changes: 2 additions & 6 deletions test/helpers-test.js
Expand Up @@ -46,9 +46,7 @@ test('test createDir function - fail', (t) => {

test('test cleanup function - success', (t) => {
const helpers = proxyquire('../lib/helpers', {
rimraf: (dir, cb) => {
return cb();
}
'./utils/rmrf': () => Promise.resolve()
});

t.equal(typeof helpers.cleanUp, 'function', 'this module exports a function');
Expand All @@ -63,9 +61,7 @@ test('test cleanup function - success', (t) => {

test('test cleanUp function - fail', (t) => {
const helpers = proxyquire('../lib/helpers', {
rimraf: (dir, cb) => {
return cb(new Error('Error: error cleaning up'));
}
'./utils/rmrf': () => Promise.reject(new Error('Error: error cleaning up'))
});

helpers.cleanUp().catch((err) => {
Expand Down

0 comments on commit 84bc8e0

Please sign in to comment.