grunt-init を使えば色々なプロジェクトのひな形の生成が簡単に

grunt-init を使うと色々なプロジェクトのひな形を簡単に自動で生成してくれるようになります。

ここでいう色々なプロジェクトとは 次の項目を開発する際のパッケージのことです。(grunt公式から引用)

Create a commonjs module, including Nodeunit unit tests.
Create a basic Gruntfile.
Create a Grunt plugin, including Nodeunit unit tests.
Create a jQuery plugin, including QUnit unit tests.
Create a Node.js module, including Nodeunit unit tests.

例えば grunt-init-jquery は、jQuery Plugin Registry に登録するための jQueryプラグインのひな形を生成することができます。

以前、自分で作った jqKeycodeBinder なんかを登録するために必要なファイル構成ですね。


$ npm install -g grunt-init
grunt-init@0.2.0 /Users/xxx/.nodebrew/node/v0.8.20/lib/node_modules/grunt-init
├── colors@0.6.0-1
├── semver@1.0.14
├── hooker@0.2.3
├── prompt@0.1.12 (async@0.1.22, pkginfo@0.3.0, winston@0.5.11)
└── grunt@0.4.1 (which@1.0.5, dateformat@1.0.2-1.2.3, async@0.1.22, eventemitter2@0.4.11, iconv-lite@0.2.7, coffee-script@1.3.3, underscore.string@2.2.0rc, rimraf@2.0.3, nopt@1.0.10, minimatch@0.2.11, glob@3.1.21, lodash@0.9.2, findup-sync@0.1.2, js-yaml@2.0.3)


$ grunt-init --help
grunt-init: Generate project scaffolding from a template. (v0.2.0)

 grunt-init [options] [template]

    --help, -h  Display this help text.                                        
    --no-color  Disable colored output.                                        
   --debug, -d  Enable debugging mode for tasks that support it.               
       --stack  Print a stack trace when exiting with a warning or fatal error.
   --force, -f  A way to force your way past warnings. Want a suggestion? Don't
                use this option, fix your code.                                
    --no-write  Disable writing files (dry run).                               
 --verbose, -v  Verbose mode. A lot more information output.                   
 --version, -V  Print the grunt version. Combine with --verbose for more info. 

Available templates

(No templates found)

Templates that exist in the /Users/kamiya/.grunt-init directory may be run with
"grunt-init TEMPLATE". Templates that exist in another location may be run with
"grunt-init /path/to/TEMPLATE". A template is a directory that must contain, at
the very minimum, a template.js file.

For more information, see

grutn-init のインストールと同時に ~/.grunt-init/ フォルダができていると思います。

ここにテンプレートのひな形を git clone で配置します。

公式のjQuery Plugin テンプレートから

$ git clone ~/.grunt-init/jquery



$ ls  ~/.grunt-init/
commonjs        gruntfile       gruntplugin     jquery          node

ダウンロードができれば grunt-init jquery が実行できるようになります。

$ mkdir grunt-jquery 
$ cd grunt-jquery

$ grunt-init jquery
Running "init:jquery" (init) task
This task will create one or more files in the current directory, based on the
environment and the answers to a few questions. Note that answering "?" to any
question will show question-specific help and answering "none" to most questions
will leave its value blank.

"jquery" template notes:
Project name should not contain "jquery" or "js" and should be a unique ID not
already in use at Project title should be a human-readable
title, and doesn't need to contain the word "jQuery", although it may. For
example, a plugin titled "Awesome Plugin" might have the name "awesome-plugin".

For more information, please see the following documentation:

Naming Your Plugin
Publishing Your Plugin
Package Manifest

Please answer the following:
[?] Project name (grunt) jqueryplugin             
[?] Project title (jQueryplugin) 
[?] Description (The best jQuery plugin ever.) 
[?] Version (0.1.0) 
[?] Project git repository (git:// 
[?] Project homepage ( 
[?] Project issues tracker ( 
[?] Licenses (MIT) 
[?] Author name (kamiyam) 
[?] Author email ( 
[?] Author url (none) 
[?] Required jQuery version (*) 
[?] Do you need to make any changes to the above before continuing? (y/N) 

Writing .gitignore...OK
Writing .jshintrc...OK
Writing Gruntfile.js...OK
Writing libs/jquery-loader.js...OK
Writing libs/jquery/jquery.js...OK
Writing libs/qunit/qunit.css...OK
Writing libs/qunit/qunit.js...OK
Writing src/.jshintrc...OK
Writing src/jqueryplugin.js...OK
Writing test/.jshintrc...OK
Writing test/jqueryplugin.html...OK
Writing test/jqueryplugin_test.js...OK
Writing package.json...OK
Writing jqueryplugin.jquery.json...OK

Initialized from template "jquery".
You should now install project dependencies with npm install. After that, you
may execute project tasks with grunt. For more information about installing
and configuring Grunt, please see the Getting Started guide:

Done, without errors.

このjQuery Plugin テンプレートは以下の構成になるようです。

src フォルダ
開発用 jsファイル
dist フォルダ
配布用 jsファイル(圧縮版)

grunt コマンドを実行すると dist フォルダが生成されます。

$ npm install
$ grunt
Running "jshint:gruntfile" (jshint) task
>> 1 file lint free.

Running "jshint:src" (jshint) task
>> 1 file lint free.

Running "jshint:test" (jshint) task
>> 1 file lint free.

Running "qunit:files" (qunit) task
Testing test/jqueryplugin.html....OK
>> 5 assertions passed (144ms)

Running "clean:files" (clean) task

Running "concat:dist" (concat) task
File "dist/jqueryplugin.js" created.

Running "uglify:dist" (uglify) task
File "dist/jqueryplugin.min.js" created.
Uncompressed size: 838 bytes.
Compressed size: 94 bytes gzipped (305 bytes minified).

Done, without errors.