First, front-end automation-project construction
We usually write code, like to build a project, and then inside is CSS, JS, images files, and index.html, and node can be automated to build a good project, as follows:
/** * Created by Yi on 2015/9/20.*/varProjectdata = { 'name':'Project', 'FileData' : [ { 'name':'CSS', 'type':'dir' }, { 'name':'JS', 'type':'dir' }, { 'name':'Images', 'type':'dir' }, { 'name':'index.html', 'type':'file', 'content':'', } ]};varFS = require ('FS');if(projectdata.name) {fs.mkdirsync (projectdata.name); varFileData =Projectdata.filedata; if(FileData &&Filedata.foreach) {Filedata.foreach (function (f) {F.path= Projectdata.name +'/'+F.name; F.content= F.content | |"'; Switch(f.type) { Case 'dir': Fs.mkdirsync (F.path); Break; Case 'file': Fs.writefilesync (F.path, f.content); Break; default : Break; } }); }}
Second, the front-end automation-monitoring file changes
Build a source folder in the auto-generated project folder above. The following code is to listen for any changes in the source folder, whether you are creating a new file or adding content to a new file. It will be merged into the js/index.js.
varFS = require ('FS');varFiledir ='./project/source'; Fs.watch (Filedir, function (EV, file) {//console.log (ev + '/' + file);//There is no need to determine if file has content//as soon as one file changes, we need to read all the files in this folder and mergeFs.readdir (Filedir, function (err, dataList) {vararr = []; Console.log (dataList); Datalist.foreach (function (f) {if(f) {varinfo = Fs.statsync (Filedir +'/'+f); //Console.log (info); if(Info.mode = =33206) {Arr.push (Filedir+'/'+f); } } }); //Console.log (arr); //reads the contents of the file in the array and merges varContent ="'; Arr.foreach (function (f) {varc =Fs.readfilesync (f); //Console.log (c);content+ = c.tostring () +'\ n'; }); Console.log (content); Fs.writefile ('./project/js/index.js', content); });})
Node front-end automation