Leave the time, need to write the project copy away, but each project has node_modules, this file is very large, of course, to delete and take away, would like to write shell script, but not every development has a Mac, so with node write, online find some code and then through their own improvement
There are two ways to delete a node is to delete the file Unlinksync (sync means this is synchronous operation) One is to delete the directory Rmdirsync, but the deletion directory is required, must be empty directory to delete, so we write a delete directory function
1 functionDeleteFolder (path) {2 varFiles = [];3 if(Fs.existssync (path)) {//See if this path exists4Files =Fs.readdirsync (path); Reading the contents of a directory does not contain '. ' An array of file names for '. ' (which would actually be included)5Files.foreach (function(file,index) {6 varCurpath = path + "/" +file;7 //originally on the Internet is Statsync I changed to Lstatsync8 if(Fs.lstatsync (Curpath). Isdirectory ()) {//Determine if it is a folder9 DeleteFolder (curpath);Ten}Else { One Fs.unlinksync (curpath); A } - }); - Fs.rmdirsync (path); the } -}
Why change to Lstatsync because I found some files (suspected may be yarn installed, as if NPM installed without this problem) when using Fs.statsync error, return
But when I use Fs.statsync, it's not, of course, to check the documentation, but the documentation explains that this is a problem. (Fstatsync is not used, because the entry is a file descriptor, my needs is to pass the file path, so I need to be stat and lstat)
And then we found the difference between stat and lstat in C. When a file is a symbolic link, Lstat returns information about the symbolic link itself, and stat returns information about the file that the link points to. (It seems a bit dizzy, so remember, lstat than stat a l, so it is capable of processing symbolic link files, so when the symbolic link file is encountered, Lstat of course will not let go. and the stat system call does not have this ability, it can only open one eye to the symbolic link file, directly to deal with the link refers to the file.
I opened the stat error "file" is really a symbolic link, because stat can not handle the symbolic link, and directly to find the link refers to the file, but my needs just link this symbol to delete the line, do not need to find the file he refers to, of course, he this error, I guess it was the status of a file that could have been returned, but the result returned a link, so it was not a file or a folder error, of course, this is the C language, and the bottom of node is C + +, The description of this stat and Lastat in the Geek's college document is similar to the C language, stating that the difference in C can also be explained in node.
Can delete the folder after the next of course is to find the target if you find the target file will be deleted to continue to find the recursive
The complete code is
1Const FS = require (' FS ');2 3Const DELTARGETDIR = ' Node_modules '4 5Const ENTERTARGETDIR = ' Total directory '6 7 Deletetargetfolder (Entertargetdir, Deltargetdir)8 9 functiondeletetargetfolder (path, target) {Ten varFiles = [] One if(Fs.existssync (path)) { AFiles =fs.readdirsync (path) -Files.foreach (function(file,index) { - varCurpath = path + "/" +file; the if(Fs.statsync (Curpath). Isdirectory ()) { - if(File = =target) { - DeleteFolder (Curpath) -}Else { + Deletetargetfolder (Curpath, target) - } + } A }) at } - } - - functionDeleteFolder (path) { - varFiles = []; - if(Fs.existssync (path)) { inFiles =Fs.readdirsync (path); -Files.foreach (function(file,index) { to varCurpath = path + "/" +file; + //originally on the Internet is Statsync I changed to Lstatsync - if(Fs.lstatsync (Curpath). Isdirectory ()) { the DeleteFolder (curpath); *}Else { $ Fs.unlinksync (curpath);Panax Notoginseng } - }); the Fs.rmdirsync (path); + } A}
Write a script that deletes node_modules with Nodejs