Titaniumifier
Roaming in the cross-lands
Pier Paolo Ramon
Senior front-end engineer at SMC
Titaniumifyarr!
Sailing in the cross-seas!
Capt'n Pier Paolo Ramon
Old front-end enginyerr at SMC
## Pirates!?
- don’t get me started on the Flying **Dutch**man and his capt’n Bernard **Fokke**
- Brendan Eich (a.k.a. `Visual Basic-in-the-browser` slayer) **loves** pirates
- who uses JavaScript **is** a pirate (not a ninja!)
## Pirates!
1. They plundered every land or ship they encountered
2. They had small, fast ships
3. They used eyepatches
JavaScripters!
1. We managed to get to:
- the browser
- mobile apps
- desktop apps
- scripting
- CLI tools
- server-side
- on the database
- on hardware
- on robots…
JavaScripters!
2. We have small, fast modules!
JavaScripters
3. well…
(that’s Brendan Eich)
Now that I’ve burnt 1 minute and half of my lightning talk…
## Disambiguation
(but don’t take my word for granted)
- a **Titanium module** is a `.zip` file
- a **CommonJS module** is just a `.js` file that follows the spec (`module.exports`, `require()`…)
- a **CommonJS package** is a directory which has a `package.json`
- Node.js uses both the CommonJS spec, and add its own peculiarity
## Situation
- we have `gittio` to manage Titanium modules (thanks Fokke!) and that’s ok for native ones
- there’s no way to define dependencies (as in “module A requires module B”)
- we can build native modules and CommonJS modules
## Titanium CommonJS modules
as `zip` modules, installable through Studio
- no documentation
- no build tool (no `ant`, `*.py`s or something)
- you **can** `require` other modules, but no `dependencies`
- you **cannot** `require` your own files!
- just **one** single file
## Prior art
- `tetanize` and `tipi` (30 Sep) https://github.com/IsCoolEntertainment/tetanize
- `ti-mocha` (20 Dec, just related) https://github.com/tonylukasavage/ti-mocha
- `npmifier` (10 Jan) not published
- `titaniumifier` (31 Jan) https://github.com/smclab/titaniumifier
- `ti-commonsj` (20 Mar) https://github.com/tonylukasavage/ti-commonjs
Scenarios
using plain JS packages published on npm
using actual Node.js packages
writing cross-env packages
writing Titanium CommonJS modules as packages
publish Titanium modules on npm
## Ti.Next
- the module distribution strategy/repository/toolchain is yet to be defined
- it will support Node.js-style requires (Yarrrrrr!)
- will it support Node.js’ builtins?
# Titaniumifier
turns a Node.js package into a Titanium module
https://github.com/smclab/titaniumifier
## Challenges
- change `require` semantics
- shim globals (`process`…)
- shim builtins (`util`, `fs`…)
## How to get there
- extract `package.json` metadata into a `manifest`
- compact your dependencies and modules into a single JS file
- build a ‘module’, which is just a `.zip`
## How we got there
- `browserify` is doing the hard work
- [`node-titanium-resolve`][ntr] extends the standard resolution algorithm
- `package.json` plus **`guid`** and **`engines.titamiumsdk`**
[ntr]: https://github.com/smclab/node-titanium-resolve
### `grunt-titaniumfier`
## Grunt plugin for Titaniumifier
```javascript
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
titaniumifier: {
'mymodule': { options: {} }
}
});
```
```bash
grunt titaniumifier:mymodule
```
### `ti-superagent`
## Titanium.Network.HTTPClient with less suck!
https://github.com/smclab/ti-superagent
```javascript
require('superagent')
.get('http://my-awesome-server.it/api.txt')
.end(function (err, res) {
console.log(res.body);
});
```
it’s our reference project for the following scenario:
> **Wrapping** a Node package for Titanium
## Steal from this code!
https://github.com/smclab/ti-superagent
**Renaming your module** (you shouldn’t)
```javascript
{ options: { as: "another-name" } }
```
**Defining `minsdk`** (mandatory!)
```javascript
{ engines: { titaniumsdk: ">= 3.2.0.GA" } }
```
### `liferay-connector`
## Connector for Liferay Portal’s JSON WS
https://github.com/smclab/liferay-connector
```javascript
require('liferay-connector')
.authenticate(HOST, { login: LOGIN, password: PWD })
.then(function () {
console.log("I’m in!")
});
```
it’s our reference project for the following scenario:
> **Writing** a cross-env package
## Steal from this code!
https://github.com/smclab/liferay-connector
you couldn’t tell from the source, but it uses
- `superagent` on Node (and the browser!)
- `ti-superagent` on Titanium
```javascript
{
// ... (other package.json lines here)
"titanium": {
"superagent": "ti-superagent"
}
}
```
## Steal from this code!
https://github.com/smclab/liferay-connector
uses Promises internally!
(and exports its implementation of choice to you to reuse)
```
var Promise = require('bluebird');
var liferay = module.exports;
liferay.Promise = Promise;
```
## Steal from this code!
https://github.com/smclab/liferay-connector
Cross-env tests!!
- uses `grunt-titanium`
- uses `ti-mocha`
- uses `should`
```bash
grunt node ios droid # pure gold
```
Thank you!
Pier Paolo Ramon
Senior front-end engineer
Twitter: @_pier
GitHub: @yuchi