Skip to content

Getting Started (with Grunt)

Andrey Tkachenko edited this page Oct 15, 2015 · 1 revision

If you don’t want to use Grunt, see Getting Started.

Requirements

You need to have Node.js and npm installed to follow this mini-guide, but you already have if you have the Titanium SDK environment set up.

If you never used it, you need to install the Command Line Interface for Grunt, the JavaScript Task Runner:

$ [sudo] npm install --global grunt-cli

Quickstart

Create your project directory:

$ mkdir my-awesome-thing
$ cd my-awesome-thing

Initialize a package.json

$ npm init

…and by following the wizard you’ll end up with something like this:

$ cat ./package.json
{
  "name": "my-awesome-thing",
  "version": "1.0.0",
  "description": "Does awesome things!",
  "main": "./index.js"
}

Setup the Grunt tasks by creating a Gruntfile.js in the root of your project and by installing the required dependencies:

$ npm install --save-dev grunt grunt-titaniumifier
// Gruntfile.js
module.exports = function (grunt) {
  grunt.initConfig({
    // Our task will be executed with `titaniumifier:module`
    "titaniumifier": {
      "module": {
        // The package is in "." and the zipfile will be written in "."       
        src: ".",
        dest: "."      
      }
    }
  });

  // Load dependencies
  grunt.loadNpmTasks('grunt-titaniumifier');
};

Create an index.js:

// index.js

module.exports.shout = function (s) {
 return s + '!!';
};

And finally launch titaniumifier:

$ grunt titaniumifier:module

But we get an error!

Fatal error: No `guid` found. Here’s one for you: 946f6eea-6afd-0508-0854-518c1150dc3b

Modify the package.json file to include the missing guid (the error has built a random one for you):

{
  "name": "my-awesome-thing",
  "version": "1.0.0",
  "description": "Does awesome things!",
  "titaniumManifest": {
    "guid": "946f6eea-6afd-0508-0854-518c1150dc3b"
  },
  "main": "./index.js"
}

Run grunt again:

$ grunt titaniumifier:module

Running "titaniumifier:module" (titaniumifier) task
>> Module zip written in /path/to/my-awesome-thing

🎉 Awesome! You now have a titaniumified package as my-awesome-thing-commonjs-1.0.0.zip in your project root directory!

What’s next?

If reached this point you probably want to read about the anatomy of a project or how to setup automated testing.

If you want, you can also have a look at some examples of titaniumifier in the wild.

Clone this wiki locally