Node.js 如何在gulp版本4.0中重新编写这个gulpfile.js

Node.js 如何在gulp版本4.0中重新编写这个gulpfile.js,node.js,gulp,node-modules,clasp,Node.js,Gulp,Node Modules,Clasp,我的目标是在gulp的帮助下,通过代表我在终端上执行“clasp push”,自动推送编辑后的文件 我在gulpfile.js中编写以下内容时遇到了这个错误 const gulp = require("gulp"); const exec = require("child_process"); gulp.task('push', function () { exec("clasp push") }); gulp.task('watch', function () { gulp

我的目标是在gulp的帮助下,通过代表我在终端上执行“clasp push”,自动推送编辑后的文件

我在gulpfile.js中编写以下内容时遇到了这个错误

const gulp = require("gulp");
const exec = require("child_process");

gulp.task('push', function () {
    exec("clasp push")
});

gulp.task('watch', function () {
    gulp.watch([
        '**/* .js',
        "**/* .html"
    ], ['push'])
});

gulp.task('default', ['watch']);
错误:

Task function must be specified
    at Gulp.set [as _setTask] (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/node_modules/undertaker/lib/set-task.js:10:3)
    at Gulp.task (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/node_modules/undertaker/lib/task.js:13:8)
    at Object.<anonymous> (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/gulpfile.js:15:6)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
但我还是犯了这个错误:

assert.js:350 犯错误; ^

AssertionError [ERR_ASSERTION]: Task never defined: 
    at getFunction (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/node_modules/undertaker/lib/helpers/normalizeArgs.js:15:5)
    at map (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/node_modules/arr-map/index.js:20:14)
    at normalizeArgs (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/node_modules/undertaker/lib/helpers/normalizeArgs.js:22:10)
    at Gulp.series (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/node_modules/undertaker/lib/series.js:13:14)
    at Object.<anonymous> (/Users/micklo/Desktop/AppsScriptProjects/AppsScriptAppName/gulpfile.js:4:24)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
AssertionError[ERR_ASSERTION]:从未定义任务:
在getFunction(/Users/micklo/Desktop/AppsScriptProjects/AppsScriptName/node_modules/undertaker/lib/helpers/normalizeArgs.js:15:5)
在地图上(/Users/micklo/Desktop/AppsScriptProjects/AppsScriptProjects/node_modules/arr map/index.js:20:14)
在normalizeArgs(/Users/micklo/Desktop/AppsScriptProjects/appsscriptpaname/node_modules/undertaker/lib/helpers/normalizeArgs.js:22:10)
在Gulp.series(/Users/micklo/Desktop/AppsScriptProjects/appsscriptpaname/node_modules/undertaker/lib/series.js:13:14)
反对。(/Users/micklo/Desktop/AppsScriptProjects/appsscriptpname/gulpfile.js:4:24)
at模块编译(内部/modules/cjs/loader.js:701:30)
at Object.Module._extensions..js(internal/modules/cjs/loader.js:712:10)
at Module.load(内部/modules/cjs/loader.js:600:32)
在tryModuleLoad(内部/modules/cjs/loader.js:539:12)
at Function.Module._load(内部/modules/cjs/loader.js:531:3)
我做错了什么?

一些信息:

操作系统:Mac操作系统(莫哈韦)

Gulp版本->CLI版本:2.1.0,本地版本:4.0.0

节点:v10.15.3

扣:2.1.0


对于该特定错误的快速修复方法是,您在
推送任务中对空/不存在的任务调用了
gulp.series
gulp.series
需要与您声明的任务对应的字符串列表(例如
push
watch
default
)或任务函数

摆脱本系列中的
''
任务将解决此问题:

gulp.task('push', gulp.series(function(){
    exec('clasp push')
}));
但是,您现在还有一些其他问题:

  • 您的任何任务都没有回调。每个Gulp任务都是异步的,因此您必须使用回调或
    return
    stream/promise/event-emitter/child-process/observable(示例如下)。例如:
  • 您没有将函数或串/并行序列传递给
    gulp.watch
    ,因此它没有做任何事情。如果您试图在任何
    .js
    .html
    文件发生更改时运行
    推送
    任务,则需要将其更改为:
  • 您的glob模式在watch命令中的文件扩展名之前有一个额外的空间:
    '***.js'
    而不是
    '***.js'
  • 您可能希望
    require(“child_process”).exec
    (附加了
    .exec
    标记)允许您使用
    exec(command)
    语法执行shell命令
  • default
    任务中调用
    watch
    命令时,无需重复该命令:
  • 您可以删除大量的
    gulp.series
    调用来删除
    任务调用并简化任务。每个任务只需要一个函数,您可能应该将
    gulp.series
    gulp.parallel
    用于命名任务集 更改这些点后,代码应该如下所示:

    const gulp = require('gulp');
    const exec = require('child_process').exec;
    
    gulp.task('push', function(done){
        exec('clasp push', function (err, stdout, stderr) {
            console.log(stdout);
            console.log(stderr);
            done(err);
          });
    });
    
    gulp.task('watch', function(done) {
        gulp.watch([
            '**/*.js',
            '**/*.html'
        ], gulp.series('push'));
        done();
    });
    
    gulp.task('default', gulp.series('push', 'watch'));
    
    您可以使用gulp v4推荐的导出任务模式重写此命令:

    const { series, watch } = require('gulp');
    const { exec } = require('child_process');
    
    function push(done) {
        exec('clasp push', function (err, stdout, stderr) {
            console.log(stdout);
            console.log(stderr);
            done(err);
          });
    }
    
    function watchPush() {
        return watch(['**/*.js', '**/*.html'], push);
    }
    
    exports.push = push;
    exports.default = exports.watch = series(push, watchPush);
    
    ////////////////////////////////////////////////////////////////////////////////

        var gulp        = require('gulp');
        var browserSync = require('browser-sync').create();
        var sass        = require('gulp-sass');
    
        // Compile sass into CSS & auto-inject into browsers
        gulp.task('sass', function() {
            return gulp.src(['node_modules/bootstrap/scss/bootstrap.scss', 
            'src/scss/*.scss'])
                .pipe(sass())
                .pipe(gulp.dest("src/css"))
                .pipe(browserSync.stream());
        });
    
        // Move the javascript files into our /src/js folder
        gulp.task('js', function() {
            return gulp.src(['node_modules/bootstrap/dist/js/bootstrap.min.js', 
             'node_modules/jquery/dist/jquery.min.js', 
             'node_modules/popper.js/dist/umd/popper.min.js'])
                .pipe(gulp.dest("src/js"))
                .pipe(browserSync.stream());
        });
    
        // Static Server + watching scss/html files
        gulp.task('serve', gulp.series('sass', function() {
    
            browserSync.init({
                server: "./src"  
            });
    
            gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 
             'src/scss/*.scss'], gulp.series('sass'));
            gulp.watch("src/*.html").on('change', browserSync.reload);
        }));
    
        gulp.task('default', gulp.parallel('js','serve'));
    

    我得到了以下错误“[09:39:39]以下任务未完成:默认、监视、推送,[09:39:39]是否忘记发出异步完成信号?”可能是
    exec
    函数需要在其自己的回调函数中发出gulp任务回调信号。为了反映这一点,我修改了答案中的最后一个代码块。如果不是这样,我会用
    gulp--version
    (因为您可能有一个全局gulp@4.0.0安装和安装一个本地gulp@3.**安装)@Micklo_Nerd我还通过删除一些
    gulp.series
    调用简化了一些任务,并使用gulp v4推荐的导出任务模式添加了重写,这可能会给你带来一些好运气。如果错误仍然存在,则在您调用
    gulp push
    时,还是在您调用
    gulp
    时,该错误仍然存在?
    clusp push--watch
    ?我承认您必须在开发会话开始时执行它,然后在结束时退出它,但从理论上讲,这正是您想要做的。
    gulp.task('default', gulp.series('push', 'watch'));
    
    const gulp = require('gulp');
    const exec = require('child_process').exec;
    
    gulp.task('push', function(done){
        exec('clasp push', function (err, stdout, stderr) {
            console.log(stdout);
            console.log(stderr);
            done(err);
          });
    });
    
    gulp.task('watch', function(done) {
        gulp.watch([
            '**/*.js',
            '**/*.html'
        ], gulp.series('push'));
        done();
    });
    
    gulp.task('default', gulp.series('push', 'watch'));
    
    const { series, watch } = require('gulp');
    const { exec } = require('child_process');
    
    function push(done) {
        exec('clasp push', function (err, stdout, stderr) {
            console.log(stdout);
            console.log(stderr);
            done(err);
          });
    }
    
    function watchPush() {
        return watch(['**/*.js', '**/*.html'], push);
    }
    
    exports.push = push;
    exports.default = exports.watch = series(push, watchPush);
    
       For re-write this gulpfile.js in gulp version 4.0 Please updated your 
       gulpfile.js according to your requirement. 
    
       this solution for assert.js:350 throw err; Bootstarp 4, SASS, gulp 4.
    
        var gulp        = require('gulp');
        var browserSync = require('browser-sync').create();
        var sass        = require('gulp-sass');
    
        // Compile sass into CSS & auto-inject into browsers
        gulp.task('sass', function() {
            return gulp.src(['node_modules/bootstrap/scss/bootstrap.scss', 
            'src/scss/*.scss'])
                .pipe(sass())
                .pipe(gulp.dest("src/css"))
                .pipe(browserSync.stream());
        });
    
        // Move the javascript files into our /src/js folder
        gulp.task('js', function() {
            return gulp.src(['node_modules/bootstrap/dist/js/bootstrap.min.js', 
             'node_modules/jquery/dist/jquery.min.js', 
             'node_modules/popper.js/dist/umd/popper.min.js'])
                .pipe(gulp.dest("src/js"))
                .pipe(browserSync.stream());
        });
    
        // Static Server + watching scss/html files
        gulp.task('serve', gulp.series('sass', function() {
    
            browserSync.init({
                server: "./src"  
            });
    
            gulp.watch(['node_modules/bootstrap/scss/bootstrap.scss', 
             'src/scss/*.scss'], gulp.series('sass'));
            gulp.watch("src/*.html").on('change', browserSync.reload);
        }));
    
        gulp.task('default', gulp.parallel('js','serve'));