Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅当src中有更改时才运行生成_Javascript_Angularjs_Build_Gruntjs_Protractor - Fatal编程技术网

Javascript 仅当src中有更改时才运行生成

Javascript 仅当src中有更改时才运行生成,javascript,angularjs,build,gruntjs,protractor,Javascript,Angularjs,Build,Gruntjs,Protractor,故事: 我们有一个测试团队,使用量角器为内部AngularJS应用程序自动化端到端测试。以下是他们通常为“本地”测试运行的任务: 它运行“构建”任务,启动Web服务器,并针对本地构建运行e2e测试 build:prod任务本身定义为: grunt.registerTask( 'build:prod', [ 'clean', 'copy:all', 'copy:assets', 'wiredep', 'ngte

故事:

我们有一个测试团队,使用量角器为内部AngularJS应用程序自动化端到端测试。以下是他们通常为“本地”测试运行的任务:

它运行“构建”任务,启动Web服务器,并针对本地构建运行e2e测试

build:prod
任务本身定义为:

grunt.registerTask(
    'build:prod', [
        'clean',
        'copy:all',
        'copy:assets',
        'wiredep',
        'ngtemplates',
        'useminPrepare',
        'concat',
        'ngAnnotate',
        'autoprefixer',
        'uglify',
        'cssmin',
        'copy:cssfix',
        'usemin',
        'copy:html',
        'bowercopy',
        'template:setProdVersion'
    ]
);
这里我们有很多子任务(当然可以改进,但现在看起来就是这样)

问题:

目前,构建完成大约需要25秒。而且,每次有人运行端到端测试时,都会执行构建任务

问题:

仅当
src
目录中有更改时,如何运行
build:prod
任务



注意,这里的要求是使它对运行测试的测试人员透明。我不想让他们记住什么时候需要执行构建,什么时候不需要

换句话说,这个过程应该是自动化的。目标是自动检测是否需要构建

请注意,理想情况下,我希望保持构建任务的原样,这样,如果通过
grunt build:prod
直接调用它,则无论上一个构建的日期戳如何,它都将重新构建


思考和尝试:

  • 这是密切相关的,但是,由于我们有一个相当复杂的构建,在开始时有一个
    clean
    任务,我不知道如何将它应用到我的案例中

  • 我还考虑的是,在
    e2e:local
    任务中,手动检查
    dist
    src
    中文件的时间戳,并在此基础上决定是否需要调用
    build:prod
    。我认为这就是
    grunt-newer
    内部正在做的事情

  • 我们开始使用它来帮助提高性能

如果您使用git,这里有一个想法: 使用类似和使用头中的最后一次提交作为基础如何

这个想法是:

  • 您创建了一个新的grunt任务来检查最新的提交散列
  • 您应该将此提交哈希保存在添加到
    gitignore
    的文件中(并且不在
    clean
    文件夹中,通常可以在repo的根目录中)
  • 在保存到文件之前,它会检查其中已有的值(标准节点
    fs
    模块可以轻松地执行读/写操作)
  • 如果哈希不匹配,请运行
    build:prod
    任务,然后保存新的提交哈希
  • 测试人员的构建将取决于您的新任务,而不是直接依赖于
    build:prod
另一个选项(仍在使用git): 您可以使用和创建一个git钩子,它在pull之后运行并调用git
build:prod
,然后您可以从测试人员运行的grunt任务的依赖项中删除它


您可能需要另一个代码来检查githook并在需要时安装它,这可能是测试人员的一次性额外步骤,也可能是他们调用的繁重任务。

我没有使用量角器的经验,但从概念上讲,我认为这是可行的


我建议在~/.cshrc中设置一个别名,仅当
diff
命令返回true时才运行build命令

#./cshrc

alias build_on_diff 'diff -r branch_dir_1 branch_dir_2\
if ( $status == 1 ) then\
build:prod\
endif'

只需将
diff
命令替换为git使用的任何命令,只要它为检测到的差异返回1状态,该命令就可以工作。我们在我的工作场所采用了类似的方法,以避免重新生成未更改的文件。

这不是您使用
grunt
寻找的答案,但是使用
gulp
可以很容易地做到这一点

var fs = require('fs');
var gulpif = require('gulp-if');

var sourceChanged = fs.statSync('build/directory').mtime > fs.statSync('source/directory').mtime;

gulp.task('build:prod', function() {
  if (!sourceChanged) {
    return false;
  } 
  return gulp.src('./src/*.js')
    .pipe(.... build ....)
    .pipe(gulp.dest('./dist/'));
});

下面是我们如何为构建做一些Git HEAD sha工作的。我们使用它来确定当前部署到生产环境中的版本-但是我很确定您可以修改它以返回一个布尔值并触发构建

grunfile.js

function getHeadSha() {
  var curr, match, next = 'HEAD';
  var repoDir = process.env.GIT_REPO_DIR || path.join(__dirname, '..');
  try {
    do {
      curr  = grunt.file.read(path.join(repoDir, '.git', next)).trim();
      match = curr.match(/^ref: (.+)$/);
      next  = match && match[1];
    } while (next);
  } catch(ex) {
    curr = 'not-found';
  }

  return curr;
}

grunt.initConfig({
  replace: {
    applicationVersion: {
      src:  '<%= config.dist %>/index.html',
      overwrite: true,
      replacements: [{
        from: '{{APPLICATION_VERSION}}',
        to:   getHeadSha
      }]
    }
  }
});

grunt.registerTask('build', {
  'replace:applicationVersion',
  /** other tasks **/
});

grunt.registerTask('e2e:local', {
  'check_if_we_should_build',
  /** other tasks **/
});
函数getHeadSha(){ var curr,match,next='HEAD'; var repoDir=process.env.GIT_REPO_DIR|path.join(u dirname,…); 试一试{ 做{ curr=grunt.file.read(path.join(repoDir,'.git',next)).trim(); 匹配=当前匹配(/^ref:(.+)$/); 下一步=匹配和匹配[1]; }while(next); }捕获(ex){ curr=‘未找到’; } 返回货币; } grunt.initConfig({ 替换:{ 应用程序版本:{ src:“/index.html”, 改写:对, 替换:[{ 发件人:{{APPLICATION_VERSION}}, 致:getHeadSha }] } } }); grunt.registerTask('build'{ “替换:应用程序版本”, /**其他任务**/ }); grunt.registerTask('e2e:local'{ “检查我们是否应该建造”, /**其他任务**/ });
index.html

<html data-version="{{APPLICATION_VERSION}}">
  <!-- -->
</html>


还有一个可以简化整个过程的软件包,我们正在考虑自己切换到这个软件包


编辑;我刚刚注意到@meligy已经为你指明了git信息的方向。信用到期时的信用

我很惊讶还没有人提到过(它在示例文件中,我认为它是众所周知的!)。来自github:“在添加、更改或删除监视的文件模式时运行预定义任务。”-这是一个示例grunt文件,它可以在src/或test/中修改任何.js文件时,或者在Gruntfile被修改时运行您的任务

var filesToWatch = ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'];
grunt.initConfig({
    watch: {
        files: filesToWatch,
        tasks: ['build:prod',
                'connect:test',
                'protractor:local']
    }
});
grunt.loadNpmTasks('grunt-contrib-watch');
您让开发人员在开始修改文件之前打开终端并运行
grunt-watch
,每次修改这些文件时,任务都将自动运行(不再每次返回终端运行
grunt-build:prod

这是一个极好的包裹,我建议你去看看。---


我不确定它是否有用,但我们在我们的项目中使用了相同的方法。我们已经编写了一个监视程序,它可以持续检查源代码的变化并运行一个qu
var filesToWatch = ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'];
grunt.initConfig({
    watch: {
        files: filesToWatch,
        tasks: ['build:prod',
                'connect:test',
                'protractor:local']
    }
});
grunt.loadNpmTasks('grunt-contrib-watch');
npm install grunt-contrib-watch --save-dev
gulp.task('dome', function () {


    gulp.src(["maintest.js"])
        .pipe(notify("Change Found , Executing Scripts."))
        .pipe(protractor({

            configFile: "conf.js",
            args: ['--baseUrl', 'http://127.0.0.1:8000']


        })).on('error', function (e) {
        throw e
    });
})


gulp.task('default', function () {


    gulp.watch('./webpages/*.js', ['dome']);
    gulp.watch('maintest.js', ['dome']);
    gulp.watch('conf.js', ['dome']);
});