Gulp:程序的异常行为

时间:2018-12-14 22:23:55

标签: node.js gulp fs

我是Gulp的新手,我对gulp有问题,这是我想做的几点

  • 我要查找扩展名为 .storyboard 的文件 (已完成

  • 我想在某个文件的内容是 更改

  • 我想观看该文件以及何时更改其中的内容 该文件

  • 我想通过删除所有其他内容来重写其内容 已经在文件中。

当我使用 .storyboard 扩展名对文件进行更改时,它只会继续显示已完成的消息,文件已保存

这是我的代码:

//fs to read and write files while path is for iterating directories
fs = require('fs'),
    path = require('path')
//DomParser to Parse Xml 
var DOMParser = new (require('xmldom')).DOMParser({ normalizeTags: { default: false } });

//Gulp for detecting changes
var gulp = require('gulp')

var mainStoryBoardFile;

function crawl(dir) {
    // console.log('[+]', dir);
    var files = fs.readdirSync(dir);
    for (var file in files) {

        var next = path.join(dir, files[file]);
        //iterate through files to check whether next is a file or direcory
        if (fs.lstatSync(next).isDirectory()) {
            //if its a directory dive into it
            crawl(next);
        } else if (next.indexOf('.storyboard') >= 0) {
            //if its a file just check it whether it is a .storyboard file or not
            mainStoryBoardFile = next;
            mainStoryBoardFile = mainStoryBoardFile.replace(/\\/g, "/");
        };
    }
}
//calling function
crawl(__dirname);

var newFilePath = './data.xml'
var document;
var dataFound;
//What to do
gulp.task('read', function (done) {
    dataFound = fs.readFileSync(mainStoryBoardFile, "utf-8");
    document = DOMParser.parseFromString(
        dataFound.toString()
    );
    done();
});

gulp.task('write', function (done) {
    fs.writeFile(mainStoryBoardFile, '', function () { console.log('done') })
    fs.writeFile(mainStoryBoardFile, document, (err) => {
        if (err) throw err;
        console.log('The file has been saved!');
    });

    done();
});
gulp.task('watch', function (done) {
    gulp.watch(mainStoryBoardFile, gulp.series('read', 'write'));

});

1 个答案:

答案 0 :(得分:1)

这是解决此问题的解决方案,您可以观看单个文件的更改,并且只要更改文件,还可以执行某种功能。在xml情况下,您可以观看文件,当文件更改时,可以添加新的属性或属性,也可以在xml文件中创建新元素。

//Dependencies
//fs to read and write files while path is for iterating directories
var fs = require('fs'),
    path = require('path'), 
    DOMParser = new (require('xmldom')).DOMParser({ normalizeTags: { default: false } }),
    gulp = require('gulp'),
    arrayOfControls = require('./object.json'),
    RandExp = require('randexp');


console.log("GulpService has been Started\n");

function crawl(dir) {
    var files = fs.readdirSync(dir);
    for (var file in files) {

        var next = path.join(dir, files[file]);
        //iterate through files to check whether next is a file or direcory
        if (fs.lstatSync(next).isDirectory()) {
            //if its a directory dive into it
            crawl(next);
        } else if (next.indexOf('.storyboard') >= 0) {
            //if its a file just check it whether it is a .storyboard file or not
            mainStoryBoardFile = next;
            mainStoryBoardFile = mainStoryBoardFile.replace(/\\/g, "/");
        }
    }
}
//calling function
crawl(__dirname);
var mainStoryBoardFile;
var document, dataFound;


function readWrite() {
    crawl(__dirname);
    dataFound = fs.readFileSync(mainStoryBoardFile, "utf-8");
    document = DOMParser.parseFromString(
        dataFound.toString()
    );
    fs.writeFileSync(mainStoryBoardFile, '', function () {
        console.log('done')
    });
    fs.writeFileSync(mainStoryBoardFile, document, (err) => {
        if (err) throw err;
        console.log('The file has been saved!');
    });
}
var watcher = gulp.watch(mainStoryBoardFile);
watcher.on('change', function (path, stats) {
    readWrite();
    console.log('File ' + path + ' was changed');
    watcher.unwatch(mainStoryBoardFile);
    watcher.add(mainStoryBoardFile);
});