1

各ファイルの先頭に著作権表示があることを確認する必要があります。

著作権ステートメントが欠落している場合、grunt を使用してビルドを失敗させるにはどうすればよいですか?

4

3 に答える 3

2

まず第一に、あなたが参照しているのは*.js や *.htmlなどの作業ファイルであり、グラフィック ファイルやバイナリ ファイルではないことを前提としています。

grunt.registerTaskこれは、 which を使用して実行できます。

1. loop through all relevant files
2. Read and compare first line to copyright line
3. If different - re-write file but a new first line which will be the copyright info

ものすごく単純。繰り返しますが、これはバイナリ ファイルでは機能しません。私はあなたのためにこれを書きましたが、非常に便利なようです。プラグインとして追加することを検討するかもしれません. フィールドテスト:

それを実行しgrunt verifyCopyright、ファイルが別のディレクトリにある場合は変更することを確認し、他のファイルを処理する場合はそれらもリストに追加します。頑張って...

 grunt.registerTask('verifyCopyright', function () {

    var fileRead, firstLine, counter = 0, fileExtension, commentWrapper;
    copyrightInfo = 'Copyright by Gilad Peleg @2013';

    //get file extension regex
    var re = /(?:\.([^.]+))?$/;

    grunt.log.writeln();

    // read all subdirectories from your modules folder
    grunt.file.expand(
        {filter: 'isFile', cwd: 'public/'},
        ["**/*.js", ['**/*.html']])
        .forEach(function (dir) {
            fileRead = grunt.file.read('public/' + dir).split('\n');
            firstLine = fileRead[0];

            if (firstLine.indexOf(copyrightInfo > -1)) {

                counter++;
                grunt.log.write(dir);
                grunt.log.writeln(" -->doesn't have copyright. Writing it.");

                //need to be careful about:
                //what kind of comment we can add to each type of file. i.e /* <text> */ to js
                fileExtension = re.exec(dir)[1];
                switch (fileExtension) {
                    case 'js':
                        commentWrapper = '/* ' + copyrightInfo + ' */';
                        break;
                    case 'html':
                        commentWrapper = '<!-- ' + copyrightInfo + ' //-->';
                        break;
                    default:
                        commentWrapper = null;
                        grunt.log.writeln('file extension not recognized');
                        break;
                }

                if (commentWrapper) {
                    fileRead.unshift(commentWrapper);
                    fileRead = fileRead.join('\n');
                    grunt.file.write( 'public/' + dir, fileRead);
                }
            }
        });

    grunt.log.ok('Found', counter, 'files without copyright');
})
于 2013-08-23T12:01:48.223 に答える
1

存在するかどうかを確認して失敗するのではなく、自動的に注入するタスクを用意するだけではどうですか? grunt-bannerを参照してください。

于 2013-08-19T12:51:48.957 に答える