Nodejs: Traverse Folder file statistics file size

Source: Internet
Author: User
Tags readfile

According to the idea of http://blog.csdn.net/hero82748274/article/details/45700465 here, a package is made of Read-write files:

Webpack can use the Assets-webpack-plugin plugin to form a JSON map of all packaged files when packaging, but since the project requires this generated JSON to not meet my needs, we currently need to generate the JSON file in the Form:

{
"jsfile": {"mainsite": "mainsite.js", "size": "296.28/kb"
}, "cssfile": {"mainsite": "mainsite.css", "size": "32.76/kb"}}

  

therefore, the Nodejs FS module realizes the function of this read-write file, as Follows:

var fs = require (' fs '), var path = require (' path '), var basepath = path.join (__dirname, ' Resources ');//traverse folder, Get file information in all folders function gefilelist (folderpath,filename) {this.folderpath=folderpath;//folder path this.filename=filename    ;    This.fileslist = [];        Traverse Read file this.readfile=function (path) {var fileslist=this.fileslist; var files = Fs.readdirsync (path),//required to synchronize read Files.foreach (function (file) {var states = Fs.statsync (path            + '/' +file);            If (states.isdirectory ()) {this.readfile (path+ '/' +file,fileslist);                } else {//create an object to hold information var obj = new Object (); Obj.size = States.size;//file size, in bytes obj.name = file;//filename Obj.path = path+ '/' +file;            File absolute path This.filesList.push (obj);    }}.bind (this)); }//write file Utf-8 format this.writefile=function (data) {fs.writefile (this.filename,data, ' Utf-8 ', function () {console.log ("file generation succeeded");    });        } this.formathandler=function () {var fileslist=this.fileslist;        var strjson={"jsfile": {}, "cssfile": {}};                For (var i=0;i<fileslist.length;i++) {var item = fileslist[i], thisname=item.name,            namenosuffix; If (/\.js$/.test (thisname)) {//determine whether the JS file is Namenosuffix=thisname.split ('. ')                [0];                strjson["jsfile"][namenosuffix]=thisname;            strjson["jsfile" ["size"]= (item.size/1024). toFixed (2) + "/kb"; } else if (/\.css$/.test (thisname)) {//determines whether the CSS file is namenosuffix=thisname.split ('. ')                ) [0];                strjson["cssfile"][namenosuffix]=thisname;            strjson["cssfile" ["size"]= (item.size/1024). toFixed (2) + "/kb";        }} var strjsonobj=json.stringify (strjson); This.writefile (strjsonobj);        } this.init=function () {var that=this;        Console.log (' test01 '); Determine if the file path exists fs.exists when packing (this.folderpath, function (exists) {if (exists) {that.readfile              (that.folderpath);            That.formathandler ();    }        }); }}module.exports=gefilelist;

Because I have multiple projects to manage together, I need to generate JSON files for multiple projects at once, so every JSON file generated will have to instantiate a function, such as:

Generate JSON map//ask Jsonvar askfilelist=new gefilelist (outputpath+ "/ask", outputpath+ '/json-ask.json '); Askfilelist.init ();

Generate JSON map//web Jsonvar askfilelist=new gefilelist (outputpath+ "/web", outputpath+ '/json-web.json '); Askfilelist.init ();

  

Note: use Webpack plug-in assets-webpack-plugin to generate json, reference address: Https://www.npmjs.com/package/assets-webpack-plugin

Nodejs FS Module's api:http://javascript.ruanyifeng.com/nodejs/fs.html

  

Nodejs: Traverse Folder file statistics file size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.