diff --git a/ava-test/rm.js b/ava-test/rm.js index f56d305..a3f6b10 100644 --- a/ava-test/rm.js +++ b/ava-test/rm.js @@ -253,7 +253,7 @@ test( ); test('remove symbolic link to a dir', t => { - const result = shell.rm('-f', `${t.context.tmp}/rm/link_to_a_dir`); + const result = shell.rm(`${t.context.tmp}/rm/link_to_a_dir`); t.falsy(shell.error()); t.is(result.code, 0); t.falsy(fs.existsSync(`${t.context.tmp}/rm/link_to_a_dir`)); diff --git a/src/rm.js b/src/rm.js index c039a3b..803adbc 100644 --- a/src/rm.js +++ b/src/rm.js @@ -123,31 +123,22 @@ function _rm(options, files) { } // If here, path exists - if (stats.isFile() || stats.isSymbolicLink()) { - // Do not check for file writing permissions - if (options.force) { - common.unlinkSync(file); - return; - } - - if (isWriteable(file)) { + if (stats.isFile()) { + if (options.force || isWriteable(file)) { + // -f was passed, or file is writable, so it can be removed common.unlinkSync(file); } else { common.error('permission denied: ' + file, { continue: true }); } - - return; - } // simple file - - // Path is an existing directory, but no -r flag given - if (stats.isDirectory() && !options.recursive) { - common.error('path is a directory', { continue: true }); - return; // skip path - } - - // Recursively remove existing directory - if (stats.isDirectory() && options.recursive) { - rmdirSyncRecursive(file, options.force); + } else if (stats.isDirectory()) { + if (options.recursive) { + // -r was passed, so directory can be removed + rmdirSyncRecursive(file, options.force); + } else { + common.error('path is a directory', { continue: true }); + } + } else if (stats.isSymbolicLink()) { + common.unlinkSync(file); } }); // forEach(file) return '';