PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/front/platforms/android/cordova/node_modules/shelljs/src/rm.js

https://gitlab.com/boxnia/NFU_MOVIL
JavaScript | 163 lines | 101 code | 23 blank | 39 comment | 32 complexity | 73c043f494d49638e4d2b8a2b4f8961a MD5 | raw file
  1. var common = require('./common');
  2. var fs = require('fs');
  3. // Recursively removes 'dir'
  4. // Adapted from https://github.com/ryanmcgrath/wrench-js
  5. //
  6. // Copyright (c) 2010 Ryan McGrath
  7. // Copyright (c) 2012 Artur Adib
  8. //
  9. // Licensed under the MIT License
  10. // http://www.opensource.org/licenses/mit-license.php
  11. function rmdirSyncRecursive(dir, force) {
  12. var files;
  13. files = fs.readdirSync(dir);
  14. // Loop through and delete everything in the sub-tree after checking it
  15. for(var i = 0; i < files.length; i++) {
  16. var file = dir + "/" + files[i],
  17. currFile = fs.lstatSync(file);
  18. if(currFile.isDirectory()) { // Recursive function back to the beginning
  19. rmdirSyncRecursive(file, force);
  20. }
  21. else if(currFile.isSymbolicLink()) { // Unlink symlinks
  22. if (force || isWriteable(file)) {
  23. try {
  24. common.unlinkSync(file);
  25. } catch (e) {
  26. common.error('could not remove file (code '+e.code+'): ' + file, true);
  27. }
  28. }
  29. }
  30. else // Assume it's a file - perhaps a try/catch belongs here?
  31. if (force || isWriteable(file)) {
  32. try {
  33. common.unlinkSync(file);
  34. } catch (e) {
  35. common.error('could not remove file (code '+e.code+'): ' + file, true);
  36. }
  37. }
  38. }
  39. // Now that we know everything in the sub-tree has been deleted, we can delete the main directory.
  40. // Huzzah for the shopkeep.
  41. var result;
  42. try {
  43. // Retry on windows, sometimes it takes a little time before all the files in the directory are gone
  44. var start = Date.now();
  45. while (true) {
  46. try {
  47. result = fs.rmdirSync(dir);
  48. if (fs.existsSync(dir)) throw { code: "EAGAIN" }
  49. break;
  50. } catch(er) {
  51. // In addition to error codes, also check if the directory still exists and loop again if true
  52. if (process.platform === "win32" && (er.code === "ENOTEMPTY" || er.code === "EBUSY" || er.code === "EPERM" || er.code === "EAGAIN")) {
  53. if (Date.now() - start > 1000) throw er;
  54. } else if (er.code === "ENOENT") {
  55. // Directory did not exist, deletion was successful
  56. break;
  57. } else {
  58. throw er;
  59. }
  60. }
  61. }
  62. } catch(e) {
  63. common.error('could not remove directory (code '+e.code+'): ' + dir, true);
  64. }
  65. return result;
  66. } // rmdirSyncRecursive
  67. // Hack to determine if file has write permissions for current user
  68. // Avoids having to check user, group, etc, but it's probably slow
  69. function isWriteable(file) {
  70. var writePermission = true;
  71. try {
  72. var __fd = fs.openSync(file, 'a');
  73. fs.closeSync(__fd);
  74. } catch(e) {
  75. writePermission = false;
  76. }
  77. return writePermission;
  78. }
  79. //@
  80. //@ ### rm([options ,] file [, file ...])
  81. //@ ### rm([options ,] file_array)
  82. //@ Available options:
  83. //@
  84. //@ + `-f`: force
  85. //@ + `-r, -R`: recursive
  86. //@
  87. //@ Examples:
  88. //@
  89. //@ ```javascript
  90. //@ rm('-rf', '/tmp/*');
  91. //@ rm('some_file.txt', 'another_file.txt');
  92. //@ rm(['some_file.txt', 'another_file.txt']); // same as above
  93. //@ ```
  94. //@
  95. //@ Removes files. The wildcard `*` is accepted.
  96. function _rm(options, files) {
  97. options = common.parseOptions(options, {
  98. 'f': 'force',
  99. 'r': 'recursive',
  100. 'R': 'recursive'
  101. });
  102. if (!files)
  103. common.error('no paths given');
  104. if (typeof files === 'string')
  105. files = [].slice.call(arguments, 1);
  106. // if it's array leave it as it is
  107. files = common.expand(files);
  108. files.forEach(function(file) {
  109. if (!fs.existsSync(file)) {
  110. // Path does not exist, no force flag given
  111. if (!options.force)
  112. common.error('no such file or directory: '+file, true);
  113. return; // skip file
  114. }
  115. // If here, path exists
  116. var stats = fs.lstatSync(file);
  117. if (stats.isFile() || stats.isSymbolicLink()) {
  118. // Do not check for file writing permissions
  119. if (options.force) {
  120. common.unlinkSync(file);
  121. return;
  122. }
  123. if (isWriteable(file))
  124. common.unlinkSync(file);
  125. else
  126. common.error('permission denied: '+file, true);
  127. return;
  128. } // simple file
  129. // Path is an existing directory, but no -r flag given
  130. if (stats.isDirectory() && !options.recursive) {
  131. common.error('path is a directory', true);
  132. return; // skip path
  133. }
  134. // Recursively remove existing directory
  135. if (stats.isDirectory() && options.recursive) {
  136. rmdirSyncRecursive(file, options.force);
  137. }
  138. }); // forEach(file)
  139. } // rm
  140. module.exports = _rm;