2016年12月11日日曜日

開発環境

Automate with Grunt (Brian P. Hogan(著)、Pragmatic Bookshel)の Chapter 1(The Very Basics)の 「What's Next?」の部分を取り組んでみる。

コード(Emacs)

package.json

{
  "name": "learning_grunt",
  "version": "0.0.0",
  "description": "'sample project'",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "kamimura",
  "license": "MIT",
  "devDependencies": {
    "grunt": ""
  }
}

Gruntfile.js

module.exports = (grunt) => {
    grunt.registerTask(
        'default',
        [
            'default1',
            'mulNumbers:2:3',
            'mulNumbers:a:3',
            'mulNumbers:2:b',
            'mulNumbers:3:2'
        ]
    );
    grunt.registerTask(
        'default1',
        'Hello, World!',
        () => {
            grunt.log.writeln('It worked!');
        }
    );
    grunt.registerTask(
        'mulNumbers',
        'multiplication',
        (x, y) => {
            let a = Number(x),
                b = Number(y);

            if (isNaN(a) || isNaN(b)) {
                grunt.warn('arguments must be a number.');
            }
            grunt.log.writeln(`${a} x ${b} = ${a * b}`);
        }
    );
};

入出力結果(Terminal)

$ grunt
Running "default1" task
It worked!

Running "mulNumbers:2:3" (mulNumbers) task
2 x 3 = 6

Running "mulNumbers:a:3" (mulNumbers) task
Warning: arguments must be a number.  Use --force to continue.

Aborted due to warnings.
$ grunt --force
Running "default1" task
It worked!

Running "mulNumbers:2:3" (mulNumbers) task
2 x 3 = 6

Running "mulNumbers:a:3" (mulNumbers) task
Warning: arguments must be a number.  Used --force, continuing.
NaN x 3 = NaN

Running "mulNumbers:2:b" (mulNumbers) task
Warning: arguments must be a number.  Used --force, continuing.
2 x NaN = NaN

Running "mulNumbers:3:2" (mulNumbers) task
3 x 2 = 6

Done, but with warnings.
$ grunt --help
Grunt: The JavaScript Task Runner (v1.0.1)

…

Available tasks
       default  Alias for "default1", "mulNumbers:2:3", "mulNumbers:a:3",      
                "mulNumbers:2:b", "mulNumbers:3:2" tasks.                      
      default1  Hello, World!                                                  
    mulNumbers  multiplication                                                 

…

$ 

0 コメント:

コメントを投稿