gulp liệt kê thư mục mỗi khi có thay đổi file *.js

Nếu có thay đổi thì liệt kê thư mục hiện thời ls command.

const spawn = require('child_process').spawn;
const gutil = require('gulp-util');
const gulp = require('gulp');
gulp.task('default', function(){

    gulp.watch('*.js', function(e) {  //Quan sát các file .js        

        // Nếu có thay đổi thì dir thư mục hiện thời cwd: current working directory
        const child = spawn("ls", ["-lA"], {cwd: process.cwd()});
        let stdout = '', stderr = '';

        child.stdout.setEncoding('utf8');

        child.stdout.on('data', function (data) {
            stdout += data;
            gutil.log(data);
        });

        child.stderr.setEncoding('utf8');
        child.stderr.on('data', function (data) {
            stderr += data;
            gutil.log(gutil.colors.red(data));
            gutil.beep();
        });

        child.on('close', function(code) {
            gutil.log("Done with exit code", code);
            gutil.log("You access complete stdout and stderr from here"); // stdout, stderr
        });


    });
});

Sử dụng gulp để start Node.js

Bình thường chúng ta sẽ gõ lệnh npm start Nếu trong thư mục hiện thời có package.json và định nghĩa start script thì nó sẽ chạy. Nếu muốn chạy npm start ở một thư mục khác chúng ta thêm option --prefix npm start --prefix ../app1/ ở đây sẽ khởi động ở thư mục app1 ra ngoài 1 cấp.

Đây là toàn bộ lệnh chạy npm start ở một thư mục bất kỳ trong gulp

const spawn = require('child_process').spawn;
const gutil = require('gulp-util');
const gulp = require('gulp');
gulp.task('default', function(){   
    const child = spawn("npm", ["start", "--prefix", "../app1/"], {cwd: process.cwd()});
    let stdout = '', stderr = '';

    child.stdout.setEncoding('utf8');

    child.stdout.on('data', function (data) {
        stdout += data;
        gutil.log(data);
    });

    child.stderr.setEncoding('utf8');
    child.stderr.on('data', function (data) {
        stderr += data;
        gutil.log(gutil.colors.red(data));
        gutil.beep();
    });

    child.on('close', function(code) {
        gutil.log("Done with exit code", code);
        gutil.log("You access complete stdout and stderr from here"); // stdout, stderr
    });
});

Gulp để tự động hóa khởi động nhiều web app

Tham khảo ví dụ ở đây gulp_npm_start

results matching ""

    No results matching ""